Python:消除录制音频片段之间的间隙



我正在使用Python声音设备库来录制音频,但我似乎无法消除连续音频文件之间约0.25到0.5秒的间隔。我想这是因为文件写入需要时间,所以我学会了使用多处理器和队列来分离文件写入,但这并没有帮助。最令人困惑的是,日志显示Main((循环中的迭代几乎是无间隙的(只有1-5毫秒(,但神秘的是,即使没有做任何其他重要的事情,audio_capture函数的时间也比预期的要长。我尽量减少这个帖子的脚本。我的研究都指向这种线程/多处理方法,所以我很困惑。

背景:3.7关于Raspbian Buster我将数据划分为多个段,这样文件就不会太大,我想编程任务必须应对这一挑战。之后,我还有4个子流程在做各种事情。

日志:音频捕获部分应该只需要10:00

08:26:29.991 --- Start of segment #0
08:36:30.627 --- End of segment #0     <<<<< This is >0.6 later than it should be
08:36:30.629 --- Start of segment #1   <<<<< This is near gapless with the prior event

脚本:

import logging
import sounddevice
from scipy.io.wavfile import write
import time
import os
from multiprocessing import Queue, Process
# this process is a near endless loop
def main():
fileQueue = Queue()
writerProcess = Process(target=writer, args=(fileQueue,))
writerProcess.start()
for i in range(9000):
fileQueue.put(audio_capture(i)) 
writerProcess.join()
# This func makes an audio data object from a sound source
def audio_capture(i): 
cycleNumber = str(i)
logging.debug('Start of segment #' + cycleNumber)
# each cycle is 10 minutes at 32000Hz sample rate
audio = sounddevice.rec(frames=600 * 32000, samplerate=32000, channels=2) 
name = time.strftime("%H-%M-%S") + '.wav' 
path = os.path.join('/audio', name)
sounddevice.wait()
logging.debug('End of segment #' + cycleNumber)
return [audio, path]

# This function writes the files.
def writer(input_queue):
while True:
try:
parameters = input_queue.get()
audio = parameters[0]
path = parameters[1]
write(filename=path, rate=32000, data=audio)
logging.debug('File is written')
except:
pass
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d --- %(message)s', datefmt='%H:%M:%S',handlers=[logging.FileHandler('/audio/log.txt'), logging.StreamHandler()])
main()  

文档告诉我们sounddevice.rec()不适用于无间隙录制:

如果您需要更多的控制(例如,逐块无间隙录制、重叠录制等(,您应该自己显式创建InputStream。如果NumPy不可用,则可以使用RawInputStream。

示例程序中有多个无间隙录制示例。

使用Pyaudio,打开一个非阻塞音频流。您可以在Pyaudio文档首页找到一个非常好的基本示例。选择一个缓冲区大小,我建议512或1024。现在只需将传入的数据附加到一个numpy数组中。我有时会在一个numpy数组中存储长达30秒的音频。当到达一个段的末尾时,创建另一个空的numpy数组并重新开始。创建一个线程并将第一个段保存在某个位置。记录将继续,不会丢弃一个样本;(

编辑:如果你想在一个文件中写10分钟,我建议只创建10个数组á1分钟,然后附加并保存它们。

最新更新