2

我在我的项目中使用sounddevicescipyplaysound。我有一个名为record.py的文件,其来源如下:

import sounddevice as sd
from scipy.io.wavfile import write
from playsound import playsound

fs = 44100  # this is the frequency sampling; also: 4999, 64000
seconds = 5  # Duration of recording

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
print("Starting: Speak now!")
sd.wait()  # Wait until recording is finished
print("finished")
write('output.wav', fs, myrecording)  # Save as WAV file
playsound("output.wav")

上面给出的代码工作正常,但我有一个问题。您可能已经注意到源代码中的seconds变量,其值设置为5。因此,在5 秒后,录音会自动停止并保存为output.wav,然后开始播放。有没有什么方法可以让我不需要定义持续时间,这意味着用户可以根据需要录制音频。此外,通过按Shift键,她/他可以暂停和恢复录制,最后按Enter停止录制,之后文件会自动保存并播放。那么这可以做到吗?

4

0 回答 0