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