This repository was archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
71 lines (64 loc) · 2.45 KB
/
util.py
File metadata and controls
71 lines (64 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import wave
def enframe(y, frameSize, overlap):
if len(y.shape) == 2:
y = np.mean(y, axis=1)
y.shape = (-1, 1)
step = frameSize - overlap
frameCount = int(np.floor((len(y)-overlap)/step))
out = np.matrix(np.zeros((frameSize, frameCount), dtype=np.float64))
for i in range(0, frameCount):
startIndex = i*step
out[:, i] = y[startIndex:(startIndex+frameSize), 0]
return out
def audioread(fileName):
#fs, au = scipy.io.wavfile.read(fileName)
audioFile = wave.open(fileName, 'rb')
# get file information and read sample data as string bytes
nChannels, bytePerSample, fs, nFrames = audioFile.getparams()[:4]
strData = audioFile.readframes(nFrames)
audioFile.close()
if bytePerSample > 4:
raise ValueError('Bit per sample can not be greater than 4.')
elif bytePerSample == 4:
au = np.matrix(np.fromstring(strData, dtype=np.int32)/(2**(bytePerSample*8-1)), dtype=np.float64).reshape(-1, nChannels)
elif bytePerSample == 3:
au = np.matrix(np.array([int.from_bytes(strData[i:i+bytePerSample], byteorder='little', signed=True) for i in range(0, len(strData), bitPerSample)], dtype=np.float64)/(2**(bitPerSample*8-1))).reshape(-1, nChannels)
elif bytePerSample == 2:
au = np.matrix(np.fromstring(strData, dtype=np.int16)/(2**(bytePerSample*8-1)), dtype=np.float64).reshape(-1, nChannels)
elif bytePerSample == 1:
au = np.matrix((np.array(np.fromstring(strData, dtype=np.uint8), dtype=np.float64)-128)/(2**(bitPerSample*8-1))).reshape(-1, nChannels)
return (fs, au)
def audiowrite(fileName, au, fs, **kwargs):
if len(kwargs) == 0:
bitPerSample = 16
else:
for i in kwargs:
if i == 'bitPerSample':
bitPerSample = kwargs[i]
if (bitPerSample % 8 != 0) or (bitPerSample > 32):
raise ValueError('Bit per sample should be 8, 16, 24 or 32.')
au = au*(2**(bitPerSample-1))
if bitPerSample == 8:
au = au + 128
au = au.astype(np.uint8)
elif bitPerSample == 16:
au = au.astype(np.int16)
elif bitPerSample == 24:
pass
elif bitPerSample == 32:
au = au.astype(np.int32)
audioFile = wave.open(fileName, 'wb')
audioFile.setnchannels(au.shape[1])
audioFile.setsampwidth(int(bitPerSample/8))
audioFile.setframerate(fs)
audioFile.writeframes(au.tostring())
audioFile.close()
def main():
fs, au = audioread('testAudio/mono.wav')
audiowrite('test.wav', au, fs)
audiowrite('test8.wav', au, fs, bitPerSample=8)
audiowrite('test16.wav', au, fs, bitPerSample=16)
audiowrite('test32.wav', au, fs, bitPerSample=32)
if __name__ == '__main__':
main()