我正在尝试理解我在PyAudio中看到的东西。我正在尝试采用200ms的音频样本,等待几秒钟,然后再采集三个200ms的音频样本。请考虑以下代码:
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
请注意,虽然第一次和第二次录制之间的时间戳有一秒钟的延迟,但是在录制2和3之间根本没有延迟,并且它们似乎是在零时间内拍摄的(尽管它们实际上是在包含200ms的数据)。录音4似乎需要200ms才能获得,但是与录音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与任何缓冲区的交互,有没有办法更好地了解我的音频实际捕获的时间?
答案 0 :(得分:0)
音频设备将每秒将RATE样本连续添加到缓冲区,并从此缓冲区中读取代码。
所以你不能在没有从缓冲区读取的情况下睡觉(1)。您可以编写一些代码来跳过示例:
def skip(seconds):
samples = int(seconds * RATE)
count = 0
while count < samples:
stream.read(chunk)
count += chunk
time.sleep(0.01)