supermedo
supermedo

Reputation: 582

Windows Beep() equivalent for Linux

I am experimenting with Beep function on Windows:

#include <windows.h>
...
Beep(frequency, duration);

The computer then beeps with some frequency for some duration. How would I do this on a Linux computer?

Edit: It IS important to output different frequencies.

Upvotes: 7

Views: 12837

Answers (6)

Schwartser
Schwartser

Reputation: 93

this site shows two ways:

char beep[] = {7, ”};
printf(“%c”, beep);

and

Beep(587,500);

Upvotes: -1

MarkR
MarkR

Reputation: 63616

In summary:

  1. Outputting a BEL character to a terminal might produce a beep - depending on what terminal it is and what its configuration is. There is no control over this however.

  2. Any sound you like can be produced by outputting audio data to /dev/dsp or some other sound device. This includes beep, but making a sound involves playing back an actual sample.

  3. The console driver provides (in some configurations) an ioctl for /dev/console which beeps with a configurable pitch (much like the NT one)

Upvotes: 0

Iraimbilanja
Iraimbilanja

Reputation:

lets have us some gabba coming from the audio speakers

#!/usr/bin/ruby

$audio = File.open("/dev/audio", "w+")
def snd char
    $audio.print char.chr
end

def evil z
    0.step(100, 4.0 / z) { |i|
        (i / z).to_i.times { snd 0 }
        (i / z).to_i.times { snd 255 }
    }
end

loop {
    evil 1 
    evil 1
    evil 1
    evil 4
}

more seriously though:

//g++ -o pa pa.cpp -lportaudio
#include <portaudio.h>
#include <cmath>

int callback(void*, void* outputBuffer, unsigned long framesPerBuffer, PaTimestamp, void*) {
    float *out = (float*)outputBuffer;
    static float phase;
    for(int i = 0; i < framesPerBuffer; ++i) {
        out[i] = std::sin(phase);
        phase += 0.1f;
    }
    return 0;
}

int main() {
    Pa_Initialize();
    PaStream* stream;
    Pa_OpenDefaultStream(&stream, 0, 1, paFloat32, 44100, 256, 1, callback, NULL);
    Pa_StartStream(stream);
    Pa_Sleep(4000);
}

Upvotes: 4

Hasturkun
Hasturkun

Reputation: 36422

I'd suggest you look at the source for the beep utility. that does exactly what you want. (specifically, it opens "/dev/console" and uses ioctl to request beep. note this will only work on the attached console)

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882716

Check out the source code for beep available with Ubuntu (and probably other distros) or have a look at http://www.johnath.com/beep/beep.c for another source (it's the same code, I believe).

It allows you to control frequency, length and repetitions (among other things) with ease.

Upvotes: 9

Daniel Sloof
Daniel Sloof

Reputation: 12706

I'm not familiar with Linux, but outputting ascii character 0x07 seems to do that trick from what I read with a quick google search.

Upvotes: 0

Related Questions