Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
aifc Namespace Reference

Data Structures

class  Error
 
class  Aifc_read
 
class  Aifc_write
 

Functions

def getparams
 def getversion(self): return self._version More...
 
def getmarkers
 
def getmark
 
def setpos
 
def readframes
 
def setparams
 def setversion(self, version): if self._nframeswritten: raise Error, 'cannot change parameters after starting to write' self._version = version More...
 
def setmark
 
def tell
 
def writeframesraw
 
def writeframes
 
def close
 
def open
 

Variables

list __all__ = ["Error","open","openfp"]
 
int _AIFC_version = 0xA2805140
 
string _skiplist = 'COMT'
 
float _HUGE_VAL = 1.79769313486231e+308
 
 openfp = open
 
list fn = sys.argv[1]
 
tuple f = open(fn, 'r')
 
list gn = sys.argv[2]
 
tuple g = open(gn, 'w')
 
tuple data = f.readframes(1024)
 
 _soundpos
 
 _ssnd_seek_needed
 
 _adpcmstate
 
 _nchannels
 
 _nframes
 
 _sampwidth
 
 _framerate
 
 _framesize
 
 _comptype
 
 _compname
 
 _convert
 
 _decomp
 
 _nframeswritten
 
 _datawritten
 
 _comp
 
 _file
 
 _datalength
 
 _form_length_pos
 
 _nframes_pos
 
 _ssnd_length_pos
 
 _marklength
 

Detailed Description

Stuff to parse AIFF-C and AIFF files.

Unless explicitly stated otherwise, the description below is true
both for AIFF-C files and AIFF files.

An AIFF-C file has the following structure.

  +-----------------+
  | FORM            |
  +-----------------+
  | <size>          |
  +----+------------+
  |    | AIFC       |
  |    +------------+
  |    | <chunks>   |
  |    |    .       |
  |    |    .       |
  |    |    .       |
  +----+------------+

An AIFF file has the string "AIFF" instead of "AIFC".

A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
big endian order), followed by the data.  The size field does not include
the size of the 8 byte header.

The following chunk types are recognized.

  FVER
      <version number of AIFF-C defining document> (AIFF-C only).
  MARK
      <# of markers> (2 bytes)
      list of markers:
          <marker ID> (2 bytes, must be > 0)
          <position> (4 bytes)
          <marker name> ("pstring")
  COMM
      <# of channels> (2 bytes)
      <# of sound frames> (4 bytes)
      <size of the samples> (2 bytes)
      <sampling frequency> (10 bytes, IEEE 80-bit extended
          floating point)
      in AIFF-C files only:
      <compression type> (4 bytes)
      <human-readable version of compression type> ("pstring")
  SSND
      <offset> (4 bytes, not used by this program)
      <blocksize> (4 bytes, not used by this program)
      <sound data>

A pstring consists of 1 byte length, a string of characters, and 0 or 1
byte pad to make the total length even.

Usage.

Reading AIFF files:
  f = aifc.open(file, 'r')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
In some types of audio files, if the setpos() method is not used,
the seek() method is not necessary.

This returns an instance of a class with the following public methods:
  getnchannels()  -- returns number of audio channels (1 for
             mono, 2 for stereo)
  getsampwidth()  -- returns sample width in bytes
  getframerate()  -- returns sampling frequency
  getnframes()    -- returns number of audio frames
  getcomptype()   -- returns compression type ('NONE' for AIFF files)
  getcompname()   -- returns human-readable version of
             compression type ('not compressed' for AIFF files)
  getparams() -- returns a tuple consisting of all of the
             above in the above order
  getmarkers()    -- get the list of marks in the audio file or None
             if there are no marks
  getmark(id) -- get mark with the specified id (raises an error
             if the mark does not exist)
  readframes(n)   -- returns at most n frames of audio
  rewind()    -- rewind to the beginning of the audio stream
  setpos(pos) -- seek to the specified position
  tell()      -- return the current position
  close()     -- close the instance (make it unusable)
The position returned by tell(), the position given to setpos() and
the position of marks are all compatible and have nothing to do with
the actual position in the file.
The close() method is called automatically when the class instance
is destroyed.

