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

Tur*_*csi 2 audio frequency beep python-3.x

我试过这个,但它只是一个空行:

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

Jos*_*lin 5

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

frequency = 1000 # Hertz
duration  = 2000 # milliseconds
Run Code Online (Sandbox Code Playgroud)

Windows 上

import winsound
winsound.Beep(frequency, duration)
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

跨平台

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

  • 谢谢,它有效 :) 我心脏病发作了 XD (2认同)