- Python
- Windows OS
- Linux and Mac OS
- C++
- Windows OS
- Linux and Mac OS
测试无线电的连通性想到用蜂鸣,整理到以下案例,亲测有效。 Python Windows OS
import winsound
duration = 1000 #毫秒
freq = 440 #Hz
winsound.Beep(freq, duration)
Linux and Mac OS
import os
duration = 1 # 秒
freq = 440 # Hz
os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))
在Linux或Mac上调用蜂鸣器需要安装sox
# On Linux
sudo apt install sox
# On Mac
sudo port install sox
C++ Windows OS
#include
#include // WinApi
using namespace std;
int main()
{
int duration = 1000;
int freq = 440;
Beep(freq, duration);
cin.get(); // wait
return 0;
}
Linux and Mac OS
int main()
{
int duration = 1;
int freq = 440;
char command[100] = {0};
sprintf(command, "play -nq -t alsa synth %d sine %d", duration, freq);
system(command);
cin.get(); // wait
return 0;
}
执行以上案例前需要安装sox
,安装方法参见Python部分。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)