• C

mm waveinopen

how to record wav file using sdk mm sound api function
if u have some sample code please send some

love
nsenthilAsked:
Who is Participating?
 
toanndsConnect With a Mentor Commented:
I send you high-level answer. I use Borland C++ 4.5 . There're 3 files: record.cpp, record.h and record.rc
//--------------------------------------------------------------
// record.h
//--------------------------------------------------------------
#if !defined(_RECORD_H_)
#define _RECORD_H_

#define TXT_FILENAME      201
#define TXT_TIME            202

#define BTN_OPEN            101
#define TXT_STATUS      106
#define TXT_SAMPLERATE      204
#define TXT_CHANNELS      205
#define TXT_DATAFORMAT      206
#define EDIT_MESSAGE      107
#define BTN_RECORD            102
#define BTN_PLAY            103
#define BTN_STOP            104
#define BTN_SAVE            105

#endif
//---------------------------------------------------------------
// record.cpp
//--------------------------------------------------------------
#include <windows.h>
#include <mmsystem.h>
#include "record.h"

#define MAXRECORDTIME 5000

HINSTANCE hInstance;
HWND hwndMain;
LPSTR lpszWaveFile = "record.wav";

int nCurrCmd;                  // current command
//-----------------------------------------------------------------------------
int EnableControl(int id, int fEnable)
      {
            EnableWindow( GetDlgItem( hwndMain, id), fEnable);
      }
//-----------------------------------------------------------------------------
int DisplayFormat(int nId)
      {
            int nSampleRate;
            int nChannels;
            int nDataFormat;
            int error;
            MCI_STATUS_PARMS mciStatus;

            // get sampling rate
            mciStatus.dwItem = MCI_WAVE_STATUS_SAMPLESPERSEC;
            error = mciSendCommand ( nId,
                                          MCI_STATUS,
                                          MCI_STATUS_ITEM,
                                          (LONG) &mciStatus);
            nSampleRate = mciStatus.dwReturn;
            SetDlgItemInt( hwndMain, TXT_SAMPLERATE, nSampleRate, FALSE );

            // get Channels
            mciStatus.dwItem = MCI_WAVE_STATUS_CHANNELS;
            error = mciSendCommand ( nId,
                                          MCI_STATUS,
                                          MCI_STATUS_ITEM,
                                          (LONG) &mciStatus);
            nChannels = mciStatus.dwReturn;
            SetDlgItemText( hwndMain, TXT_CHANNELS, (nSampleRate==2)? "Stereo":"Mono" );

            // get data format
            mciStatus.dwItem = MCI_WAVE_STATUS_BITSPERSAMPLE;
            error = mciSendCommand ( nId,
                                          MCI_STATUS,
                                          MCI_STATUS_ITEM,
                                          (LONG) &mciStatus);
            nDataFormat = mciStatus.dwReturn;
            SetDlgItemInt( hwndMain, TXT_DATAFORMAT, nDataFormat, FALSE );
      }
//-----------------------------------------------------------------------------
int DisplayMessage(LPSTR lpszMessage)
      {
            int lc, index;

            // Set the caret to the end of edit control
            // because we wanna append the text to it

            // 1. Determine how many line in EDIT control
            lc = SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_GETLINECOUNT, 0, 0);
            // 2. Determine the character index of last line
            index = SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_LINEINDEX, lc - 1, 0);
            // 3. Determine the index of the last character of edit control
            index += SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_LINELENGTH, index, 0);
            // set caret to the end of edit control
            SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_SETSEL, 0, MAKELONG(index, index));
            // send text to edit control
            SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_REPLACESEL, 0, (long) "\r\n");
            SendDlgItemMessage(hwndMain, EDIT_MESSAGE, EM_REPLACESEL, 0, (long) lpszMessage);
      }
//-----------------------------------------------------------------------------
int DisplayStatus(LPSTR lpszStatus)
      {
            SetDlgItemText(hwndMain, TXT_STATUS, lpszStatus);
      }
//-----------------------------------------------------------------------------
int DisplayError(int error)
      {
            char szErrorText[128];

            mciGetErrorString( error, szErrorText, sizeof(szErrorText) );
            MessageBox( hwndMain, szErrorText, 0, MB_OK | MB_ICONHAND);
            DisplayStatus( szErrorText );
      }
//-----------------------------------------------------------------------------
int OpenWave(LPSTR lpstrFileName)
//
//      Open WAVE file that already exists or create new WAVE file
//      Input: filename, empty string if create new WAVE file
//
//      Return device ID or -1 if error
//
      {
            int error;
            MCI_OPEN_PARMS mciOpen;

            // prepare parameters
            mciOpen.lpstrDeviceType = "waveaudio";            // wave
            mciOpen.lpstrElementName= lpstrFileName;      // file name

            // open device -> returns device ID in mciOpen.wDeviceID
            error = mciSendCommand ( 0,                              // MCI_OPEN -> deviceID=0
                                          MCI_OPEN,
                                          MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
                                          (LONG) &mciOpen );
            if ( error ){
                  DisplayError( error );
                  return -1;
            }
            return mciOpen.wDeviceID;
      }
