-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmock_sndfile.py
64 lines (52 loc) · 1.85 KB
/
mock_sndfile.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class MockSndfile(object):
"""
Wraps an audiolab Sndfile interface around a NumPy array.
Using this class enables a consistent interface through the
feature extraction pipeline for signals already in memory as well
as sound files read from disk or audio devices.
"""
def __init__(self, array, samplerate=None):
self.nframes = len(array)
try:
self.channels = len(array[0])
except TypeError:
self.channels = 1
self._array = array
self._curr_index = 0
self.samplerate = samplerate
self.encoding = None
self.endianness = None
self.file_format = None
self.format = None
def read_frames(self, nframes, dtype=None):
end = self._curr_index + nframes
if self._curr_index > self.nframes or end > self.nframes:
nread = self.nframes - self._curr_index
self._curr_index += nread
raise (RuntimeError, 'Asked %d frames, read %d' % (nframes, nread))
start = self._curr_index
self._curr_index += nframes
frames = self._array[start:end]
if dtype:
frames.dtype = dtype
return frames
def write_frames(self, frames):
pass
def seek(self, offset, whence=0, mode='r'):
if whence == 0:
new_curr_index = offset
elif whence == 1:
new_curr_index = self._curr_index + offset
elif whence == 2:
new_curr_index = self.nframes + offset
else:
raise (ValueError, 'whence must be 0, 1, or 2')
if 0 <= new_curr_index < self.nframes:
self._curr_index = new_curr_index
else:
raise (IOError, 'Tried to seek too far')
return self._curr_index
def close(self):
pass
def sync(self):
pass