Click here to Skip to main content
Email Password   helpLost your password?

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.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionRegarding License !!
Nikhil_7777
9:36 18 Feb '09  
Hi,

Is your code protected by any License or is it open source. I would like to append this to my project.

Let me know asap

Thanks

Nikhil
GeneralSaving the file in webserver
yogeshkumarkollu
2:31 12 Jan '09  
Can you suggest me how i can save the file in webserver instead of the local system.
GeneralHow do i record only when data is there?
Nikita Agarwal
22:00 9 Apr '08  
I want to record the microphone only when there is any noise on it. How do i detect it? Is there any way?

Thanks
Generalmore tracks
Letizia
2:57 11 Jan '08  
I would rec two tracks from a mixer with two microphones. Is it possible? How can I do? Thanks..

Letizia
GeneralProblem with Device Detection
imadkk1
5:20 2 Nov '06  
From the code i understand, you make sure if the Microphone is plugged in or not.

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);
}
Even if i dont have Micrphone plugged, the above conidtion is not true and it gets a device ID
// The device opened successfully; get the device ID.
wDeviceID = mciOpenParms.wDeviceID;
and goes ahead for recording.

Is this a problem with MCI or it is just an issue with XP64.

I am running the sample on Windows XP 64, MSVC2005

Thanks a lot.

Imad Khan
Questioncan i do this in c#
Kumar Muthu
1:39 29 Oct '05  
Hi,
I read your article and code, very nice. I am currently working in one application in which i would like to record audio, replay and convert audio from one format to another format. could you give me some tips to do.. ? thanks in advance

with regards
muthukumar
AnswerRe: can i do this in c#
chakkaradeepcc
20:49 11 Oct '06  
Me too now in C# and would check that how to do it. I just need to setup my laptop to development environment and a microphone! Smile
AnswerRe: can i do this in c#
BarryDorman
10:29 8 Dec '06  
Sounds like you need DirectShow.NET. You can download it here:

http://sourceforge.net/projects/directshownet/[^]
GeneralRegarding Setaudio
venugopalan
23:17 25 Oct '05  
Hi,

Nice Article. I enjoyed it.
But i got one query.... i need to record in the CCITT format any of the formats in the CCITT. Now how do i set this parameter.

Although i tried using the Setaudio...it still records in the PCM format only. Can u throw me a lifeline here buddy?

How do u record in CCITT format using MCI?

Thanks
Venugopalan
Generalhow can this be acheived in C#
Samar Aarkotti
5:58 13 Oct '05  
I have tried few thing's but did not work for me, would apprciate if you can tell me how to achieve this in C#
GeneralVoice Activating Recording
sajeepra
10:01 6 Jul '05  
Hello,

I want to make a voice activated recording application & save the generated file in mp3 format.Could you please help me ? How can I do it ?
GeneralStop recording..?
mpallavi
20:00 5 Jun '05  
Hello..
I want to allow the user to stop the recording.. On clicking the stop button the recording should stop .. Can you please help .. How to do this?
regards
pallavi
GeneralRe: Stop recording..?
Gernot Frisch
20:56 14 Jan '07  
[code]
// don't wait / don't specify length
if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD,
0 /*|MCI_TO | MCI_WAIT*/, (DWORD)(LPVOID) &mciRecordParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
// Magic

MessageBox(NULL, "Stop", "Recording", MB_OK);
mciSendCommand(wDeviceID, MCI_STOP, 0, NULL);

[/code]
Generalreally good & useful tutorial
nsavara
11:22 16 Jan '05  
This is perhaps the only place where I could find something really useful and easy to understand.

Keep up the great work.

Dont know if this is from MSDN (as suggested by another guy in comment) but I could not find anything as good as this in MSDN or anywhere else.

Lots of Luck...

Nalin
GeneralRe: really good & useful tutorial
chakkaradeepcc
5:53 18 Jan '05  
Hi nsavara,

thanks for the reply......thanks for the comment again.....

with regards,
C.C.Chakkaradeep
GeneralRip off MSDN
HerbDX
0:24 27 Oct '04  
This code is completely ripped off MSDN except for perhaps 1 line.
GeneralRe: Rip off MSDN
Edgar Rocha
11:10 25 Jul '05  
Exactly. In my opinion it must be deleted from CodeProject.

The original from MSDN is here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_recording_with_a_waveform_audio_device.asp


And I suggest the author to read this:

http://msdn.microsoft.com/library/en-us/multimed/htm/legal.asp

Edgar
GeneralChange of parameters...
Antonella Vlassi
0:53 18 Oct '04  
Good job! Smile I also sent you an e-mail... but I did manage to run it finally! I also changed the time of recording time (I wanted it 3 seconds). The question now is whether it is possible to change the parameters of recording, which are:

1.The frequency audio sample rate (now is 11KHz but I want it 16)
2.The audio sample size (now is 8 bit but I want it 16 bit)

Any idea how to do that???? Confused
GeneralRe: Change of parameters...
chakkaradeepcc
21:14 11 Oct '06  
Thanks Smile

Checkout the MCI_WAVE_SET_PARMS structure.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mci_wave_set_parms_str.asp[^]


Last Updated 3 Oct 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010