Question

I'm trying to open a wav file in python and inspect its contents. I followed 3 different approaches : use scipy.io.wavfile , use wave, directly read the contents from the file.

Approach 1: using the wave module

>>> import wave
>>> import numpy as np
>>> wf = wave.open('test.wav','rb')
>>> f = wf.readframes(160)
>>> data = np.fromstring(f)
>>> print data
[  2.82596915e+103   7.66148517e+283   1.55915666e-083  -7.82893925e-042
  -2.23287375e-293  -4.58038073e+011   8.93193919e-027   1.52567878e+292
   3.88980986e+055  -3.00054045e+099  -1.41604458e-280  -7.16366832e-116
   5.91987737e-176   3.41189939e+258   3.87973542e+168  -4.11714940e+254
  -1.03609882e-226  -4.36842462e-213  -7.51023721e+282   2.32154921e+185
   2.13137319e+248   1.08450144e-203  -1.69687232e-135  -5.92443342e-274
  -1.67041051e+126   5.56090419e+077   1.73494487e+289   4.44998489e-052
  -2.26308769e-013  -3.43049660e-294  -2.02115225e-018   1.52038226e-057
   2.80968683e+288   2.21954395e+082  -9.30599671e+131  -2.27150239e-272
  -1.49931560e-139   1.28957321e-209   1.00441910e+246   6.07714540e+188]
>>> wdata = np.array(data,dtype='int32')
>>> print wdata
[-2147483648 -2147483648           0           0           0 -2147483648
           0 -2147483648 -2147483648 -2147483648           0           0                                                                                                                        
           0 -2147483648 -2147483648 -2147483648           0           0                                                                                                                        
 -2147483648 -2147483648 -2147483648           0           0           0                                                                                                                        
 -2147483648 -2147483648 -2147483648           0           0           0                                                                                                                        
           0           0 -2147483648 -2147483648 -2147483648           0                                                                                                                        
           0           0 -2147483648 -2147483648]                                   

Approach 2 : Using the scipy.io.wavfile module. Continues from the previous approach

>>> import scipy                    
>>> from scipy.io.wavfile import read
>>> fs,wavdata=read("test.wav")
>>> print fs
11025
>>> print wavdata
[    0  7940 15384 ..., 31997 30888 27848]

Approach 3 : Directly read from the file

>>> print np.fromstring(open('test.wav').read(160),dtype='int32')
[ 1179011410       80036  1163280727   544501094          16       65537
       11025       22050     1048578  1635017060       80000   520355840
  1432960024  1992649059  2061532345  1622700376   784222732  -247394173
 -1218063638 -1888771989 -2094234250 -1783920464 -1034245812   -29761725
   981867849  1751864346  2090366516  1914010417  1266246103   306655331
  -728370426 -1584154406 -2049667020 -2010414313 -1475963714  -577978161
   462290033  1388787954  1973184366  2071690352]

In approach 1 and approach 3, I had to convert the data into int32. Scipy does this by default in approach 2. Then why would i be getting different outputs?

Was it helpful?

Solution

To get the same result. modify fromstring in the first approach as

if w.getsampwidth() == 1:
    data = np.fromstring(f, dtype=np.int8)
elif w.getsampwidth() == 2:
    data = np.fromstring(f, dtype=np.int16)
elif w.getsampwidth() == 4:
    data = np.fromstring(f, dtype=np.int32)

where w.getsampwidth() has how many byte(s) used for representing samples

The np.fromstring reads bytes as float64 then convert the values (not bytes) into int32 by astype('int32'). So you need to read bytes corresponding type among int8, 16 and 32

Second approach has no problem.

The wave file(maybe RIFF format) has 44 bytes header. So the last doesn't work properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top