我是python的新手,我编写了一个代码来记录用户的音频。为了停止录制,用户必须按下Ctrl + C'。但是此功能仅适用于命令提示符,而不适用于任何IDE。有人可以帮我吗?
代码如下:
import sys
import queue
import tempfile
import numpy as np
import sounddevice as sd
import soundfile as sf
assert np
q = queue.Queue()
def callback(indata, frames, time, status):
"""
This is called from a separate thread for each audio block
"""
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
# Unique file name for every recording
filename = tempfile.mktemp(prefix='untitled_', suffix='.wav', dir='')
# Make sure the file is open before recording anything
with sf.SoundFile(filename, mode='x', samplerate=48000, channels=2,
subtype='PCM_24') as file:
with sd.InputStream(samplerate=48000, channels=2, callback=callback):
print('#' * 80)
print('press Ctrl+C to stop the recording')
print('#' * 80)
while True:
file.write(q.get())