blob: 756748deb7b4c07643173d4b814efc20329a9961 [file] [log] [blame]
Guido van Rossum50692d61991-09-15 21:05:15 +00001import aiff
2import al
3import sys
4import time
5
6def main():
7 v = 1
8 c = al.newconfig()
9 nchannels = c.getchannels()
10 nsampframes = 0 # ???
11 sampwidth = c.getwidth()
12 samprate = 0.0 # unknown
13 filename = sys.argv[1]
14 f = open(filename, 'r')
15 type, totalsize = aiff.read_chunk_header(f)
16 if type <> 'FORM':
17 raise aiff.Error, 'FORM chunk expected at start of file'
18 aiff.read_form_chunk(f)
19 while 1:
20 try:
21 type, size = aiff.read_chunk_header(f)
22 except EOFError:
23 break
24 if v: print 'header:', `type`, size
Guido van Rossum14f43cf1992-03-30 13:30:03 +000025 if type == 'COMM':
Guido van Rossum50692d61991-09-15 21:05:15 +000026 nchannels, nsampframes, sampwidth, samprate = \
27 aiff.read_comm_chunk(f)
28 if v: print nchannels, nsampframes, sampwidth, samprate
Guido van Rossum14f43cf1992-03-30 13:30:03 +000029 elif type == 'SSND':
Guido van Rossum50692d61991-09-15 21:05:15 +000030 offset, blocksize = aiff.read_ssnd_chunk(f)
31 if v: print offset, blocksize
32 data = f.read(size-8)
33 if size%2: void = f.read(1)
34 p = makeport(nchannels, sampwidth, samprate)
35 play(p, data, offset, blocksize)
36 elif type in aiff.skiplist:
37 aiff.skip_chunk(f, size)
38 else:
39 raise aiff.Error, 'bad chunk type ' + type
40
41def makeport(nchannels, sampwidth, samprate):
42 c = al.newconfig()
43 c.setchannels(nchannels)
44 c.setwidth(sampwidth/8)
45 # can't set the rate...
46 p = al.openport('', 'w', c)
47 return p
48
49def play(p, data, offset, blocksize):
50 data = data[offset:]
51 p.writesamps(data)
52 while p.getfilled() > 0: time.millisleep(10)
53
54main()