I have one wav file which I resampled to 16.000 kHz with Audacity. Now I am trying to load the file with python with 2 different ways.
import tensorflow as tf
import librosa
f = "path/to/wav/file/xxxx.wav"
raw = tf.io.read_file(f)
audio, sr = tf.audio.decode_wav(raw, desired_channels=1)
print("Sample Rate TF: ",sr.numpy())
y, sr2 = librosa.load(f)
print("Sample Rate librosa: ",sr2)
#Sample Rate TF: 16000
#Sample Ratelibrosa: 22050
Why is the sample rate so different for the same file? Which library I can trust more?
CodePudding user response:
This is not a question of "trust". Both functions do what they are supposed to do. The TF version apparently does not resample the audio. Librosa, by default, resamples to 22,050 Hz (for whatever reason). Please read the docs. You can avoid this by calling
y, sr2 = librosa.load(f, sr=None)
In general, the sr
argument provides the sampling rate to resample to; by passing None
, you prevent resampling.