def wav_data_to_samples(wav_data, sample_rate):
"""Read PCM-formatted WAV data and return a NumPy array of samples.
Uses scipy to read and librosa to process WAV data. Audio will be converted to
mono if necessary.
Args:
wav_data: WAV audio data to read.
sample_rate: The number of samples per second at which the audio will be
returned. Resampling will be performed if necessary.
Returns:
A numpy array of audio samples, single-channel (mono) and sampled at the
specified rate, in float32 format.
Raises:
AudioIOReadException: If scipy is unable to read the WAV data.
AudioIOException: If audio processing fails.
"""
try:
# Read the wav file, converting sample rate & number of channels.
native_sr, y = scipy.io.wavfile.read(six.BytesIO(wav_data))
except Exception as e: # pylint: disable=broad-except
raise AudioIOReadException(e)
if y.dtype != np.int16:
raise AudioIOException('WAV file not 16-bit PCM, unsupported')
try:
# Convert to float, mono, and the desired sample rate.
y = int16_samples_to_float32(y)
if y.ndim == 2 and y.shape[1] == 2:
y = y.T
y = librosa.to_mono(y)
if native_sr != sample_rate:
y = librosa.resample(y, native_sr, sample_rate)
except Exception as e: # pylint: disable=broad-except
raise AudioIOException(e)
return y
评论列表
文章目录