-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavfile.py
48 lines (35 loc) · 1.51 KB
/
wavfile.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
43
44
45
46
47
48
import mmap
import sys
import warnings
import numpy
from scipy.io.wavfile import _read_data_chunk, _read_fmt_chunk, _read_riff_chunk
def read(filename):
""" Reads common .wav file formats used in bat or bird experiments.
Mainly based on scipy.io.wavfile functions, to which were added some
robustness to support Petterson file format.
"""
with open(filename, 'rb') as fid:
with mmap.mmap(fid.fileno(), 0, access=mmap.ACCESS_READ) as mfid:
# Petterson wave file have multiple RIFF headers, find the last one
rindex = mfid.rfind(b'RIFF')
if rindex == -1:
raise ValueError('Missing RIFF tag in wav file.')
mfid.seek(rindex)
fsize = _read_riff_chunk(mfid)
if mfid.read(4) != b'fmt ':
raise ValueError('Missing format tag in wav file.')
# Ignore scipy warning on unknown wave file format
with warnings.catch_warnings():
warnings.simplefilter("ignore")
size, comp, noc, fs, sbytes, ba, bits = _read_fmt_chunk(mfid, is_big_endian=False)
if mfid.read(4) != b'data':
raise ValueError('Missing data tag in wav file.')
x = _read_data_chunk(mfid, comp, noc, bits, is_big_endian=False, mmap=False)
return fs, x
if __name__ == "__main__":
if len(sys.argv) != 2:
exit()
from scipy.io import wavfile
fs1, x1 = read(sys.argv[1])
fs2, x2 = wavfile.read(sys.argv[1])
print(all(x1 == x2))