|
133 | 133 | # changed by calling aiff() or aifc() before the first writeframes or |
134 | 134 | # writeframesraw. |
135 | 135 |
|
| 136 | +import struct |
136 | 137 | import __builtin__ |
137 | 138 | try: |
138 | 139 | import CL |
|
147 | 148 | 'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO' |
148 | 149 |
|
149 | 150 | def _read_long(file): |
150 | | - x = 0L |
151 | | - for i in range(4): |
152 | | - byte = file.read(1) |
153 | | - if byte == '': |
154 | | - raise EOFError |
155 | | - x = x*256 + ord(byte) |
156 | | - if x >= 0x80000000L: |
157 | | - x = x - 0x100000000L |
158 | | - return int(x) |
| 151 | + try: |
| 152 | + return struct.unpack('>l', file.read(4))[0] |
| 153 | + except struct.error: |
| 154 | + raise EOFError |
159 | 155 |
|
160 | 156 | def _read_ulong(file): |
161 | | - x = 0L |
162 | | - for i in range(4): |
163 | | - byte = file.read(1) |
164 | | - if byte == '': |
165 | | - raise EOFError |
166 | | - x = x*256 + ord(byte) |
167 | | - return x |
| 157 | + try: |
| 158 | + return struct.unpack('>L', file.read(4))[0] |
| 159 | + except struct.error: |
| 160 | + raise EOFError |
168 | 161 |
|
169 | 162 | def _read_short(file): |
170 | | - x = 0 |
171 | | - for i in range(2): |
172 | | - byte = file.read(1) |
173 | | - if byte == '': |
174 | | - raise EOFError |
175 | | - x = x*256 + ord(byte) |
176 | | - if x >= 0x8000: |
177 | | - x = x - 0x10000 |
178 | | - return x |
| 163 | + try: |
| 164 | + return struct.unpack('>h', file.read(2))[0] |
| 165 | + except struct.error: |
| 166 | + raise EOFError |
179 | 167 |
|
180 | 168 | def _read_string(file): |
181 | 169 | length = ord(file.read(1)) |
@@ -208,20 +196,12 @@ def _read_float(f): # 10 bytes |
208 | 196 | return sign * f |
209 | 197 |
|
210 | 198 | def _write_short(f, x): |
211 | | - d, m = divmod(x, 256) |
212 | | - f.write(chr(d)) |
213 | | - f.write(chr(m)) |
| 199 | + f.write(struct.pack('>h', x)) |
214 | 200 |
|
215 | 201 | def _write_long(f, x): |
216 | | - if x < 0: |
217 | | - x = x + 0x100000000L |
218 | | - data = [] |
219 | | - for i in range(4): |
220 | | - d, m = divmod(x, 256) |
221 | | - data.insert(0, m) |
222 | | - x = d |
223 | | - for i in range(4): |
224 | | - f.write(chr(int(data[i]))) |
| 202 | + if x >= 1L<<31: |
| 203 | + x = x - (1L<<32) |
| 204 | + f.write(struct.pack('>l', x)) |
225 | 205 |
|
226 | 206 | def _write_string(f, s): |
227 | 207 | f.write(chr(len(s))) |
@@ -1013,3 +993,30 @@ def open(f, mode): |
1013 | 993 | raise Error, "mode must be 'r' or 'w'" |
1014 | 994 |
|
1015 | 995 | openfp = open # B/W compatibility |
| 996 | + |
| 997 | +if __name__ == '__main__': |
| 998 | + import sys |
| 999 | + if not sys.argv[1:]: |
| 1000 | + sys.argv.append('/usr/demos/data/audio/bach.aiff') |
| 1001 | + fn = sys.argv[1] |
| 1002 | + f = open(fn, 'r') |
| 1003 | + print "Reading", fn |
| 1004 | + print "nchannels =", f.getnchannels() |
| 1005 | + print "nframes =", f.getnframes() |
| 1006 | + print "sampwidth =", f.getsampwidth() |
| 1007 | + print "framerate =", f.getframerate() |
| 1008 | + print "comptype =", f.getcomptype() |
| 1009 | + print "compname =", f.getcompname() |
| 1010 | + if sys.argv[2:]: |
| 1011 | + gn = sys.argv[2] |
| 1012 | + print "Writing", gn |
| 1013 | + g = open(gn, 'w') |
| 1014 | + g.setparams(f.getparams()) |
| 1015 | + while 1: |
| 1016 | + data = f.readframes(1024) |
| 1017 | + if not data: |
| 1018 | + break |
| 1019 | + g.writeframes(data) |
| 1020 | + g.close() |
| 1021 | + f.close() |
| 1022 | + print "Done." |
0 commit comments