-
Notifications
You must be signed in to change notification settings - Fork 102
/
downsample_TIDIGITS.py
executable file
·42 lines (35 loc) · 1.07 KB
/
downsample_TIDIGITS.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
39
40
41
42
#!/usr/bin/python
# Downsample TIDIGITS
import os.path
import wave
OUTPUT_FS = 200; # Hz
INPUT_DIR = '../TIDIGITS-Downsampled'
WAV_EXT = '.wav'
def process(arg, dirname, names):
print 'Processing directory', dirname
wav_files = filter(lambda x: os.path.splitext(x)[1].lower() == WAV_EXT, names)
for f in wav_files:
filename = os.path.join(dirname, f)
print 'Processing', filename
wav_obj = wave.open(filename, 'rb')
original_fs = wav_obj.getframerate()
frames_num = wav_obj.getnframes()
samp_width = wav_obj.getsampwidth()
params = wav_obj.getparams()
wav_data = wav_obj.readframes(frames_num)
wav_obj.close()
N = original_fs / OUTPUT_FS # downsampling factor
downsampled_data = ''
for i in xrange(frames_num/N):
pos = i*N*samp_width
downsampled_data += wav_data[pos:pos + samp_width]
wav_obj = wave.open(filename, 'wb')
wav_obj.setparams(params)
wav_obj.setframerate(OUTPUT_FS)
wav_obj.writeframes(downsampled_data)
wav_obj.close()
def main():
print 'Downsampling TIDIGITS'
os.path.walk(INPUT_DIR, process, None)
if __name__ == '__main__':
main()