Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36bb181

Browse files
committed
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.)
1 parent 3dd68d3 commit 36bb181

1 file changed

Lines changed: 44 additions & 37 deletions

File tree

Lib/aifc.py

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
# changed by calling aiff() or aifc() before the first writeframes or
134134
# writeframesraw.
135135

136+
import struct
136137
import __builtin__
137138
try:
138139
import CL
@@ -147,35 +148,22 @@
147148
'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
148149

149150
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
159155

160156
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
168161

169162
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
179167

180168
def _read_string(file):
181169
length = ord(file.read(1))
@@ -208,20 +196,12 @@ def _read_float(f): # 10 bytes
208196
return sign * f
209197

210198
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))
214200

215201
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))
225205

226206
def _write_string(f, s):
227207
f.write(chr(len(s)))
@@ -1013,3 +993,30 @@ def open(f, mode):
1013993
raise Error, "mode must be 'r' or 'w'"
1014994

1015995
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

Comments
 (0)