-
Notifications
You must be signed in to change notification settings - Fork 11
/
files.py
50 lines (36 loc) · 1.56 KB
/
files.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
import audiotools
class AudioError(Exception):
pass
class AudioFile(object):
def __init__(self, filename):
super(AudioFile, self).__init__()
self._reader = self._open_file(filename)
def read(self, size=4096, timeout=0.0):
return self._reader.read(size).to_bytes(False, True)
def __getattr__(self, key):
try:
return getattr(self._reader, key)
except (AttributeError):
return getattr(self.file, key)
def progress(self, current, total):
"""Dummy progress function"""
pass
def _open_file(self, filename):
"""Open a file for reading and wrap it in several helpers."""
try:
reader = audiotools.open(filename)
except (audiotools.UnsupportedFile) as err:
raise AudioError("Unsupported file")
self.file = reader
total_frames = reader.total_frames()
# Wrap in a PCMReader because we want PCM
reader = reader.to_pcm()
# Wrap in a converter
reader = audiotools.PCMConverter(reader, sample_rate=44100,
channels=2,
channel_mask=audiotools.ChannelMask(0x1 | 0x2),
bits_per_sample=24)
# And for file progress!
reader = audiotools.PCMReaderProgress(reader, total_frames,
self.progress)
return reader