annotate Lib/sunau.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 6470566196b3
children 41ed98a93236 99d26f32aed3
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>
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 15872
diff changeset
122 AUDIO_UNKNOWN_SIZE = 0xFFFFFFFF # ((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):
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 15872
diff changeset
135 x = 0
15872
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)
48711
31c39a62d3f9 #3567: fix sunau for running with -bb and make test_ossaudiodev work.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44098
diff changeset
138 if not byte:
15872
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)
48711
31c39a62d3f9 #3567: fix sunau for running with -bb and make test_ossaudiodev work.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44098
diff changeset
147 data.insert(0, int(m))
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
148 x = d
48711
31c39a62d3f9 #3567: fix sunau for running with -bb and make test_ossaudiodev work.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44098
diff changeset
149 file.write(bytes(data))
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 class Au_read:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
152
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
153 def __init__(self, f):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
154 if type(f) == type(''):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43020
diff changeset
155 import builtins
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43020
diff changeset
156 f = builtins.open(f, 'rb')
65844
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
157 self._opened = True
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
158 else:
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
159 self._opened = False
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
160 self.initfp(f)
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
161
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
162 def __del__(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
163 if self._file:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
164 self.close()
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
165
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
166 def initfp(self, file):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
167 self._file = file
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
168 self._soundpos = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
169 magic = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
170 if magic != AUDIO_FILE_MAGIC:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
171 raise Error('bad magic number')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
172 self._hdr_size = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
173 if self._hdr_size < 24:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
174 raise Error('header size too small')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
175 if self._hdr_size > 100:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
176 raise Error('header size ridiculously large')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
177 self._data_size = _read_u32(file)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
178 if self._data_size != AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
179 self._data_size = int(self._data_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
180 self._encoding = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
181 if self._encoding not in _simple_encodings:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
182 raise Error('encoding not (yet) supported')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
183 if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8,
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
184 AUDIO_FILE_ENCODING_ALAW_8):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
185 self._sampwidth = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
186 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
187 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
188 self._framesize = self._sampwidth = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
189 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
190 self._framesize = self._sampwidth = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
191 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
192 self._framesize = self._sampwidth = 3
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
193 elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
194 self._framesize = self._sampwidth = 4
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
195 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
196 raise Error('unknown encoding')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
197 self._framerate = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
198 self._nchannels = int(_read_u32(file))
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
199 self._framesize = self._framesize * self._nchannels
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
200 if self._hdr_size > 24:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
201 self._info = file.read(self._hdr_size - 24)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
202 for i in range(len(self._info)):
48711
31c39a62d3f9 #3567: fix sunau for running with -bb and make test_ossaudiodev work.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44098
diff changeset
203 if self._info[i] == b'\0':
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
204 self._info = self._info[:i]
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
205 break
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
206 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
207 self._info = ''
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
208
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
209 def getfp(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
210 return self._file
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
211
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
212 def getnchannels(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
213 return self._nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
214
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
215 def getsampwidth(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
216 return self._sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
218 def getframerate(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
219 return self._framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
221 def getnframes(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
222 if self._data_size == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
223 return AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
224 if self._encoding in _simple_encodings:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
225 return self._data_size / self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
226 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
227
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
228 def getcomptype(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
229 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
230 return 'ULAW'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
231 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
232 return 'ALAW'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
233 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
234 return 'NONE'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
236 def getcompname(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
237 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
238 return 'CCITT G.711 u-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
239 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
240 return 'CCITT G.711 A-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
241 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
242 return 'not compressed'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
243
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
244 def getparams(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
245 return self.getnchannels(), self.getsampwidth(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
246 self.getframerate(), self.getnframes(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
247 self.getcomptype(), self.getcompname()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
248
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
249 def getmarkers(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
250 return None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
251
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
252 def getmark(self, id):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
253 raise Error('no marks')
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
254
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
255 def readframes(self, nframes):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
256 if self._encoding in _simple_encodings:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
257 if nframes == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
258 data = self._file.read()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
259 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
260 data = self._file.read(nframes * self._framesize * self._nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
261 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
262 import audioop
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
263 data = audioop.ulaw2lin(data, self._sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
264 return data
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
265 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
266
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
267 def rewind(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
268 self._soundpos = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
269 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
270
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
271 def tell(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
272 return self._soundpos
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
273
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
274 def setpos(self, pos):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
275 if pos < 0 or pos > self.getnframes():
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
276 raise Error('position not in range')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
277 self._file.seek(pos * self._framesize + self._hdr_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
278 self._soundpos = pos
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
279
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
280 def close(self):
65844
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
281 if self._opened and self._file:
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
282 self._file.close()
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
283 self._file = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
284
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
285 class Au_write:
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
286
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
287 def __init__(self, f):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
288 if type(f) == type(''):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43020
diff changeset
289 import builtins
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43020
diff changeset
290 f = builtins.open(f, 'wb')
65844
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
291 self._opened = True
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
292 else:
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
293 self._opened = False
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
294 self.initfp(f)
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
295
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
296 def __del__(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
297 if self._file:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
298 self.close()
65844
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
299 self._file = None
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
300
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
301 def initfp(self, file):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
302 self._file = file
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
303 self._framerate = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
304 self._nchannels = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
305 self._sampwidth = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
306 self._framesize = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
307 self._nframes = AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
308 self._nframeswritten = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
309 self._datawritten = 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
310 self._datalength = 0
62157
1a1016fa0723 Issue #8897: Fix sunau module, use bytes to write the header. Patch written by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 48711
diff changeset
311 self._info = b''
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
312 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
313
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
314 def setnchannels(self, nchannels):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
315 if self._nframeswritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
316 raise Error('cannot change parameters after starting to write')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
317 if nchannels not in (1, 2, 4):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
318 raise Error('only 1, 2, or 4 channels supported')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
319 self._nchannels = nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
320
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
321 def getnchannels(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
322 if not self._nchannels:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
323 raise Error('number of channels not set')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
324 return self._nchannels
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
325
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
326 def setsampwidth(self, sampwidth):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
327 if self._nframeswritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
328 raise Error('cannot change parameters after starting to write')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
329 if sampwidth not in (1, 2, 4):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
330 raise Error('bad sample width')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
331 self._sampwidth = sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
332
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
333 def getsampwidth(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
334 if not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
335 raise Error('sample width not specified')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
336 return self._sampwidth
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
337
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
338 def setframerate(self, framerate):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
339 if self._nframeswritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
340 raise Error('cannot change parameters after starting to write')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
341 self._framerate = framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
342
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
343 def getframerate(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
344 if not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
345 raise Error('frame rate not set')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
346 return self._framerate
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
347
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
348 def setnframes(self, nframes):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
349 if self._nframeswritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
350 raise Error('cannot change parameters after starting to write')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
351 if nframes < 0:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
352 raise Error('# of frames cannot be negative')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
353 self._nframes = nframes
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
354
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
355 def getnframes(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
356 return self._nframeswritten
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
357
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
358 def setcomptype(self, type, name):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
359 if type in ('NONE', 'ULAW'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
360 self._comptype = type
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
361 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
362 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
363
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
364 def getcomptype(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
365 return self._comptype
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
366
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
367 def getcompname(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
368 if self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
369 return 'CCITT G.711 u-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
370 elif self._comptype == 'ALAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
371 return 'CCITT G.711 A-law'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
372 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
373 return 'not compressed'
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
374
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
375 def setparams(self, params):
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
376 nchannels, sampwidth, framerate, nframes, comptype, compname = params
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
377 self.setnchannels(nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
378 self.setsampwidth(sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
379 self.setframerate(framerate)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
380 self.setnframes(nframes)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
381 self.setcomptype(comptype, compname)
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 getparams(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
384 return self.getnchannels(), self.getsampwidth(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
385 self.getframerate(), self.getnframes(), \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
386 self.getcomptype(), self.getcompname()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
387
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
388 def tell(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
389 return self._nframeswritten
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
390
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
391 def writeframesraw(self, data):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
392 self._ensure_header_written()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
393 nframes = len(data) / self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
394 if self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
395 import audioop
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
396 data = audioop.lin2ulaw(data, self._sampwidth)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
397 self._file.write(data)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
398 self._nframeswritten = self._nframeswritten + nframes
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
399 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
400
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
401 def writeframes(self, data):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
402 self.writeframesraw(data)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
403 if self._nframeswritten != self._nframes or \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
404 self._datalength != self._datawritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
405 self._patchheader()
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 def close(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
408 self._ensure_header_written()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
409 if self._nframeswritten != self._nframes or \
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
410 self._datalength != self._datawritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
411 self._patchheader()
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
412 self._file.flush()
65844
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
413 if self._opened and self._file:
6470566196b3 Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents: 62157
diff changeset
414 self._file.close()
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
415 self._file = None
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
416
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
417 #
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
418 # private methods
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
419 #
1437
e499116d45e4 aifc.py, sunau.py: Adapted comments; added access statements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1435
diff changeset
420
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
421 def _ensure_header_written(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
422 if not self._nframeswritten:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
423 if not self._nchannels:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
424 raise Error('# of channels not specified')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
425 if not self._sampwidth:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
426 raise Error('sample width not specified')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
427 if not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
428 raise Error('frame rate not specified')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
429 self._write_header()
1419
204001dddcc3 aifc.py: added missing tell() method in AIFC write class;
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
430
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
431 def _write_header(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
432 if self._comptype == 'NONE':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
433 if self._sampwidth == 1:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
434 encoding = AUDIO_FILE_ENCODING_LINEAR_8
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
435 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
436 elif self._sampwidth == 2:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
437 encoding = AUDIO_FILE_ENCODING_LINEAR_16
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
438 self._framesize = 2
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
439 elif self._sampwidth == 4:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
440 encoding = AUDIO_FILE_ENCODING_LINEAR_32
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
441 self._framesize = 4
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
442 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
443 raise Error('internal error')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
444 elif self._comptype == 'ULAW':
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
445 encoding = AUDIO_FILE_ENCODING_MULAW_8
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
446 self._framesize = 1
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
447 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
448 raise Error('internal error')
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
449 self._framesize = self._framesize * self._nchannels
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
450 _write_u32(self._file, AUDIO_FILE_MAGIC)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
451 header_size = 25 + len(self._info)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
452 header_size = (header_size + 7) & ~7
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
453 _write_u32(self._file, header_size)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
454 if self._nframes == AUDIO_UNKNOWN_SIZE:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
455 length = AUDIO_UNKNOWN_SIZE
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
456 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
457 length = self._nframes * self._framesize
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
458 _write_u32(self._file, length)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
459 self._datalength = length
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
460 _write_u32(self._file, encoding)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
461 _write_u32(self._file, self._framerate)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
462 _write_u32(self._file, self._nchannels)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
463 self._file.write(self._info)
48711
31c39a62d3f9 #3567: fix sunau for running with -bb and make test_ossaudiodev work.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44098
diff changeset
464 self._file.write(b'\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
465
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
466 def _patchheader(self):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
467 self._file.seek(8)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
468 _write_u32(self._file, self._datawritten)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
469 self._datalength = self._datawritten
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
470 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
471
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
472 def open(f, mode=None):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
473 if mode is None:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
474 if hasattr(f, 'mode'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
475 mode = f.mode
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
476 else:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
477 mode = 'rb'
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
478 if mode in ('r', 'rb'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
479 return Au_read(f)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
480 elif mode in ('w', 'wb'):
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
481 return Au_write(f)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15067
diff changeset
482 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41717
diff changeset
483 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
484
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1424
diff changeset
485 openfp = open