blob: da868f1fbd94e5cab13b9a84453f2cead4df4d52 [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):
231 self.vmode = self.c_vmode.get_choice()
232 self.form.freeze_form()
233 self.g_cont.hide_object()
234 self.g_burst.hide_object()
235 self.g_single.hide_object()
236 self.g_vcr.hide_object()
237 if self.vmode == VM_CONT:
238 self.g_cont.show_object()
239 elif self.vmode == VM_BURST:
240 self.g_burst.show_object()
241 elif self.vmode == VM_SINGLE:
242 self.g_single.show_object()
243 elif self.vmode == VM_VCR:
244 self.g_vcr.show_object()
245 self.form.unfreeze_form()
246
247 def cb_vfile(self, *args):
248 filename = self.vfile
249 hd, tl = os.path.split(filename)
250 filename = fl.file_selector('Video save file:', hd, '', tl)
251 if filename:
252 self.reset()
253 hd, tl = os.path.split(filename)
254 if hd == os.getcwd():
255 filename = tl
256 self.vfile = filename
257
258 # Video mode specific video controls
259
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000260 def cb_rate(self, *args):
261 pass
262
263 def cb_drop(self, *args):
264 self.rebindvideo()
265
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000266 def cb_maxmem(self, *args):
267 pass
268
269 def cb_nframes(self, *args):
270 pass
271
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000272 def cb_fps(self, *args):
273 pass
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000274
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000275 def cb_nframes_vcr(self, *args):
276 pass
277
278 def cb_sleeptime(self, *args):
279 pass
280
281 # Audio controls: format, file
282
283 def cb_aformat(self, *args):
284 self.get_aformat()
285
286 def cb_afile(self, *args):
287 filename = self.afile
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000288 hd, tl = os.path.split(filename)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000289 filename = fl.file_selector('Audio save file:', hd, '', tl)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000290 if filename:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000291 self.reset()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000292 hd, tl = os.path.split(filename)
293 if hd == os.getcwd():
294 filename = tl
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000295 self.afile = filename
296
297 # General controls: capture, reset, play, quit
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000298
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000299 def cb_capture(self, *args):
Guido van Rossumc5a14331993-05-07 11:20:07 +0000300 if self.capturing:
301 raise StopCapture
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000302 if not self.b_capture.get_button():
303 return
304 if not self.video or not self.vformat:
305 gl.ringbell()
306 return
307 if self.vmode == VM_CONT:
308 self.cont_capture()
309 elif self.vmode == VM_BURST:
310 self.burst_capture()
311 elif self.vmode == VM_SINGLE:
312 self.single_capture()
313 elif self.vmode == VM_VCR:
314 self.vcr_capture()
315
316 def cb_reset(self, *args):
317 self.reset()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000318
319 def cb_play(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000320 sts = os.system('Vplay -q ' + self.vfile + ' &')
Guido van Rossumc5a14331993-05-07 11:20:07 +0000321
322 def cb_quit(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000323 self.close()
324
325 # Capture routines
Guido van Rossumc5a14331993-05-07 11:20:07 +0000326
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000327 def burst_capture(self):
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000328 self.setwatch()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000329 gl.winset(self.window)
330 x, y = gl.getsize()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000331 vformat = SV.RGB8_FRAMES
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000332 nframes = self.getint(self.in_nframes, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000333 if nframes == 0:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000334 maxmem = self.getint(self.in_maxmem, 1.0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000335 memsize = int(maxmem * 1024 * 1024)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000336 nframes = self.calcnframes()
337 info = (vformat, x, y, nframes, 1)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000338 try:
339 info2, data, bitvec = self.video.CaptureBurst(info)
340 except sv.error, msg:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000341 self.b_capture.set_button(0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000342 self.setarrow()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000343 fl.show_message('Capture error:', str(msg), '')
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000344 return
Guido van Rossumc5a14331993-05-07 11:20:07 +0000345 if info <> info2: print info, '<>', info2
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000346 self.save_burst(info2, data, bitvec)
347 self.setarrow()
348
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000349 def calcnframes(self):
350 gl.winset(self.window)
351 x, y = gl.getsize()
352 pixels = x*y
353 pixels = pixels/2 # XXX always assume fields
354 if self.mono or self.grey:
355 n = memsize/pixels
356 else:
357 n = memsize/(4*pixels)
358 return max(1, n)
359
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000360 def save_burst(self, info, data, bitvec):
361 (vformat, x, y, nframes, rate) = info
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000362 self.open_if_closed()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000363 fieldsize = x*y/2
364 nskipped = 0
365 realframeno = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000366 tpf = 1000 / 50.0 # XXX
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000367 for frameno in range(0, nframes*2):
368 if frameno <> 0 and \
369 bitvec[frameno] == bitvec[frameno-1]:
370 nskipped = nskipped + 1
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000371 continue
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000372 #
373 # Save field.
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000374 # XXX Works only for fields and top-to-bottom
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000375 #
376 start = frameno*fieldsize
377 field = data[start:start+fieldsize]
378 realframeno = realframeno + 1
379 fn = int(realframeno*tpf)
380 if not self.write_frame(fn, field):
381 break
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000382
383 def cont_capture(self):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000384 saved_label = self.b_capture.label
385 self.b_capture.label = 'Stop\n' + saved_label
386 self.open_if_closed()
387 self.init_cont()
388 fps = 59.64 # Fields per second
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000389 # XXX (fps of Indigo monitor, not of PAL or NTSC!)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000390 tpf = 1000.0 / fps # Time per field in msec
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000391 self.capturing = 1
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000392 self.start_audio()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000393 while 1:
394 try:
395 void = fl.check_forms()
396 except StopCapture:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000397 break
398 try:
399 cd, id = self.video.GetCaptureData()
400 except sv.error:
401 sgi.nap(1)
402 continue
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000403 id = id + 2*self.rate
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000404 data = cd.InterleaveFields(1)
405 cd.UnlockCaptureData()
406 t = id*tpf
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000407 if not self.write_frame(t, data):
Guido van Rossume17c6c31993-05-06 16:27:25 +0000408 break
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000409 self.stop_audio()
Guido van Rossume17c6c31993-05-06 16:27:25 +0000410 self.capturing = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000411 self.end_cont()
412 self.reset()
413 self.b_capture.label = saved_label
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000414
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000415 def single_capture(self):
416 self.open_if_closed()
417 self.init_cont()
418 while 1:
419 try:
420 cd, id = self.video.GetCaptureData()
421 break
422 except sv.error:
423 pass
424 sgi.nap(1)
425 data = cd.InterleaveFields(1)
426 cd.UnlockCaptureData()
427 self.end_cont()
428 t = (self.nframes+1) * (1000/25)
429 return self.write_frame(t, data)
430
431 def vcr_capture(self):
432 if not self.vcr:
433 import VCR
434 try:
435 self.vcr = VCR.VCR().init()
436 except VCR.error, msg:
437 self.b_capture.set_button(0)
438 fl.show_message('VCR error', str(msg), '')
439 return
440 count = self.getint(self.in_nframes_vcr, 1)
441 if count <= 0: count = 1
442 sleeptime = self.getfloat(self.in_sleeptime, 1.0)
443 for i in range(count):
444 if i > 0:
445 time.sleep(sleeptime)
446 if not self.single_capture():
447 break
448 if not self.vcr.step():
449 break
450
451 # Init/end continuous capture mode
452
453 def init_cont(self):
454 qsize = 1
455 if self.vmode == VM_CONT:
456 self.rate = self.getint(self.in_rate, 2)
457 else:
458 self.rate = 2
459 x, y = self.vout.getsize()
460 info = (SV.RGB8_FRAMES, x, y, qsize, self.rate)
461 info2 = self.video.InitContinuousCapture(info)
462 if info2 <> info:
463 # XXX This is really only debug info
464 print 'Info mismatch: requested', info, 'got', info2
465
466 def end_cont(self):
467 self.video.EndContinuousCapture()
468
469 # Misc stuff
470
471 def settitle(self):
472 gl.winset(self.window)
473 x, y = gl.getsize()
474 title = 'Vb:' + self.vfile + ' (%dx%d)' % (x, y)
475 gl.wintitle(title)
476
477 def get_vformat(self):
478 i = self.c_vformat.get_choice()
479 label = VideoFormatLabels[i-1]
480 format = VideoFormats[i-1]
481 self.vformat = format
482 if self.vformat == '':
483 self.form.freeze_form()
484 self.g_video.hide_object()
485 self.g_cont.hide_object()
486 self.g_burst.hide_object()
487 self.g_single.hide_object()
488 self.form.unfreeze_form()
489 return
490 else:
491 self.g_video.show_object()
492 if self.vmode == VM_CONT:
493 self.g_cont.show_object()
494 elif self.vmode == VM_BURST:
495 self.g_burst.show_object()
496 elif self.vmode == VM_SINGLE:
497 self.g_single.show_object()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000498 #
499 self.rgb = (format[:3] == 'rgb')
500 self.mono = (format == 'mono')
501 self.grey = (format[:4] == 'grey')
502 self.mono_use_thresh = (label == 'mono thresh')
503 s = format[4:]
504 if s:
505 self.greybits = string.atoi(s)
506 else:
507 self.greybits = 8
508 if label == 'grey2 dith':
509 self.greybits = -2
510 #
511 convertor = None
512 if self.grey:
513 if self.greybits == 2:
514 convertor = imageop.grey2grey2
515 elif self.greybits == 4:
516 convertor = imageop.grey2grey4
517 elif self.greybits == -2:
518 convertor = imageop.dither2grey2
519 self.convertor = convertor
520
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000521 def get_aformat(self):
522 self.reset()
523 self.aformat = self.c_aformat.get_choice()
524 if self.aformat == A_OFF:
525 self.g_audio.hide_object()
526 else:
527 self.g_audio.show_object()
528
529 def open_if_closed(self):
530 if not self.vout:
531 self.open_video()
532 if not self.aout:
533 self.open_audio()
534
535 # File I/O handling
536
537 def open_video(self):
538 self.close_video()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000539 gl.winset(self.window)
540 x, y = gl.getsize()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000541 vout = VFile.VoutFile().init(self.vfile)
542 vout.setformat(self.vformat)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000543 vout.setsize(x, y)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000544 if self.vmode == VM_BURST:
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000545 vout.setpf((1, -2))
546 vout.writeheader()
547 self.vout = vout
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000548 self.nframes = 0
549 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000550
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000551 def write_frame(self, t, data):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000552 if not self.vout:
553 gl.ringbell()
554 return
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000555 if self.convertor:
556 data = self.convertor(data, len(data), 1)
557 elif self.mono:
558 if self.mono_use_thresh:
559 data = imageop.grey2mono(data, \
560 len(data), 1,\
561 self.mono_thresh)
562 else:
563 data = imageop.dither2mono(data, \
564 len(data), 1)
565 try:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000566 self.vout.writeframe(int(t), data, None)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000567 except IOError, msg:
568 if msg == (0, 'Error 0'):
569 msg = 'disk full??'
570 fl.show_message('IOError', str(msg), '')
571 return 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000572 self.nframes = self.nframes + 1
573 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000574 return 1
575
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000576 def close_video(self):
577 if not self.vout:
578 return
579 self.nframes = 0
580 self.t_nframes.label = ''
Guido van Rossumc5a14331993-05-07 11:20:07 +0000581 try:
582 self.vout.close()
583 except IOError, msg:
584 if msg == (0, 'Error 0'):
585 msg = 'disk full??'
586 fl.show_message('IOError', str(msg), '')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000587 self.vout = None
588
589 # Watch cursor handling
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000590
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000591 def setwatch(self):
592 gl.winset(self.form.window)
593 gl.setcursor(WATCH, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000594 gl.winset(self.window)
595 gl.setcursor(WATCH, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000596
597 def setarrow(self):
598 gl.winset(self.form.window)
599 gl.setcursor(ARROW, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000600 gl.winset(self.window)
601 gl.setcursor(ARROW, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000602
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000603 # Numeric field handling
604
605 def getint(self, field, default):
606 try:
607 value = string.atoi(field.get_input())
608 except string.atoi_error:
609 value = default
610 field.set_input(`value`)
611 return value
612
613 def getfloat(self, field, default):
614 try:
615 value = float(eval(field.get_input()))
616 except:
617 value = float(default)
618 field.set_input(value)
619 return value
620
621 # Audio stuff
622
623 def open_audio(self):
624 if self.aformat == A_OFF:
625 return
626 import aifc
627 import al
628 import AL
629 import thread
630 self.close_audio()
631 params = [AL.INPUT_RATE, 0]
632 al.getparams(AL.DEFAULT_DEVICE, params)
633 rate = params[1]
634 self.aout = aifc.open(self.afile, 'w')
635 if self.aformat in (A_16_STEREO, A_8_STEREO):
636 nch = AL.STEREO
637 else:
638 nch = AL.MONO
639 if self.aformat in (A_16_STEREO, A_16_MONO):
640 width = AL.SAMPLE_16
641 else:
642 width = AL.SAMPLE_8
643 self.aout.setnchannels(nch)
644 self.aout.setsampwidth(width)
645 self.aout.setframerate(rate)
646 self.aout.writeframes('')
647 c = al.newconfig()
648 c.setqueuesize(8000)
649 c.setchannels(nch)
650 c.setwidth(width)
651 self.aport = al.openport('Vb audio record', 'r', c)
652 self.audio_stop = 0
653 self.audio_ok = 0
654 self.audio_busy = 1
655 thread.start_new_thread(self.record_audio, ())
656
657 def start_audio(self):
658 if self.aformat == A_OFF:
659 return
660 self.audio_ok = 1
661
662 def record_audio(self, *args):
663 # This function runs in a separate thread
664 # Currently no semaphores are used
665 while not self.audio_stop:
666 data = self.aport.readsamps(4000)
667 if self.audio_ok:
668 self.aout.writeframesraw(data)
669 data = None
670 self.audio_busy = 0
671
672 def stop_audio(self):
673 self.audio_ok = 0
674
675 def close_audio(self):
676 if self.aout:
677 self.audio_ok = 0
678 self.audio_stop = 1
679 while self.audio_busy:
680 time.sleep(0.1)
681 self.aout.close()
682 self.aout = None
683 if self.aport:
684 self.aport.closeport()
685 self.aport = None
686
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000687
688try:
689 main()
690except KeyboardInterrupt:
691 print '[Interrupt]'
692 sys.exit(1)