blob: fd09d2879c6580dc1792dc5de08ede9e2e4a84f2 [file] [log] [blame]
Guido van Rossume4bddea1991-10-30 11:52:48 +00001import AL
2import al
3import sys
4import vtime
5import socket
6import time
7
8
9SLEEPTIME = 500 # 500 ms sleeps
10SAMPLEFREQ = 16000 # 16Khz samples
11SAMPLERATE = AL.RATE_16000
12NEEDBUFFERED = SAMPLEFREQ # Buffer 1 second of sound
13BUFFERSIZE = NEEDBUFFERED*4 # setqueuesize() par for 2 second sound
14
15AVSYNCPORT = 10000 # Port for time syncing
16AVCTLPORT = 10001 # Port for record start/stop
17
18def main():
19 if len(sys.argv) <> 3:
20 print 'Usage: ', sys.argv[0], 'videohostname soundfile'
21 sys.exit(1)
22 #
23 ofile = open(sys.argv[2], 'w')
24 #
25 globaltime = vtime.VTime().init(0,sys.argv[1],AVSYNCPORT)
26 #
27 ctl = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
28 ctl.bind((socket.gethostname(),AVCTLPORT))
29 #
30 inp = openmic()
31 #
32 out = 0 # Open aiff file
33 #
34 while 1:
35 if mainloop(None, ctl, inp, out, globaltime):
36 break
37 if mainloop(ofile, ctl, inp, out, globaltime):
38 break
39 pass # Close aiff file
40 sys.exit(0)
41#
42def openmic():
43 conf = al.newconfig()
44 conf.setqueuesize(BUFFERSIZE)
45 conf.setwidth(AL.SAMPLE_16)
46 conf.setchannels(AL.MONO)
47 return al.openport('micr','r',conf)
48#
49def mainloop(ofile, ctl, inp, out, globaltime):
50 #
51 # Wait for sync packet, keeping 1-2 seconds of sound in the
52 # buffer
53 #
54 totsamps = 0
55 totbytes = 0
56 starttime = time.millitimer()
57 while 1:
58 time.millisleep(SLEEPTIME)
59 if ctl.avail():
60 break
61 nsamples = inp.getfilled()-NEEDBUFFERED
62 if nsamples>0:
63 data = inp.readsamps(nsamples)
64 totsamps = totsamps + nsamples
65 totbytes = totbytes + len(data)
66 if ofile <> None:
67 ofile.write(data)
68 #
69 # Compute his starttime and the timestamp of the first byte in the
70 # buffer. Discard all buffered data upto his starttime
71 #
72 startstop,histime = eval(ctl.recv(100))
Guido van Rossumf47d0481992-02-11 14:50:22 +000073 if (ofile == None and startstop == 0) or \
74 (ofile <> None and startstop == 1):
Guido van Rossume4bddea1991-10-30 11:52:48 +000075 print 'Sync error: saving=',save,' request=',startstop
76 sys.exit(1)
77 filllevel = inp.getfilled()
78 filltime = time.millitimer()
79 filltime = filltime - filllevel / (SAMPLEFREQ/1000)
80 starttime = globaltime.his2mine(histime)
81 nsamples = starttime - filltime
82 if nsamples < 0:
83 print 'Start/stop signal came too late'
84 sys.exit(1)
85 nsamples = nsamples * (SAMPLEFREQ / 1000)
86 data = inp.readsamps(nsamples)
87 totsamps = totsamps + nsamples
88 totbytes = totbytes + len(data)
89 print 'Time: ', time.millitimer()-starttime, ', Bytes: ', totbytes, ', Samples: ', totsamps
90 if ofile <> None:
91 ofile.write(data)
Guido van Rossumf47d0481992-02-11 14:50:22 +000092 return (startstop == 2)
Guido van Rossume4bddea1991-10-30 11:52:48 +000093
94main()