def writeframes(self, data): import time from Carbon.Sound import bufferCmd, callBackCmd, extSH import struct import MacOS if not self._chan: from Carbon import Snd self._chan = Snd.SndNewChannel(5, 0, self._callback) nframes = len(data) / self._nchannels / self._sampwidth if len(data) != nframes * self._nchannels * self._sampwidth: raise error, 'data is not a whole number of frames' while self._gc and self.getfilled( ) + nframes > self._qsize / self._nchannels / self._sampwidth: time.sleep(0.1) if self._sampwidth == 1: import audioop data = audioop.add(data, '\x80' * len(data), 1) h1 = struct.pack('llHhllbbl', id(data) + MacOS.string_id_to_buffer, self._nchannels, self._outrate, 0, 0, 0, extSH, 60, nframes) h2 = 22 * '\x00' h3 = struct.pack('hhlll', self._sampwidth * 8, 0, 0, 0, 0) header = h1 + h2 + h3 self._gc.append((header, data)) self._chan.SndDoCommand((bufferCmd, 0, header), 0) self._chan.SndDoCommand((callBackCmd, 0, 0), 0)
def writeframes(self, data): if not self.__format or not self.__outrate: raise Error, 'params not specified' if not self.__chan: self.__chan = Snd.SndNewChannel(5, 0, self.__callback) nframes = len(data) / self.__nchannels / self.__sampwidth if len(data) != nframes * self.__nchannels * self.__sampwidth: raise error, 'data is not a whole number of frames' while self.__gc and \ self.getfilled() + nframes > self.__qsize: time.sleep(0.1) h1 = struct.pack( 'llHhllBb', id(data) + MacOS.string_id_to_buffer, # ARGH!!! HACK, HACK! self.__nchannels, self.__outrate, 0, 0, 0, extSH, 60) h2 = struct.pack('l', nframes) h3 = 22 * '\0' h4 = struct.pack('hhlll', self.__sampwidth * 8, 0, 0, 0, 0) header = h1 + h2 + h3 + h4 self.__gc.append((header, data)) self.__chan.SndDoCommand((bufferCmd, 0, header), 0) self.__chan.SndDoCommand((callBackCmd, 0, 0), 0)
import aifc, audioop fn = 'f:just samples:2ndbeat.aif' af = aifc.open(fn, 'r') print af.getparams() print 'nframes =', af.getnframes() print 'nchannels =', af.getnchannels() print 'framerate =', af.getframerate() nframes = min(af.getnframes(), 100000) frames = af.readframes(nframes) print 'len(frames) =', len(frames) print repr(frames[:100]) frames = audioop.add(frames, '\x80' * len(frames), 1) print repr(frames[:100]) import struct header1 = struct.pack('llhhllbbl', 0, af.getnchannels(), af.getframerate(), 0, 0, 0, 0xFF, 60, nframes) print repr(header1) header2 = struct.pack('llhlll', 0, 0, 0, 0, 0, 0) header3 = struct.pack('hhlll', af.getsampwidth() * 8, 0, 0, 0, 0) print repr(header3) header = header1 + header2 + header3 buffer = header + frames chan = Snd.SndNewChannel(5, 0x00C0) Snd.SndDoCommand(chan, (bufferCmd, 0, buffer), 0)
QSIZE = 100000
from Carbon.Sound import * from Carbon import Snd import aifc, audioop fn = 'f:just samples:2ndbeat.aif' af = aifc.open(fn, 'r') print af.getparams() print 'nframes =', af.getnframes() print 'nchannels =', af.getnchannels() print 'framerate =', af.getframerate() nframes = min(af.getnframes(), 100000) frames = af.readframes(nframes) print 'len(frames) =', len(frames) print repr(frames[:100]) frames = audioop.add(frames, '\x80' * len(frames), 1) print repr(frames[:100]) import struct header1 = struct.pack('llhhllbbl', 0, af.getnchannels(), af.getframerate(), 0, 0, 0, 0xFF, 60, nframes) print repr(header1) header2 = struct.pack('llhlll', 0, 0, 0, 0, 0, 0) header3 = struct.pack('hhlll', af.getsampwidth() * 8, 0, 0, 0, 0) print repr(header3) header = header1 + header2 + header3 buffer = header + frames chan = Snd.SndNewChannel(5, 0x00C0) Snd.SndDoCommand(chan, (bufferCmd, 0, buffer), 0)