gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:11:54 24 4
gpt4 key购买 nike

我正在使用Python sounddevice库录制音频,但是似乎无法消除应该是连续音频文件之间的〜0.25到〜0.5秒间隔。我认为这是因为文件写入会占用时间,因此我学会了使用“多处理”和“队列”来区分文件写入,但并没有帮助。最令人困惑的是,日志表明Main()循环中的迭代接近无缝(只有1-5毫秒),但是奇怪的是audio_capture函数花费的时间比预期的长,甚至没有其他重要的事情要做。我试图尽可能减少这篇文章的脚本。我的研究都针对这种线程/多处理方法,所以我感到困惑。
背景:Raspbian Buster上的3.7
我将数据分成多个部分,以使文件不会太大,并且我想编程任务必须应对这一挑战。之后,我还有其他4个子进程在做各种事情。
日志:audio_capture部分应该只需要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()

最佳答案

documentation告诉我们sounddevice.rec()并非无间断的记录:

If you need more control (e.g. block-wise gapless recording, overlapping recordings, …), you should explicitly create an InputStream yourself. If NumPy is not available, you can use a RawInputStream.


example programs中有多个无缝记录示例。

关于python - Python:消除录制的音频片段之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63497479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com