0
点赞
收藏
分享

微信扫一扫

Python/C++调用蜂鸣器

夏侯居坤叶叔尘 2022-05-02 阅读 80
c++python

Beep in Python/C++


测试无线电的连通性想到用蜂鸣,整理到以下案例,亲测有效。

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 <iostream> 
#include <windows.h> // 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 条评论