waveOutOpen - Sound recording 
Author Message
 waveOutOpen - Sound recording

The example of using sound on the VC++ CD is incomplete - it lacks an
example that includes the parameter values.  I'm failing the waveOutOpen
with error 11 (incorrect parameters) but I don't know which parameters
failed.  Is there a more complete example somewhere?



Wed, 28 Jul 1999 03:00:00 GMT  
 waveOutOpen - Sound recording

Paste the text below into sound.h:

/////////////////////////////////
sound.h

#ifndef _soundh
#define _soundh

#include "windows.h"
#include "mmsystem.h"

typedef struct WAVESTRUCTTYPE
{
        PCMWAVEFORMAT   PCMWaveFmtRecord;
        WAVEHDR                 WaveHeader;

Quote:
} WAVESTRUCT;

typedef WAVESTRUCT far  *LPWAVESTRUCT;

// forware decls
LPWAVESTRUCT    WaveOpen( LPSTR szFilename, HANDLE hFileIn );
BOOL                            WavePlay( LPWAVESTRUCT lpWaveStruct );
#endif

----------------------------------------------------------------------------
---------------------
Paste the text below into sound.c:

/////////////////////////////////
sound.c

#include <stdio.h>
#include "sound.h"

LPWAVESTRUCT WaveOpen( LPSTR szFilename, HANDLE hFileIn )
{
        MMCKINFO MMCkInfoParent;
        MMCKINFO        MMCkInfoChild;
        MMIOINFO        mmioinfo;
        HMMIO           hmmio;
        HANDLE  hWaveOut;
        int             errorCode;
        DWORD           bytesRead;
        long            lDataSize;
        LPBYTE  pWaveData;
        LPWAVESTRUCT lpWaveStruct;

        if (!szFilename) // use a handle
        {
                if (!hFileIn) return NULL; // if no handle specified, return an error
                memset( &mmioinfo, 0, sizeof( MMIOINFO ) );
                mmioinfo.dwFlags = MMIO_DENYWRITE;
                mmioinfo.adwInfo[0] = (DWORD)hFileIn;
                if ((hmmio = mmioOpen( NULL, &mmioinfo, MMIO_READ )) == NULL) return
NULL;
        } else // use a filename
                if ((hmmio = mmioOpen( szFilename, NULL, MMIO_READ )) == NULL) return
NULL;

        // allocate memory for the wave struct
        if ((lpWaveStruct =
                (LPWAVESTRUCT)GlobalAlloc( GPTR, sizeof( WAVESTRUCT ) )) == NULL )
        {
                mmioClose( hmmio, 0 );
                return NULL;
        }

        MMCkInfoParent.fccType = mmioFOURCC('W','A','V','E');
        if (errorCode = mmioDescend( hmmio, &MMCkInfoParent, NULL, MMIO_FINDRIFF
))
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        MMCkInfoChild.ckid = mmioFOURCC('f','m','t',' ');
        if (errorCode = mmioDescend( hmmio, &MMCkInfoChild, &MMCkInfoParent,
MMIO_FINDCHUNK ))
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        if ((bytesRead = mmioRead( hmmio,
(LPSTR)&(lpWaveStruct->PCMWaveFmtRecord), MMCkInfoChild.cksize )) <= 0)
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        errorCode = waveOutOpen(        &hWaveOut, WAVE_MAPPER, (WAVEFORMATEX
*)&(lpWaveStruct->PCMWaveFmtRecord),
                                                                                0L, 0L, WAVE_FORMAT_QUERY );

        if (errorCode)
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        if (errorCode = mmioAscend( hmmio, &MMCkInfoChild, 0 ))
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        MMCkInfoChild.ckid = mmioFOURCC('d','a','t','a');
        if (errorCode = mmioDescend( hmmio, &MMCkInfoChild, &MMCkInfoParent,
MMIO_FINDCHUNK ))
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        lDataSize = MMCkInfoChild.cksize;
        if ((pWaveData = GlobalAlloc( GMEM_FIXED, lDataSize )) == NULL)
        {
                GlobalFree( lpWaveStruct );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        if ( mmioRead( hmmio, (LPSTR)pWaveData, lDataSize ) != lDataSize )
        {
                GlobalFree( lpWaveStruct );
                GlobalFree( pWaveData );
                mmioClose( hmmio, 0 );
                return NULL;
        }

        lpWaveStruct->WaveHeader.lpData = pWaveData;
        lpWaveStruct->WaveHeader.dwBufferLength = lDataSize;
        lpWaveStruct->WaveHeader.dwFlags = 0L;
        lpWaveStruct->WaveHeader.dwLoops = 0L;

        mmioClose( hmmio, 0 );

        return lpWaveStruct;

Quote:
}

BOOL WavePlay( LPWAVESTRUCT lpWaveStruct )
{
        HANDLE  hWaveOut;
        MMRESULT        ReturnCode;

        if (ReturnCode = waveOutOpen(   &hWaveOut, WAVE_MAPPER, (WAVEFORMATEX
*)&(lpWaveStruct->PCMWaveFmtRecord),
                                                                                0L, 0L, 0L ))
                return FALSE;

        lpWaveStruct->WaveHeader.dwFlags = 0;

        if (ReturnCode = waveOutPrepareHeader( hWaveOut,
&(lpWaveStruct->WaveHeader), sizeof( WAVEHDR ) ))
                return FALSE;

        lpWaveStruct->WaveHeader.dwFlags = 0;
        if (ReturnCode = waveOutWrite( hWaveOut, &(lpWaveStruct->WaveHeader),
sizeof( WAVEHDR ) ))
                return FALSE;

        while (!(lpWaveStruct->WaveHeader.dwFlags & WHDR_DONE)) {}

        if (ReturnCode = waveOutUnprepareHeader( hWaveOut,
&(lpWaveStruct->WaveHeader), sizeof( WAVEHDR ) ))
                return FALSE;

        lpWaveStruct->WaveHeader.dwFlags = 0L;

        if (ReturnCode = waveOutClose( hWaveOut ))
                return FALSE;

        return TRUE;

Quote:
}

// end sound.c

----------------------------------------------------------------------------
-------------------------------------------------------------------
--
---------------------------------------------
Justin Holmes
Technical Director
Visdyn Software Corporation
Web:    www.visdyn.com

Tel:    (416) 368-3088
Fax:    (416) 368-3086
---------------------------------------------



Sun, 01 Aug 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How do I record sound from sound card?

2. Recording Sounds

3. record sound

4. How to record a sound?

5. Application That Will Record and Play Sound (real-time)

6. Protected mode sound recording

7. Continuous Sound Recording

8. Recording Sounds with a Microphone

9. Sound Recording Problem

10. Direct Sound record tutorial linking errors (0/1)

11. Recording a sound through Visual C++

12. Recording Sound Problems..

 

 
Powered by phpBB® Forum Software