Record Audio (Automation)

Graham Fawcett gmfawcett at operamail.com
Fri Feb 22 11:51:32 EST 2002


> > The best thing here would be to write your own Visual Basic app and use the
> > MS Multi Media control...
> > 
> > Write your program to take command line input.. and well, thats about it..
> 
> This shouldn't be necessary as you should be able to use the 
> OLE automation interface to the MCI control from Python directly.
> However, the MCI control has lots of baggage you don't need.
> The mciSendString() API was specifically designed to do 
> multimedia from scripting languages. (Originally Macromind Director's
> MMP files.) If mciSendString isn't already wrapped, you should be able
> to use CallDLL. 

Sam Rushing's DynWin module (
http://www.nightmare.com/~rushing/dynwin/ ) helps to make real clean
code for MCI calls (and other Win32 things). It's not necessary for
MCI calls but I like the cleanness of the code.

Here's a Recording object for you to try, using DynWin.

import os
from dynwin import windll
winmm = windll.module ('winmm')

def send(s):
	 winmm.mciSendString( windll.cstring(s), 0, 0, 0)

class Recording:
	def __init__(self, name, dirpath,
			samplesize = 16, # or 8
			samplerate = 44100, # or try 22050, 11025, ...
			channels = 1): # or 2 for stereo, of course!
		self.name = name
		self.samplesize = samplesize
		self.samplerate = samplerate
		self.channels = channels
		self.filename = '%s.wav' % (os.path.join(dirpath, self.name))
		send('open new type waveaudio alias  %s' % (self.name))
		send('set %s bitspersample %s' % (self.name, self.samplesize))
		send('set %s samplespersec %s' % (self.name, self.samplerate))
		send('set %s channels %s' % (self.name, self.channels))
	def start(self):
		send('record %s' % (self.name))
	def stop(self):
		send('stop %s' % (self.name))
	def save(self):		
		send('save %s %s' % (self.name, self.filename))
		send('close %s' % (self.name))

def test():
	'''record 5 seconds of audio, and save to c:\temp\test.wav'''
	import time
	r = Recording('test', 'c:\\temp')
	r.start()
	time.sleep(5)
	r.stop()
	r.save()

if __name__=='__main__': test()

-- Graham Fawcett
-- Centre for Flexible Learning
-- University of Windsor



More information about the Python-list mailing list