Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame^] | 1 | QSIZE = 100000 |
| 2 | |
| 3 | class Play_Audio_mac: |
| 4 | |
| 5 | def __init__(self): |
| 6 | self._chan = None |
| 7 | self._qsize = QSIZE |
| 8 | self._outrate = 22254 |
| 9 | self._sampwidth = 1 |
| 10 | self._nchannels = 1 |
| 11 | self._gc = [] |
| 12 | |
| 13 | def __del__(self): |
| 14 | self.stop() |
| 15 | |
| 16 | def wait(self): |
| 17 | import time |
| 18 | while self.getfilled(): |
| 19 | time.sleep(0.1) |
| 20 | self._chan = None |
| 21 | self._gc = [] |
| 22 | |
| 23 | def stop(self, quietNow = 1): |
| 24 | ##chan = self._chan |
| 25 | self._chan = None |
| 26 | ##chan.SndDisposeChannel(1) |
| 27 | self._gc = [] |
| 28 | |
| 29 | def setoutrate(self, outrate): |
| 30 | self._outrate = outrate |
| 31 | |
| 32 | def setsampwidth(self, sampwidth): |
| 33 | self._sampwidth = sampwidth |
| 34 | |
| 35 | def setnchannels(self, nchannels): |
| 36 | self._nchannels = nchannels |
| 37 | |
| 38 | def writeframes(self, data): |
| 39 | import time |
| 40 | from Sound import * |
| 41 | import struct |
| 42 | if not self._chan: |
| 43 | import Snd |
| 44 | self._chan = Snd.SndNewChannel(5, 0, self._callback) |
| 45 | nframes = len(data) / self._nchannels / self._sampwidth |
| 46 | if len(data) != nframes * self._nchannels * self._sampwidth: |
| 47 | raise ValueError, 'data is not a whole number of frames' |
| 48 | while self._gc and \ |
| 49 | self.getfilled() + nframes > \ |
| 50 | self._qsize / self._nchannels / self._sampwidth: |
| 51 | time.sleep(0.1) |
| 52 | if self._sampwidth == 1: |
| 53 | import audioop |
| 54 | data = audioop.add(data, '\x80'*len(data), 1) |
| 55 | h1 = struct.pack('llhhllbbl', |
| 56 | id(data)+12, |
| 57 | self._nchannels, |
| 58 | self._outrate, 0, |
| 59 | 0, |
| 60 | 0, |
| 61 | extSH, |
| 62 | 60, |
| 63 | nframes) |
| 64 | h2 = 22*'\0' |
| 65 | h3 = struct.pack('hhlll', |
| 66 | self._sampwidth*8, |
| 67 | 0, |
| 68 | 0, |
| 69 | 0, |
| 70 | 0) |
| 71 | header = h1+h2+h3 |
| 72 | self._gc.append((header, data)) |
| 73 | self._chan.SndDoCommand((bufferCmd, 0, header), 0) |
| 74 | self._chan.SndDoCommand((callBackCmd, 0, 0), 0) |
| 75 | |
| 76 | def _callback(self, *args): |
| 77 | del self._gc[0] |
| 78 | |
| 79 | def getfilled(self): |
| 80 | filled = 0 |
| 81 | for header, data in self._gc: |
| 82 | filled = filled + len(data) |
| 83 | return filled / self._nchannels / self._sampwidth |
| 84 | |
| 85 | def getfillable(self): |
| 86 | return self._qsize - self.getfilled() |
| 87 | |
| 88 | def ulaw2lin(self, data): |
| 89 | import audioop |
| 90 | return audioop.ulaw2lin(data, 2) |
| 91 | |
| 92 | def test(fn = 'f:just samples:just.aif'): |
| 93 | import aifc |
| 94 | af = aifc.open(fn, 'r') |
| 95 | print af.getparams() |
| 96 | p = Play_Audio_mac() |
| 97 | p.setoutrate(af.getframerate()) |
| 98 | p.setsampwidth(af.getsampwidth()) |
| 99 | p.setnchannels(af.getnchannels()) |
| 100 | BUFSIZ = 10000 |
| 101 | while 1: |
| 102 | data = af.readframes(BUFSIZ) |
| 103 | if not data: break |
| 104 | p.writeframes(data) |
| 105 | p.wait() |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | test() |