0

我试图理解我在 PyAudio 中看到的东西。我正在尝试采集 200 毫秒的音频样本,等待几秒钟,然后再采集三个 200 毫秒的音频样本。考虑这段代码:

import pyaudio
import time

p = pyaudio.PyAudio()
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1 #chan
RATE = 11025
stream = p.open(format = FORMAT,
            channels = CHANNELS,
            rate = RATE,
            input = True,
            output = False,
            frames_per_buffer = chunk,
            input_device_index = 0,
            output_device_index = 0)

def record(seconds):
all = []
for i in range(0, int(RATE / chunk * seconds)):
    data = stream.read(chunk)

    all.append(data)
data = ''.join(all)
return data

#record 200ms of sound
print "pre-record 1 " + str(time.time())
data = record(.2)
print "post-record 1 " + str(time.time())

#sleep for one second
#time.sleep(1)

#record 200ms of sound
print "pre-record 2 " + str(time.time())
data = record(.2)
print "post-record 2 " + str(time.time())

print "pre-record 3 " + str(time.time())
data = record(.2)
print "post-record 3 " + str(time.time())

print "pre-record 4 " + str(time.time())
data = record(.2)
print "post-record 4 " + str(time.time())

如果我“按原样”运行(即time.sleep()注释掉),我会得到这个,这是有道理的:

pre-record 1 1357526364.46
post-record 1 1357526364.67
pre-record 2 1357526364.67
post-record 2 1357526364.86
pre-record 3 1357526364.86
post-record 3 1357526365.03
pre-record 4 1357526365.03
post-record 4 1357526365.22

如果我然后取消注释 time.sleep() 行以在第一次和第二次录制之间添加一秒钟的延迟,我会得到:

pre-record 1 1357525897.09
post-record 1 1357525897.28
pre-record 2 1357525898.28
post-record 2 1357525898.28
pre-record 3 1357525898.28
post-record 3 1357525898.28
pre-record 4 1357525898.28
post-record 4 1357525898.47

请注意,虽然第一个和第二个记录之间的时间戳有 1 秒的延迟,但记录 2 和 3 之间根本没有延迟,它们似乎是在零时间内拍摄的(尽管它们实际上包含 200 毫秒数据)。录制 4 似乎需要 200 毫秒才能完成,但与录制 2 和 3 同时开始。

如果我替换time.sleep(1)record(1)(而不是保存/使用 1 秒记录中的数据),则程序的行为与我预期的一样:

pre-record 1 1357526802.57
post-record 1 1357526802.77
pre-record 2 1357526803.69
post-record 2 1357526803.88
pre-record 3 1357526803.88
post-record 3 1357526804.06
pre-record 4 1357526804.06
post-record 4 1357526804.25

因此,即使在 期间time.sleep(1),某处的缓冲区仍在被馈送音频,并且当我在睡眠后调用记录函数时,它会从该缓冲区中获取音频,这不是我想要的音频。我需要睡眠后的音频,而不是在睡眠期间。有人可以帮助我了解 PyAudio 与那里的任何缓冲区的交互,有没有办法更好地知道我的音频是在什么时候被实际捕获的?

4

1 回答 1

0

音频设备将每秒不断地向缓冲区添加 RATE 样本,并且您的代码会从此缓冲区中读取。

因此,如果不从缓冲区读取,您将无法 sleep(1)。您可以编写一些代码来跳过示例:

def skip(seconds):
    samples = int(seconds * RATE)
    count = 0
    while count < samples:
        stream.read(chunk)
        count += chunk
        time.sleep(0.01)
于 2013-01-07T03:42:39.273 回答