//-----------------------------------------------------------------------------
int RecordWave(int nId)
//
//      Return device ID or -1 if error
//
      {
            int error;
            MCI_RECORD_PARMS mciRec;

            // create new WAVE file
            if ( nId == -1 ) nId = OpenWave("");      // create new file -> filename = ""
            if ( nId == -1 ) return -1;            // error

            // prepare parameters
            mciRec.dwTo = MAXRECORDTIME;
            mciRec.dwCallback = hwndMain;

            // send Record command
            error = mciSendCommand ( nId,
                                          MCI_RECORD,
                                          MCI_TO | MCI_NOTIFY,
                                          (LONG) &mciRec );
            if ( error ){
                  mciSendCommand ( nId, MCI_CLOSE, 0, NULL );      // error -> close
                  DisplayError(error);
                  return -1;
            }

            // disable some buttons
            EnableControl( BTN_RECORD, FALSE );
            EnableControl( BTN_PLAY, FALSE );
            EnableControl( BTN_SAVE, FALSE );
            EnableControl( BTN_OPEN, FALSE );

            DisplayStatus("Recording...");

            return nId;
      }
//-----------------------------------------------------------------------------
int SaveWave(int nId)
      {
            int error;
            MCI_SAVE_PARMS mciSave;

            mciSave.lpfilename = lpszWaveFile;
            error = mciSendCommand( nId,
                        MCI_SAVE,
                        MCI_SAVE_FILE,
                        (DWORD) &mciSave );

            if (error){
                  DisplayError(error);
                  return 0;
            }
            return 1;
      }
//-----------------------------------------------------------------------------
int CloseWave(int nId)
      {
            int error;
            MCI_GENERIC_PARMS mciGen;

            if ( nId == -1) return 1;

            error = mciSendCommand( nId, MCI_CLOSE, MCI_WAIT, (DWORD) &mciGen );
            if (error){
                  DisplayError(error);
                  return 0;
            }
            return 1;
      }
//-----------------------------------------------------------------------------
int PlayWave(int nId)
//
//      Return device ID or -1 if error
//
      {
            int error;
            MCI_PLAY_PARMS mciPlay;

            if ( nId == -1 ) return -1;
            // prepare parameters
            mciPlay.dwCallback = hwndMain;

            // send Record command
            error = mciSendCommand ( nId,
                                          MCI_PLAY,
                                          MCI_NOTIFY,
                                          (LONG) &mciPlay );
            if ( error ){
                  DisplayError(error);
                  return -1;
            }

            // disable some buttons
            EnableControl( BTN_RECORD, FALSE );
            EnableControl( BTN_PLAY, FALSE );
            EnableControl( BTN_SAVE, FALSE );
            EnableControl( BTN_OPEN, FALSE );

            DisplayStatus("Playing...");

            return nId;
      }
//-----------------------------------------------------------------------------
int StopWave(int nId)
      {
            MCI_GENERIC_PARMS mciGen;

            mciGen.dwCallback = hwndMain;
            if ( nId != -1)
                  mciSendCommand( nId, MCI_STOP, MCI_WAIT | MCI_NOTIFY, (DWORD) &mciGen );
            return 1;
      }
//-----------------------------------------------------------------------------
int MCINotify(WPARAM notify, int nId)
      {
            // display message
            switch (notify){
                  case MCI_NOTIFY_ABORTED:
                        DisplayMessage("MCI_NOTIFY_ABORTED");
                        break;

                  case MCI_NOTIFY_SUCCESSFUL:
                        DisplayMessage("MCI_NOTIFY_SUCCESSFUL");
                        break;

                  case MCI_NOTIFY_SUPERSEDED:
                        DisplayMessage("MCI_NOTIFY_SUPERSEDED");
                        break;

                  case MCI_NOTIFY_FAILURE:
                        DisplayMessage("MCI_NOTIFY_FAILURE");
            }

            // action
            switch ( nCurrCmd ){
                  case BTN_RECORD:
                        EnableControl( BTN_RECORD, TRUE );
                        EnableControl( BTN_PLAY, TRUE );
                        EnableControl( BTN_SAVE, TRUE );
                        EnableControl( BTN_OPEN, TRUE );
                        mciSendCommand( nId, MCI_STOP, MCI_WAIT, 0 );
                        DisplayStatus("Done");
                        break;

                  case BTN_SAVE:
                        DisplayStatus("Done");
                        break;

                  case BTN_OPEN:
                        nId = OpenWave(lpszWaveFile);
                        DisplayStatus("Done");
                        break;

                  case BTN_PLAY:
                        EnableControl( BTN_RECORD, TRUE );
                        EnableControl( BTN_PLAY, TRUE );
                        EnableControl( BTN_SAVE, TRUE );
                        EnableControl( BTN_OPEN, TRUE );
                        DisplayStatus("Done");
                        break;

                  case BTN_STOP:
                        EnableControl( BTN_RECORD, TRUE );
                        EnableControl( BTN_PLAY, TRUE );
                        EnableControl( BTN_SAVE, TRUE );
                        EnableControl( BTN_OPEN, TRUE );
                        DisplayStatus("Done");

                        break;
            }
      }
