annotate Lib/wave.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 18451dca9469
children 86a6193049a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Stuff to parse WAVE files.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Usage.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Reading WAVE files:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # f = wave.open(file, 'r')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # where file is either the name of a file or an open file pointer.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # The open file pointer must have methods read(), seek(), and close().
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # When the setpos() and rewind() methods are not used, the seek()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # method is not necessary.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # This returns an instance of a class with the following public methods:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # getnchannels() -- returns number of audio channels (1 for
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # mono, 2 for stereo)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # getsampwidth() -- returns sample width in bytes
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # getframerate() -- returns sampling frequency
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # getnframes() -- returns number of audio frames
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # getcomptype() -- returns compression type ('NONE' for AIFF files)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # getcompname() -- returns human-readable version of
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # compression type ('not compressed' for AIFF files)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # getparams() -- returns a tuple consisting of all of the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # above in the above order
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # getmarkers() -- returns None (for compatibility with the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # aifc module)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # getmark(id) -- raises an error since the mark does not
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # exist (for compatibility with the aifc module)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # readframes(n) -- returns at most n frames of audio
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # rewind() -- rewind to the beginning of the audio stream
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # setpos(pos) -- seek to the specified position
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # tell() -- return the current position
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # close() -- close the instance (make it unusable)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # The position returned by tell() and the position given to setpos()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # are compatible and have nothing to do with the actual postion in the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # file.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # The close() method is called automatically when the class instance
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # is destroyed.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # Writing WAVE files:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # f = wave.open(file, 'w')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # where file is either the name of a file or an open file pointer.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # The open file pointer must have methods write(), tell(), seek(), and
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # close().
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # This returns an instance of a class with the following public methods:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # setnchannels(n) -- set the number of channels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # setsampwidth(n) -- set the sample width
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # setframerate(n) -- set the frame rate
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # setnframes(n) -- set the number of frames
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # setcomptype(type, name)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # -- set the compression type and the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # human-readable compression type
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # setparams(nchannels, sampwidth, framerate, nframes, comptype, compname)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # -- set all parameters at once
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # tell() -- return current position in output file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # writeframesraw(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # -- write audio frames without pathing up the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # file header
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # writeframes(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # -- write audio frames and patch up the file header
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # close() -- patch up the file header and close the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # output file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # You should set the parameters before the first writeframesraw or
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # writeframes. The total number of frames does not need to be set,
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # but when it is set to the correct value, the header does not have to
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # be patched up.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # It is best to first set all parameters, perhaps possibly the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # compression type, and then write audio frames using writeframesraw.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # When all frames have been written, either call writeframes('') or
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # close() to patch up the sizes in the header.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 # The close() method is called automatically when the class instance
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # is destroyed.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 import __builtin__
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 Error = 'wave.Error'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 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
78
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 _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
80
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def _read_long(file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 x = 0L
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 for i in range(4):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 byte = file.read(1)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if byte == '':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 raise EOFError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 x = x + (ord(byte) << (8 * i))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if x >= 0x80000000L:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 x = x - 0x100000000L
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return int(x)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def _read_ulong(file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 x = 0L
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 for i in range(4):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 byte = file.read(1)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if byte == '':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 raise EOFError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 x = x + (ord(byte) << (8 * i))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 return x
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def _read_short(file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 x = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 for i in range(2):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 byte = file.read(1)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 if byte == '':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 raise EOFError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 x = x + (ord(byte) << (8 * i))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if x >= 0x8000:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 x = x - 0x10000
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return x
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def _write_short(f, x):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 d, m = divmod(x, 256)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 f.write(chr(m))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 f.write(chr(d))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 def _write_long(f, x):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if x < 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 x = x + 0x100000000L
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 for i in range(4):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 d, m = divmod(x, 256)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 f.write(chr(int(m)))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 x = d
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 class Chunk:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 def __init__(self, file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 self.file = file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 self.chunkname = self.file.read(4)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if len(self.chunkname) < 4:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 raise EOFError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 self.chunksize = _read_long(self.file)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 self.size_read = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 self.offset = self.file.tell()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def rewind(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 self.file.seek(self.offset, 0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 self.size_read = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 def setpos(self, pos):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 if pos < 0 or pos > self.chunksize:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 raise RuntimeError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 self.file.seek(self.offset + pos, 0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 self.size_read = pos
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 def read(self, length):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 if self.size_read >= self.chunksize:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 return ''
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if length > self.chunksize - self.size_read:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 length = self.chunksize - self.size_read
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 data = self.file.read(length)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 self.size_read = self.size_read + len(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 return data
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 def skip(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 try:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 self.file.seek(self.chunksize - self.size_read, 1)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 except RuntimeError:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 while self.size_read < self.chunksize:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 dummy = self.read(8192)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 if not dummy:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 raise EOFError
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 class Wave_read:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 # Variables used in this class:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 # These variables are available to the user though appropriate
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 # methods of this class:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 # _file -- the open file with methods read(), close(), and seek()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 # set through the __init__() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 # _nchannels -- the number of audio channels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 # available through the getnchannels() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 # _nframes -- the number of audio frames
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 # available through the getnframes() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 # _sampwidth -- the number of bytes per audio sample
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 # available through the getsampwidth() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 # _framerate -- the sampling frequency
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # available through the getframerate() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # _comptype -- the AIFF-C compression type ('NONE' if AIFF)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 # available through the getcomptype() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 # _compname -- the human-readable AIFF-C compression type
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 # available through the getcomptype() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # _soundpos -- the position in the audio stream
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 # available through the tell() method, set through the
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # setpos() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # These variables are used internally only:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 # _fmt_chunk_read -- 1 iff the FMT chunk has been read
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 # _data_seek_needed -- 1 iff positioned correctly in audio
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 # file for readframes()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 # _data_chunk -- instantiation of a chunk class for the DATA chunk
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # _framesize -- size of one frame in the file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
193 ## access _file, _nchannels, _nframes, _sampwidth, _framerate, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
194 ## _comptype, _compname, _soundpos, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
195 ## _fmt_chunk_read, _data_seek_needed, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
196 ## _data_chunk, _framesize: private
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 def initfp(self, file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 self._file = file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 self._convert = None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 self._soundpos = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 form = self._file.read(4)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 if form != 'RIFF':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 raise Error, 'file does not start with RIFF id'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 formlength = _read_long(self._file)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 if formlength <= 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 raise Error, 'invalid FORM chunk data size'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 formdata = self._file.read(4)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 formlength = formlength - 4
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if formdata != 'WAVE':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 raise Error, 'not a WAVE file'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 self._fmt_chunk_read = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 while formlength > 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 self._data_seek_needed = 1
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 chunk = Chunk(self._file)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 if chunk.chunkname == 'fmt ':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 self._read_fmt_chunk(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 self._fmt_chunk_read = 1
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 elif chunk.chunkname == 'data':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 if not self._fmt_chunk_read:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 raise Error, 'data chunk before fmt chunk'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 self._data_chunk = chunk
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 self._nframes = chunk.chunksize / self._framesize
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 self._data_seek_needed = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 formlength = formlength - 8 - chunk.chunksize
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 if formlength > 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 chunk.skip()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 if not self._fmt_chunk_read or not self._data_chunk:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 raise Error, 'fmt chunk and/or data chunk missing'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 def __init__(self, f):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if type(f) == type(''):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 f = __builtin__.open(f, 'r')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 # else, assume it is an open file object already
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 self.initfp(f)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 def __del__(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if self._file:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 self.close()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 # User visible methods.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 def getfp(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 return self._file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 def rewind(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 self._data_seek_needed = 1
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self._soundpos = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 def close(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 self._file = None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 def tell(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 return self._soundpos
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 def getnchannels(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 return self._nchannels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 def getnframes(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 return self._nframes
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 def getsampwidth(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 return self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 def getframerate(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 return self._framerate
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 def getcomptype(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 return self._comptype
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 def getcompname(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 return self._compname
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 def getparams(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 return self.getnchannels(), self.getsampwidth(), \
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 self.getframerate(), self.getnframes(), \
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 self.getcomptype(), self.getcompname()
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 def getmarkers(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 return None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 def getmark(self, id):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 raise Error, 'no marks'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 def setpos(self, pos):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 if pos < 0 or pos > self._nframes:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 raise Error, 'position not in range'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self._soundpos = pos
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 self._data_seek_needed = 1
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
291
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 def readframes(self, nframes):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 if self._data_seek_needed:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self._data_chunk.rewind()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 pos = self._soundpos * self._framesize
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 if pos:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 self._data_chunk.setpos(pos)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 self._data_seek_needed = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 if nframes == 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 return ''
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 if self._sampwidth > 1:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # unfortunately the fromfile() method does not take
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 # something that only looks like a file object, so
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # we have to reach into the innards of the chunk object
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 import array
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 data = array.array(_array_fmts[self._sampwidth])
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 nitems = nframes * self._nchannels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 if nitems * self._sampwidth > self._data_chunk.chunksize - self._data_chunk.size_read:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 nitems = (self._data_chunk.chunksize - self._data_chunk.size_read) / self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 data.fromfile(self._data_chunk.file, nitems)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 self._data_chunk.size_read = self._data_chunk.size_read + nitems * self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 data.byteswap()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 data = data.tostring()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 else:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 data = self._data_chunk.read(nframes * self._framesize)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 if self._convert and data:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 data = self._convert(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 return data
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 # Internal methods.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
324 ## access *: private
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 def _read_fmt_chunk(self, chunk):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 wFormatTag = _read_short(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 self._nchannels = _read_short(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 self._framerate = _read_long(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 dwAvgBytesPerSec = _read_long(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 wBlockAlign = _read_short(chunk)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 if wFormatTag == WAVE_FORMAT_PCM:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 self._sampwidth = (_read_short(chunk) + 7) / 8
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 else:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 raise Error, 'unknown format'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 self._framesize = self._nchannels * self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 self._comptype = 'NONE'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 self._compname = 'not compressed'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 class Wave_write:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 # Variables used in this class:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 # These variables are user settable through appropriate methods
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 # of this class:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 # _file -- the open file with methods write(), close(), tell(), seek()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 # set through the __init__() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 # _comptype -- the AIFF-C compression type ('NONE' in AIFF)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 # set through the setcomptype() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 # _compname -- the human-readable AIFF-C compression type
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 # set through the setcomptype() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 # _nchannels -- the number of audio channels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 # set through the setnchannels() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 # _sampwidth -- the number of bytes per audio sample
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 # set through the setsampwidth() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 # _framerate -- the sampling frequency
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 # set through the setframerate() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 # _nframes -- the number of audio frames written to the header
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 # set through the setnframes() or setparams() method
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 # These variables are used internally only:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 # _datalength -- the size of the audio samples written to the header
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 # _nframeswritten -- the number of frames actually written
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 # _datawritten -- the size of the audio samples actually written
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
365 ## access _file, _comptype, _compname, _nchannels, _sampwidth, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
366 ## _framerate, _nframes, _nframeswritten, \
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
367 ## _datalength, _datawritten: private
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 def __init__(self, f):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 if type(f) == type(''):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 f = __builtin__.open(f, 'w')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 self.initfp(f)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 def initfp(self, file):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 self._file = file
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 self._convert = None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 self._nchannels = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 self._sampwidth = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 self._framerate = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 self._nframes = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 self._nframeswritten = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 self._datawritten = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 self._datalength = 0
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 def __del__(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if self._file:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 self.close()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 # User visible methods.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 def setnchannels(self, nchannels):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 if nchannels < 1:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 raise Error, 'bad # of channels'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 self._nchannels = nchannels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 def getnchannels(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 if not self._nchannels:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 raise Error, 'number of channels not set'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 return self._nchannels
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 def setsampwidth(self, sampwidth):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 if sampwidth < 1 or sampwidth > 4:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 raise Error, 'bad sample width'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 self._sampwidth = sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 def getsampwidth(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 if not self._sampwidth:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 raise Error, 'sample width not set'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 return self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 def setframerate(self, framerate):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 if framerate <= 0:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 raise Error, 'bad frame rate'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 self._framerate = framerate
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 def getframerate(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 if not self._framerate:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 raise Error, 'frame rate not set'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 return self._framerate
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 def setnframes(self, nframes):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 self._nframes = nframes
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 def getnframes(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 return self._nframeswritten
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
435
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 def setcomptype(self, comptype, compname):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 if comptype not in ('NONE',):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 raise Error, 'unsupported compression type'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 self._comptype = comptype
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 self._compname = compname
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 def getcomptype(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 return self._comptype
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 def getcompname(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 return self._compname
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 if self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 raise Error, 'cannot change parameters after starting to write'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 self.setnchannels(nchannels)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 self.setsampwidth(sampwidth)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 self.setframerate(framerate)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 self.setnframes(nframes)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 self.setcomptype(comptype, compname)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
458
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 def getparams(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 if not self._nchannels or not self._sampwidth or not self._framerate:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 raise Error, 'not all parameters set'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 return self._nchannels, self._sampwidth, self._framerate, \
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 self._nframes, self._comptype, self._compname
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 def setmark(self, id, pos, name):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 raise Error, 'setmark() not supported'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 def getmark(self, id):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 raise Error, 'no marks'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
470
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 def getmarkers(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 return None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 def tell(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 return self._nframeswritten
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
476
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 def writeframesraw(self, data):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 self._ensure_header_written(len(data))
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 nframes = len(data) / (self._sampwidth * self._nchannels)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 if self._convert:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 data = self._convert(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 if self._sampwidth > 1:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 import array
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 data = array.array(_array_fmts[self._sampwidth], data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 data.byteswap()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 data.tofile(self._file)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 self._datawritten = self._datawritten + len(data) * self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 else:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 self._file.write(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 self._datawritten = self._datawritten + len(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 self._nframeswritten = self._nframeswritten + nframes
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 def writeframes(self, data):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 self.writeframesraw(data)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 if self._datalength != self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 self._patchheader()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
497
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 def close(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 self._ensure_header_written(0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 if self._datalength != self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 self._patchheader()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 self._file.flush()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 self._file = None
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 #
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 # Internal methods.
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 #
4078
18451dca9469 Get rid of access statement
Guido van Rossum <guido@python.org>
parents: 1549
diff changeset
508 ## access *: private
1549
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
509
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 def _ensure_header_written(self, datasize):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 if not self._datawritten:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 if not self._nchannels:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 raise Error, '# channels not specified'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 if not self._sampwidth:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 raise Error, 'sample width not specified'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 if not self._framerate:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 raise Error, 'sampling rate not specified'
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 self._write_header(datasize)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 def _write_header(self, initlength):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 self._file.write('RIFF')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 if not self._nframes:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 self._nframes = initlength / (self._nchannels * self._sampwidth)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 self._datalength = self._nframes * self._nchannels * self._sampwidth
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 self._form_length_pos = self._file.tell()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 _write_long(self._file, 36 + self._datalength)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 self._file.write('WAVE')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 self._file.write('fmt ')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 _write_long(self._file, 16)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 _write_short(self._file, WAVE_FORMAT_PCM)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 _write_short(self._file, self._nchannels)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 _write_long(self._file, self._framerate)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 _write_long(self._file, self._nchannels * self._framerate * self._sampwidth)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 _write_short(self._file, self._nchannels * self._sampwidth)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 _write_short(self._file, self._sampwidth * 8)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 self._file.write('data')
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 self._data_length_pos = self._file.tell()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 _write_long(self._file, self._datalength)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 def _patchheader(self):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 if self._datawritten == self._datalength:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 return
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 curpos = self._file.tell()
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 self._file.seek(self._form_length_pos, 0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 _write_long(36 + self._datawritten)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 self._file.seek(self._data_length_pos, 0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 _write_long(self._file, self._datawritten)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 self._file.seek(curpos, 0)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 self._datalength = self._datawritten
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
550
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 def open(f, mode):
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 if mode == 'r':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 return Wave_read(f)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 elif mode == 'w':
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 return Wave_write(f)
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 else:
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 raise Error, "mode must be 'r' or 'w'"
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
558
81f26bf1178f aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 openfp = open # B/W compatibility