blob: df74d005a2b7c969b7d12cf77211860d0643ea63 [file] [log] [blame]
Guido van Rossum5dafd911991-11-04 18:04:21 +00001import getopt
Guido van Rossume4bddea1991-10-30 11:52:48 +00002from gl import *
3from GL import *
4from DEVICE import *
5import time
6import sys
7import al
8import AL
9
10BUFFERSIZE = 32000
11
12class Struct(): pass
13epoch = Struct()
14EndOfFile = 'End of file'
15bye = 'bye'
16
17def 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 Rossumb51afcc1991-11-04 14:29:27 +000023
Guido van Rossume4bddea1991-10-30 11:52:48 +000024def openvideo(name):
Guido van Rossumb51afcc1991-11-04 14:29:27 +000025 try:
26 f = open(name, 'r')
27 except:
28 sys.stderr.write(name + ': cannot open\n')
29 sys.exit(1)
Guido van Rossume4bddea1991-10-30 11:52:48 +000030 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 Rossumb51afcc1991-11-04 14:29:27 +000037
Guido van Rossume4bddea1991-10-30 11:52:48 +000038def 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 Rossumb51afcc1991-11-04 14:29:27 +000067 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000068 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000069
Guido van Rossume4bddea1991-10-30 11:52:48 +000070def playsound(af, spkr):
71 nsamp = spkr.getfillable()
72 data = af.read(nsamp*2)
73 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000074
Guido van Rossume4bddea1991-10-30 11:52:48 +000075def main():
Guido van Rossum5dafd911991-11-04 18:04:21 +000076 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 Rossumb51afcc1991-11-04 14:29:27 +000086 else:
87 filename = 'film.video'
88 f, w, h, pf = openvideo(filename)
Guido van Rossum5dafd911991-11-04 18:04:21 +000089 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 Rossumb51afcc1991-11-04 14:29:27 +000095 af = open(audiofilename, 'r')
96 spkr = openspkr()
Guido van Rossum5dafd911991-11-04 18:04:21 +000097 afskip = 0
Guido van Rossumb3c493c1991-11-04 18:14:23 +000098 if args[2:]:
99 afskip = eval(args[2])
100 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000101 else:
102 af, spkr = None, None
Guido van Rossum5dafd911991-11-04 18:04:21 +0000103 foreground()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000104 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 Rossum5dafd911991-11-04 18:04:21 +0000116 if looping:
117 looping = f.tell()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000118 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 Rossum5dafd911991-11-04 18:04:21 +0000134 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 Rossumb51afcc1991-11-04 14:29:27 +0000141 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 Rossume4bddea1991-10-30 11:52:48 +0000151
152main()