How To Set MCI Wave Audio Recording Format

ID: Q152180


The information in this article applies to:


SUMMARY

All parameters in the MCI_WAVE_SET_PARMS struct that apply to recording wave audio should be set at the same time. If some recording parameters are set at one time and the balance of the recording parameters are set at a later time, recording will proceed at a default setting of 8-bits per sample, mono, and 11-kHz sampling.


MORE INFORMATION

The members of the MCI_WAVE_SET_PARMS struct that pertain to recording are wFormatTag, wBitsPerSample, nChannels, nSamplesPerSec, nAvgBytesPerSec, and nBlockAlign. All of these should be set in a single call to mciSendCommand in order to achieve recording under the desired settings.

Because nBlockAlign is usually computed using information from wBitsPerSample and nChannels, it may not seem necessary to set any members other than wFormatTag, wBitsPerSample, and nChannels in conjunction with the setting of nBlockAlign. Leaving the other two pertinent members, nSamplesPerSec and nAvgBytesPerSec, to be set later, however, results in an error message at both the initial setting and the later setting of parameters. The error message states "The parameter is out of range for the specified command." Recording will then proceed at a default setting of 8-bits per sample, mono, and 11-kHz sampling.

Following is a sample of setting all the recording members of the MCI_WAVE_SET_PARMS struct properly at once to get the desired 16-bit stereo recording accomplished at 44-kHz sampling:


   //#include <mmsystem.h> at the beginning of the program.
   //Link with mmsystem.lib for 16-bit code, or
   //link with winmm.lib for 32-bit code.

   MCI_WAVE_SET_PARMS set_parms;
   MCI_OPEN_PARMS          open_parms;
   DWORD                            dwReturn;
   UINT                                 wave_device_id;
   char                                  buffer[128];

   // Open the wave audio device.
   open_parms.lpstrDeviceType = "waveaudio";
   open_parms.lpstrElementName = "";

   if (dwReturn = mciSendCommand( 0, MCI_OPEN, MCI_OPEN_TYPE |
         MCI_OPEN_ELEMENT, (DWORD) (LPVOID) &open_parms))
   {
       mciGetErrorString(dwReturn, buffer, sizeof (buffer));
       MessageBox( NULL, buffer, "MCI_OPEN",
              MB_ICONEXCLAMATION | MB_OK);
   }
   else
   {
       MessageBox( NULL, "Open Succeeded", "MCI_OPEN",
              MB_ICONEXCLAMATION | MB_OK);
   }

   // Note the wave audio device ID
   wave_device_id = open_parms.wDeviceID;

   // Set PCM format of recording.
   set_parms.wFormatTag = WAVE_FORMAT_PCM;
   set_parms.wBitsPerSample = 16;
   set_parms.nChannels = 2;
   set_parms.nSamplesPerSec = 44100;
   set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample)/8) *
                               set_parms.nChannels *
                               set_parms.nSamplesPerSec;
   set_parms.nBlockAlign = ((set_parms.wBitsPerSample)/8) *
                               set_parms.nChannels;

   if (dwReturn = mciSendCommand( wave_device_id, MCI_SET, MCI_WAIT |
                                   MCI_WAVE_SET_FORMATTAG |
                                   MCI_WAVE_SET_BITSPERSAMPLE |
                                   MCI_WAVE_SET_CHANNELS |
                                   MCI_WAVE_SET_SAMPLESPERSEC |

                                   MCI_WAVE_SET_AVGBYTESPERSEC |
                                   MCI_WAVE_SET_BLOCKALIGN,
                                   (DWORD)(LPVOID)&set_parms))
   {
       mciGetErrorString(dwReturn, buffer, sizeof(buffer));
       MessageBox( NULL, buffer, "MCI_SET",
              MB_ICONEXCLAMATION | MB_OK);
   }
   else
   {
       MessageBox( NULL, "MCI_WAVE_SET Succeeded", "MCI_SET",
              MB_ICONEXCLAMATION | MB_OK);
   }

   //Close the wave device.
   if (dwReturn = mciSendCommand( wave_device_id, MCI_CLOSE,
              (DWORD)NULL, (DWORD)NULL))
   {
       mciGetErrorString(dwReturn, buffer, sizeof (buffer));
       MessageBox( NULL, buffer, "MCI_CLOSE",
              MB_ICONEXCLAMATION | MB_OK);
   }
   else
   {
       MessageBox( NULL, "MCI_CLOSE Succeeded", "MCI_CLOSE",
              MB_ICONEXCLAMATION | MB_OK);
   } 


Following is behavior that will result in a default recording of 8-bits per sample, mono, and 11-kHz sampling, because the settings of the recording parameters are done in two parts:


   // Set PCM format recording, Part 1.
   set_parms.wFormatTag = WAVE_FORMAT_PCM;
   set_parms.wBitsPerSample = 16;
   set_parms.nChannels = 2;
   set_parms.nBlockAlign = ((set_parms.wBitsPerSample)/8) *
                               set_parms.nChannels;

   if (dwReturn = mciSendCommand( wave_device_id, MCI_SET, MCI_WAIT |
                                   MCI_WAVE_SET_FORMATTAG |
                                   MCI_WAVE_SET_BITSPERSAMPLE |
                                   MCI_WAVE_SET_CHANNELS |
                                   MCI_WAVE_SET_BLOCKALIGN,
                                   (DWORD)(LPVOID)&set_parms))
   {
       mciGetErrorString(dwReturn, buffer, sizeof(buffer));
       MessageBox( NULL, buffer, "MCI_WAVE_SET_1", MB_OK);
   }
   else
    }
       MessageBox( NULL, "MCI_WAVE_SET-1 Succeeded", "MCI_SET",
                                   MB_OK);
    }

    // Set PCM format recording, Part 2.
    set_parms.nSamplesPerSec = 44100;
    set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample)/8) *
                                set_parms.nChannels *
                                set_parms.nSamplesPerSec;

    if (dwReturn = mciSendCommand( wave_device_id, MCI_SET, MCI_WAIT |
                                    MCI_WAVE_SET_SAMPLESPERSEC |
                                    MCI_WAVE_SET_AVGBYTESPERSEC,
                                    (DWORD)(LPVOID)&set_parms))
    {
        mciGetErrorString(dwReturn, buffer, sizeof(buffer));
        MessageBox( NULL, buffer, "MCI_WAVE_SET_2", MB_OK);
    }
    else
    {
        MessageBox( NULL, "MCI_WAVE_SET_2 Succeeded", "MCI_SET",
                                    MB_OK);
    } 

Additional query words: kbsound 3.11 4.00


Keywords          : kbmm MMWave 
Version           : 3.11 4.00 | 3.51
Platform          : NT WINDOWS 
Issue type        : 

Last Reviewed: March 9, 1999