簡體   English   中英

使用python從USB輸入線程(麥克風)捕獲一個音頻樣本

[英]capturing one audio sample from a usb input thread (microphone) with 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()

這種方法有效,但是間歇性地出現上溢/下溢問題,但是一旦我將麥克風采樣率設置為16kHz而不是默認值,這些問題就消失了。 我稍稍更改了代碼,以便可以通過使用全局標志var隨時從主程序中獲取示例,而mic循環會檢查該全局標志以了解是否應獲取示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM