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