annotate Lib/aifc.py @ 54294:dba767bc3155 legacy-trunk

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