/
wavestream.py
163 lines (135 loc) · 4.4 KB
/
wavestream.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
import sys
import wave
import struct
import array
import subprocess
## WaveReader
##
class WaveReader(object):
def __init__(self, path):
self._fp = wave.open(path)
self.nchannels = self._fp.getnchannels()
self.sampwidth = self._fp.getsampwidth()
self.framerate = self._fp.getframerate()
self.nframes = self._fp.getnframes()
self._nframesleft = self.nframes
if self.sampwidth == 1:
self.ratio = 1.0/256.0
self.arraytype = 'b'
else:
self.ratio = 1.0/32768.0
self.arraytype = 'h'
return
def __len__(self):
return self.nframes
def close(self):
self._fp.close()
return
def eof(self):
return (self._nframesleft == 0)
def tell(self):
return self._fp.tell()
def seek(self, i):
self._fp.setpos(i)
self._nframesleft = self.nframes-i
return
def readraw(self, nframes=0):
assert self.nchannels == 1
if nframes == 0 or self._nframesleft < nframes:
nframes = self._nframesleft
self._nframesleft -= nframes
return self._fp.readframes(nframes)
def read(self, nframes=0):
data = self.readraw(nframes)
a = array.array(self.arraytype)
a.fromstring(data)
return [ x*self.ratio for x in a ]
## WaveWriter
##
class WaveWriter(object):
def __init__(self, fp,
nchannels=1, sampwidth=2,
framerate=44100, nframes=None):
self.fp = fp
self.nchannels = nchannels
self.sampwidth = sampwidth
self.framerate = framerate
self.nframes = nframes
self._nframeswritten = 0
if self.sampwidth == 1:
self.ratio = 255.0
self.arraytype = 'b'
else:
self.ratio = 32767.0
self.arraytype = 'h'
if nframes is None:
self._write_header(0, 0, 0, 0)
else:
self._write_header(nchannels, sampwidth, framerate, nframes)
return
def __len__(self):
return self.nframes
def _write_header(self, nchannels, sampwidth, framerate, nframes):
datalen = nchannels * sampwidth * nframes
self.fp.write('RIFF')
self.fp.write(struct.pack('<l4s4slhhllhh4sl',
36+datalen, 'WAVE', 'fmt ', 16,
0x0001, nchannels, framerate,
nchannels*sampwidth*framerate,
nchannels*sampwidth,
sampwidth*8, 'data', datalen))
return
def close(self):
if self.nframes is None:
self.fp.seek(0)
self._write_header(self.nchannels, self.sampwidth,
self.framerate, self._nframeswritten)
return
def eof(self):
return (self.nframes is not None and self.nframes <= self._nframeswritten)
def tell(self):
return self._nframeswritten
def writeraw(self, bytes):
self.fp.write(bytes)
self._nframeswritten += (len(bytes)/self.sampwidth)
return
def write(self, frames):
assert self.nchannels == 1
a = [ int(x*self.ratio) for x in frames ]
a = array.array(self.arraytype, a)
self.writeraw(a.tostring())
return
## WavePlayer
##
class WavePlayer(object):
PLAYER = ('aplay','-t','raw')
def __init__(self, nchannels=1, sampwidth=2, framerate=44100,
player=PLAYER):
if sampwidth == 1:
fmt = 'U8'
self.ratio = 255.0
self.arraytype = 'b'
else:
fmt = 'S16_LE'
self.ratio = 32767.0
self.arraytype = 'h'
cmdline = player+('-c',str(nchannels),'-r',str(framerate),'-f',fmt)
self._process = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
self._nframeswritten = 0
return
def wait(self):
self._process.wait()
return
def close(self):
self._process.stdin.close()
self._process = None
return
def tell(self):
return self._nframeswritten
def write(self, frames):
data = [ int(x*self.ratio) for x in frames ]
data = array.array(self.arraytype, data)
self._process.stdin.write(data.tostring())
self._nframeswritten += len(frames)
return