Files for 2.1b1 distribution.
[python/dscho.git] / Demo / sgi / cd / playcd.py
blob44fa5a0697ca755fcc0586c1675c6ed779852f7e
1 # Play CD audio on speaker or headphones.
3 callbacktypes = ['audio','pnum','index','ptime','atime','catalog','ident','control']
5 def playaudio(port, type, audio):
6 port.writesamps(audio)
8 def prtrack(cdinfo, type, pnum):
9 if cdinfo.track[pnum] <> '':
10 print 'playing "' + cdinfo.track[pnum] + '"'
11 else:
12 print callbacktypes[type]+': '+`pnum`
14 def callback(arg, type, data):
15 print callbacktypes[type]+': '+`data`
17 def tcallback(arg, type, data):
18 print callbacktypes[type]+': '+triple(data)
20 def triple((a, b, c)):
21 return zfill(a) + ':' + zfill(b) + ':' + zfill(c)
23 def zfill(n):
24 s = `n`
25 return '0' * (2 - len(s)) + s
27 def prtrackinfo(info):
28 for i in range(len(info)):
29 start, total = info[i]
30 print 'Track', zfill(i+1), triple(start), triple(total)
32 statedict = ['ERROR', 'NODISK', 'READY', 'PLAYING', 'PAUSED', 'STILL']
34 def prstatus(status):
35 state, track, curtime, abstime, totaltime, first, last, \
36 scsi_audio, cur_block, dummy = status
37 print 'Status:',
38 if 0 <= state < len(statedict):
39 print statedict[state]
40 else:
41 print state
42 print 'Track: ', track
43 print 'Time: ', triple(curtime)
44 print 'Abs: ', triple(abstime)
45 print 'Total: ', triple(totaltime)
46 print 'First: ', first
47 print 'Last: ', last
48 print 'SCSI: ', scsi_audio
49 print 'Block: ', cur_block
50 print 'Future:', dummy
52 def main():
53 import sys, readcd, al, AL, cd, cdplayer
54 verbose = 0
55 r = readcd.Readcd()
56 prstatus(r.getstatus())
57 prtrackinfo(r.gettrackinfo())
58 cdinfo = cdplayer.Cdplayer(r.gettrackinfo())
59 if cdinfo.title <> '':
60 print 'Title: "' + cdinfo.title + '"'
61 if cdinfo.artist <> '':
62 print 'Artist: ' + cdinfo.artist
63 for arg in sys.argv[1:]:
64 if arg == '-v':
65 verbose = 1
66 continue
67 x = eval(arg)
68 try:
69 l = len(x)
70 r.appendstretch(x[0], x[1])
71 except TypeError:
72 r.appendtrack(x)
73 try:
74 oldparams = [AL.OUTPUT_RATE, 0]
75 params = oldparams[:]
76 al.getparams(AL.DEFAULT_DEVICE, oldparams)
77 params[1] = AL.RATE_44100
78 al.setparams(AL.DEFAULT_DEVICE, params)
79 config = al.newconfig()
80 config.setwidth(AL.SAMPLE_16)
81 config.setchannels(AL.STEREO)
82 port = al.openport('CD Player', 'w', config)
84 for i in range(8):
85 r.setcallback(i, callback, None)
86 if verbose:
87 r.setcallback(cd.ptime, tcallback, None)
88 r.setcallback(cd.atime, tcallback, None)
89 else:
90 r.removecallback(cd.ptime)
91 r.removecallback(cd.atime)
92 r.setcallback(cd.pnum, prtrack, cdinfo)
93 r.setcallback(cd.audio, playaudio, port)
95 data = r.play()
96 except KeyboardInterrupt:
97 status = r.getstatus()
98 print 'Interrupted at '+triple(status[2])+' into track '+ \
99 `status[1]`+' (absolute time '+triple(status[3])+')'
100 al.setparams(AL.DEFAULT_DEVICE, oldparams)
102 main()