mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Add audio recording
This commit is contained in:
parent
3accf984b3
commit
8a861be7f3
1 changed files with 47 additions and 1 deletions
|
@ -75,11 +75,19 @@ def main():
|
||||||
elif opt == '-t':
|
elif opt == '-t':
|
||||||
TIME = string.atoi(arg)
|
TIME = string.atoi(arg)
|
||||||
|
|
||||||
|
if args[2:]:
|
||||||
|
sys.stderr.write('usage: Vrec [options] [file [audiofile]]\n')
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
if args:
|
if args:
|
||||||
filename = args[0]
|
filename = args[0]
|
||||||
else:
|
else:
|
||||||
filename = 'film.video'
|
filename = 'film.video'
|
||||||
|
|
||||||
|
if args[1:] and not audio:
|
||||||
|
sys.stderr.write('-a turned on by appearance of 2nd file\n')
|
||||||
|
audio = 1
|
||||||
|
|
||||||
if audio:
|
if audio:
|
||||||
if args[1:]:
|
if args[1:]:
|
||||||
audiofilename = args[1]
|
audiofilename = args[1]
|
||||||
|
@ -171,6 +179,8 @@ def record(v, filename, audiofilename):
|
||||||
vout.writeheader()
|
vout.writeheader()
|
||||||
buffer = []
|
buffer = []
|
||||||
thread.start_new_thread(saveframes, (vout, buffer))
|
thread.start_new_thread(saveframes, (vout, buffer))
|
||||||
|
if audiofilename:
|
||||||
|
initaudio(audiofilename, buffer)
|
||||||
gl.wintitle('(rec) ' + filename)
|
gl.wintitle('(rec) ' + filename)
|
||||||
v.StartCapture()
|
v.StartCapture()
|
||||||
t0 = time.millitimer()
|
t0 = time.millitimer()
|
||||||
|
@ -202,9 +212,9 @@ def saveframes(vout, buffer):
|
||||||
time.millisleep(10)
|
time.millisleep(10)
|
||||||
else:
|
else:
|
||||||
x = buffer[0]
|
x = buffer[0]
|
||||||
del buffer[0]
|
|
||||||
if not x:
|
if not x:
|
||||||
break
|
break
|
||||||
|
del buffer[0]
|
||||||
data, t = x
|
data, t = x
|
||||||
vout.writeframe(t, data, None)
|
vout.writeframe(t, data, None)
|
||||||
del data
|
del data
|
||||||
|
@ -212,6 +222,42 @@ def saveframes(vout, buffer):
|
||||||
vout.close()
|
vout.close()
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize audio recording
|
||||||
|
|
||||||
|
AQSIZE = 8000
|
||||||
|
|
||||||
|
def initaudio(filename, buffer):
|
||||||
|
import thread, aiff
|
||||||
|
afile = aiff.Aiff().init(filename, 'w')
|
||||||
|
afile.nchannels = AL.MONO
|
||||||
|
afile.sampwidth = AL.SAMPLE_8
|
||||||
|
params = [AL.INPUT_RATE, 0]
|
||||||
|
al.getparams(AL.DEFAULT_DEVICE, params)
|
||||||
|
print 'audio sampling rate =', params[1]
|
||||||
|
afile.samprate = params[1]
|
||||||
|
c = al.newconfig()
|
||||||
|
c.setchannels(AL.MONO)
|
||||||
|
c.setqueuesize(AQSIZE)
|
||||||
|
c.setwidth(AL.SAMPLE_8)
|
||||||
|
aport = al.openport(filename, 'r', c)
|
||||||
|
thread.start_new_thread(audiorecord, (afile, aport, buffer))
|
||||||
|
|
||||||
|
|
||||||
|
# Thread to record audio samples
|
||||||
|
|
||||||
|
# XXX should use writesampsraw for efficiency, but then destroy doesn't
|
||||||
|
# XXX seem to set the #samples in the header correctly
|
||||||
|
|
||||||
|
def audiorecord(afile, aport, buffer):
|
||||||
|
while buffer[-1:] <> [None]:
|
||||||
|
data = aport.readsamps(AQSIZE/2)
|
||||||
|
## afile.writesampsraw(data)
|
||||||
|
afile.writesamps(data)
|
||||||
|
del data
|
||||||
|
afile.destroy()
|
||||||
|
print 'Done writing audio'
|
||||||
|
|
||||||
|
|
||||||
# Don't forget to call the main program
|
# Don't forget to call the main program
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue