Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ea7bb8

Browse files
committed
Patch/new code by Sjoerd Mullender:
Separate the Chunk class out of the aifc module into a new "chunk" module.
1 parent 1f2e09b commit 8ea7bb8

2 files changed

Lines changed: 155 additions & 74 deletions

File tree

Lib/aifc.py

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -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

279241
class 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

Lib/chunk.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""Simple class to read IFF chunks.
2+
3+
An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
4+
Format)) has the following structure:
5+
6+
+----------------+
7+
| ID (4 bytes) |
8+
+----------------+
9+
| size (4 bytes) |
10+
+----------------+
11+
| data |
12+
| ... |
13+
+----------------+
14+
15+
The ID is a 4-byte string which identifies the type of chunk.
16+
17+
The size field (a 32-bit value, encoded using big-endian byte order)
18+
gives the size of the whole chunk, including the 8-byte header.
19+
20+
Usually a IFF-type file consists of one or more chunks. The proposed
21+
usage of the Chunk class defined here is to instantiate an instance at
22+
the start of each chunk and read from the instance until it reaches
23+
the end, after which a new instance can be instantiated. At the end
24+
of the file, creating a new instance will fail with a EOFError
25+
exception.
26+
27+
Usage:
28+
while 1:
29+
try:
30+
chunk = Chunk(file)
31+
except EOFError:
32+
break
33+
chunktype = chunk.getname()
34+
while 1:
35+
data = chunk.read(nbytes)
36+
if not data:
37+
pass
38+
# do something with data
39+
40+
The interface is file-like. The implemented methods are:
41+
read, close, seek, tell, isatty.
42+
Extra methods are: skip() (called by close, skips to the end of the chunk),
43+
getname() (returns the name (ID) of the chunk)
44+
45+
The __init__ method has one required argument, a file-like object
46+
(including a chunk instance), and one optional argument, a flag which
47+
specifies whether or not chunks are aligned on 2-byte boundaries. The
48+
default is 1, i.e. aligned.
49+
"""
50+
51+
class Chunk:
52+
def __init__(self, file, align = 1):
53+
import struct
54+
self.closed = 0
55+
self.align = align # whether to align to word (2-byte) boundaries
56+
self.file = file
57+
self.chunkname = file.read(4)
58+
if len(self.chunkname) < 4:
59+
raise EOFError
60+
try:
61+
self.chunksize = struct.unpack('>l', file.read(4))[0]
62+
except struct.error:
63+
raise EOFError
64+
self.size_read = 0
65+
self.offset = self.file.tell()
66+
67+
def getname(self):
68+
"""Return the name (ID) of the current chunk."""
69+
return self.chunkname
70+
71+
def close(self):
72+
if not self.closed:
73+
self.skip()
74+
self.closed = 1
75+
76+
def isatty(self):
77+
if self.closed:
78+
raise ValueError, "I/O operation on closed file"
79+
return 0
80+
81+
def seek(self, pos, mode = 0):
82+
"""Seek to specified position into the chunk.
83+
Default position is 0 (start of chunk).
84+
If the file is not seekable, this will result in an error.
85+
"""
86+
87+
if self.closed:
88+
raise ValueError, "I/O operation on closed file"
89+
if mode == 1:
90+
pos = pos + self.size_read
91+
elif mode == 2:
92+
pos = pos + self.chunk_size
93+
if pos < 0 or pos > self.chunksize:
94+
raise RuntimeError
95+
self.file.seek(self.offset + pos, 0)
96+
self.size_read = pos
97+
98+
def tell(self):
99+
if self.closed:
100+
raise ValueError, "I/O operation on closed file"
101+
return self.size_read
102+
103+
def read(self, n = -1):
104+
"""Read at most n bytes from the chunk.
105+
If n is omitted or negative, read until the end
106+
of the chunk.
107+
"""
108+
109+
if self.closed:
110+
raise ValueError, "I/O operation on closed file"
111+
if self.size_read >= self.chunksize:
112+
return ''
113+
if n < 0:
114+
n = self.chunksize - self.size_read
115+
if n > self.chunksize - self.size_read:
116+
n = self.chunksize - self.size_read
117+
data = self.file.read(n)
118+
self.size_read = self.size_read + len(data)
119+
if self.size_read == self.chunksize and \
120+
self.align and \
121+
(self.chunksize & 1):
122+
dummy = self.file.read(1)
123+
self.size_read = self.size_read + len(dummy)
124+
return data
125+
126+
def skip(self):
127+
"""Skip the rest of the chunk.
128+
If you are not interested in the contents of the chunk,
129+
this method should be called so that the file points to
130+
the start of the next chunk.
131+
"""
132+
133+
if self.closed:
134+
raise ValueError, "I/O operation on closed file"
135+
try:
136+
self.file.seek(self.chunksize - self.size_read, 1)
137+
except RuntimeError:
138+
while self.size_read < self.chunksize:
139+
dummy = self.read(8192)
140+
if not dummy:
141+
raise EOFError
142+

0 commit comments

Comments
 (0)