blob: 44fa5a0697ca755fcc0586c1675c6ed779852f7e [file] [log] [blame]
Guido van Rossum9ac90631992-04-22 13:47:50 +00001# Play CD audio on speaker or headphones.
2
Guido van Rossum6c6b6091992-06-03 16:49:29 +00003callbacktypes = ['audio','pnum','index','ptime','atime','catalog','ident','control']
4
Guido van Rossumb9506291992-04-13 18:38:20 +00005def playaudio(port, type, audio):
Guido van Rossumb9506291992-04-13 18:38:20 +00006 port.writesamps(audio)
7
Guido van Rossum6c6b6091992-06-03 16:49:29 +00008def prtrack(cdinfo, type, pnum):
9 if cdinfo.track[pnum] <> '':
10 print 'playing "' + cdinfo.track[pnum] + '"'
11 else:
12 print callbacktypes[type]+': '+`pnum`
13
Guido van Rossum60c225c1992-04-22 13:39:48 +000014def callback(arg, type, data):
Guido van Rossum6c6b6091992-06-03 16:49:29 +000015 print callbacktypes[type]+': '+`data`
16
17def tcallback(arg, type, data):
18 print callbacktypes[type]+': '+triple(data)
Guido van Rossumb9506291992-04-13 18:38:20 +000019
Guido van Rossum60c225c1992-04-22 13:39:48 +000020def triple((a, b, c)):
21 return zfill(a) + ':' + zfill(b) + ':' + zfill(c)
Guido van Rossumb9506291992-04-13 18:38:20 +000022
Guido van Rossum60c225c1992-04-22 13:39:48 +000023def zfill(n):
24 s = `n`
25 return '0' * (2 - len(s)) + s
26
27def prtrackinfo(info):
28 for i in range(len(info)):
29 start, total = info[i]
30 print 'Track', zfill(i+1), triple(start), triple(total)
31
32statedict = ['ERROR', 'NODISK', 'READY', 'PLAYING', 'PAUSED', 'STILL']
33
34def 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
Guido van Rossum67aad281992-04-15 17:49:14 +000051
Guido van Rossumb9506291992-04-13 18:38:20 +000052def main():
Sjoerd Mullenderf1bb3341995-04-06 14:46:31 +000053 import sys, readcd, al, AL, cd, cdplayer
Guido van Rossum6c6b6091992-06-03 16:49:29 +000054 verbose = 0
Guido van Rossumfc146cb1994-10-07 10:25:49 +000055 r = readcd.Readcd()
Guido van Rossum60c225c1992-04-22 13:39:48 +000056 prstatus(r.getstatus())
57 prtrackinfo(r.gettrackinfo())
Guido van Rossumfc146cb1994-10-07 10:25:49 +000058 cdinfo = cdplayer.Cdplayer(r.gettrackinfo())
Guido van Rossum6c6b6091992-06-03 16:49:29 +000059 if cdinfo.title <> '':
60 print 'Title: "' + cdinfo.title + '"'
61 if cdinfo.artist <> '':
62 print 'Artist: ' + cdinfo.artist
Guido van Rossum60c225c1992-04-22 13:39:48 +000063 for arg in sys.argv[1:]:
Guido van Rossum6c6b6091992-06-03 16:49:29 +000064 if arg == '-v':
65 verbose = 1
66 continue
Guido van Rossum60c225c1992-04-22 13:39:48 +000067 x = eval(arg)
68 try:
69 l = len(x)
70 r.appendstretch(x[0], x[1])
71 except TypeError:
72 r.appendtrack(x)
Guido van Rossumb9506291992-04-13 18:38:20 +000073 try:
Guido van Rossum60c225c1992-04-22 13:39:48 +000074 oldparams = [AL.OUTPUT_RATE, 0]
75 params = oldparams[:]
76 al.getparams(AL.DEFAULT_DEVICE, oldparams)
77 params[1] = AL.RATE_44100
Guido van Rossumb9506291992-04-13 18:38:20 +000078 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)
83
Guido van Rossum60c225c1992-04-22 13:39:48 +000084 for i in range(8):
85 r.setcallback(i, callback, None)
Guido van Rossum6c6b6091992-06-03 16:49:29 +000086 if verbose:
Sjoerd Mullenderf1bb3341995-04-06 14:46:31 +000087 r.setcallback(cd.ptime, tcallback, None)
88 r.setcallback(cd.atime, tcallback, None)
Guido van Rossum6c6b6091992-06-03 16:49:29 +000089 else:
Sjoerd Mullenderf1bb3341995-04-06 14:46:31 +000090 r.removecallback(cd.ptime)
91 r.removecallback(cd.atime)
92 r.setcallback(cd.pnum, prtrack, cdinfo)
93 r.setcallback(cd.audio, playaudio, port)
Guido van Rossumb9506291992-04-13 18:38:20 +000094
Guido van Rossum60c225c1992-04-22 13:39:48 +000095 data = r.play()
Guido van Rossumb9506291992-04-13 18:38:20 +000096 except KeyboardInterrupt:
Guido van Rossum6c6b6091992-06-03 16:49:29 +000097 status = r.getstatus()
98 print 'Interrupted at '+triple(status[2])+' into track '+ \
99 `status[1]`+' (absolute time '+triple(status[3])+')'
Guido van Rossumb9506291992-04-13 18:38:20 +0000100 al.setparams(AL.DEFAULT_DEVICE, oldparams)
Guido van Rossumb9506291992-04-13 18:38:20 +0000101
102main()