-
Notifications
You must be signed in to change notification settings - Fork 5
/
sources.py
52 lines (42 loc) · 1.36 KB
/
sources.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
import functools
import numpy as np
import scikits.audiolab as audiolab
import dataprocessor
from mock_sndfile import MockSndfile
class AudioSource(dataprocessor.Source):
"""Reads samples from an audio file or array.
Parameters
----------
Can be used as a generator that returns sequential lists of nbuf
samples arguments should be in second (or ms) units, not samples
(as they are now).
"""
def __init__(self, filename, start=0, end=None, nbuf=None):
self.filename = filename
self.start = start
self.end = end
self.nbuf = nbuf
def __iter__(self):
if isinstance(self.filename, str):
f = audiolab.Sndfile(self.filename)
elif np.iterable(self.filename):
f = MockSndfile(self.filename, samplerate=44100)
else:
raise ValueError, 'Invalid filename: %s' % self.filename
nbuf = self.nbuf
end = self.end
if not end:
end = f.nframes
if not nbuf:
nbuf = 10*f.samplerate
pos = f.seek(self.start)
nremaining = end - pos
while nremaining > 0:
if nremaining < nbuf:
nbuf = nremaining
try:
yield f.read_frames(nbuf)
nremaining -= nbuf
except RuntimeError:
nremaining = 0
f.close()