Audio_mac.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Lib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Lib » Audio_mac.py
QSIZE = 100000
error='Audio_mac.error'

class Play_Audio_mac:

  def __init__(self, qsize=QSIZE):
    self._chan = None
    self._qsize = qsize
    self._outrate = 22254
    self._sampwidth = 1
    self._nchannels = 1
    self._gc = []
    self._usercallback = None

  def __del__(self):
    self.stop()
    self._usercallback = None

  def wait(self):
    import time
    while self.getfilled():
      time.sleep(0.1)
    self._chan = None
    self._gc = []

  def stop(self, quietNow = 1):
    ##chan = self._chan
    self._chan = None
    ##chan.SndDisposeChannel(1)
    self._gc = []

  def setoutrate(self, outrate):
    self._outrate = outrate

  def setsampwidth(self, sampwidth):
    self._sampwidth = sampwidth

  def setnchannels(self, nchannels):
    self._nchannels = nchannels

  def writeframes(self, data):
    import time
    from Sound import bufferCmd,callBackCmd,extSH
    import struct
    import MacOS
    if not self._chan:
      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*'\0'
    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 _callback(self, *args):
    del self._gc[0]
    if self._usercallback:
      self._usercallback()
      
  def setcallback(self, callback):
    self._usercallback = callback

  def getfilled(self):
    filled = 0
    for header, data in self._gc:
      filled = filled + len(data)
    return filled / self._nchannels / self._sampwidth

  def getfillable(self):
    return (self._qsize / self._nchannels / self._sampwidth) - self.getfilled()

  def ulaw2lin(self, data):
    import audioop
    return audioop.ulaw2lin(data, 2)

def test():
  import aifc
  import macfs
  fss, ok = macfs.PromptGetFile("Select an AIFF soundfile", "AIFF")
  if not ok: return
  fn = fss.as_pathname()
  af = aifc.open(fn, 'r')
  print af.getparams()
  p = Play_Audio_mac()
  p.setoutrate(af.getframerate())
  p.setsampwidth(af.getsampwidth())
  p.setnchannels(af.getnchannels())
  BUFSIZ = 10000
  while 1:
    data = af.readframes(BUFSIZ)
    if not data: break
    p.writeframes(data)
    print 'wrote', len(data), 'space', p.getfillable()
  p.wait()

if __name__ == '__main__':
  test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.