5

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 ?

TharinduRanathunga
  • 221
  • 1
  • 4
  • 9

2 Answers2

8

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.

TheBlackCat
  • 9,791
  • 3
  • 24
  • 31
8

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.

satk0
  • 170
  • 1
  • 9
Maulik Madhavi
  • 156
  • 1
  • 3