annotate Lib/aifc.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 8fac90d0f4cd
children cf8d692cc847
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
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
165 def _read_ushort(file):
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
166 try:
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
167 return struct.unpack('>H', file.read(2))[0]
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
168 except struct.error:
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
169 raise EOFError
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
170
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 def _read_string(file):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
172 length = ord(file.read(1))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
173 if length == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
174 data = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
175 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
176 data = file.read(length)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
177 if length & 1 == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
178 dummy = file.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
179 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
180
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181 _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
182
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
183 def _read_float(f): # 10 bytes
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
184 expon = _read_short(f) # 2 bytes
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
185 sign = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
186 if expon < 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
187 sign = -1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
188 expon = expon + 0x8000
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
189 himant = _read_ulong(f) # 4 bytes
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
190 lomant = _read_ulong(f) # 4 bytes
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
191 if expon == himant == lomant == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
192 f = 0.0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
193 elif expon == 0x7FFF:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
194 f = _HUGE_VAL
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
195 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
196 expon = expon - 16383
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
197 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
198 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
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_short(f, x):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
201 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
202
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
203 def _write_ushort(f, x):
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
204 f.write(struct.pack('>H', x))
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
205
1192
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206 def _write_long(f, x):
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
207 f.write(struct.pack('>l', x))
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
208
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
209 def _write_ulong(f, x):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
210 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
211
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
212 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
213 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
214 raise ValueError("string exceeds maximum pstring length")
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
215 f.write(struct.pack('B', len(s)))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
216 f.write(s)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
217 if len(s) & 1 == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
218 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
219
78428243feaa New module aifc to read and write AIFF-C and AIFF files. See
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 def _write_float(f, x):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
221 import math
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
222 if x < 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
223 sign = 0x8000
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
224 x = x * -1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
225 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
226 sign = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
227 if x == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
228 expon = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
229 himant = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
230 lomant = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
231 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
232 fmant, expon = math.frexp(x)
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
233 if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
234 expon = sign|0x7FFF
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
235 himant = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
236 lomant = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
237 else: # Finite
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
238 expon = expon + 16382
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
239 if expon < 0: # denormalized
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
240 fmant = math.ldexp(fmant, expon)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
241 expon = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
242 expon = expon | sign
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
243 fmant = math.ldexp(fmant, 32)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
244 fsmant = math.floor(fmant)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
245 himant = long(fsmant)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
246 fmant = math.ldexp(fmant - fsmant, 32)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
247 fsmant = math.floor(fmant)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
248 lomant = long(fsmant)
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
249 _write_ushort(f, expon)
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
250 _write_ulong(f, himant)
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
251 _write_ulong(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
252
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10817
diff changeset
253 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
254
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
255 class Aifc_read:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
256 # Variables used in this class:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
257 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
258 # 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
259 # methods of this class:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
260 # _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
261 # set through the __init__() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
262 # _nchannels -- the number of audio channels
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
263 # available through the getnchannels() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
264 # _nframes -- the number of audio frames
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
265 # available through the getnframes() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
266 # _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
267 # available through the getsampwidth() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
268 # _framerate -- the sampling frequency
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
269 # available through the getframerate() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
270 # _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
271 # available through the getcomptype() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
272 # _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
273 # available through the getcomptype() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
274 # _markers -- the marks in the audio file
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
275 # available through the getmarkers() and getmark()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
276 # methods
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
277 # _soundpos -- the position in the audio stream
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
278 # 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
279 # setpos() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
280 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
281 # These variables are used internally only:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
282 # _version -- the AIFF-C version number
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
283 # _decomp -- the decompressor from builtin module cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
284 # _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
285 # _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
286 # _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
287 # file for readframes()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
288 # _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
289 # _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
290
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
291 def initfp(self, file):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
292 self._version = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
293 self._decomp = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
294 self._convert = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
295 self._markers = []
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
296 self._soundpos = 0
53876
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
297 self._file = file
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
298 chunk = Chunk(file)
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
299 if chunk.getname() != 'FORM':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
300 raise Error, 'file does not start with FORM id'
53876
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
301 formdata = chunk.read(4)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
302 if formdata == 'AIFF':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
303 self._aifc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
304 elif formdata == 'AIFC':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
305 self._aifc = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
306 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
307 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
308 self._comm_chunk_read = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
309 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
310 self._ssnd_seek_needed = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
311 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
312 chunk = Chunk(self._file)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
313 except EOFError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
314 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
315 chunkname = chunk.getname()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
316 if chunkname == 'COMM':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
317 self._read_comm_chunk(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
318 self._comm_chunk_read = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
319 elif chunkname == 'SSND':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
320 self._ssnd_chunk = chunk
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
321 dummy = chunk.read(8)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
322 self._ssnd_seek_needed = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
323 elif chunkname == 'FVER':
24776
fe3472f2f8eb Fix wanrings about unsigned hex constants.
Guido van Rossum <guido@python.org>
parents: 21916
diff changeset
324 self._version = _read_ulong(chunk)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
325 elif chunkname == 'MARK':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
326 self._readmark(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
327 chunk.skip()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
328 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
329 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
330 if self._aifc and self._decomp:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
331 import cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
332 params = [cl.ORIGINAL_FORMAT, 0,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
333 cl.BITS_PER_COMPONENT, self._sampwidth * 8,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
334 cl.FRAME_RATE, self._framerate]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
335 if self._nchannels == 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
336 params[1] = cl.MONO
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
337 elif self._nchannels == 2:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
338 params[1] = cl.STEREO_INTERLEAVED
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
339 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
340 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
341 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
342
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
343 def __init__(self, f):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
344 if type(f) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
345 f = __builtin__.open(f, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
346 # 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
347 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
348
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
349 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
350 # User visible methods.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
351 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
352 def getfp(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
353 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
354
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
355 def rewind(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
356 self._ssnd_seek_needed = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
357 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
358
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
359 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
360 if self._decomp:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
361 self._decomp.CloseDecompressor()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
362 self._decomp = None
53876
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
363 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
364
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
365 def tell(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
366 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
367
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
368 def getnchannels(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
369 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
370
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
371 def getnframes(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
372 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
373
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
374 def getsampwidth(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
375 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
376
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
377 def getframerate(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
378 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
379
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
380 def getcomptype(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
381 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
382
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
383 def getcompname(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
384 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
385
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
386 ## def getversion(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
387 ## 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
388
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
389 def getparams(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
390 return self.getnchannels(), self.getsampwidth(), \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
391 self.getframerate(), self.getnframes(), \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
392 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
393
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
394 def getmarkers(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
395 if len(self._markers) == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
396 return None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
397 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
398
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
399 def getmark(self, id):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
400 for marker in self._markers:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
401 if id == marker[0]:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
402 return marker
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 24776
diff changeset
403 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
404
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
405 def setpos(self, pos):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
406 if pos < 0 or pos > self._nframes:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
407 raise Error, 'position not in range'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
408 self._soundpos = pos
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
409 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
410
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
411 def readframes(self, nframes):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
412 if self._ssnd_seek_needed:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
413 self._ssnd_chunk.seek(0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
414 dummy = self._ssnd_chunk.read(8)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
415 pos = self._soundpos * self._framesize
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
416 if pos:
11940
de8404573fe5 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
417 self._ssnd_chunk.seek(pos + 8)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
418 self._ssnd_seek_needed = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
419 if nframes == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
420 return ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
421 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
422 if self._convert and data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
423 data = self._convert(data)
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
424 self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
425 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
426
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
427 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
428 # Internal methods.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
429 #
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
430
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
431 def _decomp_data(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
432 import cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
433 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
434 len(data) * 2)
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
435 return self._decomp.Decompress(len(data) // self._nchannels,
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
436 data)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
437
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
438 def _ulaw2lin(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
439 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
440 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
441
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
442 def _adpcm2lin(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
443 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
444 if not hasattr(self, '_adpcmstate'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
445 # first time
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
446 self._adpcmstate = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
447 data, self._adpcmstate = audioop.adpcm2lin(data, 2,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
448 self._adpcmstate)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
449 return data
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
450
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
451 def _read_comm_chunk(self, chunk):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
452 self._nchannels = _read_short(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
453 self._nframes = _read_long(chunk)
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
454 self._sampwidth = (_read_short(chunk) + 7) // 8
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
455 self._framerate = int(_read_float(chunk))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
456 self._framesize = self._nchannels * self._sampwidth
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
457 if self._aifc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
458 #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
459 kludge = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
460 if chunk.chunksize == 18:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
461 kludge = 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
462 print 'Warning: bad COMM chunk size'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
463 chunk.chunksize = 23
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
464 #DEBUG end
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
465 self._comptype = chunk.read(4)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
466 #DEBUG start
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
467 if kludge:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
468 length = ord(chunk.file.read(1))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
469 if length & 1 == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
470 length = length + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
471 chunk.chunksize = chunk.chunksize + length
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
472 chunk.file.seek(-1, 1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
473 #DEBUG end
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
474 self._compname = _read_string(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
475 if self._comptype != 'NONE':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
476 if self._comptype == 'G722':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
477 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
478 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
479 except ImportError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
480 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
481 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
482 self._convert = self._adpcm2lin
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
483 self._framesize = self._framesize // 4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
484 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
485 # for ULAW and ALAW try Compression Library
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
486 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
487 import cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
488 except ImportError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
489 if self._comptype == 'ULAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
490 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
491 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
492 self._convert = self._ulaw2lin
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
493 self._framesize = self._framesize // 2
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
494 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
495 except ImportError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
496 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
497 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
498 if self._comptype == 'ULAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
499 scheme = cl.G711_ULAW
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
500 self._framesize = self._framesize // 2
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
501 elif self._comptype == 'ALAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
502 scheme = cl.G711_ALAW
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
503 self._framesize = self._framesize // 2
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
504 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
505 raise Error, 'unsupported compression type'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
506 self._decomp = cl.OpenDecompressor(scheme)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
507 self._convert = self._decomp_data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
508 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
509 self._comptype = 'NONE'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
510 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
511
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
512 def _readmark(self, chunk):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
513 nmarkers = _read_short(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
514 # Some files appear to contain invalid counts.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
515 # Cope with this by testing for EOF.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
516 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
517 for i in range(nmarkers):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
518 id = _read_short(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
519 pos = _read_long(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
520 name = _read_string(chunk)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
521 if pos or name:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
522 # some files appear to have
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
523 # dummy markers consisting of
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
524 # a position 0 and name ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
525 self._markers.append((id, pos, name))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
526 except EOFError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
527 print 'Warning: MARK chunk contains only',
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
528 print len(self._markers),
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
529 if len(self._markers) == 1: print 'marker',
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
530 else: print 'markers',
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
531 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
532
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1208
diff changeset
533 class Aifc_write:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
534 # Variables used in this class:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
535 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
536 # 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
537 # of this class:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
538 # _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
539 # set through the __init__() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
540 # _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
541 # set through the setcomptype() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
542 # _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
543 # set through the setcomptype() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
544 # _nchannels -- the number of audio channels
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
545 # set through the setnchannels() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
546 # _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
547 # set through the setsampwidth() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
548 # _framerate -- the sampling frequency
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
549 # set through the setframerate() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
550 # _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
551 # set through the setnframes() or setparams() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
552 # _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
553 # 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
554 # aiff() method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
555 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
556 # These variables are used internally only:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
557 # _version -- the AIFF-C version number
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
558 # _comp -- the compressor from builtin module cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
559 # _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
560 # _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
561 # _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
562
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
563 def __init__(self, f):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
564 if type(f) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
565 filename = f
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
566 f = __builtin__.open(f, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
567 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
568 # 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
569 filename = '???'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
570 self.initfp(f)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
571 if filename[-5:] == '.aiff':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
572 self._aifc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
573 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
574 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
575
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
576 def initfp(self, file):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
577 self._file = file
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
578 self._version = _AIFC_version
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
579 self._comptype = 'NONE'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
580 self._compname = 'not compressed'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
581 self._comp = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
582 self._convert = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
583 self._nchannels = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
584 self._sampwidth = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
585 self._framerate = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
586 self._nframes = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
587 self._nframeswritten = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
588 self._datawritten = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
589 self._datalength = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
590 self._markers = []
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
591 self._marklength = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
592 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
593
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
594 def __del__(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
595 if self._file:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
596 self.close()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
597
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
598 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
599 # User visible methods.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
600 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
601 def aiff(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
602 if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
603 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
604 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
605
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
606 def aifc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
607 if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
608 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
609 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
610
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
611 def setnchannels(self, nchannels):
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 nchannels < 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
615 raise Error, 'bad # of channels'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
616 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
617
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
618 def getnchannels(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
619 if not self._nchannels:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
620 raise Error, 'number of channels not set'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
621 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
622
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
623 def setsampwidth(self, sampwidth):
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 sampwidth < 1 or sampwidth > 4:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
627 raise Error, 'bad sample width'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
628 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
629
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
630 def getsampwidth(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
631 if not self._sampwidth:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
632 raise Error, 'sample width not set'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
633 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
634
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
635 def setframerate(self, framerate):
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 if framerate <= 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
639 raise Error, 'bad frame rate'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
640 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
641
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
642 def getframerate(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
643 if not self._framerate:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
644 raise Error, 'frame rate not set'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
645 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
646
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
647 def setnframes(self, nframes):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
648 if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
649 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
650 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
651
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
652 def getnframes(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
653 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
654
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
655 def setcomptype(self, comptype, compname):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
656 if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
657 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
658 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
659 raise Error, 'unsupported compression type'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
660 self._comptype = comptype
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
661 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
662
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
663 def getcomptype(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
664 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
665
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
666 def getcompname(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
667 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
668
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
669 ## def setversion(self, version):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
670 ## if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
671 ## 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
672 ## 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
673
48179
df277e642e58 Remove tuple parameter unpacking in aifc to silence warnings under -3.
Brett Cannon <bcannon@gmail.com>
parents: 38494
diff changeset
674 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
675 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
676 if self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
677 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
678 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
679 raise Error, 'unsupported compression type'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
680 self.setnchannels(nchannels)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
681 self.setsampwidth(sampwidth)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
682 self.setframerate(framerate)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
683 self.setnframes(nframes)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
684 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
685
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
686 def getparams(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
687 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
688 raise Error, 'not all parameters set'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
689 return self._nchannels, self._sampwidth, self._framerate, \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
690 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
691
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
692 def setmark(self, id, pos, name):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
693 if id <= 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
694 raise Error, 'marker ID must be > 0'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
695 if pos < 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
696 raise Error, 'marker position must be >= 0'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
697 if type(name) != type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
698 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
699 for i in range(len(self._markers)):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
700 if id == self._markers[i][0]:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
701 self._markers[i] = id, pos, name
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
702 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
703 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
704
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
705 def getmark(self, id):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
706 for marker in self._markers:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
707 if id == marker[0]:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
708 return marker
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 24776
diff changeset
709 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
710
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
711 def getmarkers(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
712 if len(self._markers) == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
713 return None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
714 return self._markers
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14022
diff changeset
715
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
716 def tell(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
717 return self._nframeswritten
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents: 1336
diff changeset
718
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
719 def writeframesraw(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
720 self._ensure_header_written(len(data))
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
721 nframes = len(data) // (self._sampwidth * self._nchannels)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
722 if self._convert:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
723 data = self._convert(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
724 self._file.write(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
725 self._nframeswritten = self._nframeswritten + nframes
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
726 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
727
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
728 def writeframes(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
729 self.writeframesraw(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
730 if self._nframeswritten != self._nframes or \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
731 self._datalength != self._datawritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
732 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
733
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
734 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
735 self._ensure_header_written(0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
736 if self._datawritten & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
737 # quick pad to even size
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
738 self._file.write(chr(0))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
739 self._datawritten = self._datawritten + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
740 self._writemarkers()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
741 if self._nframeswritten != self._nframes or \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
742 self._datalength != self._datawritten or \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
743 self._marklength:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
744 self._patchheader()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
745 if self._comp:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
746 self._comp.CloseCompressor()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
747 self._comp = None
53627
d682c9346848 prevent ref cycles by removing bound method on close()
Benjamin Peterson <benjamin@python.org>
parents: 53598
diff changeset
748 # Prevent ref cycles
d682c9346848 prevent ref cycles by removing bound method on close()
Benjamin Peterson <benjamin@python.org>
parents: 53598
diff changeset
749 self._convert = None
53876
7a3bd179b180 Merged revisions 72422,72425-72426 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53598
diff changeset
750 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
751
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
752 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
753 # Internal methods.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
754 #
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1436
diff changeset
755
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
756 def _comp_data(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
757 import cl
21916
15c0e7aef666 SF #515004 cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 16070
diff changeset
758 dummy = self._comp.SetParam(cl.FRAME_BUFFER_SIZE, len(data))
15c0e7aef666 SF #515004 cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 16070
diff changeset
759 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
760 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
761
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
762 def _lin2ulaw(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
763 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
764 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
765
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
766 def _lin2adpcm(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
767 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
768 if not hasattr(self, '_adpcmstate'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
769 self._adpcmstate = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
770 data, self._adpcmstate = audioop.lin2adpcm(data, 2,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
771 self._adpcmstate)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
772 return data
1864
aaf1e05e6842 Added support for ADPCM compression.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1746
diff changeset
773
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
774 def _ensure_header_written(self, datasize):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
775 if not self._nframeswritten:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
776 if self._comptype in ('ULAW', 'ALAW'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
777 if not self._sampwidth:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
778 self._sampwidth = 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
779 if self._sampwidth != 2:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
780 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
781 if self._comptype == 'G722':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
782 if not self._sampwidth:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
783 self._sampwidth = 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
784 if self._sampwidth != 2:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
785 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
786 if not self._nchannels:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
787 raise Error, '# channels not specified'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
788 if not self._sampwidth:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
789 raise Error, 'sample width not specified'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
790 if not self._framerate:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
791 raise Error, 'sampling rate not specified'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
792 self._write_header(datasize)
1314
918bee90f73e * calendar.py: minor cleanups
Guido van Rossum <guido@python.org>
parents: 1297
diff changeset
793
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
794 def _init_compression(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
795 if self._comptype == 'G722':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
796 self._convert = self._lin2adpcm
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
797 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
798 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
799 import cl
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
800 except ImportError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
801 if self._comptype == 'ULAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
802 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
803 import audioop
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
804 self._convert = self._lin2ulaw
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
805 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
806 except ImportError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
807 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
808 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
809 if self._comptype == 'ULAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
810 scheme = cl.G711_ULAW
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
811 elif self._comptype == 'ALAW':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
812 scheme = cl.G711_ALAW
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, 'unsupported compression type'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
815 self._comp = cl.OpenCompressor(scheme)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
816 params = [cl.ORIGINAL_FORMAT, 0,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
817 cl.BITS_PER_COMPONENT, self._sampwidth * 8,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
818 cl.FRAME_RATE, self._framerate,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
819 cl.FRAME_BUFFER_SIZE, 100,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
820 cl.COMPRESSED_BUFFER_SIZE, 100]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
821 if self._nchannels == 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
822 params[1] = cl.MONO
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
823 elif self._nchannels == 2:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
824 params[1] = cl.STEREO_INTERLEAVED
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
825 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
826 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
827 self._comp.SetParams(params)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
828 # 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
829 dummy = self._comp.Compress(0, '')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
830 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
831
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
832 def _write_header(self, initlength):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
833 if self._aifc and self._comptype != 'NONE':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
834 self._init_compression()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
835 self._file.write('FORM')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
836 if not self._nframes:
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
837 self._nframes = initlength // (self._nchannels * self._sampwidth)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
838 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
839 if self._datalength & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
840 self._datalength = self._datalength + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
841 if self._aifc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
842 if self._comptype in ('ULAW', 'ALAW'):
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
843 self._datalength = self._datalength // 2
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
844 if self._datalength & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
845 self._datalength = self._datalength + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
846 elif self._comptype == 'G722':
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53876
diff changeset
847 self._datalength = (self._datalength + 3) // 4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
848 if self._datalength & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
849 self._datalength = self._datalength + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
850 self._form_length_pos = self._file.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
851 commlength = self._write_form_length(self._datalength)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
852 if self._aifc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
853 self._file.write('AIFC')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
854 self._file.write('FVER')
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
855 _write_ulong(self._file, 4)
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
856 _write_ulong(self._file, self._version)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
857 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
858 self._file.write('AIFF')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
859 self._file.write('COMM')
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
860 _write_ulong(self._file, commlength)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
861 _write_short(self._file, self._nchannels)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
862 self._nframes_pos = self._file.tell()
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
863 _write_ulong(self._file, self._nframes)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
864 _write_short(self._file, self._sampwidth * 8)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
865 _write_float(self._file, self._framerate)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
866 if self._aifc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
867 self._file.write(self._comptype)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
868 _write_string(self._file, self._compname)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
869 self._file.write('SSND')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
870 self._ssnd_length_pos = self._file.tell()
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
871 _write_ulong(self._file, self._datalength + 8)
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
872 _write_ulong(self._file, 0)
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
873 _write_ulong(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
874
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
875 def _write_form_length(self, datalength):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
876 if self._aifc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
877 commlength = 18 + 5 + len(self._compname)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
878 if commlength & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
879 commlength = commlength + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
880 verslength = 12
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 commlength = 18
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
883 verslength = 0
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
884 _write_ulong(self._file, 4 + verslength + self._marklength + \
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
885 8 + commlength + 16 + datalength)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
886 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
887
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
888 def _patchheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
889 curpos = self._file.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
890 if self._datawritten & 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
891 datalength = self._datawritten + 1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
892 self._file.write(chr(0))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
893 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
894 datalength = self._datawritten
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
895 if datalength == self._datalength and \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
896 self._nframes == self._nframeswritten and \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
897 self._marklength == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
898 self._file.seek(curpos, 0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
899 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
900 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
901 dummy = self._write_form_length(datalength)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
902 self._file.seek(self._nframes_pos, 0)
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
903 _write_ulong(self._file, self._nframeswritten)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
904 self._file.seek(self._ssnd_length_pos, 0)
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
905 _write_ulong(self._file, datalength + 8)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
906 self._file.seek(curpos, 0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
907 self._nframes = self._nframeswritten
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
908 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
909
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
910 def _writemarkers(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
911 if len(self._markers) == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
912 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
913 self._file.write('MARK')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
914 length = 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
915 for marker in self._markers:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
916 id, pos, name = marker
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
917 length = length + len(name) + 1 + 6
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
918 if len(name) & 1 == 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
919 length = length + 1
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
920 _write_ulong(self._file, length)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
921 self._marklength = length + 8
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
922 _write_short(self._file, len(self._markers))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
923 for marker in self._markers:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
924 id, pos, name = marker
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
925 _write_short(self._file, id)
74467
8fac90d0f4cd Issue #13589: Fix some serialization primitives in the aifc module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
926 _write_ulong(self._file, pos)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
927 _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
928
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
929 def open(f, mode=None):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
930 if mode is None:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
931 if hasattr(f, 'mode'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
932 mode = f.mode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
933 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
934 mode = 'rb'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
935 if mode in ('r', 'rb'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
936 return Aifc_read(f)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
937 elif mode in ('w', 'wb'):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
938 return Aifc_write(f)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
939 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
940 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
941
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1423
diff changeset
942 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
943
0bdd31538d8f Rewrote _{read,write}_{short,long} to use the newly revamped struct
Guido van Rossum <guido@python.org>
parents: 4078
diff changeset
944 if __name__ == '__main__':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
945 import sys
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
946 if not sys.argv[1:]:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
947 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
948 fn = sys.argv[1]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
949 f = open(fn, 'r')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
950 print "Reading", fn
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
951 print "nchannels =", f.getnchannels()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
952 print "nframes =", f.getnframes()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
953 print "sampwidth =", f.getsampwidth()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
954 print "framerate =", f.getframerate()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
955 print "comptype =", f.getcomptype()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
956 print "compname =", f.getcompname()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
957 if sys.argv[2:]:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
958 gn = sys.argv[2]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
959 print "Writing", gn
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
960 g = open(gn, 'w')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
961 g.setparams(f.getparams())
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
962 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
963 data = f.readframes(1024)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
964 if not data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
965 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
966 g.writeframes(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
967 g.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
968 f.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
969 print "Done."