Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
fix(audio): restrict max length for mfcc encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmyshin committed Aug 9, 2019
1 parent e773aa3 commit 4cb8338
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 4 additions & 5 deletions gnes/encoder/audio/mfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
2 changes: 2 additions & 0 deletions gnes/service/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4cb8338

Please sign in to comment.