Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
wave.Wave_write Class Reference

List of all members.

Public Member Functions

def __init__
def initfp
def __del__
def setnchannels
def getnchannels
def setsampwidth
def getsampwidth
def setframerate
def getframerate
def setnframes
def getnframes
def setcomptype
def getcomptype
def getcompname
def setparams
def getparams
def setmark
def getmark
def getmarkers
def tell
def writeframesraw
def writeframes
def close

Private Member Functions

def _ensure_header_written
def _write_header
def _patchheader

Private Attributes

 _i_opened_the_file
 _file
 _convert
 _nchannels
 _sampwidth
 _framerate
 _nframes
 _nframeswritten
 _datawritten
 _datalength
 _headerwritten
 _comptype
 _compname
 _form_length_pos
 _data_length_pos

Detailed Description

Variables used in this class:

These variables are user settable through appropriate methods
of this class:
_file -- the open file with methods write(), close(), tell(), seek()
          set through the __init__() method
_comptype -- the AIFF-C compression type ('NONE' in AIFF)
          set through the setcomptype() or setparams() method
_compname -- the human-readable AIFF-C compression type
          set through the setcomptype() or setparams() method
_nchannels -- the number of audio channels
          set through the setnchannels() or setparams() method
_sampwidth -- the number of bytes per audio sample
          set through the setsampwidth() or setparams() method
_framerate -- the sampling frequency
          set through the setframerate() or setparams() method
_nframes -- the number of audio frames written to the header
          set through the setnframes() or setparams() method

These variables are used internally only:
_datalength -- the size of the audio samples written to the header
_nframeswritten -- the number of frames actually written
_datawritten -- the size of the audio samples actually written

Definition at line 274 of file wave.py.


Constructor & Destructor Documentation

def wave.Wave_write.__init__ (   self,
  f 
)

Definition at line 300 of file wave.py.

00300 
00301     def __init__(self, f):
00302         self._i_opened_the_file = None
00303         if isinstance(f, str):
00304             f = builtins.open(f, 'wb')
00305             self._i_opened_the_file = f
00306         try:
00307             self.initfp(f)
00308         except:
00309             if self._i_opened_the_file:
00310                 f.close()
00311             raise

Here is the caller graph for this function:

def wave.Wave_write.__del__ (   self)

Definition at line 324 of file wave.py.

00324 
00325     def __del__(self):
00326         self.close()


Member Function Documentation

def wave.Wave_write._ensure_header_written (   self,
  datasize 
) [private]

Definition at line 452 of file wave.py.

00452 
00453     def _ensure_header_written(self, datasize):
00454         if not self._headerwritten:
00455             if not self._nchannels:
00456                 raise Error('# channels not specified')
00457             if not self._sampwidth:
00458                 raise Error('sample width not specified')
00459             if not self._framerate:
00460                 raise Error('sampling rate not specified')
00461             self._write_header(datasize)

Here is the call graph for this function:

def wave.Wave_write._patchheader (   self) [private]

Definition at line 479 of file wave.py.

00479 
00480     def _patchheader(self):
00481         assert self._headerwritten
00482         if self._datawritten == self._datalength:
00483             return
00484         curpos = self._file.tell()
00485         self._file.seek(self._form_length_pos, 0)
00486         self._file.write(struct.pack('<l', 36 + self._datawritten))
00487         self._file.seek(self._data_length_pos, 0)
00488         self._file.write(struct.pack('<l', self._datawritten))
00489         self._file.seek(curpos, 0)
00490         self._datalength = self._datawritten

def wave.Wave_write._write_header (   self,
  initlength 
) [private]

Definition at line 462 of file wave.py.

00462 
00463     def _write_header(self, initlength):
00464         assert not self._headerwritten
00465         self._file.write(b'RIFF')
00466         if not self._nframes:
00467             self._nframes = initlength // (self._nchannels * self._sampwidth)
00468         self._datalength = self._nframes * self._nchannels * self._sampwidth
00469         self._form_length_pos = self._file.tell()
00470         self._file.write(struct.pack('<l4s4slhhllhh4s',
00471             36 + self._datalength, b'WAVE', b'fmt ', 16,
00472             WAVE_FORMAT_PCM, self._nchannels, self._framerate,
00473             self._nchannels * self._framerate * self._sampwidth,
00474             self._nchannels * self._sampwidth,
00475             self._sampwidth * 8, b'data'))
00476         self._data_length_pos = self._file.tell()
00477         self._file.write(struct.pack('<l', self._datalength))
00478         self._headerwritten = True

Here is the caller graph for this function:

def wave.Wave_write.close (   self)

Definition at line 437 of file wave.py.

00437 
00438     def close(self):
00439         if self._file:
00440             self._ensure_header_written(0)
00441             if self._datalength != self._datawritten:
00442                 self._patchheader()
00443             self._file.flush()
00444             self._file = None
00445         if self._i_opened_the_file:
00446             self._i_opened_the_file.close()
00447             self._i_opened_the_file = None

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 385 of file wave.py.

00385 
00386     def getcompname(self):
00387         return self._compname

Definition at line 382 of file wave.py.

00382 
00383     def getcomptype(self):
00384         return self._comptype

Definition at line 361 of file wave.py.

00361 
00362     def getframerate(self):
00363         if not self._framerate:
00364             raise Error('frame rate not set')
00365         return self._framerate

def wave.Wave_write.getmark (   self,
  id 
)

Definition at line 407 of file wave.py.

00407 
00408     def getmark(self, id):
00409         raise Error('no marks')

Definition at line 410 of file wave.py.

00410 
00411     def getmarkers(self):
00412         return None

Definition at line 337 of file wave.py.

