TharinduRanathunga
TharinduRanathunga

Reputation: 221

How to read a audio file in Python similar to Matlab audioread?

I'm using wavefile.read() in Python to import a audio file to Python. What I want is read a audio file where every sample is in double and normalized to -1.0 to +1.0 similar to Matlab audioread() function. How can I do it ?

Upvotes: 5

Views: 12951

Answers (2)

Maulik Madhavi
Maulik Madhavi

Reputation: 166

Use SciPy:

import scipy.io.wavfile as wav

fs, signal = wav.read(file_name)

signal = signal / 32767 # 2**15 - 1

You need to divide it by the maximal value of Int16 if you want to have exactly the same thing as in Matlab.

Warning: if the WAV file is Int32 encoded, you need to normalize it by the maximal value of Int32.

NOTE: Using the following shortcut: signal /= 32767 fails due to the numpy cast issue.

Upvotes: 8

TheBlackCat
TheBlackCat

Reputation: 10328

Use read function of the PySoundFile package. By default it will return exactly what you request: a numpy array containing the sound file samples in double-precision (Float64) format in the range -1.0 to +1.0.

Upvotes: 8

Related Questions