1

i,m writing a code using pydub module to get data from an audio file , but i want to do the same actions using librosa module , how to convert my code and get the same result with librosa here's my code :

import numpy as np
from pydub import AudioSegment

audiofile = AudioSegment.from_file(filename)
data = np.fromstring(audiofile._data,np.int16)

channels = []
for c in range(audiofile.channels):
    channels.append(data[c::audiofile.channels])

fs = audiofile.frame_rate

return channels, fs 
noob
  • 11
  • 2

1 Answers1

3

You can Use:

from pydub import AudioSegment

sound = AudioSegment.from_file("file.wav")
samples = sound.get_array_of_samples()
arr = np.array(samples).astype(np.float32)/32768 # 16 bit 
arr = librosa.core.resample(arr, sound.frame_rate, 22050, res_type='kaiser_best') 
print(arr)

Output:

array([-0.0065596 , -0.00243502,  0.00489785, ..., -0.04385557,
       -0.04421588, -0.05063475], dtype=float32)

Test in librosa:

y, sr = librosa.load('file.wav', sr=22050)
print(y)

Output:

array([-0.0065596 , -0.00243502,  0.00489785, ..., -0.04385557,
       -0.04421588, -0.05063475], dtype=float32)