0

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?

teg_brightly
  • 468
  • 6
  • 20

3 Answers3

3

You can also do

powershell.exe [console]::beep(500,600)

So in node.js it would look like this

require("child_process").exec("powershell.exe [console]::beep(500,600)");
opcode
  • 419
  • 5
  • 11
2

I think for windows more easy to do via rundll32 tool and beep

const cp = require('child_process');

function beep(frequency, duration) {
  cp.execSync(`rundll32.exe Kernel32.dll,Beep ${frequency},${duration}`);
}

beep(750,300);
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
  • Not beeping currently. Do I need any additional files? I just put everything above in a js file and run it with node? Also it's Win7 64 – teg_brightly Nov 15 '19 at 16:52
  • On my box it works without any problems. Just run in console `rundll32.exe Kernel32.dll,Beep 750,300` – Yaroslav Gaponov Nov 15 '19 at 16:55
  • Couldn't make it work after experimenting, just returns a new line. Something like this `rundll32 user32.dll,MessageBeep` works, but doesn't produce the sound I need – teg_brightly Nov 15 '19 at 19:17
  • Here it's said Beep isn't supported on 64 Vista and 64 XP: `https://learn.microsoft.com/en-us/dotnet/api/system.console.beep?view=netframework-4.8`. Also maybe it has to do with the fact that I replaced `Beep.sys` with the one from XP 64 to use motherboard for beeps instead of the speakers. It works fine in AutoHotKey though – teg_brightly Nov 15 '19 at 19:34
  • Also it worked in c++ for me: `https://stackoverflow.com/questions/4060601/make-sounds-beep-with-c` with the second answer – teg_brightly Nov 15 '19 at 19:46
0

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);
teg_brightly
  • 468
  • 6
  • 20