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 evaluate(self, audio: np.ndarray) -> np.ndarray: print('MFCCs...') mfccs = vectorize_raw(audio) print('Splitting...') mfcc_hops = self.args.chunk_size // pr.hop_samples inputs = np.array([ mfccs[i - pr.n_features:i] for i in range(pr.n_features, len(mfccs), mfcc_hops) ]) print('Predicting...') predictions = self.runner.predict(inputs) return predictions