⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cwavefile.cpp

📁 windows embedded ce (wince6.0)上录音事例程序源码。可编成.exe在系统上运行。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                    return dwLastError;
                break;

            default:    // Skip unknown Chunk
                bRtn = ReadFile( hWaveData, &dwBuffer, 4, &dwRtn, NULL );
                if( !bRtn ) return WaveFileError();

                dwRtn = SetFilePointer( hWaveData, dwBuffer, NULL, FILE_CURRENT );
                if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

        } // switch( dwBuffer )

        // Read Next FOURCC
        bRtn = ReadFile( hWaveData, &dwBuffer, 4, &dwRtn, NULL );
        if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    } // end while ( data_FOURCC != dwBuffer )

    /* --------------------------------------------------------------------
    	If writing Set hWaveSize pointer.
    -------------------------------------------------------------------- */
    if( INVALID_HANDLE_VALUE != hWaveSize )
    {
        // Get current file offset
        dwRtn = SetFilePointer( hWaveData, 0, NULL, FILE_CURRENT );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

        // Set hWaveSize to current file offset
        // thus we are moving the file dwRtn bytes from the file beginning
        dwRtn = SetFilePointer( hWaveSize, dwRtn, NULL, FILE_BEGIN );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();
    }

    /* --------------------------------------------------------------------
    	Read the wave data size
    -------------------------------------------------------------------- */
    bRtn = ReadFile( hWaveData, &nWaveSize, 4, &dwRtn, NULL );
    if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    /* --------------------------------------------------------------------
    	Set the data offset
    -------------------------------------------------------------------- */
    dwRtn = SetFilePointer( hWaveData, 0, NULL, FILE_CURRENT );
    if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

    dwDataOffSet = dwRtn;

    return ERROR_SUCCESS;

} // DWORD CWaveFile::GetFormat( ... )


/*++

CWaveFile::ReadListInfo:

	This function reads a list info chunk started and hWaveData

Arguments:

	lplpInfo    pointer to pointer to info data

Return Value:

	ERROR_SUCCESS( 0 ) on success
	
Notes:

--*/
DWORD CWaveFile::ReadListInfo( LPVOID *lplpInfo )
{
    DWORD   dwInfoSize = 0;
    DWORD   dwRtn;
    BOOL    bRtn;

    if(NULL == lplpInfo) {
		dwLastError = ERROR_INVALID_PARAMETER;
		return bRtn = FALSE;
    }

    /* ----------------------------------------------------------------
    	Set the LIST-INFO offset
    ---------------------------------------------------------------- */
    dwInfoOffSet = SetFilePointer( hWaveData, 0, NULL, FILE_CURRENT ) - 4;

    /* ----------------------------------------------------------------
    	Get the LIST chunk size
    ---------------------------------------------------------------- */
    bRtn = ReadFile( hWaveData, &dwInfoSize, 4, &dwRtn, NULL );
    if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    /* ----------------------------------------------------------------
    	If the user wants the Info read it in else skip it.
    ---------------------------------------------------------------- */
    if( NULL != lplpInfo )
    {
        dwRtn = SetFilePointer( hWaveData, -8, NULL, FILE_CURRENT );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

        dwInfoSize += 8; // The complete size of the buffer.

        // Get a new buffer.
        *lplpInfo = new BYTE[dwInfoSize];

        /* ------------------------------------------------------------
        	NOTE: if memory allocation fails simply skip reading
        	info block and don't indicate error.
        ------------------------------------------------------------ */
        if( NULL != *lplpInfo )
        {

            bRtn = ReadFile( hWaveData, *lplpInfo, dwInfoSize, &dwRtn, NULL );
            if( (!bRtn) || (dwRtn != (dwInfoSize+8)) )
            {
                delete [] *lplpInfo;
                *lplpInfo = NULL;
                dwInfoOffSet = 0;
                return WaveFileError( ERROR_FILE_CORRUPT );

            } // end if( (!bRtn) || (dwRtn != (dwInfoSize+8)) )

        } // end if( NULL != *lplpInfo )

        else // ship info
        {
            dwRtn = SetFilePointer( hWaveData, dwInfoSize+8, NULL, FILE_CURRENT );
            if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

        } // end if( NULL != *lplpInfo ) else

    } // end if( NULL != lplpInfo )

    else // skip info
    {
        dwRtn = SetFilePointer( hWaveData, dwInfoSize, NULL, FILE_CURRENT );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

    } // end if( NULL != lplpInfo )


    return dwLastError = ERROR_SUCCESS;

} // end DWORD CWaveFile::ReadListInfo( LPVOID *lplpInfo )

