如何在python3中使用频率发出声音?

时间:2017-05-10 23:08:16

标签: python-3.x audio frequency beep

我尝试了这个,但它没有多于空行:

import os
a=300
b=2000
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % ( a, b))

1 个答案:

答案 0 :(得分:2)

在Python中播放给定频率和持续时间的哔声的简单方法:

frequency = 1000 # Hertz
duration  = 2000 # milliseconds

Windows

import winsound
winsound.Beep(frequency, duration)

Linux

# SoX must be installed using 'sudo apt-get install sox' in the terminal
import os
os.system('play -n synth %s sin %s' % (duration/1000, frequency))

macOS

# First install Homebrew (https://brew.sh/) 
# and then SoX using 'brew install sox' in the terminal
import os
os.system('play -n synth %s sin %s' % (duration/1000, frequency))

跨平台

使用PyAudio模块和一些编码:https://stackoverflow.com/a/27978895