Guido van Rossum | 9ac9063 | 1992-04-22 13:47:50 +0000 | [diff] [blame] | 1 | # Play CD audio on speaker or headphones. |
| 2 | |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 3 | callbacktypes = ['audio','pnum','index','ptime','atime','catalog','ident','control'] |
| 4 | |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 5 | def playaudio(port, type, audio): |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 6 | port.writesamps(audio) |
| 7 | |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 8 | def 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 Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 14 | def callback(arg, type, data): |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 15 | print callbacktypes[type]+': '+`data` |
| 16 | |
| 17 | def tcallback(arg, type, data): |
| 18 | print callbacktypes[type]+': '+triple(data) |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 20 | def triple((a, b, c)): |
| 21 | return zfill(a) + ':' + zfill(b) + ':' + zfill(c) |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 23 | def zfill(n): |
| 24 | s = `n` |
| 25 | return '0' * (2 - len(s)) + s |
| 26 | |
| 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) |
| 31 | |
| 32 | statedict = ['ERROR', 'NODISK', 'READY', 'PLAYING', 'PAUSED', 'STILL'] |
| 33 | |
| 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 |
Guido van Rossum | 67aad28 | 1992-04-15 17:49:14 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 52 | def main(): |
Sjoerd Mullender | f1bb334 | 1995-04-06 14:46:31 +0000 | [diff] [blame] | 53 | import sys, readcd, al, AL, cd, cdplayer |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 54 | verbose = 0 |
Guido van Rossum | fc146cb | 1994-10-07 10:25:49 +0000 | [diff] [blame] | 55 | r = readcd.Readcd() |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 56 | prstatus(r.getstatus()) |
| 57 | prtrackinfo(r.gettrackinfo()) |
Guido van Rossum | fc146cb | 1994-10-07 10:25:49 +0000 | [diff] [blame] | 58 | cdinfo = cdplayer.Cdplayer(r.gettrackinfo()) |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 59 | if cdinfo.title <> '': |
| 60 | print 'Title: "' + cdinfo.title + '"' |
| 61 | if cdinfo.artist <> '': |
| 62 | print 'Artist: ' + cdinfo.artist |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 63 | for arg in sys.argv[1:]: |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 64 | if arg == '-v': |
| 65 | verbose = 1 |
| 66 | continue |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 67 | 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 Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 73 | try: |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 74 | oldparams = [AL.OUTPUT_RATE, 0] |
| 75 | params = oldparams[:] |
| 76 | al.getparams(AL.DEFAULT_DEVICE, oldparams) |
| 77 | params[1] = AL.RATE_44100 |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 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) |
| 83 | |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 84 | for i in range(8): |
| 85 | r.setcallback(i, callback, None) |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 86 | if verbose: |
Sjoerd Mullender | f1bb334 | 1995-04-06 14:46:31 +0000 | [diff] [blame] | 87 | r.setcallback(cd.ptime, tcallback, None) |
| 88 | r.setcallback(cd.atime, tcallback, None) |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 89 | else: |
Sjoerd Mullender | f1bb334 | 1995-04-06 14:46:31 +0000 | [diff] [blame] | 90 | 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 Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 60c225c | 1992-04-22 13:39:48 +0000 | [diff] [blame] | 95 | data = r.play() |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 96 | except KeyboardInterrupt: |
Guido van Rossum | 6c6b609 | 1992-06-03 16:49:29 +0000 | [diff] [blame] | 97 | status = r.getstatus() |
| 98 | print 'Interrupted at '+triple(status[2])+' into track '+ \ |
| 99 | `status[1]`+' (absolute time '+triple(status[3])+')' |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 100 | al.setparams(AL.DEFAULT_DEVICE, oldparams) |
Guido van Rossum | b950629 | 1992-04-13 18:38:20 +0000 | [diff] [blame] | 101 | |
| 102 | main() |