Capturing audio using MCI
by Richard Russell, October 2010
The article Inputting real-time audio describes how to input audio data and make it available to your program for display, analysis or capture. If you simply need to record audio to a WAV file you can use that technique, but a much easier method is to use the capture facilities built into the Windows Media Control Interface (MCI).
The potential disadvantage of this method is that MCI is an old technology and it's not clear to what extent it will continue to be supported. For example in some versions of the Platform SDK Documentation the mciSendString function is bluntly stated to be “Not supported in 64-bit versions” although that remark has been removed from the current MSDN article.
The code below illustrates how to capture audio data to a file:
REM Record audio using MCI wavfile$ = @usr$ + "capture.wav" bitspersample% = 16 channels% = 2 samplespersec% = 44100 alignment% = bitspersample% * channels% / 8 bytespersec% = alignment% * samplespersec% params$ = " bitspersample " + STR$(bitspersample%) + \ \ " channels " + STR$(channels%) + \ \ " alignment " + STR$(alignment%) + \ \ " samplespersec " + STR$(samplespersec%) + \ \ " bytespersec " + STR$(bytespersec%) SYS "mciSendString", "close all", 0, 0, 0 SYS "mciSendString", "open new type waveaudio alias capture", 0, 0, 0 SYS "mciSendString", "set capture" + params$, 0, 0, 0 TO res% IF res% ERROR 100, "Couldn't set capture parameters: " + STR$(res% AND &FFFF) PRINT "Press SPACE to start recording..." REPEAT UNTIL INKEY(1) = 32 SYS "mciSendString", "record capture", 0, 0, 0 TO res% IF res% ERROR 100, "Couldn't start audio capture: " + STR$(res% AND &FFFF) PRINT "Recording, press SPACE to stop..." REPEAT UNTIL INKEY(1) = 32 SYS "mciSendString", "stop capture", 0, 0, 0 SYS "mciSendString", "save capture " + wavfile$, 0, 0, 0 TO res% IF res% ERROR 100, "Couldn't save to WAV file: " + STR$(res% AND &FFFF) SYS "mciSendString", "delete capture", 0, 0, 0 SYS "mciSendString", "close capture", 0, 0, 0 PRINT "Captured audio is stored in " wavfile$ END