简体   繁体   中英

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.

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.

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);
}

我不熟悉Linux,但输出ascii字符0x07似乎是通过快速谷歌搜索读取的。

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)

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)

this site shows two ways:

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

and

Beep(587,500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM