如何在python中记录音频的持续时间不确定并允许暂停和恢复功能? [英] How to record audio in python for undetermined duration AND allow for pause and resume features?
问题描述
我正在编写一个Python应用,将音频记录为WAV文件,直到用户按下暂停或停止为止.暂停音频后,用户还应该能够恢复录音.另外:
I'm writing a Python app to record audio as a WAV file until a user presses pause or stop. After pausing the audio, the user should also be able to resume recording. Additionally:
- 该应用无法预先知道录音会持续多久
- 应用程序应避免内存不足(因为记录可能会很长).例如,它可以实时写入WAV文件,以防止将不断增长的记录存储在内存中.
什么是解决此问题的好方法?您能否为您的解决方案提供一些代码片段?
What's a good approach for this problem? Can you please provide some code snippets for your solution?
使用 python-sounddevice ,我可以在stop()
和start()
流中模拟暂停"功能.我可以指定一个numpy数组作为记录的输出.但是:
With python-sounddevice, I could stop()
and start()
the stream to mimic a 'pause' features. And I can specify a numpy array as an output for the recording. But:
- 我不知道要制作多大的阵列(因为我不知道录制时间)
- 数组填满后我该怎么办?
python-sounddevice和声音文件 可以支持录音,而无需事先知道大小.但是:
python-sounddevice and sound-file can support recordings without know the size beforehand. But:
- 如何合并暂停"和恢复"功能?声音文件只有
read
和write
方法. - 是否有比使用
KeyBoardInterrupt
更好的停止流的方法? - 在每次暂停"和合并用户单击停止"后会得到WAV文件吗?
- 我尝试使用
Threading.Event()
阻止记录线程以模仿暂停功能,但是记录不断写入文件中
- How would I incorporate 'pause' and 'resume' features? Sound-file has only
read
andwrite
methods. - Is there a better way to stop the stream than using a
KeyBoardInterrupt
? - Could I create different recording after every 'pause' and combine the WAV files after the user clicks 'stop'?
- I tried using
Threading.Event()
to block the recording thread to mimic a pause feature, but the recording kept writing to the file
我尝试使用 sound-device
方法
My attempt at sound-device
approach
paused = False
def record():
self.recording = ? # create numpy.ndarray of the correct size
# (not sure the best way to do this without
# knowing the recording duration)
with sd.InputStream(samplerate=44100, device=mic, channels=1,
callback=self.callback):
while self.paused:
sd.stop()
sd.rec(out=recording) # but what happens if
# recording is very long
# or numpy array fills up?
def stop_and_save():
sd.stop()
scipy.io.wavfile.write("recording.wav", 44100, self.recording)
sound-device
和 sound-file
方法:
The sound-device
and sound-file
approach:
with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate,
channels=args.channels, subtype=args.subtype) as file:
with sd.InputStream(samplerate=args.samplerate, device=args.device,
channels=args.channels, callback=callback):
print('press Ctrl+C to stop the recording')
while True:
file.write(q.get()) # but how do you stop writing when 'paused'?
except KeyboardInterrupt:
print('\nRecording finished: ' + repr(args.filename))
parser.exit(0)
except Exception as e:
parser.exit(type(e).__name__ + ': ' + str(e))
推荐答案
我想出了此暂停/恢复功能解决方案,该功能利用了sound-device
和sound-file
方法,只要用户单击暂停,然后在恢复时开始新的录制.然后,在用户单击停止后,所有WAV文件将按顺序合并.
I came up with this solution to the pause/resume feature, which utilizes the sound-device
and sound-file
approach, where the current recording is stopped whenever the user clicks Pause and a new recording is started upon Resume. Then, after the user clicks Stop, all the WAV files are combined in order.
( Matthias的代码看起来像是一个很好的解决方案,可以更多地利用线程.)
(Matthias' code also looks like a fine solution that takes more advantage of threads.)
要开始录制音频:
def record(self):
try:
with sf.SoundFile(self.filepath,
mode='x', samplerate=self.SAMPLE_RATE,
channels=self.CHANNELS, subtype=None) as file:
with sd.InputStream(samplerate=self.SAMPLE_RATE, device=self.mic_id,
channels=self.CHANNELS, callback=self.callback):
logger.info(f"New recording started: {self.sound_file.name}")
try:
while True:
file.write(self.mic_queue.get())
except RuntimeError as re:
logger.debug(f"{re}. If recording was stopped by the user, then this can be ignored")
record()
的回调:
def callback(self, indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
self.mic_queue.put(indata.copy())
要暂停:
def pause_recording(self):
"""Mimics a 'pause' functionality by writing the current sound file changes to disk.
Upon 'resume' a new recording will be made. Note: close() is not called here, because
that would kill the recording thread
"""
self.sound_file.flush()
logger.info(f"'Paused' (closed) recording: {self.sound_file.name}")
要恢复:
def resume_recording(self):
"""
Mimics 'resuming' by starting a new recording, which will be merged with the others
when the user selects Stop & Save (or deleted upon Stop & Delete)
Note: get_full_sound_file_name() outputs a new recording with the same base name as the first, but appends a `_part2` or `_part3` etc. to the suffix to distinguish it from the first and maintain order.
"""
self.sound_file = self.get_full_sound_file_name()
self.record()
要停止记录:
def stop_mic_recording(self):
try:
self.sound_file.flush()
self.sound_file.close()
logger.info(f"Stopped and closed recording: {self.sound_file.name}")
except RuntimeError as e:
logger.info(f"Error stopping/saving {self.sound_file.name}. Make sure the file exists and can be modified")
logger.info(f"RunTimeError: \n{e}")
要合并音频(在stop_recording()
之后调用):
To combine audio (called after stop_recording()
):
def combine_recordings_if_needed(self):
"""
If recording was paused, combines all sections in alphabetical order into a new audio file
"""
if self.section_count > 1: # this is incremented when a recording is paused/resumed
combined_audio = AudioSegment.empty()
files_combined = []
for rec in glob.glob(os.path.join(RECORDING_DIR, "*" + self.FILE_EXT)):
combined_audio = combined_audio + AudioSegment.from_wav(rec) # this is why alphabetical order is important
files_combined.append(rec)
combined_file_name = os.path.join(RECORDING_DIR, self.base_filename + "_combined" + self.FILE_EXT)
combined_audio.export(out_f=combined_file_name, format="wav")
logger.info(f"Combined the following recordings into {combined_file_name}:"
f"\n {files_combined}")
这篇关于如何在python中记录音频的持续时间不确定并允许暂停和恢复功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!