Writing AIFF files:
  f = aifc.open(file, 'w')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods write(), tell(), seek(), and
close().

This returns an instance of a class with the following public methods:
  aiff()      -- create an AIFF file (AIFF-C default)
  aifc()      -- create an AIFF-C file
  setnchannels(n) -- set the number of channels
  setsampwidth(n) -- set the sample width
  setframerate(n) -- set the frame rate
  setnframes(n)   -- set the number of frames
  setcomptype(type, name)
          -- set the compression type and the
             human-readable compression type
  setparams(tuple)
          -- set all parameters at once
  setmark(id, pos, name)
          -- add specified mark to the list of marks
  tell()      -- return current position in output file (useful
             in combination with setmark())
  writeframesraw(data)
          -- write audio frames without pathing up the
             file header
  writeframes(data)
          -- write audio frames and patch up the file header
  close()     -- patch up the file header and close the
             output file
You should set the parameters before the first writeframesraw or
writeframes.  The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to
be patched up.
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes('') or
close() to patch up the sizes in the header.
Marks can be added anytime.  If there are any marks, ypu must call
close() after all frames have been written.
The close() method is called automatically when the class instance
is destroyed.

When a file is opened with the extension '.aiff', an AIFF file is
written, otherwise an AIFF-C file is written.  This default can be
changed by calling aiff() or aifc() before the first writeframes or
writeframesraw.

Function Documentation

def aifc.close (   self)

Definition at line 726 of file aifc.py.

727  def close(self):
728  self._ensure_header_written(0)
729  if self._datawritten & 1:
730  # quick pad to even size
731  self._file.write(chr(0))
732  self._datawritten = self._datawritten + 1
733  self._writemarkers()
734  if self._nframeswritten != self._nframes or \
735  self._datalength != self._datawritten or \
736  self._marklength:
737  self._patchheader()
738  if self._comp:
739  self._comp.CloseCompressor()
740  self._comp = None
741  self._file.flush()
742  self._file = None
def getmark (   self,
  id 
)

Definition at line 392 of file aifc.py.

393  def getmark(self, id):
394  for marker in self._markers:
395  if id == marker[0]:
396  return marker
397  raise Error, 'marker ' + `id` + ' does not exist'
def getmarkers (   self)

Definition at line 387 of file aifc.py.

388  def getmarkers(self):
389  if len(self._markers) == 0:
390  return None
391  return self._markers
def getparams (   self)

def getversion(self): return self._version

Definition at line 382 of file aifc.py.

383  def getparams(self):
384  return self.getnchannels(), self.getsampwidth(), \
385  self.getframerate(), self.getnframes(), \
386  self.getcomptype(), self.getcompname()
def aifc.open (   f,
  mode = None 
)

Definition at line 921 of file aifc.py.

922 def open(f, mode=None):
923  if mode is None:
924  if hasattr(f, 'mode'):
925  mode = f.mode
926  else:
927  mode = 'rb'
928  if mode in ('r', 'rb'):
929  return Aifc_read(f)
930  elif mode in ('w', 'wb'):
931  return Aifc_write(f)
932  else:
933  raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
def aifc.readframes (   self,
  nframes 
)

Definition at line 404 of file aifc.py.

405  def readframes(self, nframes):
406  if self._ssnd_seek_needed:
407  self._ssnd_chunk.seek(0)
408  dummy = self._ssnd_chunk.read(8)
409  pos = self._soundpos * self._framesize
410  if pos:
411  self._ssnd_chunk.seek(pos + 8)
412  self._ssnd_seek_needed = 0
413  if nframes == 0:
414  return ''
415  data = self._ssnd_chunk.read(nframes * self._framesize)
416  if self._convert and data:
417  data = self._convert(data)
418  self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
419  return data
def aifc.setmark (   self,
  id,
  pos,
  name 
)

Definition at line 684 of file aifc.py.