/*++

CWaveFile::ReadWaveFormat:

	This function reads the wave format from hWaveData

Arguments:

	lplpWaveFormat  Wave format data

Return Value:

	ERROR_SUCCESS(0) on success

Notes:

--*/
DWORD CWaveFile::ReadWaveFormat( LPVOID *lplpWaveFormat )
{
    DWORD   dwRtn;
    DWORD   dwBuffer;
    BOOL    bRtn;

   if(NULL == lplpWaveFormat){
		dwLastError = ERROR_INVALID_PARAMETER;
		return bRtn = FALSE;
    }

    /* --------------------------------------------------------------------
    	Verify format data size.  Must be >= to sizeof(PCMWAVEFORMAT)
    -------------------------------------------------------------------- */
    bRtn = ReadFile( hWaveData, &dwBuffer, 4, &dwRtn, NULL );
    if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    if( sizeof(PCMWAVEFORMAT) > dwBuffer )
        return WaveFileError( ERROR_FILE_CORRUPT );

    // Assumption lplpWaveFormat should not be NULL by above check
    // The additional size allows access to a WAVEFORMATEX
    // size member
    *lplpWaveFormat = new BYTE[dwBuffer+sizeof(WORD)];
    if( NULL == *lplpWaveFormat )
        return WaveFileError( ERROR_NOT_ENOUGH_MEMORY );

     ZeroMemory( *lplpWaveFormat, dwBuffer+sizeof(WORD) );
    /* --------------------------------------------------------------------
    	Read the Wave file format information. All data from the fmt
    	chunk is copyed.  Then the PCM data is saved locally and the
    	chunk data is returned to the client.
    -------------------------------------------------------------------- */
    bRtn = ReadFile( hWaveData,
                     *lplpWaveFormat,
                     dwBuffer,
                     &dwRtn, NULL );
    if( (!bRtn) || (dwBuffer != dwRtn) ) return WaveFileError();

    /* --------------------------------------------------------------------
    	Copy PCM Wave file fomat
    -------------------------------------------------------------------- */
    CopyMemory( &m_PcmWaveFormat, *lplpWaveFormat, sizeof(PCMWAVEFORMAT) );

    return dwLastError = ERROR_SUCCESS;

} // end DWORD CWaveFile::ReadWaveFormat( LPVOID *lplpWaveFormat )

/*++

CWaveFile::ReadFactChunk:

	This fuction reads nSamples from the fact chunk at hWaveData.

Arguments:

	NONE

Return Value:

	ERROR_SUCCESS(0) on success

Notes:

--*/

DWORD CWaveFile::ReadFactChunk( void )
{

    DWORD   dwRtn;
    DWORD   dwChunkLength;
    BOOL    bRtn;

    // Get the chunk size.
    bRtn = ReadFile( hWaveData, &dwChunkLength, 4, &dwRtn, NULL );
    if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    if( 4 > dwChunkLength ) return dwLastError = ERROR_SUCCESS;

    // Read samples
    bRtn = ReadFile( hWaveData, &nSamples, 4, &dwRtn, NULL );
    if( (!bRtn) || (4 != dwRtn) ) return WaveFileError();

    // Skip the rest, if any.
    if( 4 == dwChunkLength ) return dwLastError = ERROR_SUCCESS;


    dwRtn = SetFilePointer( hWaveData, dwChunkLength - 4, NULL, FILE_CURRENT );
    if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

    return dwLastError = ERROR_SUCCESS;

}  // end DWORD CWaveFile::ReadFactChunk( )

