如何从python访问4个外部有线麦克风?

时间:2019-11-03 21:25:40

标签: python python-3.x pyaudio

我想从我的python脚本访问4个外部有线麦克风。 我可以使用笔记本电脑的内置麦克风,但现在想访问外部麦克风

我已经使用了笔记本电脑的麦克风

import pyaudio
import numpy as np
import time
import wave



# open stream
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

CHUNK = 100 # RATE / number of updates per second

RECORD_SECONDS = 1


# use a Blackman window
window = np.blackman(CHUNK)

x = 0



def soundPlot(stream,y):
    t1=time.time()
    data = stream.read(CHUNK, exception_on_overflow=False)
    waveData = wave.struct.unpack("%dh"%(CHUNK), data)
    npArrayData = np.array(waveData)
    indata = npArrayData*window

    fftData=np.abs(np.fft.rfft(indata))
    fftTime=np.fft.rfftfreq(CHUNK, 1./RATE)
    which = fftData[1:].argmax() + 1

   # print("took %.02f ms"%((time.time()-t1)*1000))
    # use quadratic interpolation around the max
    if which != len(fftData)-1:
        y0,y1,y2 = np.log(fftData[which-1:which+2:])
        x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
        # find the frequency and output it
        thefreq = (which+x1)*RATE/CHUNK

        if thefreq>19400 and thefreq<19415:
            print ("The freq is %f Hz." % (thefreq),y)
            y=y+1
    else:
        thefreq = which*RATE/CHUNK

        if thefreq>19400 and thefreq<19415:
            print ("The freq is %f Hz." % (thefreq),y)
            y=y+1

p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=CHANNELS,rate=RATE,input=True,
              frames_per_buffer=CHUNK)

y=0    
while True:
    y=y+1
    soundPlot(stream,y)

stream.stop_stream()
stream.close()
p.terminate()

这是我在19400到19415之间打印频率的代码。目前,我正在使用笔记本电脑的麦克风。现在,我要访问外部有线麦克风。

0 个答案:

没有答案