685  def setmark(self, id, pos, name):
686  if id <= 0:
687  raise Error, 'marker ID must be > 0'
688  if pos < 0:
689  raise Error, 'marker position must be >= 0'
690  if type(name) != type(''):
691  raise Error, 'marker name must be a string'
692  for i in range(len(self._markers)):
693  if id == self._markers[i][0]:
694  self._markers[i] = id, pos, name
695  return
696  self._markers.append((id, pos, name))
def aifc.setparams (   self,
  nchannels,
  sampwidth,
  framerate,
  nframes,
  comptype,
  compname 
)

def setversion(self, version): if self._nframeswritten: raise Error, 'cannot change parameters after starting to write' self._version = version

Definition at line 667 of file aifc.py.

668  def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)):
669  if self._nframeswritten:
670  raise Error, 'cannot change parameters after starting to write'
671  if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
672  raise Error, 'unsupported compression type'
673  self.setnchannels(nchannels)
674  self.setsampwidth(sampwidth)
675  self.setframerate(framerate)
676  self.setnframes(nframes)
677  self.setcomptype(comptype, compname)
def aifc.setpos (   self,
  pos 
)

Definition at line 398 of file aifc.py.

399  def setpos(self, pos):
400  if pos < 0 or pos > self._nframes:
401  raise Error, 'position not in range'
402  self._soundpos = pos
403  self._ssnd_seek_needed = 1
def aifc.tell (   self)

Definition at line 708 of file aifc.py.

709  def tell(self):
710  return self._nframeswritten
def aifc.writeframes (   self,
  data 
)

Definition at line 720 of file aifc.py.

721  def writeframes(self, data):
722  self.writeframesraw(data)
723  if self._nframeswritten != self._nframes or \
724  self._datalength != self._datawritten:
725  self._patchheader()
def aifc.writeframesraw (   self,
  data 
)

Definition at line 711 of file aifc.py.

712  def writeframesraw(self, data):
713  self._ensure_header_written(len(data))
714  nframes = len(data) / (self._sampwidth * self._nchannels)
715  if self._convert:
716  data = self._convert(data)
717  self._file.write(data)
718  self._nframeswritten = self._nframeswritten + nframes
719  self._datawritten = self._datawritten + len(data)

Variable Documentation

list __all__ = ["Error","open","openfp"]

Definition at line 140 of file aifc.py.

_adpcmstate

Definition at line 439 of file aifc.py.

int _AIFC_version = 0xA2805140

Definition at line 145 of file aifc.py.

_comp

Definition at line 739 of file aifc.py.

_compname

Definition at line 467 of file aifc.py.

_comptype

Definition at line 458 of file aifc.py.

_convert

Definition at line 475 of file aifc.py.

_datalength

Definition at line 830 of file aifc.py.

_datawritten

Definition at line 718 of file aifc.py.

_decomp

Definition at line 499 of file aifc.py.

_file

Definition at line 741 of file aifc.py.

_form_length_pos

Definition at line 842 of file aifc.py.

_framerate

Definition at line 448 of file aifc.py.

_framesize

Definition at line 449 of file aifc.py.

float _HUGE_VAL = 1.79769313486231e+308

Definition at line 178 of file aifc.py.

_marklength

Definition at line 889 of file aifc.py.

_nchannels

Definition at line 445 of file aifc.py.

_nframes

Definition at line 446 of file aifc.py.

_nframes_pos

Definition at line 854 of file aifc.py.

_nframeswritten

Definition at line 717 of file aifc.py.

_sampwidth

Definition at line 447 of file aifc.py.

string _skiplist = 'COMT'

Definition at line 147 of file aifc.py.

_soundpos

Definition at line 401 of file aifc.py.

_ssnd_length_pos

Definition at line 862 of file aifc.py.

_ssnd_seek_needed

Definition at line 402 of file aifc.py.

tuple data = f.readframes(1024)

Definition at line 955 of file aifc.py.

tuple f = open(fn, 'r')

Definition at line 941 of file aifc.py.

list fn = sys.argv[1]

Definition at line 940 of file aifc.py.

tuple g = open(gn, 'w')

Definition at line 952 of file aifc.py.

list gn = sys.argv[2]

Definition at line 950 of file aifc.py.

openfp = open

Definition at line 934 of file aifc.py.