5

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.

Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    AFAIK, librosa itself only wraps other libs like [audioread](https://pypi.org/project/audioread/) to decode audio. librosa's focus is definitely not providing this kind of info. – Hendrik Jul 17 '19 at 09:28

1 Answers1

8

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?

Julian Fortune
  • 172
  • 1
  • 7
  • 1
    thank you for the knowledge sharing. Just a matter of interest, no specific problem so far. :) – Mark K Jul 19 '19 at 06:38
  • Just fyi: In v0.7 you should use `mono=False` parameter – akashrajkn Aug 07 '21 at 22:17
  • This method requires reading the file. @MarkK is that also the case for `wave.open().getparams()`? Does librosa offer a way to get parameters without need to read file? – Thiago Gouvea Oct 25 '22 at 14:37
  • @ThiagoGouvea, reading the file looks like a usual way to get info from it. So I feel ok no matter a method reads the file or not. :) – Mark K Oct 26 '22 at 08:39
  • I should have been more precise: librosa.load() seems to read the entire file content, when reading [the header should suffice to obtain the metadata](https://docs.fileformat.com/audio/wav/) you are asking for. That could make a big difference when trying to obtain only metadata for a large collection of files (which is my case, and potentially that of many coming to this page). – Thiago Gouvea Oct 26 '22 at 11:48