blob: e6fecdfd1850654dbf0aac55b4eaa287228e4c59 [file] [log] [blame]
Guido van Rossumb7e3cc11993-05-06 16:06:44 +00001#! /ufs/guido/bin/sgi/python
2
Guido van Rossum9f42f4f1993-05-10 15:07:20 +00003# Video bag of tricks: record video(+audio) in various formats and modes
Guido van Rossumb7e3cc11993-05-06 16:06:44 +00004
Guido van Rossumc5a14331993-05-07 11:20:07 +00005# XXX To do:
6# - audio
Guido van Rossumc5a14331993-05-07 11:20:07 +00007# - improve user interface
8# - help button?
9# - command line options to set initial settings
10# - save settings in a file
11# - ...?
Guido van Rossumbc6d3c31993-05-07 09:37:42 +000012
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000013import sys
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000014import time
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000015import getopt
16import string
17import os
Guido van Rossumc5a14331993-05-07 11:20:07 +000018sts = os.system('makemap') # Must be before "import fl" to work
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000019import sgi
20import gl
21import GL
22import DEVICE
23import fl
24import FL
25import flp
26import watchcursor
27import sv
28import SV
29import VFile
30import VGrabber
31import imageop
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000032sys.path.append('/ufs/jack/src/av/vcr')
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000033
34ARROW = 0
35WATCH = 1
36watchcursor.defwatch(WATCH)
37
38def main():
Guido van Rossumc5a14331993-05-07 11:20:07 +000039## fl.set_graphics_mode(0, 1)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000040 vb = VideoBagOfTricks().init()
41 while 1:
42 dummy = fl.do_forms()
43 [dummy]
44
45StopCapture = 'StopCapture'
46
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000047VideoFormatLabels = ['Video off', 'rgb8', 'grey8', 'grey4', 'grey2', \
Guido van Rossumbc6d3c31993-05-07 09:37:42 +000048 'grey2 dith', 'mono dith', 'mono thresh']
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000049VideoFormats = ['', 'rgb8', 'grey', 'grey4', 'grey2', \
50 'grey2', 'mono', 'mono']
51
52VideoModeLabels = ['Continuous', 'Burst', 'Single frame', 'VCR sync']
53[VM_CONT, VM_BURST, VM_SINGLE, VM_VCR] = range(1, 5)
54
55AudioFormatLabels = ['Audio off', \
56 '16 bit mono', '16 bit stereo', '8 bit mono', '8 bit stereo']
57[A_OFF, A_16_MONO, A_16_STEREO, A_8_MONO, A_8_STEREO] = range(1, 6)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000058
59class VideoBagOfTricks:
60
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000061 # Init/close stuff
62
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000063 def init(self):
64 formdef = flp.parse_form('VbForm', 'form')
65 flp.create_full_form(self, formdef)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000066 self.g_cont.hide_object()
Guido van Rossumc5a14331993-05-07 11:20:07 +000067 self.g_burst.hide_object()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000068 self.g_single.hide_object()
69 self.g_vcr.hide_object()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000070 self.setdefaults()
71 self.openvideo()
72 self.makewindow()
73 self.bindvideo()
Guido van Rossumc5a14331993-05-07 11:20:07 +000074 self.showform()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000075 fl.set_event_call_back(self.do_event)
76 return self
77
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000078 def close(self):
79 self.close_video()
80 self.close_audio()
81 raise SystemExit, 0
82
Guido van Rossumc5a14331993-05-07 11:20:07 +000083 def showform(self):
84 # Get position of video window
85 gl.winset(self.window)
86 x, y = gl.getorigin()
87 width, height = gl.getsize()
88 # Calculate position of form window
89 x1 = x + width + 10
90 x2 = x1 + int(self.form.w) - 1
91 y2 = y + height - 1
92 y1 = y2 - int(self.form.h) + 1
93 # Position and show form window
94 gl.prefposition(x1, x2, y1, y2)
95 self.form.show_form(FL.PLACE_FREE, FL.TRUE, \
96 'Video Bag Of Tricks')
97
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000098 def setdefaults(self):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000099 self.vcr = None
100 self.vout = None
101 self.capturing = 0
102 # Video defaults
103 self.vfile = 'film.video'
104 self.vmode = VM_CONT
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000105 self.mono_thresh = 128
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000106 self.vformat = 'rgb8'
107 self.c_vformat.clear_choice()
108 for label in VideoFormatLabels:
109 self.c_vformat.addto_choice(label)
110 self.c_vformat.set_choice(1 + VideoFormats.index(self.vformat))
111 self.c_vmode.clear_choice()
112 for label in VideoModeLabels:
113 self.c_vmode.addto_choice(label)
114 self.c_vmode.set_choice(self.vmode)
115 self.get_vformat()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000116 self.b_drop.set_button(1)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000117 self.in_rate.set_input('2')
118 self.in_maxmem.set_input('1.0')
119 self.in_nframes.set_input('0')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000120 self.in_nframes_vcr.set_input('1')
121 self.in_sleeptime.set_input('1.0')
122 # Audio defaults
123 self.aout = None
124 self.aport = None
125 self.afile = 'film.aiff'
126 self.aformat = A_OFF
127 self.c_aformat.clear_choice()
128 for label in AudioFormatLabels:
129 self.c_aformat.addto_choice(label)
130 self.c_aformat.set_choice(self.aformat)
131 self.get_aformat()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000132
133 def openvideo(self):
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000134 try:
135 self.video = sv.OpenVideo()
136 except sv.error, msg:
137 print 'Error opening video:', msg
138 self.video = None
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000139 param = [SV.BROADCAST, SV.PAL]
140 if self.video: self.video.GetParam(param)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000141 if param[1] == SV.PAL:
142 x = SV.PAL_XMAX
143 y = SV.PAL_YMAX
144 elif param[1] == SV.NTSC:
145 x = SV.NTSC_XMAX
146 y = SV.NTSC_YMAX
147 else:
148 print 'Unknown video standard:', param[1]
149 sys.exit(1)
150 self.maxx, self.maxy = x, y
151
152 def makewindow(self):
153 x, y = self.maxx, self.maxy
154 gl.foreground()
155 gl.maxsize(x, y)
156 gl.keepaspect(x, y)
157 gl.stepunit(8, 6)
158 width = 256 # XXX
159 if width:
160 height = width*3/4
161 x1 = 150
162 x2 = x1 + width-1
163 y2 = 768-150
164 y1 = y2-height+1
165 gl.prefposition(x1, x2, y1, y2)
166 self.window = gl.winopen('Vb: initializing')
167 self.settitle()
168 if width:
169 gl.maxsize(x, y)
170 gl.keepaspect(x, y)
171 gl.stepunit(8, 6)
172 gl.winconstraints()
173 gl.qdevice(DEVICE.LEFTMOUSE)
174 gl.qdevice(DEVICE.WINQUIT)
175 gl.qdevice(DEVICE.WINSHUT)
176
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000177 def bindvideo(self):
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000178 if not self.video: return
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000179 x, y = gl.getsize()
180 self.video.SetSize(x, y)
181 drop = self.b_drop.get_button()
182 if drop:
183 param = [SV.FIELDDROP, 1, SV.GENLOCK, SV.GENLOCK_OFF]
184 else:
185 param = [SV.FIELDDROP, 0, SV.GENLOCK, SV.GENLOCK_ON]
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000186 if self.rgb:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000187 param = param+[SV.COLOR, SV.DEFAULT_COLOR, \
188 SV.DITHER, 1, \
189 SV.INPUT_BYPASS, 0]
190 else:
191 param = param+[SV.COLOR, SV.MONO, SV.DITHER, 0, \
192 SV.INPUT_BYPASS, 1]
193 self.video.BindGLWindow(self.window, SV.IN_REPLACE)
194 self.video.SetParam(param)
195
196 def rebindvideo(self):
197 gl.winset(self.window)
198 self.bindvideo()
199
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000200 def reset(self):
201 self.close_video()
202 self.close_audio()
203
204 # Event handler (catches resize of video window)
205
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000206 def do_event(self, dev, val):
207 #print 'Event:', dev, val
208 if dev in (DEVICE.WINSHUT, DEVICE.WINQUIT):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000209 self.close()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000210 if dev == DEVICE.REDRAW and val == self.window:
211 self.rebindvideo()
212 self.settitle()
213
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000214 # Video controls: format, mode, file
215
216 def cb_vformat(self, *args):
217 self.reset()
218 self.get_vformat()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000219 if self.mono_use_thresh:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000220 s = `self.mono_thresh`
221 s = fl.show_input('Please enter mono threshold', s)
222 if s:
223 try:
224 self.mono_thresh = string.atoi(s)
225 except string.atoi_error:
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000226 fl.show_message('Bad input, using', \
227 `self.mono_thresh`, '')
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000228 self.rebindvideo()
229
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000230 def cb_vmode(self, *args):
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000231 if self.vcr:
232 self.vcr = None
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000233 self.vmode = self.c_vmode.get_choice()
234 self.form.freeze_form()
235 self.g_cont.hide_object()
236 self.g_burst.hide_object()
237 self.g_single.hide_object()
238 self.g_vcr.hide_object()
239 if self.vmode == VM_CONT:
240 self.g_cont.show_object()
241 elif self.vmode == VM_BURST:
242 self.g_burst.show_object()
243 elif self.vmode == VM_SINGLE:
244 self.g_single.show_object()
245 elif self.vmode == VM_VCR:
246 self.g_vcr.show_object()
247 self.form.unfreeze_form()
248
249 def cb_vfile(self, *args):
250 filename = self.vfile
251 hd, tl = os.path.split(filename)
252 filename = fl.file_selector('Video save file:', hd, '', tl)
253 if filename:
254 self.reset()
255 hd, tl = os.path.split(filename)
256 if hd == os.getcwd():
257 filename = tl
258 self.vfile = filename
259
260 # Video mode specific video controls
261
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000262 def cb_rate(self, *args):
263 pass
264
265 def cb_drop(self, *args):
266 self.rebindvideo()
267
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000268 def cb_maxmem(self, *args):
269 pass
270
271 def cb_nframes(self, *args):
272 pass
273
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000274 def cb_fps(self, *args):
275 pass
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000276
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000277 def cb_nframes_vcr(self, *args):
278 pass
279
280 def cb_sleeptime(self, *args):
281 pass
282
283 # Audio controls: format, file
284
285 def cb_aformat(self, *args):
286 self.get_aformat()
287
288 def cb_afile(self, *args):
289 filename = self.afile
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000290 hd, tl = os.path.split(filename)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000291 filename = fl.file_selector('Audio save file:', hd, '', tl)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000292 if filename:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000293 self.reset()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000294 hd, tl = os.path.split(filename)
295 if hd == os.getcwd():
296 filename = tl
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000297 self.afile = filename
298
299 # General controls: capture, reset, play, quit
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000300
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000301 def cb_capture(self, *args):
Guido van Rossumc5a14331993-05-07 11:20:07 +0000302 if self.capturing:
303 raise StopCapture
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000304 if not self.b_capture.get_button():
305 return
306 if not self.video or not self.vformat:
307 gl.ringbell()
308 return
309 if self.vmode == VM_CONT:
310 self.cont_capture()
311 elif self.vmode == VM_BURST:
312 self.burst_capture()
313 elif self.vmode == VM_SINGLE:
314 self.single_capture()
315 elif self.vmode == VM_VCR:
316 self.vcr_capture()
317
318 def cb_reset(self, *args):
319 self.reset()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000320
321 def cb_play(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000322 sts = os.system('Vplay -q ' + self.vfile + ' &')
Guido van Rossumc5a14331993-05-07 11:20:07 +0000323
324 def cb_quit(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000325 self.close()
326
327 # Capture routines
Guido van Rossumc5a14331993-05-07 11:20:07 +0000328
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000329 def burst_capture(self):
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000330 self.setwatch()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000331 gl.winset(self.window)
332 x, y = gl.getsize()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000333 vformat = SV.RGB8_FRAMES
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000334 nframes = self.getint(self.in_nframes, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000335 if nframes == 0:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000336 maxmem = self.getint(self.in_maxmem, 1.0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000337 memsize = int(maxmem * 1024 * 1024)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000338 nframes = self.calcnframes()
339 info = (vformat, x, y, nframes, 1)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000340 try:
341 info2, data, bitvec = self.video.CaptureBurst(info)
342 except sv.error, msg:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000343 self.b_capture.set_button(0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000344 self.setarrow()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000345 fl.show_message('Capture error:', str(msg), '')
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000346 return
Guido van Rossumc5a14331993-05-07 11:20:07 +0000347 if info <> info2: print info, '<>', info2
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000348 self.save_burst(info2, data, bitvec)
349 self.setarrow()
350
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000351 def calcnframes(self):
352 gl.winset(self.window)
353 x, y = gl.getsize()
354 pixels = x*y
355 pixels = pixels/2 # XXX always assume fields
356 if self.mono or self.grey:
357 n = memsize/pixels
358 else:
359 n = memsize/(4*pixels)
360 return max(1, n)
361
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000362 def save_burst(self, info, data, bitvec):
363 (vformat, x, y, nframes, rate) = info
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000364 self.open_if_closed()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000365 fieldsize = x*y/2
366 nskipped = 0
367 realframeno = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000368 tpf = 1000 / 50.0 # XXX
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000369 for frameno in range(0, nframes*2):
370 if frameno <> 0 and \
371 bitvec[frameno] == bitvec[frameno-1]:
372 nskipped = nskipped + 1
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000373 continue
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000374 #
375 # Save field.
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000376 # XXX Works only for fields and top-to-bottom
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000377 #
378 start = frameno*fieldsize
379 field = data[start:start+fieldsize]
380 realframeno = realframeno + 1
381 fn = int(realframeno*tpf)
382 if not self.write_frame(fn, field):
383 break
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000384
385 def cont_capture(self):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000386 saved_label = self.b_capture.label
387 self.b_capture.label = 'Stop\n' + saved_label
388 self.open_if_closed()
389 self.init_cont()
390 fps = 59.64 # Fields per second
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000391 # XXX (fps of Indigo monitor, not of PAL or NTSC!)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000392 tpf = 1000.0 / fps # Time per field in msec
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000393 self.capturing = 1
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000394 self.start_audio()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000395 while 1:
396 try:
397 void = fl.check_forms()
398 except StopCapture:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000399 break
400 try:
401 cd, id = self.video.GetCaptureData()
402 except sv.error:
403 sgi.nap(1)
404 continue
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000405 id = id + 2*self.rate
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000406 data = cd.InterleaveFields(1)
407 cd.UnlockCaptureData()
408 t = id*tpf
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000409 if not self.write_frame(t, data):
Guido van Rossume17c6c31993-05-06 16:27:25 +0000410 break
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000411 self.stop_audio()
Guido van Rossume17c6c31993-05-06 16:27:25 +0000412 self.capturing = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000413 self.end_cont()
414 self.reset()
415 self.b_capture.label = saved_label
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000416
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000417 def single_capture(self):
418 self.open_if_closed()
419 self.init_cont()
420 while 1:
421 try:
422 cd, id = self.video.GetCaptureData()
423 break
424 except sv.error:
425 pass
426 sgi.nap(1)
427 data = cd.InterleaveFields(1)
428 cd.UnlockCaptureData()
429 self.end_cont()
430 t = (self.nframes+1) * (1000/25)
431 return self.write_frame(t, data)
432
433 def vcr_capture(self):
434 if not self.vcr:
435 import VCR
436 try:
437 self.vcr = VCR.VCR().init()
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000438 self.vcr.wait()
439 self.vcr.fmmode('dnr')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000440 except VCR.error, msg:
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000441 self.vcr = None
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000442 self.b_capture.set_button(0)
443 fl.show_message('VCR error', str(msg), '')
444 return
445 count = self.getint(self.in_nframes_vcr, 1)
446 if count <= 0: count = 1
447 sleeptime = self.getfloat(self.in_sleeptime, 1.0)
448 for i in range(count):
449 if i > 0:
450 time.sleep(sleeptime)
451 if not self.single_capture():
452 break
453 if not self.vcr.step():
454 break
455
456 # Init/end continuous capture mode
457
458 def init_cont(self):
459 qsize = 1
460 if self.vmode == VM_CONT:
461 self.rate = self.getint(self.in_rate, 2)
462 else:
463 self.rate = 2
464 x, y = self.vout.getsize()
465 info = (SV.RGB8_FRAMES, x, y, qsize, self.rate)
466 info2 = self.video.InitContinuousCapture(info)
467 if info2 <> info:
468 # XXX This is really only debug info
469 print 'Info mismatch: requested', info, 'got', info2
470
471 def end_cont(self):
472 self.video.EndContinuousCapture()
473
474 # Misc stuff
475
476 def settitle(self):
477 gl.winset(self.window)
478 x, y = gl.getsize()
479 title = 'Vb:' + self.vfile + ' (%dx%d)' % (x, y)
480 gl.wintitle(title)
481
482 def get_vformat(self):
483 i = self.c_vformat.get_choice()
484 label = VideoFormatLabels[i-1]
485 format = VideoFormats[i-1]
486 self.vformat = format
487 if self.vformat == '':
488 self.form.freeze_form()
489 self.g_video.hide_object()
490 self.g_cont.hide_object()
491 self.g_burst.hide_object()
492 self.g_single.hide_object()
493 self.form.unfreeze_form()
494 return
495 else:
496 self.g_video.show_object()
497 if self.vmode == VM_CONT:
498 self.g_cont.show_object()
499 elif self.vmode == VM_BURST:
500 self.g_burst.show_object()
501 elif self.vmode == VM_SINGLE:
502 self.g_single.show_object()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000503 #
504 self.rgb = (format[:3] == 'rgb')
505 self.mono = (format == 'mono')
506 self.grey = (format[:4] == 'grey')
507 self.mono_use_thresh = (label == 'mono thresh')
508 s = format[4:]
509 if s:
510 self.greybits = string.atoi(s)
511 else:
512 self.greybits = 8
513 if label == 'grey2 dith':
514 self.greybits = -2
515 #
516 convertor = None
517 if self.grey:
518 if self.greybits == 2:
519 convertor = imageop.grey2grey2
520 elif self.greybits == 4:
521 convertor = imageop.grey2grey4
522 elif self.greybits == -2:
523 convertor = imageop.dither2grey2
524 self.convertor = convertor
525
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000526 def get_aformat(self):
527 self.reset()
528 self.aformat = self.c_aformat.get_choice()
529 if self.aformat == A_OFF:
530 self.g_audio.hide_object()
531 else:
532 self.g_audio.show_object()
533
534 def open_if_closed(self):
535 if not self.vout:
536 self.open_video()
537 if not self.aout:
538 self.open_audio()
539
540 # File I/O handling
541
542 def open_video(self):
543 self.close_video()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000544 gl.winset(self.window)
545 x, y = gl.getsize()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000546 vout = VFile.VoutFile().init(self.vfile)
547 vout.setformat(self.vformat)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000548 vout.setsize(x, y)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000549 if self.vmode == VM_BURST:
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000550 vout.setpf((1, -2))
551 vout.writeheader()
552 self.vout = vout
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000553 self.nframes = 0
554 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000555
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000556 def write_frame(self, t, data):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000557 if not self.vout:
558 gl.ringbell()
559 return
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000560 if self.convertor:
561 data = self.convertor(data, len(data), 1)
562 elif self.mono:
563 if self.mono_use_thresh:
564 data = imageop.grey2mono(data, \
565 len(data), 1,\
566 self.mono_thresh)
567 else:
568 data = imageop.dither2mono(data, \
569 len(data), 1)
570 try:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000571 self.vout.writeframe(int(t), data, None)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000572 except IOError, msg:
573 if msg == (0, 'Error 0'):
574 msg = 'disk full??'
575 fl.show_message('IOError', str(msg), '')
576 return 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000577 self.nframes = self.nframes + 1
578 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000579 return 1
580
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000581 def close_video(self):
582 if not self.vout:
583 return
584 self.nframes = 0
585 self.t_nframes.label = ''
Guido van Rossumc5a14331993-05-07 11:20:07 +0000586 try:
587 self.vout.close()
588 except IOError, msg:
589 if msg == (0, 'Error 0'):
590 msg = 'disk full??'
591 fl.show_message('IOError', str(msg), '')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000592 self.vout = None
593
594 # Watch cursor handling
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000595
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000596 def setwatch(self):
597 gl.winset(self.form.window)
598 gl.setcursor(WATCH, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000599 gl.winset(self.window)
600 gl.setcursor(WATCH, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000601
602 def setarrow(self):
603 gl.winset(self.form.window)
604 gl.setcursor(ARROW, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000605 gl.winset(self.window)
606 gl.setcursor(ARROW, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000607
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000608 # Numeric field handling
609
610 def getint(self, field, default):
611 try:
612 value = string.atoi(field.get_input())
613 except string.atoi_error:
614 value = default
615 field.set_input(`value`)
616 return value
617
618 def getfloat(self, field, default):
619 try:
620 value = float(eval(field.get_input()))
621 except:
622 value = float(default)
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000623 field.set_input(`value`)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000624 return value
625
626 # Audio stuff
627
628 def open_audio(self):
629 if self.aformat == A_OFF:
630 return
631 import aifc
632 import al
633 import AL
634 import thread
635 self.close_audio()
636 params = [AL.INPUT_RATE, 0]
637 al.getparams(AL.DEFAULT_DEVICE, params)
638 rate = params[1]
639 self.aout = aifc.open(self.afile, 'w')
640 if self.aformat in (A_16_STEREO, A_8_STEREO):
641 nch = AL.STEREO
642 else:
643 nch = AL.MONO
644 if self.aformat in (A_16_STEREO, A_16_MONO):
645 width = AL.SAMPLE_16
646 else:
647 width = AL.SAMPLE_8
648 self.aout.setnchannels(nch)
649 self.aout.setsampwidth(width)
650 self.aout.setframerate(rate)
651 self.aout.writeframes('')
652 c = al.newconfig()
653 c.setqueuesize(8000)
654 c.setchannels(nch)
655 c.setwidth(width)
656 self.aport = al.openport('Vb audio record', 'r', c)
657 self.audio_stop = 0
658 self.audio_ok = 0
659 self.audio_busy = 1
660 thread.start_new_thread(self.record_audio, ())
661
662 def start_audio(self):
663 if self.aformat == A_OFF:
664 return
665 self.audio_ok = 1
666
667 def record_audio(self, *args):
668 # This function runs in a separate thread
669 # Currently no semaphores are used
670 while not self.audio_stop:
671 data = self.aport.readsamps(4000)
672 if self.audio_ok:
673 self.aout.writeframesraw(data)
674 data = None
675 self.audio_busy = 0
676
677 def stop_audio(self):
678 self.audio_ok = 0
679
680 def close_audio(self):
681 if self.aout:
682 self.audio_ok = 0
683 self.audio_stop = 1
684 while self.audio_busy:
685 time.sleep(0.1)
686 self.aout.close()
687 self.aout = None
688 if self.aport:
689 self.aport.closeport()
690 self.aport = None
691
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000692
693try:
694 main()
695except KeyboardInterrupt:
696 print '[Interrupt]'
697 sys.exit(1)