def update(self, stream: Union[BinaryIO, np.ndarray, bytes]) -> float:

        #print('chunk_size:', self.chunk_size)

        if isinstance(stream, np.ndarray):
            buffer_audio = stream
        else:
            if isinstance(stream, (bytes, bytearray)):
                chunk = stream
            else:
                chunk = stream.read(self.chunk_size)
            if len(chunk) == 0:
                raise EOFError
            buffer_audio = buffer_to_audio(chunk)

        self.window_audio = np.concatenate((self.window_audio, buffer_audio))

        if len(self.window_audio) >= self.pr.window_samples:
            new_features = vectorize_raw(self.window_audio)
            self.window_audio = self.window_audio[len(new_features) *
                                                  self.pr.hop_samples:]
            if len(new_features) > len(self.mfccs):
                new_features = new_features[-len(self.mfccs):]
            self.mfccs = np.concatenate(
                (self.mfccs[len(new_features):], new_features))

        mfccs = self.mfccs
        if self.pr.use_delta:
            mfccs = add_deltas(self.mfccs)
        return self.runner.run(mfccs)
    def update(self, stream: Union[BinaryIO, np.ndarray, bytes]) -> float:
        if isinstance(stream, np.ndarray):
            buffer_audio = stream
        else:
            if isinstance(stream, (bytes, bytearray)):
                chunk = stream
            else:
                chunk = stream.read(self.chunk_size)
            if len(chunk) == 0:
                raise EOFError
            buffer_audio = buffer_to_audio(chunk)

        self.window_audio = np.concatenate((self.window_audio, buffer_audio))

        if len(self.window_audio) >= self.pr.window_samples:
            new_features = self.mfcc(self.window_audio, self.pr.sample_rate,
                                     self.pr.window_t, self.pr.hop_t,
                                     self.pr.n_mfcc, self.pr.n_filt,
                                     self.pr.n_fft)
            self.window_audio = self.window_audio[len(new_features) *
                                                  self.pr.hop_samples:]
            if len(new_features) > len(self.features):
                new_features = new_features[-len(self.features):]
            self.features = np.concatenate(
                (self.features[len(new_features):], new_features))

        return self.runner.run(self.features)
Example #3
0
 def get_prediction(chunk):
     nonlocal audio_buffer
     audio = buffer_to_audio(chunk)
     audio_buffer = np.concatenate((audio_buffer[len(audio):], audio))
     return listener.update(chunk)
Example #4
0
 def get_prediction(self, chunk):
     audio = buffer_to_audio(chunk)
     self.audio_buffer = np.concatenate(
         (self.audio_buffer[len(audio):], audio))
     return self.listener.update(chunk)