Back to index

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

List of all members.

Public Member Functions

def initfp
def __init__
def __del__
def getfp
def rewind
def close
def tell
def getnchannels
def getnframes
def getsampwidth
def getframerate
def getcomptype
def getcompname
def getparams
def getmarkers
def getmark
def setpos
def readframes

Private Member Functions

def _read_fmt_chunk

Private Attributes

 _convert
 _soundpos
 _file
 _fmt_chunk_read
 _data_chunk
 _data_seek_needed
 _nframes
 _i_opened_the_file
 _sampwidth
 _framesize
 _comptype
 _compname

Detailed Description

Variables used in this class:

These variables are available to the user though appropriate
methods of this class:
_file -- the open file with methods read(), close(), and seek()
          set through the __init__() method
_nchannels -- the number of audio channels
          available through the getnchannels() method
_nframes -- the number of audio frames
          available through the getnframes() method
_sampwidth -- the number of bytes per audio sample
          available through the getsampwidth() method
_framerate -- the sampling frequency
          available through the getframerate() method
_comptype -- the AIFF-C compression type ('NONE' if AIFF)
          available through the getcomptype() method
_compname -- the human-readable AIFF-C compression type
          available through the getcomptype() method
_soundpos -- the position in the audio stream
          available through the tell() method, set through the
          setpos() method

These variables are used internally only:
_fmt_chunk_read -- 1 iff the FMT chunk has been read
_data_seek_needed -- 1 iff positioned correctly in audio
          file for readframes()
_data_chunk -- instantiation of a chunk class for the DATA chunk
_framesize -- size of one frame in the file

Definition at line 94 of file wave.py.


Constructor & Destructor Documentation

def wave.Wave_read.__init__ (   self,
  f 
)

Definition at line 156 of file wave.py.

00156 
00157     def __init__(self, f):
00158         self._i_opened_the_file = None
00159         if isinstance(f, str):
00160             f = builtins.open(f, 'rb')
00161             self._i_opened_the_file = f
00162         # else, assume it is an open file object already
00163         try:
00164             self.initfp(f)
00165         except:
00166             if self._i_opened_the_file:
00167                 f.close()
00168             raise

Here is the caller graph for this function:

def wave.Wave_read.__del__ (   self)

Definition at line 169 of file wave.py.

00169 
00170     def __del__(self):
        self.close()

Member Function Documentation

def wave.Wave_read._read_fmt_chunk (   self,
  chunk 
) [private]

Definition at line 263 of file wave.py.

00263 
00264     def _read_fmt_chunk(self, chunk):
00265         wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<hhllh', chunk.read(14))
00266         if wFormatTag == WAVE_FORMAT_PCM:
00267             sampwidth = struct.unpack_from('<h', chunk.read(2))[0]
00268             self._sampwidth = (sampwidth + 7) // 8
00269         else:
00270             raise Error('unknown format: %r' % (wFormatTag,))
00271         self._framesize = self._nchannels * self._sampwidth
00272         self._comptype = 'NONE'
00273         self._compname = 'not compressed'

def wave.Wave_read.close (   self)

Definition at line 181 of file wave.py.

00181 
00182     def close(self):
00183         if self._i_opened_the_file:
00184             self._i_opened_the_file.close()
00185             self._i_opened_the_file = None
00186         self._file = None

Here is the caller graph for this function:

Definition at line 205 of file wave.py.

00205 
00206     def getcompname(self):
00207         return self._compname

Definition at line 202 of file wave.py.

00202 
00203     def getcomptype(self):
00204         return self._comptype

def wave.Wave_read.getfp (   self)

Definition at line 174 of file wave.py.

00174 
00175     def getfp(self):
00176         return self._file

Definition at line 199 of file wave.py.

00199 
00200     def getframerate(self):
00201         return self._framerate

def wave.Wave_read.getmark (   self,
  id 
)

Definition at line 216 of file wave.py.

00216 
00217     def getmark(self, id):
00218         raise Error('no marks')

def wave.Wave_read.getmarkers (   self)

Definition at line 213 of file wave.py.

00213 
00214     def getmarkers(self):
00215         return None

Definition at line 190 of file wave.py.

00190 
00191     def getnchannels(self):
00192         return self._nchannels

def wave.Wave_read.getnframes (   self)

Definition at line 193 of file wave.py.

00193 
00194     def getnframes(self):
00195         return self._nframes

def wave.Wave_read.getparams (   self)

