def pya_callback(self, in_data, frame_count, time_info, status):
import pyaudio
if status != 0:
self.junklog("pya_callback status %d\n" % (status))
pcm = numpy.fromstring(in_data, dtype=numpy.int16)
if self.chan == 1:
pcm = pcm[self.chan::2]
assert frame_count == len(pcm)
# time of first sample in pcm[], in seconds since start.
adc_time = time_info['input_buffer_adc_time']
# time of last sample
adc_end = adc_time + (len(pcm) / float(self.cardrate))
if self.last_adc_end != None:
expected = (adc_end - self.last_adc_end) * float(self.cardrate)
expected = int(round(expected))
shortfall = expected - len(pcm)
if abs(shortfall) > 20:
self.junklog("pya expected %d got %d" % (expected, len(pcm)))
#if shortfall > 100:
# pcm = numpy.append(numpy.zeros(shortfall, dtype=pcm.dtype), pcm)
self.last_adc_end = adc_end
# translate time of last sample to UNIX time
ut = time.time()
st = self.pya_strm.get_time()
unix_end = (adc_end - st) + ut
self.cardlock.acquire()
self.cardbufs.append([ pcm, unix_end ])
self.cardlock.release()
return ( None, pyaudio.paContinue )
评论列表
文章目录