Python Forum

Full Version: Problems feeding live input from my microphone into a keras model (SegFault: 11)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to write a voice-activated goal horn in Python.

I'm using my own audio recordings to train the model, but I need my model to make predictions on live input. I believe I've figured out how to store my most recent audio data, but I run into problems when I try to extract the MFCC's.

import pyaudio
import librosa
import numpy as np

class Counter:
    def __init__(self):
        self.value = 0
    
    def add(self):
        self.value += 1
    
    def get(self):
        return self.value

class RingBuffer:
    """ class that implements a not-yet-full buffer """
    def __init__(self,size_max):
        self.max = size_max
        self.data = []

    class __Full:
        """ class that implements a full buffer """
        def append(self, x):
            """ Append an element overwriting the oldest one. """
            self.data[self.cur] = x
            self.cur = (self.cur+1) % self.max
        def get(self):
            """ return list of elements in correct order """
            return self.data[self.cur:]+self.data[:self.cur]

        def clear(self):
            self.data = []
            self.__class__ = RingBuffer


    def append(self,x):
        """append an element at the end of the buffer"""
        self.data.append(x)
        if len(self.data) == self.max:
            self.cur = 0
            # Permanently change self's class from non-full to full
            self.__class__ = self.__Full

    def get(self):
        return self.data

j = Counter()
ringBuffer = RingBuffer(1*22050)

def callback(in_data, frame_count, time_info, flag):
        
    audio_data = np.frombuffer(in_data, dtype=np.float32)
    audio_data = librosa.resample(audio_data, 44100, 22050)

    for i in audio_data:
        ringBuffer.append(i)

    signal = ringBuffer.get()
    signal = np.array(signal)

    if signal.shape[0] == 22050:
        print("\ncheckpoint 1")
        X_new = librosa.feature.mfcc(signal, sr=22050, n_mfcc=44, hop_length=512, n_fft=2048) # causes segfault: 11... WHY?
        print("checkpoint 2")
        print(j.get())

        j.add()
        
    return (in_data, pyaudio.paContinue)

pa = pyaudio.PyAudio()
stream = pa.open(format = pyaudio.paFloat32,
                 channels = 1,
                 rate = 44100,
                 output = False,
                 input = True,
                 stream_callback=callback)

stream.start_stream()

while stream.is_active():
    1
            
stream.close()
pa.terminate()

print("Program terminated. \n")
Output:
... checkpoint 1 checkpoint 2 136 checkpoint 1 checkpoint 2 137 checkpoint 1 checkpoint 2 138 checkpoint 1 Segmentation fault: 11
For some reason, when I call librosa.features.mfcc enough times (the exact number is inconsistent but always appears to be somewhere between 110 and 150), the program crashes. I've pinpointed the line that's producing the segfault, but I don't know why.

How can I resolve this issue?

Thank you.
It turns out because I was calling librosa.feature.mfcc inside my callback function, that was causing the problem. I still don’t know exactly why that was an issue, but I solved the problem.