diff options
author | Guido van Rossum <guido@python.org> | 1996-12-31 05:57:34 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1996-12-31 05:57:34 (GMT) |
commit | 36bb181cdffbb80b555fd8454c80e66e10657dde (patch) | |
tree | 685c415853b788b2927d90596154d22d1254ea45 | |
parent | 3dd68d3364c58aa835fad93ab97c9aa2f3834b26 (diff) | |
download | cpython-36bb181cdffbb80b555fd8454c80e66e10657dde.zip cpython-36bb181cdffbb80b555fd8454c80e66e10657dde.tar.gz cpython-36bb181cdffbb80b555fd8454c80e66e10657dde.tar.bz2 |
Rewrote _{read,write}_{short,long} to use the newly revamped struct
module. (Small problem: struct.pack() won't deal with the Python long
ints returned by struct.unpack() for the 'L' format. Worked around
that for now.)
-rw-r--r-- | Lib/aifc.py | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/Lib/aifc.py b/Lib/aifc.py index 0c2b10b..2e4b19c 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -133,6 +133,7 @@ # changed by calling aiff() or aifc() before the first writeframes or # writeframesraw. +import struct import __builtin__ try: import CL @@ -147,35 +148,22 @@ _skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \ 'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO' def _read_long(file): - x = 0L - for i in range(4): - byte = file.read(1) - if byte == '': - raise EOFError - x = x*256 + ord(byte) - if x >= 0x80000000L: - x = x - 0x100000000L - return int(x) + try: + return struct.unpack('>l', file.read(4))[0] + except struct.error: + raise EOFError def _read_ulong(file): - x = 0L - for i in range(4): - byte = file.read(1) - if byte == '': - raise EOFError - x = x*256 + ord(byte) - return x + try: + return struct.unpack('>L', file.read(4))[0] + except struct.error: + raise EOFError def _read_short(file): - x = 0 - for i in range(2): - byte = file.read(1) - if byte == '': - raise EOFError - x = x*256 + ord(byte) - if x >= 0x8000: - x = x - 0x10000 - return x + try: + return struct.unpack('>h', file.read(2))[0] + except struct.error: + raise EOFError def _read_string(file): length = ord(file.read(1)) @@ -208,20 +196,12 @@ def _read_float(f): # 10 bytes return sign * f def _write_short(f, x): - d, m = divmod(x, 256) - f.write(chr(d)) - f.write(chr(m)) + f.write(struct.pack('>h', x)) def _write_long(f, x): - if x < 0: - x = x + 0x100000000L - data = [] - for i in range(4): - d, m = divmod(x, 256) - data.insert(0, m) - x = d - for i in range(4): - f.write(chr(int(data[i]))) + if x >= 1L<<31: + x = x - (1L<<32) + f.write(struct.pack('>l', x)) def _write_string(f, s): f.write(chr(len(s))) @@ -1013,3 +993,30 @@ def open(f, mode): raise Error, "mode must be 'r' or 'w'" openfp = open # B/W compatibility + +if __name__ == '__main__': + import sys + if not sys.argv[1:]: + sys.argv.append('/usr/demos/data/audio/bach.aiff') + fn = sys.argv[1] + f = open(fn, 'r') + print "Reading", fn + print "nchannels =", f.getnchannels() + print "nframes =", f.getnframes() + print "sampwidth =", f.getsampwidth() + print "framerate =", f.getframerate() + print "comptype =", f.getcomptype() + print "compname =", f.getcompname() + if sys.argv[2:]: + gn = sys.argv[2] + print "Writing", gn + g = open(gn, 'w') + g.setparams(f.getparams()) + while 1: + data = f.readframes(1024) + if not data: + break + g.writeframes(data) + g.close() + f.close() + print "Done." |