home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Lib / Audio_mac.py < prev    next >
Encoding:
Python Source  |  1994-09-16  |  2.3 KB  |  108 lines  |  [TEXT/R*ch]

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