2017-03-15 50 views
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() 

回答

0

此方法有效,但間歇性溢/下溢的問題,但這些問題消失,一旦我設定的麥克風採樣速率達16kHz,而不是默認。我稍微改了一下代碼,以便我可以隨時從主程序中抽取一個樣本,使用全局標誌var,麥克風循環檢查是否應該抓取樣本。