annotate Lib/aifc.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 18451dca9469
children 0bdd31538d8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 # Stuff to parse AIFF-C and AIFF files.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 # Unless explicitly stated otherwise, the description below is true
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 # both for AIFF-C files and AIFF files.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
6 # An AIFF-C file has the following structure.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8 # +-----------------+
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
9 # | FORM |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10 # +-----------------+
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 # | <size> |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 # +----+------------+
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 # | | AIFC |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 # | +------------+
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15 # | | <chunks> |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16 # | | . |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 # | | . |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
18 # | | . |
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19 # +----+------------+
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
21 # An AIFF file has the string "AIFF" instead of "AIFC".
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
22 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
23 # A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
24 # big endian order), followed by the data. The size field does not include
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
25 # the size of the 8 byte header.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 # The following chunk types are recognized.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
28 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
29 # FVER
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30 # <version number of AIFF-C defining document> (AIFF-C only).
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31 # MARK
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 # <# of markers> (2 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
33 # list of markers:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
34 # <marker ID> (2 bytes, must be > 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
35 # <position> (4 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
36 # <marker name> ("pstring")
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
37 # COMM
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
38 # <# of channels> (2 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
39 # <# of sound frames> (4 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40 # <size of the samples> (2 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
41 # <sampling frequency> (10 bytes, IEEE 80-bit extended
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
42 # floating point)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
43 # in AIFF-C files only:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
44 # <compression type> (4 bytes)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
45 # <human-readable version of compression type> ("pstring")
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
46 # SSND
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47 # <offset> (4 bytes, not used by this program)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
48 # <blocksize> (4 bytes, not used by this program)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
49 # <sound data>
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
50 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51 # A pstring consists of 1 byte length, a string of characters, and 0 or 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 # byte pad to make the total length even.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
54 # Usage.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
55 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
56 # Reading AIFF files:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
57 # f = aifc.open(file, 'r')
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
58 # where file is either the name of a file or an open file pointer.
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
59 # The open file pointer must have methods read(), seek(), and close().
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
60 # In some types of audio files, if the setpos() method is not used,
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
61 # the seek() method is not necessary.
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
62 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
63 # This returns an instance of a class with the following public methods:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
64 # getnchannels() -- returns number of audio channels (1 for
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
65 # mono, 2 for stereo)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
66 # getsampwidth() -- returns sample width in bytes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
67 # getframerate() -- returns sampling frequency
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
68 # getnframes() -- returns number of audio frames
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
69 # getcomptype() -- returns compression type ('NONE' for AIFF files)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70 # getcompname() -- returns human-readable version of
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
71 # compression type ('not compressed' for AIFF files)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
72 # getparams() -- returns a tuple consisting of all of the
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
73 # above in the above order
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
74 # getmarkers() -- get the list of marks in the audio file or None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
75 # if there are no marks
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
76 # getmark(id) -- get mark with the specified id (raises an error
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
77 # if the mark does not exist)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78 # readframes(n) -- returns at most n frames of audio
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
79 # rewind() -- rewind to the beginning of the audio stream
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
80 # setpos(pos) -- seek to the specified position
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
81 # tell() -- return the current position
1297
9234c961ec43 * toaiff.py: import whatsound instead of sndhdr
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
82 # close() -- close the instance (make it unusable)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
83 # The position returned by tell(), the position given to setpos() and
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
84 # the position of marks are all compatible and have nothing to do with
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
85 # the actual postion in the file.
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
86 # The close() method is called automatically when the class instance
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
87 # is destroyed.
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
88 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
89 # Writing AIFF files:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
90 # f = aifc.open(file, 'w')
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
91 # where file is either the name of a file or an open file pointer.
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
92 # The open file pointer must have methods write(), tell(), seek(), and
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
93 # close().
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
94 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
95 # This returns an instance of a class with the following public methods:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
96 # aiff() -- create an AIFF file (AIFF-C default)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
97 # aifc() -- create an AIFF-C file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
98 # setnchannels(n) -- set the number of channels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
99 # setsampwidth(n) -- set the sample width
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
100 # setframerate(n) -- set the frame rate
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
101 # setnframes(n) -- set the number of frames
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
102 # setcomptype(type, name)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
103 # -- set the compression type and the
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
104 # human-readable compression type
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
105 # setparams(nchannels, sampwidth, framerate, nframes, comptype, compname)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
106 # -- set all parameters at once
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
107 # setmark(id, pos, name)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
108 # -- add specified mark to the list of marks
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
109 # tell() -- return current position in output file (useful
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
110 # in combination with setmark())
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
111 # writeframesraw(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
112 # -- write audio frames without pathing up the
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 # file header
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
114 # writeframes(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
115 # -- write audio frames and patch up the file header
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
116 # close() -- patch up the file header and close the
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
117 # output file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 # You should set the parameters before the first writeframesraw or
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
119 # writeframes. The total number of frames does not need to be set,
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
120 # but when it is set to the correct value, the header does not have to
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
121 # be patched up.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
122 # It is best to first set all parameters, perhaps possibly the
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
123 # compression type, and then write audio frames using writeframesraw.
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
124 # When all frames have been written, either call writeframes('') or
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
125 # close() to patch up the sizes in the header.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
126 # Marks can be added anytime. If there are any marks, ypu must call
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
127 # close() after all frames have been written.
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
128 # The close() method is called automatically when the class instance
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
129 # is destroyed.
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
131 # When a file is opened with the extension '.aiff', an AIFF file is
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
132 # written, otherwise an AIFF-C file is written. This default can be
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
133 # changed by calling aiff() or aifc() before the first writeframes or
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 # writeframesraw.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
135
1540
4047f224efdb Use __builtin__ instead of builtin
Guido van Rossum <guido@python.org>
parents: 1539
diff changeset
136 import __builtin__
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
137 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
138 import CL
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 except ImportError:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
140 pass
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 Error = 'aifc.Error'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
144 _AIFC_version = 0xA2805140 # Version 1 of AIFF-C
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
146 _skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147 'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
149 def _read_long(file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150 x = 0L
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 for i in range(4):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
152 byte = file.read(1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
153 if byte == '':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
154 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155 x = x*256 + ord(byte)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156 if x >= 0x80000000L:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
157 x = x - 0x100000000L
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 return int(x)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
160 def _read_ulong(file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
161 x = 0L
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 for i in range(4):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 byte = file.read(1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
164 if byte == '':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
165 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
166 x = x*256 + ord(byte)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 return x
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
169 def _read_short(file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
170 x = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 for i in range(2):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 byte = file.read(1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
173 if byte == '':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
174 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
175 x = x*256 + ord(byte)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
176 if x >= 0x8000:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177 x = x - 0x10000
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
178 return x
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
180 def _read_string(file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181 length = ord(file.read(1))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
182 if length == 0:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
183 data = ''
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
184 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
185 data = file.read(length)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
186 if length & 1 == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
187 dummy = file.read(1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
188 return data
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
189
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
190 _HUGE_VAL = 1.79769313486231e+308 # See <limits.h>
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
191
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
192 def _read_float(f): # 10 bytes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
193 import math
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
194 expon = _read_short(f) # 2 bytes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
195 sign = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
196 if expon < 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
197 sign = -1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 expon = expon + 0x8000
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
199 himant = _read_ulong(f) # 4 bytes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
200 lomant = _read_ulong(f) # 4 bytes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 if expon == himant == lomant == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
202 f = 0.0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
203 elif expon == 0x7FFF:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
204 f = _HUGE_VAL
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
205 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206 expon = expon - 16383
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
207 f = (himant * 0x100000000L + lomant) * pow(2.0, expon - 63)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
208 return sign * f
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
209
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
210 def _write_short(f, x):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
211 d, m = divmod(x, 256)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
212 f.write(chr(d))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
213 f.write(chr(m))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
214
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
215 def _write_long(f, x):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
216 if x < 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217 x = x + 0x100000000L
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
218 data = []
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
219 for i in range(4):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 d, m = divmod(x, 256)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
221 data.insert(0, m)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
222 x = d
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
223 for i in range(4):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
224 f.write(chr(int(data[i])))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 def _write_string(f, s):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
227 f.write(chr(len(s)))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
228 f.write(s)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
229 if len(s) & 1 == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
230 f.write(chr(0))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
231
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
232 def _write_float(f, x):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
233 import math
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 if x < 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235 sign = 0x8000
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
236 x = x * -1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
237 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
238 sign = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
239 if x == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
240 expon = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
241 himant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
242 lomant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
243 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
244 fmant, expon = math.frexp(x)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
245 if expon > 16384 or fmant >= 1: # Infinity or NaN
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
246 expon = sign|0x7FFF
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
247 himant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
248 lomant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
249 else: # Finite
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
250 expon = expon + 16382
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
251 if expon < 0: # denormalized
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
252 fmant = math.ldexp(fmant, expon)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
253 expon = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
254 expon = expon | sign
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
255 fmant = math.ldexp(fmant, 32)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
256 fsmant = math.floor(fmant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
257 himant = long(fsmant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
258 fmant = math.ldexp(fmant - fsmant, 32)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
259 fsmant = math.floor(fmant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
260 lomant = long(fsmant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
261 _write_short(f, expon)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
262 _write_long(f, himant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
263 _write_long(f, lomant)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
264
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
265 class Chunk:
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
266 def __init__(self, file):
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
267 self.file = file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
268 self.chunkname = self.file.read(4)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
269 if len(self.chunkname) < 4:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
270 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
271 self.chunksize = _read_long(self.file)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
272 self.size_read = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
273 self.offset = self.file.tell()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
274
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
275 def rewind(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
276 self.file.seek(self.offset, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
277 self.size_read = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
278
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
279 def setpos(self, pos):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
280 if pos < 0 or pos > self.chunksize:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
281 raise RuntimeError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
282 self.file.seek(self.offset + pos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
283 self.size_read = pos
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
284
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
285 def read(self, length):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
286 if self.size_read >= self.chunksize:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
287 return ''
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
288 if length > self.chunksize - self.size_read:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
289 length = self.chunksize - self.size_read
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
290 data = self.file.read(length)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
291 self.size_read = self.size_read + len(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
292 return data
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
293
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
294 def skip(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
295 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
296 self.file.seek(self.chunksize - self.size_read, 1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
297 except RuntimeError:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
298 while self.size_read < self.chunksize:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
299 dummy = self.read(8192)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
300 if not dummy:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
301 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
302 if self.chunksize & 1:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
303 dummy = self.read(1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
304
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
305 class Aifc_read:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
306 # Variables used in this class:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
307 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
308 # These variables are available to the user though appropriate
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
309 # methods of this class:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
310 # _file -- the open file with methods read(), close(), and seek()
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
311 # set through the __init__() method
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
312 # _nchannels -- the number of audio channels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
313 # available through the getnchannels() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
314 # _nframes -- the number of audio frames
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
315 # available through the getnframes() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
316 # _sampwidth -- the number of bytes per audio sample
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
317 # available through the getsampwidth() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
318 # _framerate -- the sampling frequency
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
319 # available through the getframerate() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
320 # _comptype -- the AIFF-C compression type ('NONE' if AIFF)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
321 # available through the getcomptype() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
322 # _compname -- the human-readable AIFF-C compression type
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
323 # available through the getcomptype() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
324 # _markers -- the marks in the audio file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
325 # available through the getmarkers() and getmark()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
326 # methods
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
327 # _soundpos -- the position in the audio stream
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
328 # available through the tell() method, set through the
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
329 # setpos() method
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
330 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
331 # These variables are used internally only:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
332 # _version -- the AIFF-C version number
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
333 # _decomp -- the decompressor from builtin module cl
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
334 # _comm_chunk_read -- 1 iff the COMM chunk has been read
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
335 # _aifc -- 1 iff reading an AIFF-C file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
336 # _ssnd_seek_needed -- 1 iff positioned correctly in audio
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
337 # file for readframes()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
338 # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
339 # _framesize -- size of one frame in the file
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
340
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
341 ## if 0: access _file, _nchannels, _nframes, _sampwidth, _framerate, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
342 ## _comptype, _compname, _markers, _soundpos, _version, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
343 ## _decomp, _comm_chunk_read, __aifc, _ssnd_seek_needed, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
344 ## _ssnd_chunk, _framesize: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
345
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
346 def initfp(self, file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
347 self._file = file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
348 self._version = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
349 self._decomp = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
350 self._convert = None
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
351 self._markers = []
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
352 self._soundpos = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
353 form = self._file.read(4)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
354 if form != 'FORM':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
355 raise Error, 'file does not start with FORM id'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
356 formlength = _read_long(self._file)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
357 if formlength <= 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
358 raise Error, 'invalid FORM chunk data size'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
359 formdata = self._file.read(4)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
360 formlength = formlength - 4
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
361 if formdata == 'AIFF':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
362 self._aifc = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
363 elif formdata == 'AIFC':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
364 self._aifc = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
365 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
366 raise Error, 'not an AIFF or AIFF-C file'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
367 self._comm_chunk_read = 0
1193
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
368 while formlength > 0:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
369 self._ssnd_seek_needed = 1
1204
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
370 #DEBUG: SGI's soundfiler has a bug. There should
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
371 # be no need to check for EOF here.
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
372 try:
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
373 chunk = Chunk(self._file)
1204
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
374 except EOFError:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
375 if formlength == 8:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
376 print 'Warning: FORM chunk size too large'
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
377 formlength = 0
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
378 break
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
379 raise EOFError # different error, raise exception
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
380 if chunk.chunkname == 'COMM':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
381 self._read_comm_chunk(chunk)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
382 self._comm_chunk_read = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
383 elif chunk.chunkname == 'SSND':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
384 self._ssnd_chunk = chunk
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
385 dummy = chunk.read(8)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
386 self._ssnd_seek_needed = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
387 elif chunk.chunkname == 'FVER':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
388 self._version = _read_long(chunk)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
389 elif chunk.chunkname == 'MARK':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
390 self._readmark(chunk)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
391 elif chunk.chunkname in _skiplist:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
392 pass
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
393 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
394 raise Error, 'unrecognized chunk type '+chunk.chunkname
1336
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
395 formlength = formlength - 8 - chunk.chunksize
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
396 if chunk.chunksize & 1:
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
397 formlength = formlength - 1
1196
ce67d91adf5e Fixed a bug where the library inadvertently skipped over the sound
Sjoerd Mullender <sjoerd@acm.org>
parents: 1193
diff changeset
398 if formlength > 0:
ce67d91adf5e Fixed a bug where the library inadvertently skipped over the sound
Sjoerd Mullender <sjoerd@acm.org>
parents: 1193
diff changeset
399 chunk.skip()
1193
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
400 if not self._comm_chunk_read or not self._ssnd_chunk:
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
401 raise Error, 'COMM chunk and/or SSND chunk missing'
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
402 if self._aifc and self._decomp:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
403 params = [CL.ORIGINAL_FORMAT, 0,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
404 CL.BITS_PER_COMPONENT, self._sampwidth * 8,
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
405 CL.FRAME_RATE, self._framerate]
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
406 if self._nchannels == 1:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
407 params[1] = CL.MONO
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
408 elif self._nchannels == 2:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
409 params[1] = CL.STEREO_INTERLEAVED
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
410 else:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
411 raise Error, 'cannot compress more than 2 channels'
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
412 self._decomp.SetParams(params)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
413
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
414 def __init__(self, f):
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
415 if type(f) == type(''):
1893
aba51621244e audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents: 1864
diff changeset
416 f = __builtin__.open(f, 'rb')
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
417 # else, assume it is an open file object already
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
418 self.initfp(f)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
419
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
420 def __del__(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
421 if self._file:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
422 self.close()
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
423
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
424 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
425 # User visible methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
426 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
427 def getfp(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
428 return self._file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
429
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
430 def rewind(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
431 self._ssnd_seek_needed = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
432 self._soundpos = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
433
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
434 def close(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
435 if self._decomp:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
436 self._decomp.CloseDecompressor()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
437 self._decomp = None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
438 self._file = None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
439
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
440 def tell(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
441 return self._soundpos
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
442
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
443 def getnchannels(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
444 return self._nchannels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
445
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
446 def getnframes(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
447 return self._nframes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
448
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
449 def getsampwidth(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
450 return self._sampwidth
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
451
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
452 def getframerate(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
453 return self._framerate
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
454
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
455 def getcomptype(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
456 return self._comptype
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
457
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
458 def getcompname(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
459 return self._compname
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
460
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
461 ## def getversion(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
462 ## return self._version
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
463
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
464 def getparams(self):
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
465 return self.getnchannels(), self.getsampwidth(), \
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
466 self.getframerate(), self.getnframes(), \
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
467 self.getcomptype(), self.getcompname()
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
468
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
469 def getmarkers(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
470 if len(self._markers) == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
471 return None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
472 return self._markers
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
473
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
474 def getmark(self, id):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
475 for marker in self._markers:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
476 if id == marker[0]:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
477 return marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
478 raise Error, 'marker ' + `id` + ' does not exist'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
479
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
480 def setpos(self, pos):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
481 if pos < 0 or pos > self._nframes:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
482 raise Error, 'position not in range'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
483 self._soundpos = pos
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
484 self._ssnd_seek_needed = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
485
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
486 def readframes(self, nframes):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
487 if self._ssnd_seek_needed:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
488 self._ssnd_chunk.rewind()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
489 dummy = self._ssnd_chunk.read(8)
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
490 pos = self._soundpos * self._framesize
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
491 if pos:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
492 self._ssnd_chunk.setpos(pos + 8)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
493 self._ssnd_seek_needed = 0
1204
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
494 if nframes == 0:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
495 return ''
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
496 data = self._ssnd_chunk.read(nframes * self._framesize)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
497 if self._convert and data:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
498 data = self._convert(data)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
499 self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
500 return data
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
501
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
502 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
503 # Internal methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
504 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
505 ## if 0: access *: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
506
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
507 def _decomp_data(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
508 dummy = self._decomp.SetParam(CL.FRAME_BUFFER_SIZE,
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
509 len(data) * 2)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
510 return self._decomp.Decompress(len(data) / self._nchannels,
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
511 data)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
512
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
513 def _ulaw2lin(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
514 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
515 return audioop.ulaw2lin(data, 2)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
516
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
517 def _adpcm2lin(self, data):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
518 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
519 if not hasattr(self, '_adpcmstate'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
520 # first time
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
521 self._adpcmstate = None
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
522 data, self._adpcmstate = audioop.adpcm2lin(data, 2,
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
523 self._adpcmstate)
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
524 return data
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
525
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
526 def _read_comm_chunk(self, chunk):
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
527 self._nchannels = _read_short(chunk)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
528 self._nframes = _read_long(chunk)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
529 self._sampwidth = (_read_short(chunk) + 7) / 8
1539
5a17c43dcff0 aifc.py: framerate should be an int.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1499
diff changeset
530 self._framerate = int(_read_float(chunk))
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
531 self._framesize = self._nchannels * self._sampwidth
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
532 if self._aifc:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
533 #DEBUG: SGI's soundeditor produces a bad size :-(
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
534 kludge = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
535 if chunk.chunksize == 18:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
536 kludge = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
537 print 'Warning: bad COMM chunk size'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
538 chunk.chunksize = 23
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
539 #DEBUG end
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
540 self._comptype = chunk.read(4)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
541 #DEBUG start
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
542 if kludge:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
543 length = ord(chunk.file.read(1))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
544 if length & 1 == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
545 length = length + 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
546 chunk.chunksize = chunk.chunksize + length
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
547 chunk.file.seek(-1, 1)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
548 #DEBUG end
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
549 self._compname = _read_string(chunk)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
550 if self._comptype != 'NONE':
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
551 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
552 try:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
553 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
554 except ImportError:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
555 pass
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
556 else:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
557 self._convert = self._adpcm2lin
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
558 self._framesize = self._framesize / 4
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
559 return
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
560 # for ULAW and ALAW try Compression Library
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
561 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
562 import cl, CL
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
563 except ImportError:
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
564 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
565 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
566 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
567 self._convert = self._ulaw2lin
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
568 self._framesize = self._framesize / 2
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
569 return
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
570 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
571 pass
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
572 raise Error, 'cannot read compressed AIFF-C files'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
573 if self._comptype == 'ULAW':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
574 scheme = CL.G711_ULAW
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
575 self._framesize = self._framesize / 2
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
576 elif self._comptype == 'ALAW':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
577 scheme = CL.G711_ALAW
1205
ecfbc246eeaa CL.py, clmodule.c: Adapted to new CL library. Lots of new methods.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1204
diff changeset
578 self._framesize = self._framesize / 2
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
579 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
580 raise Error, 'unsupported compression type'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
581 self._decomp = cl.OpenDecompressor(scheme)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
582 self._convert = self._decomp_data
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
583 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
584 self._comptype = 'NONE'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
585 self._compname = 'not compressed'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
586
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
587 def _readmark(self, chunk):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
588 nmarkers = _read_short(chunk)
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
589 # Some files appear to contain invalid counts.
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
590 # Cope with this by testing for EOF.
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
591 try:
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
592 for i in range(nmarkers):
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
593 id = _read_short(chunk)
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
594 pos = _read_long(chunk)
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
595 name = _read_string(chunk)
1923
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
596 if pos or name:
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
597 # some files appear to have
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
598 # dummy markers consisting of
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
599 # a position 0 and name ''
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
600 self._markers.append((id, pos, name))
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
601 except EOFError:
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
602 print 'Warning: MARK chunk contains only',
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
603 print len(self._markers),
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
604 if len(self._markers) == 1: print 'marker',
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
605 else: print 'markers',
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
606 print 'instead of', nmarkers
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
607
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
608 class Aifc_write:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
609 # Variables used in this class:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
610 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
611 # These variables are user settable through appropriate methods
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
612 # of this class:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
613 # _file -- the open file with methods write(), close(), tell(), seek()
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
614 # set through the __init__() method
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
615 # _comptype -- the AIFF-C compression type ('NONE' in AIFF)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
616 # set through the setcomptype() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
617 # _compname -- the human-readable AIFF-C compression type
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
618 # set through the setcomptype() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
619 # _nchannels -- the number of audio channels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
620 # set through the setnchannels() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
621 # _sampwidth -- the number of bytes per audio sample
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
622 # set through the setsampwidth() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
623 # _framerate -- the sampling frequency
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
624 # set through the setframerate() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
625 # _nframes -- the number of audio frames written to the header
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
626 # set through the setnframes() or setparams() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
627 # _aifc -- whether we're writing an AIFF-C file or an AIFF file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
628 # set through the aifc() method, reset through the
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
629 # aiff() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
630 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
631 # These variables are used internally only:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
632 # _version -- the AIFF-C version number
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
633 # _comp -- the compressor from builtin module cl
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
634 # _nframeswritten -- the number of audio frames actually written
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
635 # _datalength -- the size of the audio samples written to the header
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
636 # _datawritten -- the size of the audio samples actually written
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
637
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
638 ## if 0: access _file, _comptype, _compname, _nchannels, _sampwidth, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
639 ## _framerate, _nframes, _aifc, _version, _comp, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
640 ## _nframeswritten, _datalength, _datawritten: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
641
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
642 def __init__(self, f):
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
643 if type(f) == type(''):
1436
89a5ce19d13a * aifc.py (Aifc_write): fix bogus reference to filename
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
644 filename = f
1893
aba51621244e audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents: 1864
diff changeset
645 f = __builtin__.open(f, 'wb')
1436
89a5ce19d13a * aifc.py (Aifc_write): fix bogus reference to filename
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
646 else:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
647 # else, assume it is an open file object already
1436
89a5ce19d13a * aifc.py (Aifc_write): fix bogus reference to filename
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
648 filename = '???'
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
649 self.initfp(f)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
650 if filename[-5:] == '.aiff':
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
651 self._aifc = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
652 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
653 self._aifc = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
654
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
655 def initfp(self, file):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
656 self._file = file
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
657 self._version = _AIFC_version
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
658 self._comptype = 'NONE'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
659 self._compname = 'not compressed'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
660 self._comp = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
661 self._convert = None
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
662 self._nchannels = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
663 self._sampwidth = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
664 self._framerate = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
665 self._nframes = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
666 self._nframeswritten = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
667 self._datawritten = 0
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
668 self._datalength = 0
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
669 self._markers = []
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
670 self._marklength = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
671 self._aifc = 1 # AIFF-C is default
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
672
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
673 def __del__(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
674 if self._file:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
675 self.close()
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
676
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
677 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
678 # User visible methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
679 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
680 def aiff(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
681 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
682 raise Error, 'cannot change parameters after starting to write'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
683 self._aifc = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
684
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
685 def aifc(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
686 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
687 raise Error, 'cannot change parameters after starting to write'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
688 self._aifc = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
689
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
690 def setnchannels(self, nchannels):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
691 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
692 raise Error, 'cannot change parameters after starting to write'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
693 if nchannels < 1:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
694 raise Error, 'bad # of channels'
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
695 self._nchannels = nchannels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
696
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
697 def getnchannels(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
698 if not self._nchannels:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
699 raise Error, 'number of channels not set'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
700 return self._nchannels
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
701
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
702 def setsampwidth(self, sampwidth):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
703 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
704 raise Error, 'cannot change parameters after starting to write'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
705 if sampwidth < 1 or sampwidth > 4:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
706 raise Error, 'bad sample width'
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
707 self._sampwidth = sampwidth
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
708
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
709 def getsampwidth(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
710 if not self._sampwidth:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
711 raise Error, 'sample width not set'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
712 return self._sampwidth
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
713
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
714 def setframerate(self, framerate):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
715 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
716 raise Error, 'cannot change parameters after starting to write'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
717 if framerate <= 0:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
718 raise Error, 'bad frame rate'
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
719 self._framerate = framerate
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
720
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
721 def getframerate(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
722 if not self._framerate:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
723 raise Error, 'frame rate not set'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
724 return self._framerate
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
725
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
726 def setnframes(self, nframes):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
727 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
728 raise Error, 'cannot change parameters after starting to write'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
729 self._nframes = nframes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
730
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
731 def getnframes(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
732 return self._nframeswritten
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
733
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
734 def setcomptype(self, comptype, compname):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
735 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
736 raise Error, 'cannot change parameters after starting to write'
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
737 if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
738 raise Error, 'unsupported compression type'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
739 self._comptype = comptype
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
740 self._compname = compname
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
741
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
742 def getcomptype(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
743 return self._comptype
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
744
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
745 def getcompname(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
746 return self._compname
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
747
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
748 ## def setversion(self, version):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
749 ## if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
750 ## raise Error, 'cannot change parameters after starting to write'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
751 ## self._version = version
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
752
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
753 def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
754 if self._nframeswritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
755 raise Error, 'cannot change parameters after starting to write'
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
756 if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
757 raise Error, 'unsupported compression type'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
758 self.setnchannels(nchannels)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
759 self.setsampwidth(sampwidth)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
760 self.setframerate(framerate)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
761 self.setnframes(nframes)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
762 self.setcomptype(comptype, compname)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
763
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
764 def getparams(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
765 if not self._nchannels or not self._sampwidth or not self._framerate:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
766 raise Error, 'not all parameters set'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
767 return self._nchannels, self._sampwidth, self._framerate, \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
768 self._nframes, self._comptype, self._compname
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
769
1193
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
770 def setmark(self, id, pos, name):
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
771 if id <= 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
772 raise Error, 'marker ID must be > 0'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
773 if pos < 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
774 raise Error, 'marker position must be >= 0'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
775 if type(name) != type(''):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
776 raise Error, 'marker name must be a string'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
777 for i in range(len(self._markers)):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
778 if id == self._markers[i][0]:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
779 self._markers[i] = id, pos, name
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
780 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
781 self._markers.append((id, pos, name))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
782
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
783 def getmark(self, id):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
784 for marker in self._markers:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
785 if id == marker[0]:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
786 return marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
787 raise Error, 'marker ' + `id` + ' does not exist'
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
788
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
789 def getmarkers(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
790 if len(self._markers) == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
791 return None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
792 return self._markers
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
793
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
794 def tell(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
795 return self._nframeswritten
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
796
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
797 def writeframesraw(self, data):
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
798 self._ensure_header_written(len(data))
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
799 nframes = len(data) / (self._sampwidth * self._nchannels)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
800 if self._convert:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
801 data = self._convert(data)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
802 self._file.write(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
803 self._nframeswritten = self._nframeswritten + nframes
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
804 self._datawritten = self._datawritten + len(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
805
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
806 def writeframes(self, data):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
807 self.writeframesraw(data)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
808 if self._nframeswritten != self._nframes or \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
809 self._datalength != self._datawritten:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
810 self._patchheader()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
811
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
812 def close(self):
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
813 self._ensure_header_written(0)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
814 if self._datawritten & 1:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
815 # quick pad to even size
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
816 self._file.write(chr(0))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
817 self._datawritten = self._datawritten + 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
818 self._writemarkers()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
819 if self._nframeswritten != self._nframes or \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
820 self._datalength != self._datawritten or \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
821 self._marklength:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
822 self._patchheader()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
823 if self._comp:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
824 self._comp.CloseCompressor()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
825 self._comp = None
1423
0b32ec8a3e20 Don't close the file.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1419
diff changeset
826 self._file.flush()
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
827 self._file = None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
828
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
829 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
830 # Internal methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
831 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
832 ## if 0: access *: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
833
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
834 def _comp_data(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
835 dum = self._comp.SetParam(CL.FRAME_BUFFER_SIZE, len(data))
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
836 dum = self._comp.SetParam(CL.COMPRESSED_BUFFER_SIZE, len(data))
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
837 return self._comp.Compress(nframes, data)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
838
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
839 def _lin2ulaw(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
840 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
841 return audioop.lin2ulaw(data, 2)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
842
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
843 def _lin2adpcm(self, data):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
844 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
845 if not hasattr(self, '_adpcmstate'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
846 self._adpcmstate = None
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
847 data, self._adpcmstate = audioop.lin2adpcm(data, 2,
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
848 self._adpcmstate)
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
849 return data
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
850
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
851 def _ensure_header_written(self, datasize):
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
852 if not self._nframeswritten:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
853 if self._comptype in ('ULAW', 'ALAW'):
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
854 if not self._sampwidth:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
855 self._sampwidth = 2
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
856 if self._sampwidth != 2:
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
857 raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
858 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
859 if not self._sampwidth:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
860 self._sampwidth = 2
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
861 if self._sampwidth != 2:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
862 raise Error, 'sample width must be 2 when compressing with G7.22 (ADPCM)'
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
863 if not self._nchannels:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
864 raise Error, '# channels not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
865 if not self._sampwidth:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
866 raise Error, 'sample width not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
867 if not self._framerate:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
868 raise Error, 'sampling rate not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
869 self._write_header(datasize)
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
870
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
871 def _init_compression(self):
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
872 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
873 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
874 self._convert = self._lin2adpcm
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
875 return
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
876 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
877 import cl, CL
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
878 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
879 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
880 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
881 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
882 self._convert = self._lin2ulaw
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
883 return
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
884 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
885 pass
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
886 raise Error, 'cannot write compressed AIFF-C files'
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
887 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
888 scheme = CL.G711_ULAW
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
889 elif self._comptype == 'ALAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
890 scheme = CL.G711_ALAW
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
891 else:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
892 raise Error, 'unsupported compression type'
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
893 self._comp = cl.OpenCompressor(scheme)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
894 params = [CL.ORIGINAL_FORMAT, 0,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
895 CL.BITS_PER_COMPONENT, self._sampwidth * 8,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
896 CL.FRAME_RATE, self._framerate,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
897 CL.FRAME_BUFFER_SIZE, 100,
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
898 CL.COMPRESSED_BUFFER_SIZE, 100]
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
899 if self._nchannels == 1:
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
900 params[1] = CL.MONO
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
901 elif self._nchannels == 2:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
902 params[1] = CL.STEREO_INTERLEAVED
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
903 else:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
904 raise Error, 'cannot compress more than 2 channels'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
905 self._comp.SetParams(params)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
906 # the compressor produces a header which we ignore
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
907 dummy = self._comp.Compress(0, '')
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
908 self._convert = self._comp_data
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
909
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
910 def _write_header(self, initlength):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
911 if self._aifc and self._comptype != 'NONE':
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
912 self._init_compression()
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
913 self._file.write('FORM')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
914 if not self._nframes:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
915 self._nframes = initlength / (self._nchannels * self._sampwidth)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
916 self._datalength = self._nframes * self._nchannels * self._sampwidth
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
917 if self._datalength & 1:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
918 self._datalength = self._datalength + 1
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
919 if self._aifc:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
920 if self._comptype in ('ULAW', 'ALAW'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
921 self._datalength = self._datalength / 2
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
922 if self._datalength & 1:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
923 self._datalength = self._datalength + 1
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
924 elif self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
925 self._datalength = (self._datalength + 3) / 4
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
926 if self._datalength & 1:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
927 self._datalength = self._datalength + 1
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
928 self._form_length_pos = self._file.tell()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
929 commlength = self._write_form_length(self._datalength)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
930 if self._aifc:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
931 self._file.write('AIFC')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
932 self._file.write('FVER')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
933 _write_long(self._file, 4)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
934 _write_long(self._file, self._version)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
935 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
936 self._file.write('AIFF')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
937 self._file.write('COMM')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
938 _write_long(self._file, commlength)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
939 _write_short(self._file, self._nchannels)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
940 self._nframes_pos = self._file.tell()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
941 _write_long(self._file, self._nframes)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
942 _write_short(self._file, self._sampwidth * 8)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
943 _write_float(self._file, self._framerate)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
944 if self._aifc:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
945 self._file.write(self._comptype)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
946 _write_string(self._file, self._compname)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
947 self._file.write('SSND')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
948 self._ssnd_length_pos = self._file.tell()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
949 _write_long(self._file, self._datalength + 8)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
950 _write_long(self._file, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
951 _write_long(self._file, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
952
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
953 def _write_form_length(self, datalength):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
954 if self._aifc:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
955 commlength = 18 + 5 + len(self._compname)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
956 if commlength & 1:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
957 commlength = commlength + 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
958 verslength = 12
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
959 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
960 commlength = 18
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
961 verslength = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
962 _write_long(self._file, 4 + verslength + self._marklength + \
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
963 8 + commlength + 16 + datalength)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
964 return commlength
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
965
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
966 def _patchheader(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
967 curpos = self._file.tell()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
968 if self._datawritten & 1:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
969 datalength = self._datawritten + 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
970 self._file.write(chr(0))
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
971 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
972 datalength = self._datawritten
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
973 if datalength == self._datalength and \
1193
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
974 self._nframes == self._nframeswritten and \
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
975 self._marklength == 0:
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
976 self._file.seek(curpos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
977 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
978 self._file.seek(self._form_length_pos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
979 dummy = self._write_form_length(datalength)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
980 self._file.seek(self._nframes_pos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
981 _write_long(self._file, self._nframeswritten)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
982 self._file.seek(self._ssnd_length_pos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
983 _write_long(self._file, datalength + 8)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
984 self._file.seek(curpos, 0)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
985 self._nframes = self._nframeswritten
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
986 self._datalength = datalength
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
987
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
988 def _writemarkers(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
989 if len(self._markers) == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
990 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
991 self._file.write('MARK')
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
992 length = 2
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
993 for marker in self._markers:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
994 id, pos, name = marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
995 length = length + len(name) + 1 + 6
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
996 if len(name) & 1 == 0:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
997 length = length + 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
998 _write_long(self._file, length)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
999 self._marklength = length + 8
1193
b7aee799dc71 Found and fixed a few bugs in the handling of audio files with MARKs.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1192
diff changeset
1000 _write_short(self._file, len(self._markers))
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1001 for marker in self._markers:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1002 id, pos, name = marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1003 _write_short(self._file, id)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1004 _write_long(self._file, pos)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1005 _write_string(self._file, name)
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1006
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
1007 def open(f, mode):
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1008 if mode == 'r':
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
1009 return Aifc_read(f)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1010 elif mode == 'w':
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
1011 return Aifc_write(f)
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1012 else:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
1013 raise Error, "mode must be 'r' or 'w'"
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1014
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
1015 openfp = open # B/W compatibility