@@ -236,45 +236,7 @@ def _write_float(f, x):
236236 _write_long (f , himant )
237237 _write_long (f , lomant )
238238
239- class Chunk :
240- def __init__ (self , file ):
241- self .file = file
242- self .chunkname = self .file .read (4 )
243- if len (self .chunkname ) < 4 :
244- raise EOFError
245- self .chunksize = _read_long (self .file )
246- self .size_read = 0
247- self .offset = self .file .tell ()
248-
249- def rewind (self ):
250- self .file .seek (self .offset , 0 )
251- self .size_read = 0
252-
253- def setpos (self , pos ):
254- if pos < 0 or pos > self .chunksize :
255- raise RuntimeError
256- self .file .seek (self .offset + pos , 0 )
257- self .size_read = pos
258-
259- def read (self , length ):
260- if self .size_read >= self .chunksize :
261- return ''
262- if length > self .chunksize - self .size_read :
263- length = self .chunksize - self .size_read
264- data = self .file .read (length )
265- self .size_read = self .size_read + len (data )
266- return data
267-
268- def skip (self ):
269- try :
270- self .file .seek (self .chunksize - self .size_read , 1 )
271- except RuntimeError :
272- while self .size_read < self .chunksize :
273- dummy = self .read (8192 )
274- if not dummy :
275- raise EOFError
276- if self .chunksize & 1 :
277- dummy = self .read (1 )
239+ from chunk import Chunk
278240
279241class Aifc_read :
280242 # Variables used in this class:
@@ -312,65 +274,48 @@ class Aifc_read:
312274 # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
313275 # _framesize -- size of one frame in the file
314276
315- ## if 0: access _file, _nchannels, _nframes, _sampwidth, _framerate, \
316- ## _comptype, _compname, _markers, _soundpos, _version, \
317- ## _decomp, _comm_chunk_read, __aifc, _ssnd_seek_needed, \
318- ## _ssnd_chunk, _framesize: private
319-
320277 def initfp (self , file ):
321- self ._file = file
322278 self ._version = 0
323279 self ._decomp = None
324280 self ._convert = None
325281 self ._markers = []
326282 self ._soundpos = 0
327- form = self ._file . read ( 4 )
328- if form != 'FORM' :
283+ self ._file = Chunk ( file )
284+ if self . _file . getname () != 'FORM' :
329285 raise Error , 'file does not start with FORM id'
330- formlength = _read_long (self ._file )
331- if formlength <= 0 :
332- raise Error , 'invalid FORM chunk data size'
333286 formdata = self ._file .read (4 )
334- formlength = formlength - 4
335287 if formdata == 'AIFF' :
336288 self ._aifc = 0
337289 elif formdata == 'AIFC' :
338290 self ._aifc = 1
339291 else :
340292 raise Error , 'not an AIFF or AIFF-C file'
341293 self ._comm_chunk_read = 0
342- while formlength > 0 :
294+ while 1 :
343295 self ._ssnd_seek_needed = 1
344296 #DEBUG: SGI's soundfiler has a bug. There should
345297 # be no need to check for EOF here.
346298 try :
347299 chunk = Chunk (self ._file )
348300 except EOFError :
349- if formlength == 8 :
350- print 'Warning: FORM chunk size too large'
351- formlength = 0
352- break
353- raise EOFError # different error, raise exception
354- if chunk .chunkname == 'COMM' :
301+ break
302+ chunkname = chunk .getname ()
303+ if chunkname == 'COMM' :
355304 self ._read_comm_chunk (chunk )
356305 self ._comm_chunk_read = 1
357- elif chunk . chunkname == 'SSND' :
306+ elif chunkname == 'SSND' :
358307 self ._ssnd_chunk = chunk
359308 dummy = chunk .read (8 )
360309 self ._ssnd_seek_needed = 0
361- elif chunk . chunkname == 'FVER' :
310+ elif chunkname == 'FVER' :
362311 self ._version = _read_long (chunk )
363- elif chunk . chunkname == 'MARK' :
312+ elif chunkname == 'MARK' :
364313 self ._readmark (chunk )
365- elif chunk . chunkname in _skiplist :
314+ elif chunkname in _skiplist :
366315 pass
367316 else :
368317 raise Error , 'unrecognized chunk type ' + chunk .chunkname
369- formlength = formlength - 8 - chunk .chunksize
370- if chunk .chunksize & 1 :
371- formlength = formlength - 1
372- if formlength > 0 :
373- chunk .skip ()
318+ chunk .skip ()
374319 if not self ._comm_chunk_read or not self ._ssnd_chunk :
375320 raise Error , 'COMM chunk and/or SSND chunk missing'
376321 if self ._aifc and self ._decomp :
@@ -460,7 +405,7 @@ def setpos(self, pos):
460405
461406 def readframes (self , nframes ):
462407 if self ._ssnd_seek_needed :
463- self ._ssnd_chunk .rewind ( )
408+ self ._ssnd_chunk .seek ( 0 )
464409 dummy = self ._ssnd_chunk .read (8 )
465410 pos = self ._soundpos * self ._framesize
466411 if pos :
@@ -477,7 +422,6 @@ def readframes(self, nframes):
477422 #
478423 # Internal methods.
479424 #
480- ## if 0: access *: private
481425
482426 def _decomp_data (self , data ):
483427 import cl
@@ -611,10 +555,6 @@ class Aifc_write:
611555 # _datalength -- the size of the audio samples written to the header
612556 # _datawritten -- the size of the audio samples actually written
613557
614- ## if 0: access _file, _comptype, _compname, _nchannels, _sampwidth, \
615- ## _framerate, _nframes, _aifc, _version, _comp, \
616- ## _nframeswritten, _datalength, _datawritten: private
617-
618558 def __init__ (self , f ):
619559 if type (f ) == type ('' ):
620560 filename = f
@@ -805,7 +745,6 @@ def close(self):
805745 #
806746 # Internal methods.
807747 #
808- ## if 0: access *: private
809748
810749 def _comp_data (self , data ):
811750 import cl
0 commit comments