Search code examples
javascriptnode.jsbeep

System beep NodeJS custom frequency and duration


I'm trying to make a custom motherboard beep with NodeJS. Right now I'm using process.stderr.write("\007");, which makes a default system beep on my motherboard (Windows 7 64).

But some programming languages have something like: SoundBeep, Frequency, Duration (AutoHotKey: https://www.autohotkey.com/docs/commands/SoundBeep.htm)

And apparently it's also possible in C++: Beep(hertz, milli) (How to make Motherboard Beep through C++ Code?)

Is setting custom system beep frequency and duration possible in NodeJS?


Solution

  • I ended up using a C++ addon for this and C++ Beep(frequency, duration) function. I took examples from this repository: https://github.com/nodejs/node-addon-examples (the second example, nan), modified it to pass my arguments (frequency and duration), then compiled the addon.cc file using the instructions in that repository.

    So in nodeJS I can pass this to the addon:

    addon.add(frequency,duration);
    

    And in C++

    double arg0 = info[0]->NumberValue(context).FromJust();
    double arg1 = info[1]->NumberValue(context).FromJust();
    Beep(arg0,arg1);