1
1
import numpy as np
2
+ import wave
2
3
import scipy .io .wavfile
3
4
4
5
def enframe (y , frameSize , overlap ):
@@ -15,23 +16,41 @@ def enframe(y, frameSize, overlap):
15
16
return out
16
17
17
18
def audioread (fileName ):
18
- fs , au = scipy .io .wavfile .read (fileName )
19
- # conver to float64 and normalize between -1 and 1
19
+ #fs, au = scipy.io.wavfile.read(fileName)
20
+ audioFile = wave .open (fileName , 'rb' )
21
+ # get file information and read sample data as string bytes
22
+ nChannels , bitPerSample , fs , nFrames = audioFile .getparams ()[:4 ]
23
+ strData = audioFile .readframes (nFrames )
24
+ audioFile .close ()
25
+ if bitPerSample > 4 :
26
+ raise ValueError ('Bit per sample can not be greater than 4.' )
27
+ elif bitPerSample == 4 :
28
+ au = np .matrix (np .fromstring (strData , dtype = np .int32 )/ (2 ** (bitPerSample * 8 - 1 )), dtype = np .float64 ).reshape (- 1 , nChannels )
29
+ elif bitPerSample == 3 :
30
+ au = np .matrix (np .array ([int .from_bytes (strData [i :i + bitPerSample ], byteorder = 'little' , signed = True ) for i in range (0 , len (strData ), bitPerSample )], dtype = np .float64 )/ (2 ** (bitPerSample * 8 - 1 ))).reshape (- 1 , nChannels )
31
+ elif bitPerSample == 2 :
32
+ au = np .matrix (np .fromstring (strData , dtype = np .int16 )/ (2 ** (bitPerSample * 8 - 1 )), dtype = np .float64 ).reshape (- 1 , nChannels )
33
+ elif bitPerSample == 1 :
34
+ au = np .matrix ((np .array (np .fromstring (strData , dtype = np .uint8 ), dtype = np .float64 )- 128 )/ (2 ** (bitPerSample * 8 - 1 ))).reshape (- 1 , nChannels )
35
+
36
+ '''
20
37
if au.dtype == 'uint8':
21
38
au = np.matrix(au.astype(np.float64) / (2**7)).reshape(au.shape[0], -1)
22
39
elif au.dtype == 'int16':
23
40
au = np.matrix(au.astype(np.float64) / (2**15)).reshape(au.shape[0], -1)
24
41
else:
25
42
au = np.matrix(au.astype(np.float64)).reshape(au.shape[0], -1)
26
- return (fs , au )
43
+ '''
44
+ return (fs , au , bitPerSample )
27
45
28
46
def main ():
29
47
import os
30
48
fileList = os .listdir ('./testAudio/' )
31
49
for i in fileList :
32
- fs , au = audioread (os .path .join (os .getcwd (), 'testAudio' , i ))
50
+ fs , au , bitPerSample = audioread (os .path .join (os .getcwd (), 'testAudio' , i ))
33
51
print ('==========================' )
34
52
print (i )
53
+ print (bitPerSample )
35
54
print (au .dtype )
36
55
print (au .shape )
37
56
print ('==========================' )
0 commit comments