Introduction
Compared to my previous tutorial on recording voice using SAPI, this application records your voice through a microphone and saves it to a temporary file after you speak for 10 seconds.
Just wait for my next tutorial which will record your voice till you press "Stop" and not for seconds. This tutorial just gives you an idea on how to use MCI, the Media Control Interface.
Media Control Interface
The Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device.
Using Code
The recording scenario consists of the following steps:
- Open a waveform-audio device with a new file for recording.
- Once the device is opened successfully; get the device ID.
- Begin recording and record for the specified number of milliseconds. Wait for recording to complete before continuing.
- Assume the default time format for the waveform-audio device (milliseconds).
- Play the recording and query user to save the file.
- Save the recording to a file named TEMPFILE.WAV. Wait for the operation to complete before continuing.
See how simple the operation is, the code snippet for the above steps is as follows:
DWORD CMyRecordDlg::RecordWAVEFile(DWORD dwMilliSeconds)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_RECORD_PARMS mciRecordParms;
MCI_SAVE_PARMS mciSaveParms;
MCI_PLAY_PARMS mciPlayParms;
mciOpenParms.lpstrDeviceType = "waveaudio";
mciOpenParms.lpstrElementName = "";
if (dwReturn = mciSendCommand(0, MCI_OPEN,
MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,
(DWORD)(LPVOID) &mciOpenParms))
{
return (dwReturn);
}
wDeviceID = mciOpenParms.wDeviceID;
mciRecordParms.dwTo = dwMilliSeconds;
if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD,
MCI_TO | MCI_WAIT, (DWORD)(LPVOID) &mciRecordParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
mciPlayParms.dwFrom = 0L;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY,
MCI_FROM | MCI_WAIT, (DWORD)(LPVOID) &mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
if (MessageBox("Do you want to save this recording?",
"", MB_YESNO) == IDNO)
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
mciSaveParms.lpfilename = "tempfile.wav";
if (dwReturn = mciSendCommand(wDeviceID, MCI_SAVE,
MCI_SAVE_FILE | MCI_WAIT, (DWORD)(LPVOID) &mciSaveParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
return (0L);
}
Reference
- Microsoft Developer Network (MSDN)
My Contact
- chakkaradeepcc@yahoo.com
Note
- SAPI - Speech Application Programmer Interface.
- MCI - Multimedia Control Interface.