extract MFCCs feature out of pyaudio live audio stream

Viewed 11

I want to obtain this live audio stream of data to extract MFCCs feature without writing the stream to a wave file and reading back. How to convert the stream data to MFCC features?

I have this code here that has 2 functions:

  1. function takes microphone input using Pyaudio (Listen())
  2. function that takes the data from the first function and try to predict if it's class 0 or 1 (predict())

this script is to use a machine learning model to predict live audio (binary classification)

import pyaudio
def listen():
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    CHUNK = 1024

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

    data = np.frombuffer(stream.read(CHUNK),dtype=np.int16)
    peak=np.average(np.abs(data))*2
    bars="#"*int(50*peak/2**16)
    arr = np.array("%04d %05d %s"%(i,peak,bars))

    mfcc = librosa.feature.mfcc(y=arr, sr=RATE, n_mfcc=40)
    mfcc_processed = np.mean(mfcc.T, axis=0)
    prediction_thread(mfcc_processed)
    time.sleep(0.001)
    

def predict(y):
    prediction = model.predict(y)
    if prediction[:, 1] > 0.96:
        print("ready!")
    else:
        print("not ready")

However it raises this error

raise ParameterError("Audio data must be floating-point")
librosa.util.exceptions.ParameterError: Audio data must be floating-point

and I can't predict on the live audio that pyaudio is catching.

0 Answers
Related