0
目前,我正在啓動我的主程序,該程序控制何時啓動揚聲器和麥克風線程。在那裏,我還可以控制設備上的靜音/取消靜音(USB耳機)等。音頻線程位於音頻類中的單獨文件中。使用Python捕獲USB輸入線程(麥克風)中的一個音頻採樣
此代碼有效。現在它以預設的特定循環數捕捉音頻樣本。我希望在主程序請求時抓取音頻採樣,但是我沒有成功設置標誌並在麥克風線程中檢查它。我會得到pyaudio錯誤,如溢出/下溢。
如果有人會提出一種技術來抓取音頻輸入樣本(麥克風數據),我將不勝感激。感謝
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()