[英]capturing one audio sample from a usb input thread (microphone) with python
Currently, I'm launching my main program that controls when I launch the speaker and mic threads. 目前,我正在启动主程序,该程序控制何时启动扬声器和麦克风线程。 From there I can also control mute/unmute on the device (a usb headset), etc. The audio threads are in a separate file in a an audio class. 从那里,我还可以控制设备(USB耳机)上的静音/取消静音等。音频线程位于音频类的单独文件中。
This code works. 此代码有效。 Right now it captures an audio sample at a particular loop count that is pre-set. 现在,它以预设的特定循环计数捕获音频样本。 I would prefer to grab an audio sample whenever requested from the main program, but I have not had any success at setting a flag and checking it in the mic thread. 每当从主程序请求时,我都希望获取音频样本,但是在设置标志并在麦克风线程中检查它并没有取得任何成功。 I would get pyaudio errors, like overflow/underflow. 我会得到pyaudio错误,例如上溢/下溢。
I would appreciate if somebody would suggest a technique to grab audio input samples (mic data). 如果有人建议使用一种技术来捕获音频输入样本(麦克风数据),我将不胜感激。 thanks 谢谢
def openTheMic(self, **kwargs):
# script can over-ride any value in the myAudio __init__
print ("***in openTheMic ***")
# picks up values passed by the test_script and maps them to myAudio class,
# otherwise will use defaults set in class
for (k,v) in kwargs.iteritems():
#print("k = %s, v = %s" % (k,v))
setattr(self, myAudio._map[k], v)
stream = self.p.open(
format = self.FORMAT,
channels = self.CHANNELS,
rate = self.RATE,
input = True,
output = True,
frames_per_buffer = self.CHUNK
)
setMicThreadStartTime(time.time())
print("time @ start of mic thread is: %s" % time.time())
starttime = time.time()
while myAudio.openTheMicThreadActive == True:
for i in range(0, 1200):
data = stream.read(self.CHUNK)
captureCount = 1000
if i == captureCount:
currentData = data
# abort the mic and spkr threads
myAudio.openTheMicThreadActive = False
myAudio.playDeadAirThreadActive = False
print("i is: %i: " % (i))
# set a global variable to get the data to the main program
setAudioData(currentData)
print("capture time: i = %s, time is %s " % (i, time.time()))
stream.stop_stream()
stream.close()
print ("***closed the stream in openTheMic *** and the time is: %s" % time.time())
self.p.terminate()
This method worked, but intermittently with overflow/underflow issues, but those problems disappeared once I set the mic sample rate to 16kHz instead of the default. 这种方法有效,但是间歇性地出现上溢/下溢问题,但是一旦我将麦克风采样率设置为16kHz而不是默认值,这些问题就消失了。 I changed the code a little so that I can take a sample at any time from the main program by using a global flag var that the mic loop checks to know if it should grab a sample or not. 我稍稍更改了代码,以便可以通过使用全局标志var随时从主程序中获取示例,而mic循环会检查该全局标志以了解是否应获取示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.