annotate Lib/aifc.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 536935c29401
children 8b49c918a25f
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
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
136 import struct
1540
4047f224efdb Use __builtin__ instead of builtin
Guido van Rossum <guido@python.org>
parents: 1539
diff changeset
137 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
138 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 import CL
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
140 except ImportError:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141 pass
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 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
144
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145 _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
146
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147 _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
148 '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
149
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150 def _read_long(file):
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
151 try:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
152 return struct.unpack('>l', file.read(4))[0]
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
153 except struct.error:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
154 raise EOFError
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156 def _read_ulong(file):
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
157 try:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
158 return struct.unpack('>L', file.read(4))[0]
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
159 except struct.error:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
160 raise EOFError
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
161
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 def _read_short(file):
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
163 try:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
164 return struct.unpack('>h', file.read(2))[0]
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
165 except struct.error:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
166 raise EOFError
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168 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
169 length = ord(file.read(1))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
170 if length == 0:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
171 data = ''
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
172 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1540
diff changeset
173 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
174 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
175 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
176 return data
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
178 _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
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_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
181 import math
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
182 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
183 sign = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184 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
185 sign = -1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
194 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
195 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
196 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
197
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 def _write_short(f, x):
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
199 f.write(struct.pack('>h', x))
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
200
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 def _write_long(f, x):
4930
536935c29401 Use newer struct, which handles unsigned long right
Guido van Rossum <guido@python.org>
parents: 4845
diff changeset
202 f.write(struct.pack('>L', x))
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
203
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
204 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
205 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
206 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
207 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
208 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
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_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
211 import math
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
212 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
213 sign = 0x8000
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
214 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
215 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
216 sign = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217 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
218 expon = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
219 himant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 lomant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
221 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
222 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
223 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
224 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
225 himant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 lomant = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
227 else: # Finite
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
228 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
229 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
230 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
231 expon = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 _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
240 _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
241 _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
242
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
243 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
244 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
245 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
246 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
247 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
248 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
249 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
250 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
251 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
252
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
253 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
254 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
255 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
256
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
257 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
258 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
259 raise RuntimeError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
260 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
261 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
262
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
263 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
264 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
265 return ''
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
266 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
267 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
268 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
269 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
270 return data
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
271
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
272 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
273 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
274 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
275 except RuntimeError:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
276 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
277 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
278 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
279 raise EOFError
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
280 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
281 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
282
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
283 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
284 # 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
285 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
286 # 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
287 # 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
288 # _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
289 # 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
290 # _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
291 # 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
292 # _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
293 # 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
294 # _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
295 # 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
296 # _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
297 # 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
298 # _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
299 # 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
300 # _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
301 # 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
302 # _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
303 # 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
304 # methods
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
305 # _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
306 # 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
307 # 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
308 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
309 # 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
310 # _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
311 # _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
312 # _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
313 # _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
314 # _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
315 # 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
316 # _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
317 # _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
318
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
319 ## if 0: access _file, _nchannels, _nframes, _sampwidth, _framerate, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
320 ## _comptype, _compname, _markers, _soundpos, _version, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
321 ## _decomp, _comm_chunk_read, __aifc, _ssnd_seek_needed, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
322 ## _ssnd_chunk, _framesize: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
323
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
324 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
325 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
326 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
327 self._decomp = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
328 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
329 self._markers = []
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
342 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
343 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
344 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
345 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
346 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
347 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
348 #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
349 # 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
350 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
351 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
352 except EOFError:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
353 if formlength == 8:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
354 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
355 formlength = 0
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
356 break
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364 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
365 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
366 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
367 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
368 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
369 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
370 pass
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
371 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
372 raise Error, 'unrecognized chunk type '+chunk.chunkname
1336
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
373 formlength = formlength - 8 - chunk.chunksize
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
374 if chunk.chunksize & 1:
c44545e595a1 Fixed dealing with faulty COMM chunks.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1315
diff changeset
375 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
376 if formlength > 0:
ce67d91adf5e Fixed a bug where the library inadvertently skipped over the sound
Sjoerd Mullender <sjoerd@acm.org>
parents: 1193
diff changeset
377 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
378 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
379 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
380 if self._aifc and self._decomp:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
381 params = [CL.ORIGINAL_FORMAT, 0,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
382 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
383 CL.FRAME_RATE, self._framerate]
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
384 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
385 params[1] = CL.MONO
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
386 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
387 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
388 else:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
389 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
390 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
391
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
392 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
393 if type(f) == type(''):
1893
aba51621244e audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents: 1864
diff changeset
394 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
395 # 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
396 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
397
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
398 def __del__(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
399 if self._file:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
400 self.close()
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
401
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
402 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
403 # 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
404 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
405 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
406 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
407
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
408 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
409 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
410 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
411
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
412 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
413 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
414 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
415 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
416 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
417
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
418 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
419 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
420
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
421 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
422 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
423
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
424 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
425 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
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 getsampwidth(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._sampwidth
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 getframerate(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
431 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
432
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
433 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
434 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
435
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
436 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
437 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
438
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
439 ## 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
440 ## 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
441
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
442 def getparams(self):
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
443 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
444 self.getframerate(), self.getnframes(), \
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
445 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
446
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
447 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
448 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
449 return None
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._markers
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 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
453 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
454 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
455 return marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
456 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
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 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
459 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
460 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
461 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
462 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
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 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
465 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
466 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
467 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
468 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
469 if pos:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
470 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
471 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
472 if nframes == 0:
3573ef421bf4 added kludge for buggy soundfiler from SGI; when reading 0 frames,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1196
diff changeset
473 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
474 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
475 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
476 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
477 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
478 return data
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 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
481 # Internal methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
482 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
483 ## if 0: access *: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
484
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
485 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
486 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
487 len(data) * 2)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
488 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
489 data)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
490
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
491 def _ulaw2lin(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
492 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
493 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
494
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
495 def _adpcm2lin(self, data):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
496 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
497 if not hasattr(self, '_adpcmstate'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
498 # first time
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
499 self._adpcmstate = None
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
500 data, self._adpcmstate = audioop.adpcm2lin(data, 2,
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
501 self._adpcmstate)
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
502 return data
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
503
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
504 def _read_comm_chunk(self, chunk):
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
505 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
506 self._nframes = _read_long(chunk)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
507 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
508 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
509 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
510 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
511 #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
512 kludge = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
513 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
514 kludge = 1
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
515 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
516 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
517 #DEBUG end
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
518 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
519 #DEBUG start
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
520 if kludge:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
521 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
522 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
523 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
524 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
525 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
526 #DEBUG end
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
527 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
528 if self._comptype != 'NONE':
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
529 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
530 try:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
531 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
532 except ImportError:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
533 pass
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
534 else:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
535 self._convert = self._adpcm2lin
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
536 self._framesize = self._framesize / 4
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
537 return
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
538 # 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
539 try:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
540 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
541 except ImportError:
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
542 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
543 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
544 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
545 self._convert = self._ulaw2lin
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
546 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
547 return
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
548 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
549 pass
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
550 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
551 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
552 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
553 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
554 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
555 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
556 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
557 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
558 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
559 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
560 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
561 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
562 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
563 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
564
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
565 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
566 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
567 # 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
568 # 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
569 try:
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
570 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
571 id = _read_short(chunk)
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
572 pos = _read_long(chunk)
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
573 name = _read_string(chunk)
1923
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
574 if pos or name:
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
575 # some files appear to have
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
576 # dummy markers consisting of
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
577 # a position 0 and name ''
c1088d0bb562 Ignore empty markers.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1893
diff changeset
578 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
579 except EOFError:
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
580 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
581 print len(self._markers),
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
582 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
583 else: print 'markers',
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
584 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
585
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
586 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
587 # 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
588 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
589 # 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
590 # 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
591 # _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
592 # 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
593 # _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
594 # 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
595 # _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
596 # 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
597 # _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
598 # 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
599 # _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
600 # 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
601 # _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
602 # 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
603 # _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
604 # 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
605 # _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
606 # 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
607 # aiff() method
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
608 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
609 # 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
610 # _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
611 # _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
612 # _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
613 # _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
614 # _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
615
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
616 ## if 0: access _file, _comptype, _compname, _nchannels, _sampwidth, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
617 ## _framerate, _nframes, _aifc, _version, _comp, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
618 ## _nframeswritten, _datalength, _datawritten: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
619
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
620 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
621 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
622 filename = f
1893
aba51621244e audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents: 1864
diff changeset
623 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
624 else:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
625 # 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
626 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
627 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
628 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
629 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
630 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
631 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
632
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
633 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
634 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
635 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
636 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
637 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
638 self._comp = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
639 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
640 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
641 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
642 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
643 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
644 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
645 self._datawritten = 0
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
646 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
647 self._markers = []
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
648 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
649 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
650
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
651 def __del__(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
652 if self._file:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
653 self.close()
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
654
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
655 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
656 # 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
657 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
658 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
659 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
660 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
661 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
662
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
663 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
664 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
665 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
666 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
667
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
668 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
669 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
670 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
671 if nchannels < 1:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
672 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
673 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
674
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
675 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
676 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
677 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
678 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
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 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
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'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
683 if sampwidth < 1 or sampwidth > 4:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
684 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
685 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
686
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
687 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
688 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
689 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
690 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
691
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
692 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
693 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
694 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
695 if framerate <= 0:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
696 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
697 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
698
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
699 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
700 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
701 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
702 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
703
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
704 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
705 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
706 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
707 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
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 getnframes(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
710 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
711
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
712 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
713 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
714 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
715 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
716 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
717 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
718 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
719
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
720 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
721 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
722
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
723 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
724 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
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 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
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._version = version
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 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
732 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
733 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
734 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
735 raise Error, 'unsupported compression type'
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
736 self.setnchannels(nchannels)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
737 self.setsampwidth(sampwidth)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
738 self.setframerate(framerate)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
739 self.setnframes(nframes)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
740 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
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 getparams(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
743 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
744 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
745 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
746 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
747
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
748 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
749 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
750 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
751 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
752 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
753 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
754 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
755 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
756 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
757 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
758 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
759 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
760
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
761 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
762 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
763 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
764 return marker
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
765 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
766
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
767 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
768 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
769 return None
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
770 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
771
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
772 def tell(self):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
773 return self._nframeswritten
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
774
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
775 def writeframesraw(self, data):
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
776 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
777 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
778 if self._convert:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
779 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
780 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
781 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
782 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
783
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
784 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
785 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
786 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
787 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
788 self._patchheader()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
789
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
790 def close(self):
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
791 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
792 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
793 # 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
794 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
795 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
796 self._writemarkers()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
797 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
798 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
799 self._marklength:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
800 self._patchheader()
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
801 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
802 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
803 self._comp = None
1423
0b32ec8a3e20 Don't close the file.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1419
diff changeset
804 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
805 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
806
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
807 #
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
808 # Internal methods.
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
809 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 2948
diff changeset
810 ## if 0: access *: private
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
811
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
812 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
813 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
814 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
815 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
816
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
817 def _lin2ulaw(self, data):
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
818 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
819 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
820
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
821 def _lin2adpcm(self, data):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
822 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
823 if not hasattr(self, '_adpcmstate'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
824 self._adpcmstate = None
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
825 data, self._adpcmstate = audioop.lin2adpcm(data, 2,
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
826 self._adpcmstate)
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
827 return data
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
828
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
829 def _ensure_header_written(self, datasize):
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
830 if not self._nframeswritten:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
831 if self._comptype in ('ULAW', 'ALAW'):
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
832 if not self._sampwidth:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
833 self._sampwidth = 2
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
834 if self._sampwidth != 2:
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
835 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
836 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
837 if not self._sampwidth:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
838 self._sampwidth = 2
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
839 if self._sampwidth != 2:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
840 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
841 if not self._nchannels:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
842 raise Error, '# channels not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
843 if not self._sampwidth:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
844 raise Error, 'sample width not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
845 if not self._framerate:
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
846 raise Error, 'sampling rate not specified'
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
847 self._write_header(datasize)
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
848
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
849 def _init_compression(self):
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
850 if self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
851 import audioop
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
852 self._convert = self._lin2adpcm
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
853 return
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
854 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
855 import cl, CL
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
856 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
857 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
858 try:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
859 import audioop
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
860 self._convert = self._lin2ulaw
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
861 return
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
862 except ImportError:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
863 pass
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
864 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
865 if self._comptype == 'ULAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
866 scheme = CL.G711_ULAW
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
867 elif self._comptype == 'ALAW':
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
868 scheme = CL.G711_ALAW
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
869 else:
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
870 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
871 self._comp = cl.OpenCompressor(scheme)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
872 params = [CL.ORIGINAL_FORMAT, 0,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
873 CL.BITS_PER_COMPONENT, self._sampwidth * 8,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
874 CL.FRAME_RATE, self._framerate,
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
875 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
876 CL.COMPRESSED_BUFFER_SIZE, 100]
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
877 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
878 params[1] = CL.MONO
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
879 elif self._nchannels == 2:
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
880 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
881 else:
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
882 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
883 self._comp.SetParams(params)
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
884 # 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
885 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
886 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
887
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
888 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
889 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
890 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
891 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
892 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
893 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
894 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
895 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
896 self._datalength = self._datalength + 1
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
897 if self._aifc:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
898 if self._comptype in ('ULAW', 'ALAW'):
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
899 self._datalength = self._datalength / 2
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
900 if self._datalength & 1:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
901 self._datalength = self._datalength + 1
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
902 elif self._comptype == 'G722':
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
903 self._datalength = (self._datalength + 3) / 4
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
904 if self._datalength & 1:
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
905 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
906 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
907 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
908 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
909 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
910 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
911 _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
912 _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
913 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
914 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
915 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
916 _write_long(self._file, commlength)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
917 _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
918 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
919 _write_long(self._file, self._nframes)
1498
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
920 _write_short(self._file, self._sampwidth * 8)
a75f70ca1be0 Removed silly dependence on AL module.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1437
diff changeset
921 _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
922 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
923 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
924 _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
925 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
926 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
927 _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
928 _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
929 _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
930
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
931 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
932 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
933 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
934 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
935 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
936 verslength = 12
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
937 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
938 commlength = 18
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
939 verslength = 0
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
940 _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
941 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
942 return commlength
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
943
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
944 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
945 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
946 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
947 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
948 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
949 else:
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
950 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
951 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
952 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
953 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
954 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
955 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
956 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
957 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
958 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
959 _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
960 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
961 _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
962 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
963 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
964 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
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 _writemarkers(self):
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
967 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
968 return
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
969 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
970 length = 2
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
971 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
972 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
973 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
974 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
975 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
976 _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
977 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
978 _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
979 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
980 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
981 _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
982 _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
983 _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
984
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
985 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
986 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
987 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
988 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
989 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
990 else:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
991 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
992
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
993 openfp = open # B/W compatibility
4845
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
994
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
995 if __name__ == '__main__':
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
996 import sys
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
997 if not sys.argv[1:]:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
998 sys.argv.append('/usr/demos/data/audio/bach.aiff')
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
999 fn = sys.argv[1]
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1000 f = open(fn, 'r')
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1001 print "Reading", fn
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1002 print "nchannels =", f.getnchannels()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1003 print "nframes =", f.getnframes()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1004 print "sampwidth =", f.getsampwidth()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1005 print "framerate =", f.getframerate()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1006 print "comptype =", f.getcomptype()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1007 print "compname =", f.getcompname()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1008 if sys.argv[2:]:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1009 gn = sys.argv[2]
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1010 print "Writing", gn
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1011 g = open(gn, 'w')
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1012 g.setparams(f.getparams())
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1013 while 1:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1014 data = f.readframes(1024)
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1015 if not data:
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1016 break
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1017 g.writeframes(data)
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1018 g.close()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1019 f.close()
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
1020 print "Done."