Librosa.resample() resamples to a lower rate than needed

I am doing some audio pre-processing to train a ML model. All the audio files of the dataset are: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz. I am using the following snippet of code to resample the dataset to 8000 Hz:

I am doing some audio pre-processing to train a ML model.
All the audio files of the dataset are:

RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz.

I am using the following snippet of code to resample the dataset to 8000 Hz:

samples, sample_rate = librosa.load(filename, sr = 16000) samples = librosa.resample(samples, sample_rate, 8000) 

then I use the following snippet to reshape the new samples:

samples.reshape(1,8000,1)

but for some reason, I keep getting the following error: ValueError: cannot reshape array of size 4000 into shape (1,8000,1) but the size differs from a file to another, but it's always less than 8000 HZ (the desired sample rate).

I doubled checked the original sample rate and it was 16000 Hz, I also tried to load the files with a sample rate of 8000, but I had no luck.

2

1 Answer

I think you mess between sample rate and length of the samples array. The samples.reshape reshapes the array and depends on the length of the array rather the sample rate. I'm not sure what shape you need exact , but the following code should work without errors

samples.reshape(1,len(samples),1) 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobXBwZ2WDdYOOpaCbqp%2Borm6%2BxKyYpqicmnqzsdKapKmklah6tbuMmmSlp6eav26%2BwK2cZqyYlrtuusSem56c

 Share!