我正在使用以下代码在Python中生成正弦波声音:
import pyaudio
import numpy as np
p = pyaudio.PyAudio()
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 600.0 # in seconds, may be float
f = 50.0 # sine frequency, Hz, may be float
# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
因此,代码创建了给定持续时间的样本并将其作为音频流输出。为此,首先将样本加载到内存中,所以对于大样本,我会遇到内存问题。
我的下一个目标是创建一个连续的50 Hz输出流。 pyAudio有可能吗?我还是python新手,此刻我还不知道如何解决这个问题。如果有人可以帮助我,那就太好了!
预先感谢
我的设置:Python 3.5,Raspberry Pi 3,pyAudio v0.2.11