栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python/C++调用蜂鸣器

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python/C++调用蜂鸣器

Beep in Python/C++
    • 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部分。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号