Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 1 | import getopt |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 2 | from gl import * |
| 3 | from GL import * |
| 4 | from DEVICE import * |
| 5 | import time |
| 6 | import sys |
| 7 | import al |
| 8 | import AL |
| 9 | |
| 10 | BUFFERSIZE = 32000 |
| 11 | |
| 12 | class Struct(): pass |
| 13 | epoch = Struct() |
| 14 | EndOfFile = 'End of file' |
| 15 | bye = 'bye' |
| 16 | |
| 17 | def openspkr(): |
| 18 | conf = al.newconfig() |
| 19 | conf.setqueuesize(BUFFERSIZE) |
| 20 | conf.setwidth(AL.SAMPLE_16) |
| 21 | conf.setchannels(AL.MONO) |
| 22 | return al.openport('spkr','w',conf) |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 24 | def openvideo(name): |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 25 | try: |
| 26 | f = open(name, 'r') |
| 27 | except: |
| 28 | sys.stderr.write(name + ': cannot open\n') |
| 29 | sys.exit(1) |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 30 | line = f.readline() |
| 31 | if not line: raise EndOfFile |
| 32 | if line[:4] = 'CMIF': line = f.readline() |
| 33 | x = eval(line[:-1]) |
| 34 | if len(x) = 3: w, h, pf = x |
| 35 | else: w, h = x; pf = 2 |
| 36 | return f, w, h, pf |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 38 | def loadframe(f,w,h,pf,af,spkr): |
| 39 | line = f.readline() |
| 40 | if line = '': |
| 41 | raise EndOfFile |
| 42 | x = eval(line[:-1]) |
| 43 | if type(x) = type(0) or type(x) = type(0.0): |
| 44 | tijd = x |
| 45 | if pf = 0: |
| 46 | size = w*h*4 |
| 47 | else: |
| 48 | size = (w/pf) * (h/pf) |
| 49 | else: |
| 50 | tijd, size = x |
| 51 | data = f.read(size) |
| 52 | if len(data) <> size: |
| 53 | raise EndOfFile |
| 54 | if pf: |
| 55 | rectzoom(pf, pf) |
| 56 | w = w/pf |
| 57 | h = h/pf |
| 58 | data = unpackrect(w, h, 1, data) |
| 59 | lrectwrite(0,0,w-1,h-1,data) |
| 60 | # This is ugly here, but the only way to get the two |
| 61 | # channels started in sync |
| 62 | #if af <> None: |
| 63 | # playsound(af,spkr) |
| 64 | ct = time.millitimer() - epoch.epoch |
| 65 | if tijd > 0 and ct < tijd: |
| 66 | time.millisleep(tijd-ct) |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 67 | #swapbuffers() |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 68 | return tijd |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 70 | def playsound(af, spkr): |
| 71 | nsamp = spkr.getfillable() |
| 72 | data = af.read(nsamp*2) |
| 73 | spkr.writesamps(data) |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 75 | def main(): |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 76 | looping = 0 |
| 77 | packfactor = 0 |
| 78 | opts, args = getopt.getopt(sys.argv[1:], 'p:l') |
| 79 | for opt, arg in opts: |
| 80 | if opt = '-p': |
| 81 | packfactor = int(eval(arg)) |
| 82 | elif opt = '-l': |
| 83 | looping = 1 |
| 84 | if args: |
| 85 | filename = args[0] |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 86 | else: |
| 87 | filename = 'film.video' |
| 88 | f, w, h, pf = openvideo(filename) |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 89 | if 0 < packfactor <> pf: |
| 90 | w = w/pf*packfactor |
| 91 | h = h/pf*packfactor |
| 92 | pf = packfactor |
| 93 | if args[1:]: |
| 94 | audiofilename = args[1] |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 95 | af = open(audiofilename, 'r') |
| 96 | spkr = openspkr() |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 97 | afskip = 0 |
Guido van Rossum | b3c493c | 1991-11-04 18:14:23 +0000 | [diff] [blame] | 98 | if args[2:]: |
| 99 | afskip = eval(args[2]) |
| 100 | af.seek(afskip) |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 101 | else: |
| 102 | af, spkr = None, None |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 103 | foreground() |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 104 | prefsize(w,h) |
| 105 | win = winopen(filename) |
| 106 | RGBmode() |
| 107 | #doublebuffer() |
| 108 | gconfig() |
| 109 | qdevice(ESCKEY) |
| 110 | qdevice(WINSHUT) |
| 111 | qdevice(WINQUIT) |
| 112 | running = 1 |
| 113 | epoch.epoch = time.millitimer() |
| 114 | nframe = 0 |
| 115 | tijd = 1 |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 116 | if looping: |
| 117 | looping = f.tell() |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 118 | try: |
| 119 | while 1: |
| 120 | if running: |
| 121 | try: |
| 122 | tijd = loadframe(f, w, h, pf, af, spkr) |
| 123 | nframe = nframe + 1 |
| 124 | except EndOfFile: |
| 125 | running = 0 |
| 126 | t = time.millitimer() |
| 127 | if tijd > 0: |
| 128 | print 'Recorded at', |
| 129 | print 0.1 * int(nframe * 10000.0 / tijd), |
| 130 | print 'frames/sec' |
| 131 | print 'Played', nframe, 'frames at', |
| 132 | print 0.1 * int(nframe * 10000.0 / (t-epoch.epoch)), |
| 133 | print 'frames/sec' |
Guido van Rossum | 5dafd91 | 1991-11-04 18:04:21 +0000 | [diff] [blame] | 134 | if looping: |
| 135 | f.seek(looping) |
| 136 | epoch.epoch = time.millitimer() |
| 137 | nframe = 0 |
| 138 | running = 1 |
| 139 | if af <> None: |
| 140 | af.seek(afskip) |
Guido van Rossum | b51afcc | 1991-11-04 14:29:27 +0000 | [diff] [blame] | 141 | if af <> None: |
| 142 | playsound(af,spkr) |
| 143 | if not running or qtest(): |
| 144 | dev, val = qread() |
| 145 | if dev in (ESCKEY, WINSHUT, WINQUIT): |
| 146 | raise bye |
| 147 | elif dev = REDRAW: |
| 148 | reshapeviewport() |
| 149 | except bye: |
| 150 | pass |
Guido van Rossum | e4bddea | 1991-10-30 11:52:48 +0000 | [diff] [blame] | 151 | |
| 152 | main() |