【问题标题】:System beep NodeJS custom frequency and duration系统蜂鸣 NodeJS 自定义频率和持续时间
【发布时间】:2020-03-11 19:44:51
【问题描述】:

我正在尝试使用 NodeJS 制作自定义主板蜂鸣声。 现在我正在使用process.stderr.write("\007");,它会在我的主板(Windows 7 64)上发出默认系统蜂鸣声。

但有些编程语言有类似的东西: SoundBeep, Frequency, Duration (AutoHotKey: https://www.autohotkey.com/docs/commands/SoundBeep.htm)

显然它在 C++ 中也是可能的: Beep(hertz, milli) (How to make Motherboard Beep through C++ Code?)

是否可以在 NodeJS 中设置自定义系统蜂鸣频率和持续时间?

【问题讨论】:

标签: javascript node.js beep


【解决方案1】:

我认为windows 更容易通过rundll32 工具和beep 完成

const cp = require('child_process');

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

beep(750,300);

【讨论】:

  • 目前没有蜂鸣声。我需要任何其他文件吗?我只是把上面的所有东西都放在一个 js 文件中,然后用 node 运行它?也是Win7 64
  • 在我的盒子上它可以正常工作。只需在控制台中运行rundll32.exe Kernel32.dll,Beep 750,300
  • 实验后无法正常工作,只返回一个新行。像 rundll32 user32.dll,MessageBeep 这样的东西可以工作,但不能产生我需要的声音
  • 这里说 64 Vista 和 64 XP 不支持 Beep:https://docs.microsoft.com/en-us/dotnet/api/system.console.beep?view=netframework-4.8。也可能与我将Beep.sys 替换为 XP 64 中的Beep.sys 以使用主板而不是扬声器的事实有关。虽然它在 AutoHotKey 中工作正常
  • 它在 c++ 中也适用于我:https://stackoverflow.com/questions/4060601/make-sounds-beep-with-c 和第二个答案
【解决方案2】:

你也可以这样做

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

所以在 node.js 中它看起来像这样

require("child_process").exec("powershell.exe [console]::beep(500,600)");

【讨论】:

    【解决方案3】:

    我最终为此使用了 C++ 插件和 C++ Beep(frequency, duration) 函数。 我从这个存储库中获取了示例:https://github.com/nodejs/node-addon-examples(第二个示例,nan), 修改它以传递我的参数(频率和持续时间),然后使用该存储库中的说明编译 addon.cc 文件。

    所以在 nodeJS 中我可以将它传递给插件:

    addon.add(frequency,duration);
    

    在 C++ 中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      相关资源
      最近更新 更多