00337 
00338     def getnchannels(self):
00339         if not self._nchannels:
00340             raise Error('number of channels not set')
00341         return self._nchannels

Definition at line 371 of file wave.py.

00371 
00372     def getnframes(self):
00373         return self._nframeswritten

def wave.Wave_write.getparams (   self)

Definition at line 398 of file wave.py.

00398 
00399     def getparams(self):
00400         if not self._nchannels or not self._sampwidth or not self._framerate:
00401             raise Error('not all parameters set')
00402         return self._nchannels, self._sampwidth, self._framerate, \
00403               self._nframes, self._comptype, self._compname

Definition at line 349 of file wave.py.

00349 
00350     def getsampwidth(self):
00351         if not self._sampwidth:
00352             raise Error('sample width not set')
00353         return self._sampwidth

def wave.Wave_write.initfp (   self,
  file 
)

Definition at line 312 of file wave.py.

00312 
00313     def initfp(self, file):
00314         self._file = file
00315         self._convert = None
00316         self._nchannels = 0
00317         self._sampwidth = 0
00318         self._framerate = 0
00319         self._nframes = 0
00320         self._nframeswritten = 0
00321         self._datawritten = 0
00322         self._datalength = 0
00323         self._headerwritten = False

def wave.Wave_write.setcomptype (   self,
  comptype,
  compname 
)

Definition at line 374 of file wave.py.

00374 
00375     def setcomptype(self, comptype, compname):
00376         if self._datawritten:
00377             raise Error('cannot change parameters after starting to write')
00378         if comptype not in ('NONE',):
00379             raise Error('unsupported compression type')
00380         self._comptype = comptype
00381         self._compname = compname

def wave.Wave_write.setframerate (   self,
  framerate 
)

Definition at line 354 of file wave.py.

00354 
00355     def setframerate(self, framerate):
00356         if self._datawritten:
00357             raise Error('cannot change parameters after starting to write')
00358         if framerate <= 0:
00359             raise Error('bad frame rate')
00360         self._framerate = int(round(framerate))

Here is the call graph for this function:

def wave.Wave_write.setmark (   self,
  id,
  pos,
  name 
)

Definition at line 404 of file wave.py.

00404 
00405     def setmark(self, id, pos, name):
00406         raise Error('setmark() not supported')

def wave.Wave_write.setnchannels (   self,
  nchannels 
)

Definition at line 330 of file wave.py.

00330 
00331     def setnchannels(self, nchannels):
00332         if self._datawritten:
00333             raise Error('cannot change parameters after starting to write')
00334         if nchannels < 1:
00335             raise Error('bad # of channels')
00336         self._nchannels = nchannels

def wave.Wave_write.setnframes (   self,
  nframes 
)

Definition at line 366 of file wave.py.

00366 
00367     def setnframes(self, nframes):
00368         if self._datawritten:
00369             raise Error('cannot change parameters after starting to write')
00370         self._nframes = nframes

def wave.Wave_write.setparams (   self,
  params 
)

Definition at line 388 of file wave.py.

00388 
00389     def setparams(self, params):
00390         nchannels, sampwidth, framerate, nframes, comptype, compname = params
00391         if self._datawritten:
00392             raise Error('cannot change parameters after starting to write')
00393         self.setnchannels(nchannels)
00394         self.setsampwidth(sampwidth)
00395         self.setframerate(framerate)
00396         self.setnframes(nframes)
00397         self.setcomptype(comptype, compname)

Here is the call graph for this function:

def wave.Wave_write.setsampwidth (   self,
  sampwidth 
)

Definition at line 342 of file wave.py.

00342 
00343     def setsampwidth(self, sampwidth):
00344         if self._datawritten:
00345             raise Error('cannot change parameters after starting to write')
00346         if sampwidth < 1 or sampwidth > 4:
00347             raise Error('bad sample width')
00348         self._sampwidth = sampwidth

Here is the caller graph for this function:

def wave.Wave_write.tell (   self)

Definition at line 413 of file wave.py.

00413 
00414     def tell(self):
00415         return self._nframeswritten

def wave.Wave_write.writeframes (   self,
  data 
)

Definition at line 432 of file wave.py.

00432 
00433     def writeframes(self, data):
00434         self.writeframesraw(data)
00435         if self._datalength != self._datawritten:
00436             self._patchheader()

Here is the call graph for this function:

def wave.Wave_write.writeframesraw (   self,
  data 
)

Definition at line 416 of file wave.py.

00416 
00417     def writeframesraw(self, data):
00418         self._ensure_header_written(len(data))
00419         nframes = len(data) // (self._sampwidth * self._nchannels)
00420         if self._convert:
00421             data = self._convert(data)
00422         if self._sampwidth > 1 and big_endian:
00423             import array
00424             data = array.array(_array_fmts[self._sampwidth], data)
00425             data.byteswap()
00426             data.tofile(self._file)
00427             self._datawritten = self._datawritten + len(data) * self._sampwidth
00428         else:
00429             self._file.write(data)
00430             self._datawritten = self._datawritten + len(data)
00431         self._nframeswritten = self._nframeswritten + nframes

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 380 of file wave.py.

Definition at line 379 of file wave.py.

Definition at line 314 of file wave.py.

Definition at line 475 of file wave.py.

Definition at line 321 of file wave.py.

Definition at line 320 of file wave.py.

Definition at line 313 of file wave.py.

Definition at line 468 of file wave.py.

Definition at line 317 of file wave.py.

Definition at line 322 of file wave.py.

Definition at line 301 of file wave.py.

Definition at line 315 of file wave.py.

Definition at line 318 of file wave.py.

Definition at line 319 of file wave.py.

Definition at line 316 of file wave.py.


The documentation for this class was generated from the following file: