def encode(self, s):
"""Transform a string with a filename into a list of float32.
Args:
s: path to the file with a waveform.
Returns:
samples: list of int16s
"""
# Make sure that the data is a single channel, 16bit, 16kHz wave.
# TODO(chorowski): the directory may not be writable, this should fallback
# to a temp path, and provide instructions for instaling sox.
if not s.endswith(".wav"):
out_filepath = s + ".wav"
if not os.path.exists(out_filepath):
call(["sox", "-r", "16k", "-b", "16", "-c", "1", s, out_filepath])
s = out_filepath
rate, data = wavfile.read(s)
assert rate == self._sample_rate
assert len(data.shape) == 1
if data.dtype not in [np.float32, np.float64]:
data = data.astype(np.float32) / np.iinfo(data.dtype).max
return data.tolist()
评论列表
文章目录