/*++

CWaveFile::InitFile:

	This function sets up a new WAVE-RIFF file.

Arguments:

   	PCMWAVEFORMAT*  lpPcmWaveFormat -> Pointer to the structure to write
   	                                   the wave format data.
       LPWAVEAUXINFO   lpWaveAuxInfo   -> Currently not used, not writen to
                                       the file.

Return Value:

    Return file error codes.

Notes:

--*/
DWORD CWaveFile::InitFile( PCMWAVEFORMAT*    lpPcmWaveFormat,
                           LPVOID*           lplpInfo )
{
    DWORD   dwRtn;
    BOOL    bRtn;
    RIFFCK  riffck;
    CKHDR   ckhdr;

    if(NULL == lpPcmWaveFormat){
		dwLastError = ERROR_INVALID_PARAMETER;
		return bRtn = FALSE;
    }

    /* --------------------------------------------------------------------
        Write the RIFF-WAVE chunk 	
    -------------------------------------------------------------------- */
    riffck.ckID = RIFF_FOURCC;
    riffck.ckSize = 4 + sizeof(CKHDR) + sizeof(PCMWAVEFORMAT) + sizeof(CKHDR);
    riffck.ckForm = WAVE_FOURCC;

    bRtn = WriteFile( hWaveData, &riffck, sizeof(RIFFCK), &dwRtn, NULL);
    if( (!bRtn) || (sizeof(RIFFCK) != dwRtn) ) return WaveFileError();

    dwRtn = SetFilePointer( hRiffSize, 4, NULL, FILE_BEGIN );
    if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

    /* --------------------------------------------------------------------
    	There is wave info writ it here.
    -------------------------------------------------------------------- */
    if( NULL != lplpInfo )
    {
        if( NULL != *lplpInfo )
        {
            dwInfoOffSet = SetFilePointer( hWaveData, 0, NULL, FILE_CURRENT );

            DWORD dwInfoSize = (DWORD)((LPDWORD)*lplpInfo + 1) + 8;
            bRtn = WriteFile( hWaveData, *lplpInfo, dwInfoSize, &dwRtn, NULL);
            if( (!bRtn) || (sizeof(RIFFCK) != dwRtn) ) return WaveFileError();

        } // end if( NULL != *lplpInfo )

    } // end if( NULL != lplpInfo )

    /* --------------------------------------------------------------------
        Write the WAVE-fmt chunk header
    -------------------------------------------------------------------- */
    ckhdr.ckID = fmt_FOURCC;
    ckhdr.ckSize = sizeof(PCMWAVEFORMAT);

    bRtn = WriteFile( hWaveData, &ckhdr, sizeof(CKHDR), &dwRtn, NULL);
    if( (!bRtn) || (sizeof(CKHDR) != dwRtn) ) return WaveFileError();

    bRtn = WriteFile( hWaveData,
                      lpPcmWaveFormat,
                      sizeof(PCMWAVEFORMAT),
                      &dwRtn, NULL);
    if( (!bRtn) || (sizeof(PCMWAVEFORMAT) != dwRtn) ) return WaveFileError();

    /* --------------------------------------------------------------------
    	Copy PCM Wave file fomat
    -------------------------------------------------------------------- */
    CopyMemory( &m_PcmWaveFormat, lpPcmWaveFormat, sizeof(PCMWAVEFORMAT) );

    /* --------------------------------------------------------------------
    	Write the WAVE-data chunk header
    -------------------------------------------------------------------- */
    ckhdr.ckID = data_FOURCC;
    ckhdr.ckSize = 0;

    bRtn = WriteFile( hWaveData, &ckhdr, sizeof(CKHDR), &dwRtn, NULL);
    if( (!bRtn) || (sizeof(CKHDR) != dwRtn) ) return WaveFileError();

    if( INVALID_HANDLE_VALUE != hWaveSize )
    {
        dwRtn = SetFilePointer( hWaveData, 0, NULL, FILE_CURRENT );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();

        dwDataOffSet = dwRtn;

        dwRtn = SetFilePointer( hWaveSize, (dwDataOffSet - 4), NULL, FILE_BEGIN );
        if( 0xFFFFFFFF == dwRtn ) return WaveFileError();
    }

    return ERROR_SUCCESS;

} // end DWORD CWaveFile::InitFile( ... )


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -