PyAudio Input overflowed

后端 未结 9 1594
长发绾君心
长发绾君心 2020-11-30 04:04

I\'m trying to make real-time plotting sound in python. I need to get chunks from my microphone.

Using PyAudio, try to use

import pyaudio
import wav         


        
9条回答
  •  半阙折子戏
    2020-11-30 04:18

    I had the same issue on the really slow raspberry pi, but I was able to solve it (for most cases) by using the faster array module for storing the data.

    import array
    import pyaudio 
    
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    INPUT_CHANNEL=2
    RATE = 48000
    CHUNK = 512
    
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=INPUT_CHANNEL,
                    frames_per_buffer =CHUNK)
    
    print("* recording")
    
    
    try:
        data = array.array('h')
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data.fromstring(stream.read(CHUNK))
    finally:
        stream.stop_stream()
        stream.close()
        p.terminate()
    
    print("* done recording")
    

    The content of data is rather binary afterwards. But you can use numpy.array(data, dtype='i') to get a numpy array of intergers.

提交回复
热议问题