annotate Lib/wave.py @ 86127:a3d09c19bb93

merge with 3.3
author Georg Brandl <georg@python.org>
date Sun, 06 Oct 2013 19:24:00 +0200
parents a14ec46de0a4
children cd0fe2b4a2cd
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: 11227
diff changeset
1 """Stuff to parse WAVE files.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
2
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
3 Usage.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
4
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
5 Reading WAVE files:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
6 f = wave.open(file, 'r')
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
7 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: 11227
diff changeset
8 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: 11227
diff changeset
9 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: 11227
diff changeset
10 method is not necessary.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
11
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
12 This returns an instance of a class with the following public methods:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
13 getnchannels() -- returns number of audio channels (1 for
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
14 mono, 2 for stereo)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
15 getsampwidth() -- returns sample width in bytes
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
16 getframerate() -- returns sampling frequency
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
17 getnframes() -- returns number of audio frames
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
18 getcomptype() -- returns compression type ('NONE' for linear samples)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
19 getcompname() -- returns human-readable version of
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
20 compression type ('not compressed' linear samples)
83234
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
21 getparams() -- returns a namedtuple consisting of all of the
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
22 above in the above order
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
23 getmarkers() -- returns None (for compatibility with the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
24 aifc module)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
25 getmark(id) -- raises an error since the mark does not
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
26 exist (for compatibility with the aifc module)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
27 readframes(n) -- returns at most n frames of audio
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
28 rewind() -- rewind to the beginning of the audio stream
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
29 setpos(pos) -- seek to the specified position
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
30 tell() -- return the current position
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
31 close() -- close the instance (make it unusable)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
32 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
33 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: 11227
diff changeset
34 file.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
35 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: 11227
diff changeset
36 is destroyed.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
37
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
38 Writing WAVE files:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
39 f = wave.open(file, 'w')
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
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: 11227
diff changeset
41 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: 11227
diff changeset
42 close().
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
43
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
44 This returns an instance of a class with the following public methods:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
45 setnchannels(n) -- set the number of channels
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
46 setsampwidth(n) -- set the sample width
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
47 setframerate(n) -- set the frame rate
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
48 setnframes(n) -- set the number of frames
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
49 setcomptype(type, name)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
50 -- set the compression type and the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
51 human-readable compression type
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
52 setparams(tuple)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
53 -- set all parameters at once
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
54 tell() -- return current position in output file
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
55 writeframesraw(data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
56 -- write audio frames without pathing up the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
57 file header
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
58 writeframes(data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
59 -- write audio frames and patch up the file header
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
60 close() -- patch up the file header and close the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
61 output file
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
62 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: 11227
diff changeset
63 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: 11227
diff changeset
64 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: 11227
diff changeset
65 be patched up.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
66 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: 11227
diff changeset
67 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: 11227
diff changeset
68 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: 11227
diff changeset
69 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: 11227
diff changeset
70 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: 11227
diff changeset
71 is destroyed.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
72 """
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
74 import builtins
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
76 __all__ = ["open", "openfp", "Error"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
77
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
78 class Error(Exception):
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
79 pass
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 WAVE_FORMAT_PCM = 0x0001
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 _array_fmts = None, 'b', 'h', None, 'l'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
10106
16aa73588d93 Two important fixes:
Guido van Rossum <guido@python.org>
parents: 8131
diff changeset
85 import struct
83983
ccffce2dde49 Issue #18084: Use sys.byteorder in wave.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 83234
diff changeset
86 import sys
11227
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 10999
diff changeset
87 from chunk import Chunk
83234
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
88 from collections import namedtuple
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
89
85513
a14ec46de0a4 Issue #17487: The result of the wave getparams method now is pickleable again.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84944
diff changeset
90 _wave_params = namedtuple('_wave_params',
83234
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
91 'nchannels sampwidth framerate nframes comptype compname')
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 class Wave_read:
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
94 """Variables used in this class:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
95
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
96 These variables are available to the user though appropriate
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
97 methods of this class:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
98 _file -- the open file with methods read(), close(), and seek()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
99 set through the __init__() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
100 _nchannels -- the number of audio channels
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
101 available through the getnchannels() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
102 _nframes -- the number of audio frames
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
103 available through the getnframes() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
104 _sampwidth -- the number of bytes per audio sample
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
105 available through the getsampwidth() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
106 _framerate -- the sampling frequency
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
107 available through the getframerate() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
108 _comptype -- the AIFF-C compression type ('NONE' if AIFF)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
109 available through the getcomptype() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
110 _compname -- the human-readable AIFF-C compression type
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
111 available through the getcomptype() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
112 _soundpos -- the position in the audio stream
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
113 available through the tell() method, set through the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
114 setpos() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
115
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
116 These variables are used internally only:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
117 _fmt_chunk_read -- 1 iff the FMT chunk has been read
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
118 _data_seek_needed -- 1 iff positioned correctly in audio
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
119 file for readframes()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
120 _data_chunk -- instantiation of a chunk class for the DATA chunk
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
121 _framesize -- size of one frame in the file
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
122 """
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
124 def initfp(self, file):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
125 self._convert = None
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
126 self._soundpos = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
127 self._file = Chunk(file, bigendian = 0)
42354
36fbcda80b1c Make test_wave.py pass.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
128 if self._file.getname() != b'RIFF':
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
129 raise Error('file does not start with RIFF id')
42354
36fbcda80b1c Make test_wave.py pass.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
130 if self._file.read(4) != b'WAVE':
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
131 raise Error('not a WAVE file')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
132 self._fmt_chunk_read = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
133 self._data_chunk = None
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
134 while 1:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
135 self._data_seek_needed = 1
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
136 try:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
137 chunk = Chunk(self._file, bigendian = 0)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
138 except EOFError:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
139 break
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
140 chunkname = chunk.getname()
42354
36fbcda80b1c Make test_wave.py pass.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
141 if chunkname == b'fmt ':
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
142 self._read_fmt_chunk(chunk)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
143 self._fmt_chunk_read = 1
42354
36fbcda80b1c Make test_wave.py pass.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
144 elif chunkname == b'data':
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
145 if not self._fmt_chunk_read:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
146 raise Error('data chunk before fmt chunk')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
147 self._data_chunk = chunk
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 16889
diff changeset
148 self._nframes = chunk.chunksize // self._framesize
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
149 self._data_seek_needed = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
150 break
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
151 chunk.skip()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
152 if not self._fmt_chunk_read or not self._data_chunk:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
153 raise Error('fmt chunk and/or data chunk missing')
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
155 def __init__(self, f):
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
156 self._i_opened_the_file = None
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
157 if isinstance(f, str):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
158 f = builtins.open(f, 'rb')
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
159 self._i_opened_the_file = f
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
160 # else, assume it is an open file object already
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
161 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
162 self.initfp(f)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
163 except:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
164 if self._i_opened_the_file:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
165 f.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
166 raise
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
168 def __del__(self):
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
169 self.close()
84932
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
170
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
171 def __enter__(self):
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
172 return self
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
173
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
174 def __exit__(self, *args):
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
175 self.close()
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
176
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
177 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
178 # User visible methods.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
179 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
180 def getfp(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
181 return self._file
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
183 def rewind(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
184 self._data_seek_needed = 1
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
185 self._soundpos = 0
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
187 def close(self):
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
188 if self._i_opened_the_file:
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
189 self._i_opened_the_file.close()
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
190 self._i_opened_the_file = None
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
191 self._file = None
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
193 def tell(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
194 return self._soundpos
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
196 def getnchannels(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
197 return self._nchannels
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
199 def getnframes(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
200 return self._nframes
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
202 def getsampwidth(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
203 return self._sampwidth
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
205 def getframerate(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
206 return self._framerate
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
208 def getcomptype(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
209 return self._comptype
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
211 def getcompname(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
212 return self._compname
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
214 def getparams(self):
85513
a14ec46de0a4 Issue #17487: The result of the wave getparams method now is pickleable again.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84944
diff changeset
215 return _wave_params(self.getnchannels(), self.getsampwidth(),
83234
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
216 self.getframerate(), self.getnframes(),
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
217 self.getcomptype(), self.getcompname())
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
219 def getmarkers(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
220 return None
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
222 def getmark(self, id):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
223 raise Error('no marks')
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
225 def setpos(self, pos):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
226 if pos < 0 or pos > self._nframes:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
227 raise Error('position not in range')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
228 self._soundpos = pos
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
229 self._data_seek_needed = 1
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
231 def readframes(self, nframes):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
232 if self._data_seek_needed:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
233 self._data_chunk.seek(0, 0)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
234 pos = self._soundpos * self._framesize
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
235 if pos:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
236 self._data_chunk.seek(pos, 0)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
237 self._data_seek_needed = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
238 if nframes == 0:
42354
36fbcda80b1c Make test_wave.py pass.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
239 return b''
83983
ccffce2dde49 Issue #18084: Use sys.byteorder in wave.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 83234
diff changeset
240 if self._sampwidth > 1 and sys.byteorder == 'big':
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
241 # unfortunately the fromfile() method does not take
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
242 # something that only looks like a file object, so
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
243 # we have to reach into the innards of the chunk object
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
244 import array
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
245 chunk = self._data_chunk
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
246 data = array.array(_array_fmts[self._sampwidth])
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
247 nitems = nframes * self._nchannels
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
248 if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
58286
a6bf68e6e824 use floor division where needed #7681
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
249 nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
250 data.fromfile(chunk.file.file, nitems)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
251 # "tell" data chunk how much was read
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
252 chunk.size_read = chunk.size_read + nitems * self._sampwidth
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
253 # do the same for the outermost chunk
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
254 chunk = chunk.file
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
255 chunk.size_read = chunk.size_read + nitems * self._sampwidth
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
256 data.byteswap()
64355
fa8b57f987c5 Issue #8990: array.fromstring() and array.tostring() get renamed to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64297
diff changeset
257 data = data.tobytes()
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
258 else:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
259 data = self._data_chunk.read(nframes * self._framesize)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
260 if self._convert and data:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
261 data = self._convert(data)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 16889
diff changeset
262 self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
263 return data
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
265 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
266 # Internal methods.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
267 #
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
269 def _read_fmt_chunk(self, chunk):
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
270 wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
271 if wFormatTag == WAVE_FORMAT_PCM:
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
272 sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 16889
diff changeset
273 self._sampwidth = (sampwidth + 7) // 8
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
274 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
275 raise Error('unknown format: %r' % (wFormatTag,))
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
276 self._framesize = self._nchannels * self._sampwidth
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
277 self._comptype = 'NONE'
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
278 self._compname = 'not compressed'
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 class Wave_write:
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
281 """Variables used in this class:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
282
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
283 These variables are user settable through appropriate methods
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
284 of this class:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
285 _file -- the open file with methods write(), close(), tell(), seek()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
286 set through the __init__() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
287 _comptype -- the AIFF-C compression type ('NONE' in AIFF)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
288 set through the setcomptype() or setparams() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
289 _compname -- the human-readable AIFF-C compression type
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
290 set through the setcomptype() or setparams() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
291 _nchannels -- the number of audio channels
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
292 set through the setnchannels() or setparams() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
293 _sampwidth -- the number of bytes per audio sample
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
294 set through the setsampwidth() or setparams() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
295 _framerate -- the sampling frequency
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
296 set through the setframerate() or setparams() method
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
297 _nframes -- the number of audio frames written to the header
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
298 set through the setnframes() or setparams() method
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
300 These variables are used internally only:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
301 _datalength -- the size of the audio samples written to the header
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
302 _nframeswritten -- the number of frames actually written
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
303 _datawritten -- the size of the audio samples actually written
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
304 """
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
305
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
306 def __init__(self, f):
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
307 self._i_opened_the_file = None
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
308 if isinstance(f, str):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
309 f = builtins.open(f, 'wb')
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
310 self._i_opened_the_file = f
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
311 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
312 self.initfp(f)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
313 except:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
314 if self._i_opened_the_file:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
315 f.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41449
diff changeset
316 raise
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
318 def initfp(self, file):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
319 self._file = file
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
320 self._convert = None
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
321 self._nchannels = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
322 self._sampwidth = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
323 self._framerate = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
324 self._nframes = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
325 self._nframeswritten = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
326 self._datawritten = 0
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
327 self._datalength = 0
66275
5d55c5bb8ab1 Merged revisions 85970 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58286
diff changeset
328 self._headerwritten = False
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
330 def __del__(self):
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
331 self.close()
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
84932
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
333 def __enter__(self):
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
334 return self
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
335
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
336 def __exit__(self, *args):
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
337 self.close()
8327780d3841 #17616: wave.open now supports the 'with' statement.
R David Murray <rdmurray@bitdance.com>
parents: 83983
diff changeset
338
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
339 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
340 # User visible methods.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
341 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
342 def setnchannels(self, nchannels):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
343 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
344 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
345 if nchannels < 1:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
346 raise Error('bad # of channels')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
347 self._nchannels = nchannels
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
348
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
349 def getnchannels(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
350 if not self._nchannels:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
351 raise Error('number of channels not set')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
352 return self._nchannels
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
354 def setsampwidth(self, sampwidth):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
355 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
356 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
357 if sampwidth < 1 or sampwidth > 4:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
358 raise Error('bad sample width')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
359 self._sampwidth = sampwidth
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
361 def getsampwidth(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
362 if not self._sampwidth:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
363 raise Error('sample width not set')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
364 return self._sampwidth
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
366 def setframerate(self, framerate):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
367 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
368 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
369 if framerate <= 0:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
370 raise Error('bad frame rate')
64297
5dc06a059630 Issue #1512791: In setframerate method of Wave_write, round non-integral
Mark Dickinson <dickinsm@gmail.com>
parents: 58286
diff changeset
371 self._framerate = int(round(framerate))
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
373 def getframerate(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
374 if not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
375 raise Error('frame rate not set')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
376 return self._framerate
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
378 def setnframes(self, nframes):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
379 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
380 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
381 self._nframes = nframes
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
383 def getnframes(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
384 return self._nframeswritten
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
385
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
386 def setcomptype(self, comptype, compname):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
387 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
388 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
389 if comptype not in ('NONE',):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
390 raise Error('unsupported compression type')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
391 self._comptype = comptype
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
392 self._compname = compname
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
393
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
394 def getcomptype(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
395 return self._comptype
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
396
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
397 def getcompname(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
398 return self._compname
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
400 def setparams(self, params):
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
401 nchannels, sampwidth, framerate, nframes, comptype, compname = params
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
402 if self._datawritten:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
403 raise Error('cannot change parameters after starting to write')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
404 self.setnchannels(nchannels)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
405 self.setsampwidth(sampwidth)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
406 self.setframerate(framerate)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
407 self.setnframes(nframes)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
408 self.setcomptype(comptype, compname)
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
410 def getparams(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
411 if not self._nchannels or not self._sampwidth or not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
412 raise Error('not all parameters set')
85513
a14ec46de0a4 Issue #17487: The result of the wave getparams method now is pickleable again.
Serhiy Storchaka <storchaka@gmail.com>
parents: 84944
diff changeset
413 return _wave_params(self._nchannels, self._sampwidth, self._framerate,
83234
340a12c18b7f #17487: wave.getparams now returns a namedtuple.
R David Murray <rdmurray@bitdance.com>
parents: 80456
diff changeset
414 self._nframes, self._comptype, self._compname)
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
416 def setmark(self, id, pos, name):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
417 raise Error('setmark() not supported')
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
419 def getmark(self, id):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
420 raise Error('no marks')
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
422 def getmarkers(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
423 return None
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15135
diff changeset
424
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
425 def tell(self):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
426 return self._nframeswritten
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
428 def writeframesraw(self, data):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
429 self._ensure_header_written(len(data))
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 16889
diff changeset
430 nframes = len(data) // (self._sampwidth * self._nchannels)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
431 if self._convert:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
432 data = self._convert(data)
83983
ccffce2dde49 Issue #18084: Use sys.byteorder in wave.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 83234
diff changeset
433 if self._sampwidth > 1 and sys.byteorder == 'big':
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
434 import array
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
435 data = array.array(_array_fmts[self._sampwidth], data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
436 data.byteswap()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
437 data.tofile(self._file)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
438 self._datawritten = self._datawritten + len(data) * self._sampwidth
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
439 else:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
440 self._file.write(data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
441 self._datawritten = self._datawritten + len(data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
442 self._nframeswritten = self._nframeswritten + nframes
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
444 def writeframes(self, data):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
445 self.writeframesraw(data)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
446 if self._datalength != self._datawritten:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
447 self._patchheader()
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
449 def close(self):
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
450 if self._file:
84944
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
451 try:
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
452 self._ensure_header_written(0)
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
453 if self._datalength != self._datawritten:
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
454 self._patchheader()
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
455 self._file.flush()
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
456 finally:
4f3b6eff2ede #17616: Improve context manager tests, fix bugs in close method and mode docs.
R David Murray <rdmurray@bitdance.com>
parents: 84932
diff changeset
457 self._file = None
15135
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
458 if self._i_opened_the_file:
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
459 self._i_opened_the_file.close()
91201de25f36 When the classes in wave.py opened files themselves, their .close() methods
Tim Peters <tim.peters@gmail.com>
parents: 15122
diff changeset
460 self._i_opened_the_file = None
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
462 #
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
463 # Internal methods.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
464 #
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
465
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
466 def _ensure_header_written(self, datasize):
66275
5d55c5bb8ab1 Merged revisions 85970 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58286
diff changeset
467 if not self._headerwritten:
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
468 if not self._nchannels:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
469 raise Error('# channels not specified')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
470 if not self._sampwidth:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
471 raise Error('sample width not specified')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
472 if not self._framerate:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
473 raise Error('sampling rate not specified')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
474 self._write_header(datasize)
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
475
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
476 def _write_header(self, initlength):
66275
5d55c5bb8ab1 Merged revisions 85970 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58286
diff changeset
477 assert not self._headerwritten
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42354
diff changeset
478 self._file.write(b'RIFF')
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
479 if not self._nframes:
58286
a6bf68e6e824 use floor division where needed #7681
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
480 self._nframes = initlength // (self._nchannels * self._sampwidth)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
481 self._datalength = self._nframes * self._nchannels * self._sampwidth
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
482 self._form_length_pos = self._file.tell()
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
483 self._file.write(struct.pack('<L4s4sLHHLLHH4s',
67181
b901da561cd1 Issue #10783: struct.pack() doesn't encode implicitly unicode to UTF-8
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65756
diff changeset
484 36 + self._datalength, b'WAVE', b'fmt ', 16,
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
485 WAVE_FORMAT_PCM, self._nchannels, self._framerate,
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
486 self._nchannels * self._framerate * self._sampwidth,
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
487 self._nchannels * self._sampwidth,
67181
b901da561cd1 Issue #10783: struct.pack() doesn't encode implicitly unicode to UTF-8
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65756
diff changeset
488 self._sampwidth * 8, b'data'))
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
489 self._data_length_pos = self._file.tell()
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
490 self._file.write(struct.pack('<L', self._datalength))
66275
5d55c5bb8ab1 Merged revisions 85970 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58286
diff changeset
491 self._headerwritten = True
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
493 def _patchheader(self):
66275
5d55c5bb8ab1 Merged revisions 85970 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58286
diff changeset
494 assert self._headerwritten
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
495 if self._datawritten == self._datalength:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
496 return
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
497 curpos = self._file.tell()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
498 self._file.seek(self._form_length_pos, 0)
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
499 self._file.write(struct.pack('<L', 36 + self._datawritten))
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
500 self._file.seek(self._data_length_pos, 0)
80456
542bf1c1f2e3 Closes #16461: Wave library should be able to deal with 4GB wav files, and sample rate of 44100 Hz.
Jesus Cea <jcea@jcea.es>
parents: 68257
diff changeset
501 self._file.write(struct.pack('<L', self._datawritten))
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
502 self._file.seek(curpos, 0)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
503 self._datalength = self._datawritten
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
10999
8a4fc4da768d open(): Make the mode parameter optional; if omitted or None, use the
Fred Drake <fdrake@acm.org>
parents: 10107
diff changeset
505 def open(f, mode=None):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
506 if mode is None:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
507 if hasattr(f, 'mode'):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
508 mode = f.mode
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
509 else:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
510 mode = 'rb'
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
511 if mode in ('r', 'rb'):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
512 return Wave_read(f)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
513 elif mode in ('w', 'wb'):
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
514 return Wave_write(f)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11227
diff changeset
515 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42992
diff changeset
516 raise Error("mode must be 'r', 'rb', 'w', or 'wb'")
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
517
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 openfp = open # B/W compatibility