Search code examples
pythonaudiolibrosa

Librosa to get basic parameters of audio


In getting basic parameters of an audio file, by Wave:

import wave

data = wave.open('c:\\sample.wav', mode = 'rb')
params = data.getparams()
print params

It returns:

(1, 2, 4000, 160000, 'NONE', 'not compressed')

That's for: nchannels=1, sampwidth=2, framerate=16000, nframes=47104, comptype='NONE', compname='not compressed

I assume Librosa has similar functions but no find-outs after searches.

Does Librosa have commands to produce similar results?

Thank you.


Solution

  • Librosa Core has some of the functionality you're looking for. librosa.core.load will load a file like wave, but will not give as detailed information.

    import librosa
    
    # Load an example file in .ogg format
    fileName = librosa.util.example_audio_file()
    audioData, sampleRate = librosa.load(fileName)
    
    print(audioData)
    >>> [ -4.756e-06,  -6.020e-06, ...,  -1.040e-06,   0.000e+00]
    
    print(audioData.shape)
    >>> (1355168,)
    
    print(sampleRate)
    >>> 22050
    

    The shape of audioData will tell you the number of channels. A shape like (n,) is mono, and (2, n) is stereo. The n in the shape is the length of the audio in samples. If you want the length in seconds check out librosa.core.get_duration.

    Like @hendrick mentions in his comment, the Librosa advanced I/O page says librosa uses soundfile and audioread for audio I/O, and the load source code shows it's just wrapping around those libraries.

    However, there shouldn't be any issue with using wave for loading the audio file and librosa for analysis as long as you follow the librosa API. Is there a particular problem you're having, or goal you need to achieve?