blob: ec135af0b87f333b9b341c915c34cc21010b9773 [file] [log] [blame]
Guido van Rossume4bddea1991-10-30 11:52:48 +00001from gl import *
2from GL import *
3from DEVICE import *
4import time
5import sys
6import al
7import AL
8
9BUFFERSIZE = 32000
10
11class Struct(): pass
12epoch = Struct()
13EndOfFile = 'End of file'
14bye = 'bye'
15
16def 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 Rossumb51afcc1991-11-04 14:29:27 +000022
Guido van Rossume4bddea1991-10-30 11:52:48 +000023def openvideo(name):
Guido van Rossumb51afcc1991-11-04 14:29:27 +000024 try:
25 f = open(name, 'r')
26 except:
27 sys.stderr.write(name + ': cannot open\n')
28 sys.exit(1)
Guido van Rossume4bddea1991-10-30 11:52:48 +000029 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 Rossumb51afcc1991-11-04 14:29:27 +000036
Guido van Rossume4bddea1991-10-30 11:52:48 +000037def 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 Rossumb51afcc1991-11-04 14:29:27 +000066 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000067 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000068
Guido van Rossume4bddea1991-10-30 11:52:48 +000069def playsound(af, spkr):
70 nsamp = spkr.getfillable()
71 data = af.read(nsamp*2)
72 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000073
Guido van Rossume4bddea1991-10-30 11:52:48 +000074def main():
Guido van Rossumb51afcc1991-11-04 14:29:27 +000075 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 Rossume4bddea1991-10-30 11:52:48 +0000127
128main()