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