Commit 36bb181c authored by Guido van Rossum's avatar Guido van Rossum

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.)
parent 3dd68d33
...@@ -133,6 +133,7 @@ ...@@ -133,6 +133,7 @@
# changed by calling aiff() or aifc() before the first writeframes or # changed by calling aiff() or aifc() before the first writeframes or
# writeframesraw. # writeframesraw.
import struct
import __builtin__ import __builtin__
try: try:
import CL import CL
...@@ -147,35 +148,22 @@ _skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \ ...@@ -147,35 +148,22 @@ _skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \
'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO' 'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
def _read_long(file): def _read_long(file):
x = 0L try:
for i in range(4): return struct.unpack('>l', file.read(4))[0]
byte = file.read(1) except struct.error:
if byte == '': raise EOFError
raise EOFError
x = x*256 + ord(byte)
if x >= 0x80000000L:
x = x - 0x100000000L
return int(x)
def _read_ulong(file): def _read_ulong(file):
x = 0L try:
for i in range(4): return struct.unpack('>L', file.read(4))[0]
byte = file.read(1) except struct.error:
if byte == '': raise EOFError
raise EOFError
x = x*256 + ord(byte)
return x
def _read_short(file): def _read_short(file):
x = 0 try:
for i in range(2): return struct.unpack('>h', file.read(2))[0]
byte = file.read(1) except struct.error:
if byte == '': raise EOFError
raise EOFError
x = x*256 + ord(byte)
if x >= 0x8000:
x = x - 0x10000
return x
def _read_string(file): def _read_string(file):
length = ord(file.read(1)) length = ord(file.read(1))
...@@ -208,20 +196,12 @@ def _read_float(f): # 10 bytes ...@@ -208,20 +196,12 @@ def _read_float(f): # 10 bytes
return sign * f return sign * f
def _write_short(f, x): def _write_short(f, x):
d, m = divmod(x, 256) f.write(struct.pack('>h', x))
f.write(chr(d))
f.write(chr(m))
def _write_long(f, x): def _write_long(f, x):
if x < 0: if x >= 1L<<31:
x = x + 0x100000000L x = x - (1L<<32)
data = [] f.write(struct.pack('>l', x))
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])))
def _write_string(f, s): def _write_string(f, s):
f.write(chr(len(s))) f.write(chr(len(s)))
...@@ -1013,3 +993,30 @@ def open(f, mode): ...@@ -1013,3 +993,30 @@ def open(f, mode):
raise Error, "mode must be 'r' or 'w'" raise Error, "mode must be 'r' or 'w'"
openfp = open # B/W compatibility 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."
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment