Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Voice Recording Application Using MCI

0.00/5 (No votes)
3 Oct 2004 1  
Record your voice using microphone.

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:

  1. Open a waveform-audio device with a new file for recording.
  2. Once the device is opened successfully; get the device ID.
  3. Begin recording and record for the specified number of milliseconds. Wait for recording to complete before continuing.
  4. Assume the default time format for the waveform-audio device (milliseconds).
  5. Play the recording and query user to save the file.
  6. 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;

    // Open a waveform-audio device with a new file for recording.

    mciOpenParms.lpstrDeviceType = "waveaudio";
    mciOpenParms.lpstrElementName = "";
    if (dwReturn = mciSendCommand(0, MCI_OPEN,
        MCI_OPEN_ELEMENT | MCI_OPEN_TYPE, 
        (DWORD)(LPVOID) &mciOpenParms))
    {
        // Failed to open device; don't close it, just return error.

        return (dwReturn);
    }

    // The device opened successfully; get the device ID.

    wDeviceID = mciOpenParms.wDeviceID;

    // 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).

    mciRecordParms.dwTo = dwMilliSeconds;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD, 
        MCI_TO | MCI_WAIT, (DWORD)(LPVOID) &mciRecordParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    // Play the recording and query user to save the file.

    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);
    }

    // Save the recording to a file named TEMPFILE.WAV. Wait for

    // the operation to complete before continuing.

    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

  1. Microsoft Developer Network (MSDN)

My Contact

  1. chakkaradeepcc@yahoo.com

Note

  1. SAPI - Speech Application Programmer Interface.
  2. MCI - Multimedia Control Interface.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here