Skip to content
Permalink
Browse files

fix(audio): restrict max length for mfcc encoding

  • Loading branch information...
jemmyshin committed Aug 9, 2019
1 parent e773aa3 commit 4cb83383c341799536b8337eedfec8be13eeb912
Showing with 6 additions and 5 deletions.
  1. +4 −5 gnes/encoder/audio/mfcc.py
  2. +2 −0 gnes/service/indexer.py
@@ -26,10 +26,11 @@
class MfccEncoder(BaseAudioEncoder):
batch_size = 64

def __init__(self, n_mfcc: int = 13, sample_rate: int = 16000, *args, **kwargs):
def __init__(self, n_mfcc: int = 13, sample_rate: int = 16000, max_length: int = 100, *args, **kwargs):
super().__init__(*args, **kwargs)
self.n_mfcc = n_mfcc
self.sample_rate = sample_rate
self.max_length = max_length

@batching
def encode(self, data: List['np.array'], *args, **kwargs) -> np.ndarray:
@@ -38,10 +39,8 @@ def encode(self, data: List['np.array'], *args, **kwargs) -> np.ndarray:
mfccs = [np.array(librosa.feature.mfcc(y=audio, sr=self.sample_rate, n_mfcc=self.n_mfcc).T)
for audio in data]

max_lenth = max([len(mf) for mf in mfccs])

mfccs = [np.concatenate((mf, np.zeros((max_lenth - mf.shape[0], self.n_mfcc), dtype=np.float32)), axis=0)
if mf.shape[0] < max_lenth else mf for mf in mfccs]
mfccs = [np.concatenate((mf, np.zeros((self.max_length - mf.shape[0], self.n_mfcc), dtype=np.float32)), axis=0)
if mf.shape[0] < self.max_length else mf[:self.max_length] for mf in mfccs]
mfccs = [mfcc.reshape((1, -1)) for mfcc in mfccs]
mfccs = np.squeeze(np.array(mfccs), axis=1)
return mfccs
@@ -44,6 +44,8 @@ def _handler_index(self, msg: 'gnes_pb2.Message'):
offsets += [c.offset_nd for c in d.chunks]
elif d.doc_type == gnes_pb2.Document.VIDEO:
offsets += [c.offset_1d for c in d.chunks]
elif d.doc_type == gnes_pb2.Document.AUDIO:
offsets += [c.offset_1d for c in d.chunks]
weights += [c.weight for c in d.chunks]

from ..indexer.base import BaseVectorIndexer, BaseTextIndexer

0 comments on commit 4cb8338

Please sign in to comment.
You can’t perform that action at this time.