-
Notifications
You must be signed in to change notification settings - Fork 71
/
cutoff.py
38 lines (29 loc) · 1.03 KB
/
cutoff.py
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
import wave
import numpy as np
def cutoff(input_wav, output_wav):
'''
input_wav --- input wav file path
output_wav --- output wav file path
'''
# read input wave file and get parameters.
with wave.open(input_wav, 'r') as fw:
params = fw.getparams()
# print(params)
nchannels, sampwidth, framerate, nframes = params[:4]
strData = fw.readframes(nframes)
waveData = np.fromstring(strData, dtype=np.int16)
max_v = np.max(abs(waveData))
for i in range(waveData.shape[0]):
if abs(waveData[i]) > 0.08 * max_v:
break
for j in range(waveData.shape[0] - 1, 0, -1):
if abs(waveData[j]) > 0.08 * max_v:
break
# write new wav file
with wave.open(output_wav, 'w') as fw:
params = list(params)
params[3] = nframes - i - (waveData.shape[0] - 1 - j)
fw.setparams(params)
fw.writeframes(strData[2 * i:2 * (j + 1)])
if __name__ == '__main__':
cutoff('eval.wav', 'eval_cutoff.wav')