Data changed after saving with soundfile and reading with librosa

350 Views Asked by At

I am processing an audio file with librosa as:

import librosa
import soundfile as sf

y,sr = librosa.cora.load('test.wav', sr=22050)
y_processed = some_processing(y)
sf.write('test_processed.wav', y_processed , sr)
y_read = librosa.cora.load('test_processed.wav', sr=22050)

Now the issue is that y_processed and y_read do not match. My understanding is that this comes from some encoding done by soundfile library. Why is this happening and how can I get from y_processed to y_read without saving?

1

There are 1 best solutions below

0
On

According to this article, librosa.load(), along with other things, normalizes the bit depth between -1 and 1.

I experienced the same problem as you did, where the min and max values of the "loaded" signal were much closer to each other.

Since I don't exactly how your data differs from each other, this may not help you, but this has helped me.

y_processed_buf = librosa.util.buf_to_float(y_processed)

This seems to be the culprit, which would normalizes your values (source code). It is also called during librosa.load(), which is how I stumbled over it.