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) |
| 22 | def openvideo(name): |
| 23 | f = open(name, 'r') |
| 24 | line = f.readline() |
| 25 | if not line: raise EndOfFile |
| 26 | if line[:4] = 'CMIF': line = f.readline() |
| 27 | x = eval(line[:-1]) |
| 28 | if len(x) = 3: w, h, pf = x |
| 29 | else: w, h = x; pf = 2 |
| 30 | return f, w, h, pf |
| 31 | def loadframe(f,w,h,pf,af,spkr): |
| 32 | line = f.readline() |
| 33 | if line = '': |
| 34 | raise EndOfFile |
| 35 | x = eval(line[:-1]) |
| 36 | if type(x) = type(0) or type(x) = type(0.0): |
| 37 | tijd = x |
| 38 | if pf = 0: |
| 39 | size = w*h*4 |
| 40 | else: |
| 41 | size = (w/pf) * (h/pf) |
| 42 | else: |
| 43 | tijd, size = x |
| 44 | data = f.read(size) |
| 45 | if len(data) <> size: |
| 46 | raise EndOfFile |
| 47 | if pf: |
| 48 | rectzoom(pf, pf) |
| 49 | w = w/pf |
| 50 | h = h/pf |
| 51 | data = unpackrect(w, h, 1, data) |
| 52 | lrectwrite(0,0,w-1,h-1,data) |
| 53 | # This is ugly here, but the only way to get the two |
| 54 | # channels started in sync |
| 55 | #if af <> None: |
| 56 | # playsound(af,spkr) |
| 57 | ct = time.millitimer() - epoch.epoch |
| 58 | if tijd > 0 and ct < tijd: |
| 59 | time.millisleep(tijd-ct) |
| 60 | swapbuffers() |
| 61 | return tijd |
| 62 | def playsound(af, spkr): |
| 63 | nsamp = spkr.getfillable() |
| 64 | data = af.read(nsamp*2) |
| 65 | spkr.writesamps(data) |
| 66 | def main(): |
| 67 | if len(sys.argv) > 1: |
| 68 | f, w, h, pf = openvideo(sys.argv[1]) |
| 69 | else: |
| 70 | f, w, h, pf = openvideo('film.video') |
| 71 | af = None |
| 72 | spkr = None |
| 73 | if len(sys.argv) > 2: |
| 74 | af = open(sys.argv[2], 'r') |
| 75 | spkr = openspkr() |
| 76 | if len(sys.argv) > 3: |
| 77 | data = af.read(eval(sys.argv[3])) |
| 78 | del data |
| 79 | foreground() |
| 80 | prefsize(w,h) |
| 81 | win = winopen('Video player') |
| 82 | RGBmode() |
| 83 | doublebuffer() |
| 84 | gconfig() |
| 85 | qdevice(ESCKEY) |
| 86 | running = 1 |
| 87 | epoch.epoch = time.millitimer() |
| 88 | nframe = 0 |
| 89 | tijd = 1 |
| 90 | try: |
| 91 | while 1: |
| 92 | if running: |
| 93 | try: |
| 94 | tijd = loadframe(f, w, h, pf, af, spkr) |
| 95 | nframe = nframe + 1 |
| 96 | except EndOfFile: |
| 97 | running = 0 |
| 98 | t = time.millitimer() |
| 99 | if tijd > 0: |
| 100 | print 'Recorded at ', nframe * 1000.0 / tijd, |
| 101 | print 'frames/second (', tijd, 'ms total)' |
| 102 | print 'Played at', nframe * 1000.0 / (t-epoch.epoch), |
| 103 | print 'frames/second' |
| 104 | if af <> None: |
| 105 | playsound(af,spkr) |
| 106 | if qtest(): |
| 107 | if qread() = (ESCKEY,1): |
| 108 | raise bye |
| 109 | except bye: |
| 110 | pass |
| 111 | |
| 112 | main() |