annotate Lib/sunau.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 751c6dd71b5e
children fdbbbc7b40d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
1 """Stuff to parse Sun and NeXT audio files.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
2
15067
690b41d47c1d Fix a couple of typos in docstrings.
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
3 An audio file consists of a header followed by the data. The structure
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
4 of the header is as follows.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
5
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
6 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
7 | magic word |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
8 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
9 | header size |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
10 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
11 | data size |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
12 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
13 | encoding |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
14 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
15 | sample rate |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
16 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
17 | # of channels |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
18 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
19 | info |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
20 | |
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
21 +---------------+
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
22
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
23 The magic word consists of the 4 characters '.snd'. Apart from the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
24 info field, all header fields are 4 bytes in size. They are all
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
25 32-bit unsigned integers encoded in big-endian byte order.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
26
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
27 The header size really gives the start of the data.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
28 The data size is the physical size of the data. From the other
15067
690b41d47c1d Fix a couple of typos in docstrings.
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
29 parameters the number of frames can be calculated.
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
30 The encoding gives the way in which audio samples are encoded.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
31 Possible values are listed below.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
32 The info field currently consists of an ASCII string giving a
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
33 human-readable description of the audio file. The info field is
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
34 padded with NUL bytes to the header size.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
35
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
36 Usage.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
37
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
38 Reading audio files:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
39 f = sunau.open(file, 'r')
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
40 where file is either the name of a file or an open file pointer.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
41 The open file pointer must have methods read(), seek(), and close().
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
42 When the setpos() and rewind() methods are not used, the seek()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
43 method is not necessary.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
44
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
45 This returns an instance of a class with the following public methods:
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
46 getnchannels() -- returns number of audio channels (1 for
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
47 mono, 2 for stereo)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
48 getsampwidth() -- returns sample width in bytes
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
49 getframerate() -- returns sampling frequency
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
50 getnframes() -- returns number of audio frames
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
51 getcomptype() -- returns compression type ('NONE' or 'ULAW')
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
52 getcompname() -- returns human-readable version of
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
53 compression type ('not compressed' matches 'NONE')
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
54 getparams() -- returns a tuple consisting of all of the
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
55 above in the above order
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
56 getmarkers() -- returns None (for compatibility with the
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
57 aifc module)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
58 getmark(id) -- raises an error since the mark does not
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
59 exist (for compatibility with the aifc module)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
60 readframes(n) -- returns at most n frames of audio
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
61 rewind() -- rewind to the beginning of the audio stream
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
62 setpos(pos) -- seek to the specified position
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
63 tell() -- return the current position
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
64 close() -- close the instance (make it unusable)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
65 The position returned by tell() and the position given to setpos()
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 11707
diff changeset
66 are compatible and have nothing to do with the actual position in the
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
67 file.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
68 The close() method is called automatically when the class instance
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
69 is destroyed.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
70
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
71 Writing audio files:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
72 f = sunau.open(file, 'w')
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
73 where file is either the name of a file or an open file pointer.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
74 The open file pointer must have methods write(), tell(), seek(), and
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
75 close().
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
76
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
77 This returns an instance of a class with the following public methods:
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
78 setnchannels(n) -- set the number of channels
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
79 setsampwidth(n) -- set the sample width
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
80 setframerate(n) -- set the frame rate
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
81 setnframes(n) -- set the number of frames
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
82 setcomptype(type, name)
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
83 -- set the compression type and the
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
84 human-readable compression type
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
85 setparams(tuple)-- set all parameters at once
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
86 tell() -- return current position in output file
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
87 writeframesraw(data)
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
88 -- write audio frames without pathing up the
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
89 file header
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
90 writeframes(data)
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
91 -- write audio frames and patch up the file header
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
92 close() -- patch up the file header and close the
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
93 output file
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
94 You should set the parameters before the first writeframesraw or
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
95 writeframes. The total number of frames does not need to be set,
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
96 but when it is set to the correct value, the header does not have to
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
97 be patched up.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
98 It is best to first set all parameters, perhaps possibly the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
99 compression type, and then write audio frames using writeframesraw.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
100 When all frames have been written, either call writeframes('') or
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
101 close() to patch up the sizes in the header.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
102 The close() method is called automatically when the class instance
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
103 is destroyed.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11034
diff changeset
104 """
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
105
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
106 # from <multimedia/audio_filehdr.h>
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
107 AUDIO_FILE_MAGIC = 0x2e736e64
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
108 AUDIO_FILE_ENCODING_MULAW_8 = 1
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
109 AUDIO_FILE_ENCODING_LINEAR_8 = 2
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
110 AUDIO_FILE_ENCODING_LINEAR_16 = 3
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
111 AUDIO_FILE_ENCODING_LINEAR_24 = 4
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
112 AUDIO_FILE_ENCODING_LINEAR_32 = 5
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 AUDIO_FILE_ENCODING_FLOAT = 6
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
114 AUDIO_FILE_ENCODING_DOUBLE = 7
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
115 AUDIO_FILE_ENCODING_ADPCM_G721 = 23
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
116 AUDIO_FILE_ENCODING_ADPCM_G722 = 24
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
117 AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
119 AUDIO_FILE_ENCODING_ALAW_8 = 27
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
120
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
121 # from <multimedia/audio_hdr.h>
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
122 AUDIO_UNKNOWN_SIZE = 0xFFFFFFFFL # ((unsigned)(~0))
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
123
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
124 _simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8,
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
125 AUDIO_FILE_ENCODING_LINEAR_8,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
126 AUDIO_FILE_ENCODING_LINEAR_16,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
127 AUDIO_FILE_ENCODING_LINEAR_24,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
128 AUDIO_FILE_ENCODING_LINEAR_32,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
129 AUDIO_FILE_ENCODING_ALAW_8]
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
131 class Error(Exception):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
132 pass
2971
d23d73b4e58a Temporary fix for access statement.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1542
diff changeset
133
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 def _read_u32(file):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
135 x = 0L
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
136 for i in range(4):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
137 byte = file.read(1)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
138 if byte == '':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
139 raise EOFError
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
140 x = x*256 + ord(byte)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
141 return x
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 def _write_u32(file, x):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
144 data = []
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
145 for i in range(4):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
146 d, m = divmod(x, 256)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
147 data.insert(0, m)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
148 x = d
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
149 for i in range(4):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
150 file.write(chr(int(data[i])))
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
152 class Au_read:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
153
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
154 def __init__(self, f):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
155 if type(f) == type(''):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
156 import __builtin__
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
157 f = __builtin__.open(f, 'rb')
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
158 self.initfp(f)
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
159
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
160 def __del__(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
161 if self._file:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
162 self.close()
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
163
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
164 def initfp(self, file):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
165 self._file = file
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
166 self._soundpos = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
167 magic = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
168 if magic != AUDIO_FILE_MAGIC:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
169 raise Error, 'bad magic number'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
170 self._hdr_size = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
171 if self._hdr_size < 24:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
172 raise Error, 'header size too small'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
173 if self._hdr_size > 100:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
174 raise Error, 'header size ridiculously large'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
175 self._data_size = _read_u32(file)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
176 if self._data_size != AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
177 self._data_size = int(self._data_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
178 self._encoding = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
179 if self._encoding not in _simple_encodings:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
180 raise Error, 'encoding not (yet) supported'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
181 if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
182 AUDIO_FILE_ENCODING_ALAW_8):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
183 self._sampwidth = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
184 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
185 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
186 self._framesize = self._sampwidth = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
187 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
188 self._framesize = self._sampwidth = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
189 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
190 self._framesize = self._sampwidth = 3
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
191 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
192 self._framesize = self._sampwidth = 4
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
193 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
194 raise Error, 'unknown encoding'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
195 self._framerate = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
196 self._nchannels = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
197 self._framesize = self._framesize * self._nchannels
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
198 if self._hdr_size > 24:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
199 self._info = file.read(self._hdr_size - 24)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
200 for i in range(len(self._info)):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
201 if self._info[i] == '\0':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
202 self._info = self._info[:i]
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
203 break
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
204 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
205 self._info = ''
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
207 def getfp(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
208 return self._file
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
209
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
210 def getnchannels(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
211 return self._nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
212
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
213 def getsampwidth(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
214 return self._sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
215
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
216 def getframerate(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
217 return self._framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
218
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
219 def getnframes(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
220 if self._data_size == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
221 return AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
222 if self._encoding in _simple_encodings:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
223 return self._data_size / self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
224 return 0 # XXX--must do some arithmetic here
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
226 def getcomptype(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
227 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
228 return 'ULAW'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
229 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
230 return 'ALAW'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
231 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
232 return 'NONE'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
233
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
234 def getcompname(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
235 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
236 return 'CCITT G.711 u-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
237 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
238 return 'CCITT G.711 A-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
239 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
240 return 'not compressed'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
241
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
242 def getparams(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
243 return self.getnchannels(), self.getsampwidth(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
244 self.getframerate(), self.getnframes(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
245 self.getcomptype(), self.getcompname()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
246
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
247 def getmarkers(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
248 return None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
249
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
250 def getmark(self, id):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
251 raise Error, 'no marks'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
252
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
253 def readframes(self, nframes):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
254 if self._encoding in _simple_encodings:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
255 if nframes == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
256 data = self._file.read()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
257 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
258 data = self._file.read(nframes * self._framesize * self._nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
259 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
260 import audioop
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
261 data = audioop.ulaw2lin(data, self._sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
262 return data
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
263 return None # XXX--not implemented yet
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
264
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
265 def rewind(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
266 self._soundpos = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
267 self._file.seek(self._hdr_size)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
268
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
269 def tell(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
270 return self._soundpos
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
271
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
272 def setpos(self, pos):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
273 if pos < 0 or pos > self.getnframes():
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
274 raise Error, 'position not in range'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
275 self._file.seek(pos * self._framesize + self._hdr_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
276 self._soundpos = pos
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
277
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
278 def close(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
279 self._file = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
280
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
281 class Au_write:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
282
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
283 def __init__(self, f):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
284 if type(f) == type(''):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
285 import __builtin__
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
286 f = __builtin__.open(f, 'wb')
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
287 self.initfp(f)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
288
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
289 def __del__(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
290 if self._file:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
291 self.close()
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
292
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
293 def initfp(self, file):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
294 self._file = file
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
295 self._framerate = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
296 self._nchannels = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
297 self._sampwidth = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
298 self._framesize = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
299 self._nframes = AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
300 self._nframeswritten = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
301 self._datawritten = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
302 self._datalength = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
303 self._info = ''
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
304 self._comptype = 'ULAW' # default is U-law
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
305
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
306 def setnchannels(self, nchannels):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
307 if self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
308 raise Error, 'cannot change parameters after starting to write'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
309 if nchannels not in (1, 2, 4):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
310 raise Error, 'only 1, 2, or 4 channels supported'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
311 self._nchannels = nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
312
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
313 def getnchannels(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
314 if not self._nchannels:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
315 raise Error, 'number of channels not set'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
316 return self._nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
317
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
318 def setsampwidth(self, sampwidth):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
319 if self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
320 raise Error, 'cannot change parameters after starting to write'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
321 if sampwidth not in (1, 2, 4):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
322 raise Error, 'bad sample width'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
323 self._sampwidth = sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
324
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
325 def getsampwidth(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
326 if not self._framerate:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
327 raise Error, 'sample width not specified'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
328 return self._sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
329
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
330 def setframerate(self, framerate):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
331 if self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
332 raise Error, 'cannot change parameters after starting to write'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
333 self._framerate = framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
334
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
335 def getframerate(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
336 if not self._framerate:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
337 raise Error, 'frame rate not set'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
338 return self._framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
339
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
340 def setnframes(self, nframes):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
341 if self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
342 raise Error, 'cannot change parameters after starting to write'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
343 if nframes < 0:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
344 raise Error, '# of frames cannot be negative'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
345 self._nframes = nframes
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
346
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
347 def getnframes(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
348 return self._nframeswritten
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
349
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
350 def setcomptype(self, type, name):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
351 if type in ('NONE', 'ULAW'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
352 self._comptype = type
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
353 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
354 raise Error, 'unknown compression type'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
355
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
356 def getcomptype(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
357 return self._comptype
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
358
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
359 def getcompname(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
360 if self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
361 return 'CCITT G.711 u-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
362 elif self._comptype == 'ALAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
363 return 'CCITT G.711 A-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
364 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
365 return 'not compressed'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
366
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 15872
diff changeset
367 def setparams(self, params):
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 15872
diff changeset
368 nchannels, sampwidth, framerate, nframes, comptype, compname = params
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
369 self.setnchannels(nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
370 self.setsampwidth(sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
371 self.setframerate(framerate)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
372 self.setnframes(nframes)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
373 self.setcomptype(comptype, compname)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
374
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
375 def getparams(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
376 return self.getnchannels(), self.getsampwidth(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
377 self.getframerate(), self.getnframes(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
378 self.getcomptype(), self.getcompname()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
379
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
380 def tell(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
381 return self._nframeswritten
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
382
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
383 def writeframesraw(self, data):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
384 self._ensure_header_written()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
385 nframes = len(data) / self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
386 if self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
387 import audioop
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
388 data = audioop.lin2ulaw(data, self._sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
389 self._file.write(data)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
390 self._nframeswritten = self._nframeswritten + nframes
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
391 self._datawritten = self._datawritten + len(data)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
392
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
393 def writeframes(self, data):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
394 self.writeframesraw(data)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
395 if self._nframeswritten != self._nframes or \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
396 self._datalength != self._datawritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
397 self._patchheader()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
398
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
399 def close(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
400 self._ensure_header_written()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
401 if self._nframeswritten != self._nframes or \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
402 self._datalength != self._datawritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
403 self._patchheader()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
404 self._file.flush()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
405 self._file = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
406
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
407 #
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
408 # private methods
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
409 #
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
410
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
411 def _ensure_header_written(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
412 if not self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
413 if not self._nchannels:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
414 raise Error, '# of channels not specified'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
415 if not self._sampwidth:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
416 raise Error, 'sample width not specified'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
417 if not self._framerate:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
418 raise Error, 'frame rate not specified'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
419 self._write_header()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
420
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
421 def _write_header(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
422 if self._comptype == 'NONE':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
423 if self._sampwidth == 1:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
424 encoding = AUDIO_FILE_ENCODING_LINEAR_8
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
425 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
426 elif self._sampwidth == 2:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
427 encoding = AUDIO_FILE_ENCODING_LINEAR_16
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
428 self._framesize = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
429 elif self._sampwidth == 4:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
430 encoding = AUDIO_FILE_ENCODING_LINEAR_32
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
431 self._framesize = 4
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
432 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
433 raise Error, 'internal error'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
434 elif self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
435 encoding = AUDIO_FILE_ENCODING_MULAW_8
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
436 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
437 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
438 raise Error, 'internal error'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
439 self._framesize = self._framesize * self._nchannels
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
440 _write_u32(self._file, AUDIO_FILE_MAGIC)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
441 header_size = 25 + len(self._info)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
442 header_size = (header_size + 7) & ~7
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
443 _write_u32(self._file, header_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
444 if self._nframes == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
445 length = AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
446 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
447 length = self._nframes * self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
448 _write_u32(self._file, length)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
449 self._datalength = length
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
450 _write_u32(self._file, encoding)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
451 _write_u32(self._file, self._framerate)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
452 _write_u32(self._file, self._nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
453 self._file.write(self._info)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
454 self._file.write('\0'*(header_size - len(self._info) - 24))
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
455
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
456 def _patchheader(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
457 self._file.seek(8)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
458 _write_u32(self._file, self._datawritten)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
459 self._datalength = self._datawritten
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
460 self._file.seek(0, 2)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
461
11034
b6a438cc195b Make the mode parameter to open() default in the same way as for wave.open().
Fred Drake <fdrake@acm.org>
parents: 10103
diff changeset
462 def open(f, mode=None):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
463 if mode is None:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
464 if hasattr(f, 'mode'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
465 mode = f.mode
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
466 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
467 mode = 'rb'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
468 if mode in ('r', 'rb'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
469 return Au_read(f)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
470 elif mode in ('w', 'wb'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
471 return Au_write(f)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
472 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
473 raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
474
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1424
diff changeset
475 openfp = open