# Understand librosa.load() is Between -1.0 and 1.0 – Librosa Tutorial

By | November 17, 2021

When we use librosa.load() to read an audio file, we may get a numpy ndarray, the value of it is -1.0 and 1.0. In this tutorial, we will introduce you the reason.

We also can use scipy.io.wavfile.read() to read an audio file, we will get an integer numpy array. The difference between scipy.io.wavfile.read() and librosa.load() you can read this tutorial:

We can use code below to read an audio.

import librosa
import soundfile as sf

audio_file = r'F:\6.wav'

audio, sr = librosa.load(audio_file, sr= 8000, mono=True)
print(audio.shape, sr)
print(audio)

Run this code, we will get this result:

(101600,) 8000
[-0.00024414 -0.00024414  0.00024414 ... -0.00170898 -0.00219727
-0.0012207 ]

The audio data is -1.0 and 1.0

librosa.load(path, sr=22050, mono=True, offset=0.0, duration=None, dtype=<class 'numpy.float32'>, res_type='kaiser_best')

It will call soundfile.read() to read an audio file, you can find answer in its source code. Look at example code below:

#read wav data
audio, sr = librosa.load(audio_file, sr= 8000, mono=True)
print(audio.shape, sr)
print(audio)

print(audio)

We will see: We will find:

As to soundfile.read(), it will get different audio data based on dtype.

dtype ({‘float64’, ‘float32’, ‘int32’, ‘int16’}, optional) –

Data type of the returned array, by default ‘float64’. Floating point audio data is typically in the range from -1.0 to 1.0. Integer data is in the range from -2**15 to 2**15-1 for ‘int16’ and from -2**31 to 2**31-1 for ‘int32’.

As to librosa.load(), the default type of it is numpy.float32, it will determines the dtype is float32 in soundfile.read(). It means the audio data is -1.0 to 1.0

As to our wav file, it is pcm 16bits. You can find your audio data format by following this tutorial:

View Audio Sample Rate, Data Format PCM or ALAW Using ffprobe – Python Tutorial

The wav data is limited in -2**15 to 2**15-1

We can set the data type is numpy.int32. Here is an example:

#read wav data
audio, sr = librosa.load(audio_file, sr= 8000, mono=True)
print(audio) 