//-----------------------------------------------------------------------------
BOOL CALLBACK MainDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
      {
            static int nId = -1;

            switch ( msg ){
                  case WM_INITDIALOG:
                        hwndMain = hwnd;
                        return FALSE;

                  case WM_COMMAND:
                        switch ( wParam ){
                              case IDCANCEL:
                                    EndDialog( hwnd, 1);
                                    return FALSE;

                              case BTN_RECORD:
                                    RecordWave( nId );
                                    nCurrCmd = wParam;
                                    return FALSE;

                              case BTN_SAVE:
                                    SaveWave(nId);
                                    nCurrCmd = wParam;
                                    return FALSE;

                              case BTN_OPEN:
                                    nId = OpenWave(lpszWaveFile);
                                    DisplayFormat(nId);
                                    nCurrCmd = wParam;
                                    return FALSE;

                              case BTN_PLAY:
                                    PlayWave(nId);
                                    nCurrCmd = wParam;
                                    return FALSE;

                              case BTN_STOP:
                                    StopWave(nId);
                                    nCurrCmd = wParam;
                                    return FALSE;
                        }
                        break;

                  case MM_MCINOTIFY:
                        MCINotify( wParam, LOWORD(lParam) );      // pass deviceDI
                        break;
            }
            return FALSE;
      }
//-----------------------------------------------------------------------------
int pascal WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpCmdLine, int nCmdShow)
      {
            DLGPROC dlgprc;
            HWND hwndParent;

            hInstance = hinstCurrent;

            dlgprc = (DLGPROC) MakeProcInstance( (FARPROC) MainDialogProc, hInstance);
            DialogBox( hInstance, MAKEINTRESOURCE(1), 0, dlgprc);
            FreeProcInstance ( (FARPROC) dlgprc);
      }
//--------------------------------------------------------------
// record.rc
//--------------------------------------------------------------
#include "record.h"
1 DIALOG 49, 31, 195, 223
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CLASS "bordlg"
CAPTION "Wave record"
FONT 8, "MS Sans Serif"
{
 LTEXT "d:\\toan\\cpp\\win\\examples\\wave", TXT_FILENAME, 10, 20, 174, 8
 CONTROL "File name", -1, "BorShade", BSS_GROUP | BSS_CAPTION | BSS_LEFT | WS_CHILD | WS_VISIBLE, 6, 8, 182, 25
 RTEXT "0.0", TXT_TIME, 145, 54, 24, 8
 LTEXT "sec", -1, 171, 54, 14, 8
 CONTROL "Time", -1, "BorShade", BSS_GROUP | BSS_CAPTION | BSS_LEFT | WS_CHILD | WS_VISIBLE, 142, 40, 46, 25
 CONTROL "", -1, "BorShade", BSS_HDIP | BSS_LEFT | WS_CHILD | WS_VISIBLE, 0, 112, 194, 3
 PUSHBUTTON "&Play", BTN_PLAY, 80, 120, 30, 14
 PUSHBUTTON "S&top", BTN_STOP, 10, 120, 30, 14
 PUSHBUTTON "&Record", BTN_RECORD, 45, 120, 30, 14
 PUSHBUTTON "&Save", BTN_SAVE, 116, 120, 30, 14
 PUSHBUTTON "&Open", BTN_OPEN, 151, 120, 30, 14
 CONTROL "Messages", -1, "BorShade", BSS_GROUP | BSS_CAPTION | BSS_LEFT | WS_CHILD | WS_VISIBLE, 6, 140, 182, 76
 EDITTEXT EDIT_MESSAGE, 8, 152, 178, 52, ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_WANTRETURN | WS_BORDER | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP
 LTEXT "Status", TXT_STATUS, 10, 206, 174, 8
 CONTROL "Format", -1, "BorShade", BSS_GROUP | BSS_CAPTION | BSS_LEFT | WS_CHILD | WS_VISIBLE, 6, 40, 128, 50
 LTEXT "Sampling rate", -1, 9, 52, 62, 8
 LTEXT "Channels", -1, 9, 64, 62, 8
 LTEXT "Data format", -1, 9, 76, 62, 8
 LTEXT "44100", TXT_SAMPLERATE, 76, 52, 56, 8
 LTEXT "Stereo", TXT_CHANNELS, 76, 64, 56, 8
 LTEXT "16 bit", TXT_DATAFORMAT, 76, 76, 56, 8
}


0
 
nsenthilAuthor Commented:
Hai guy's
just help me out
0
 
trilloCommented:
Sure, I help you...
But tell me, please, if you'd like to use high level or low level functions to record sound... The difference between both options is huge. If you need to record some sound and store it in a wav file for later playback, you should use high level functions... I you need to analyze wave data the you should need low level functions...
It'll be a pleasure helping.

Trillo
0
 
nsenthilAuthor Commented:
hi toannd's
thanku for u'r reply.this was my first question asked in expert-exchage,i have got very good responce,i am very happy about this..

love u all
senthil
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.