Definition at line 208 of file wave.py.

00208 
00209     def getparams(self):
00210         return self.getnchannels(), self.getsampwidth(), \
00211                self.getframerate(), self.getnframes(), \
00212                self.getcomptype(), self.getcompname()

Here is the call graph for this function:

Definition at line 196 of file wave.py.

00196 
00197     def getsampwidth(self):
00198         return self._sampwidth

Here is the caller graph for this function:

def wave.Wave_read.initfp (   self,
  file 
)

Definition at line 125 of file wave.py.

00125 
00126     def initfp(self, file):
00127         self._convert = None
00128         self._soundpos = 0
00129         self._file = Chunk(file, bigendian = 0)
00130         if self._file.getname() != b'RIFF':
00131             raise Error('file does not start with RIFF id')
00132         if self._file.read(4) != b'WAVE':
00133             raise Error('not a WAVE file')
00134         self._fmt_chunk_read = 0
00135         self._data_chunk = None
00136         while 1:
00137             self._data_seek_needed = 1
00138             try:
00139                 chunk = Chunk(self._file, bigendian = 0)
00140             except EOFError:
00141                 break
00142             chunkname = chunk.getname()
00143             if chunkname == b'fmt ':
00144                 self._read_fmt_chunk(chunk)
00145                 self._fmt_chunk_read = 1
00146             elif chunkname == b'data':
00147                 if not self._fmt_chunk_read:
00148                     raise Error('data chunk before fmt chunk')
00149                 self._data_chunk = chunk
00150                 self._nframes = chunk.chunksize // self._framesize
00151                 self._data_seek_needed = 0
00152                 break
00153             chunk.skip()
00154         if not self._fmt_chunk_read or not self._data_chunk:
00155             raise Error('fmt chunk and/or data chunk missing')

def wave.Wave_read.readframes (   self,
  nframes 
)

Definition at line 225 of file wave.py.

00225 
00226     def readframes(self, nframes):
00227         if self._data_seek_needed:
00228             self._data_chunk.seek(0, 0)
00229             pos = self._soundpos * self._framesize
00230             if pos:
00231                 self._data_chunk.seek(pos, 0)
00232             self._data_seek_needed = 0
00233         if nframes == 0:
00234             return b''
00235         if self._sampwidth > 1 and big_endian:
00236             # unfortunately the fromfile() method does not take
00237             # something that only looks like a file object, so
00238             # we have to reach into the innards of the chunk object
00239             import array
00240             chunk = self._data_chunk
00241             data = array.array(_array_fmts[self._sampwidth])
00242             nitems = nframes * self._nchannels
00243             if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
00244                 nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth
00245             data.fromfile(chunk.file.file, nitems)
00246             # "tell" data chunk how much was read
00247             chunk.size_read = chunk.size_read + nitems * self._sampwidth
00248             # do the same for the outermost chunk
00249             chunk = chunk.file
00250             chunk.size_read = chunk.size_read + nitems * self._sampwidth
00251             data.byteswap()
00252             data = data.tobytes()
00253         else:
00254             data = self._data_chunk.read(nframes * self._framesize)
00255         if self._convert and data:
00256             data = self._convert(data)
00257         self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
00258         return data

def wave.Wave_read.rewind (   self)

Definition at line 177 of file wave.py.

00177 
00178     def rewind(self):
00179         self._data_seek_needed = 1
00180         self._soundpos = 0

def wave.Wave_read.setpos (   self,
  pos 
)

Definition at line 219 of file wave.py.

00219 
00220     def setpos(self, pos):
00221         if pos < 0 or pos > self._nframes:
00222             raise Error('position not in range')
00223         self._soundpos = pos
00224         self._data_seek_needed = 1

Here is the caller graph for this function:

def wave.Wave_read.tell (   self)

Definition at line 187 of file wave.py.

00187 
00188     def tell(self):
00189         return self._soundpos


Member Data Documentation

Definition at line 272 of file wave.py.

Definition at line 271 of file wave.py.

Definition at line 126 of file wave.py.

Definition at line 134 of file wave.py.

Definition at line 136 of file wave.py.

Definition at line 128 of file wave.py.

Definition at line 133 of file wave.py.

Definition at line 270 of file wave.py.

Definition at line 157 of file wave.py.

Definition at line 149 of file wave.py.

Definition at line 267 of file wave.py.

Definition at line 127 of file wave.py.


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