Windows Beep() equivalent for Linux

前端 未结 6 772
我在风中等你
我在风中等你 2021-02-09 12:38

I am experimenting with Beep function on Windows:

#include 
...
Beep(frequency, duration);

The computer then beeps with some f

相关标签:
6条回答
  • 2021-02-09 13:02

    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)

    0 讨论(0)
  • 2021-02-09 13:13

    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);
    }
    
    0 讨论(0)
  • 2021-02-09 13:14

    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)

    0 讨论(0)
  • 2021-02-09 13:14

    this site shows two ways:

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

    and

    Beep(587,500);
    
    0 讨论(0)
  • 2021-02-09 13:15

    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.

    0 讨论(0)
  • 2021-02-09 13:20

    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.

    0 讨论(0)
提交回复
热议问题