?? cwavefile.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
Module Name:
WaveFile.cpp
Abstract:
This file contains the code for WAVE-RIFF file library. This library
creates, opens, reads, writes, and closes WAVE-RIFF files.
Notes:
--*/
#include "CWaveFile.h"
inline DWORD CWaveFile::WaveFileError( void )
{
RETAILMSG(1, (TEXT("ERROR: WaveFileError void")));
dwLastError = GetLastError();
Close();
return dwLastError;
} // end inline DWORD CWaveFile::WaveFileError( void )
inline DWORD CWaveFile::WaveFileError( DWORD dwError )
{
RETAILMSG(1, (TEXT("ERROR: WaveFileError %d"), dwError));
dwLastError = dwError;
Close();
return dwLastError;
} // end
/*++
CWaveFile::Close:
This function closes all of the file handles.
Arguments:
NONE
Return Value:
NONE
Notes:
--*/
inline VOID CWaveFile::Close()
{
BOOL bRtn;
DWORD dwRtn;
BYTE byTemp;
if(INVALID_HANDLE_VALUE != hWaveData)
{
dwRtn = GetFileSize( hWaveData, NULL );
if( dwRtn % 2 )
{
SetFilePointer( hWaveData, 0, NULL, FILE_END );
byTemp = 0;
WriteFile( hWaveData, &byTemp, 1, &dwRtn, NULL );
}
}
if (INVALID_HANDLE_VALUE != hWaveData)
{
bRtn = CloseHandle( hWaveData );
if( bRtn )
hWaveData = INVALID_HANDLE_VALUE;
}
if (INVALID_HANDLE_VALUE != hRiffSize)
{
bRtn = CloseHandle( hRiffSize );
if( bRtn )
hRiffSize = INVALID_HANDLE_VALUE;
}
if (INVALID_HANDLE_VALUE != hWaveSize)
{
bRtn = CloseHandle( hWaveSize );
if( bRtn )
hWaveSize = INVALID_HANDLE_VALUE;
}
/* --------------------------------------------------------------------
Reset all offset and sizes to zero
-------------------------------------------------------------------- */
nRiffSize = 0;
nWaveSize = 0;
dwDataOffSet = 0;
dwInfoOffSet = 0;
nSamples = 0;
} // end CWaveFile:Close();
/*++
CWaveFile::WriteSize:
This function writes the new value of the RIFF chunks data size.
Arguments:
HANDLE hFileSize -> Handle to write to.
DWORD nBytes -> The number of bytes in the chunks data size.
Return Value:
TRUE -> Success
FALSE -> Failed
Notes:
--*/
inline BOOL CWaveFile::WriteSize(HANDLE hFileSize, DWORD nBytes)
{
BOOL bRtn;
DWORD dwRtn;
// CHECK ME
if (INVALID_HANDLE_VALUE == hFileSize)
{
RETAILMSG(1, (TEXT("ERROR:WRITESIZE: hFileSize == INVALID_HANDLE_VALUE")));
return FALSE;
}
bRtn = WriteFile( hFileSize, &nBytes, 4, &dwRtn, NULL );
dwLastError = GetLastError();
if( dwRtn < 4 )
bRtn = FALSE;
dwRtn = SetFilePointer( hFileSize,
-4,
NULL,
FILE_CURRENT );
if( 0xFFFFFFFF == dwRtn )
{
bRtn = FALSE;
dwLastError = GetLastError();
}
return bRtn;
} // end inline BOOL CWaveFile::WriteSize(HANDLE hFileSize, DWORD nBytes)
/*++
CWaveFile::CWaveFile:
Default Constructor for CWaveFile
Arguments:
NONE
Return Value:
NONE
Notes:
--*/
CWaveFile::CWaveFile( )
{
hWaveData = INVALID_HANDLE_VALUE;
hRiffSize = INVALID_HANDLE_VALUE;
hWaveSize = INVALID_HANDLE_VALUE;
nRiffSize = 0;
nWaveSize = 0;
dwDataOffSet = 0;
dwInfoOffSet = 0;
nSamples = 0;
dwLastError = ERROR_FILE_NOT_FOUND;
} // end CWaveFile::~CWaveFile()
/*++
CWaveFile::CWaveFile:
Full constructor for a WAVE-RIFF file.
Arguments:
lpFileName -> Same As CreateFile
dwDesiredAccess -> Same As CreateFile, except can't be 0
dwShareMode -> Same As CreateFile except, can't be FILE_SHARE_DELETE
dwCreationDistribution -> Same As CreateFile
dwFlagsAndAttributes -> Same As CreateFile
Return Value:
NONE:
Notes:
Most file CreateFile options are supported with the limitation that an
open WAVE-RIFF file must be able to played and/or recorded.
Also, security and templetes are not supported.
--*/
CWaveFile::CWaveFile( LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwCreationDistribution,
DWORD dwFlagsAndAttributes,
LPVOID* lplpWaveFormat,
LPVOID* lplpInfo )
{
hWaveData = INVALID_HANDLE_VALUE;
hRiffSize = INVALID_HANDLE_VALUE;
hWaveSize = INVALID_HANDLE_VALUE;
nRiffSize = 0;
nWaveSize = 0;
dwDataOffSet = 0;
dwInfoOffSet = 0;
nSamples = 0;
dwLastError = Create(lpFileName,
dwDesiredAccess,
dwCreationDistribution,
dwFlagsAndAttributes,
lplpWaveFormat,
lplpInfo );
} // end CWaveFile::CWaveFile( ... )
/*++
CWaveFile::~CWaveFile:
This is the class Destructor.
Arguments:
NONE
Return Value:
NONE
Notes:
--*/
CWaveFile::~CWaveFile()
{
Close();
} // CWaveFile::~CWaveFile()
/*++
CWaveFile::Create:
This function creates or opens WAVE-RIFF file with complete header
information.
If the file already exists and the file creation parameters allow it
to be opened. lpPcmWaveFormat is filled with the WAVE-RIFF file's
format data. It is the calling function's responsability to call
GetLastError in order to see if the file already existed.
If the file already exists but isn't a legal wave file the HANDLE will
be closed and INVALID_HANDLE_VALUE will be returned. GetLastError will
be set to ERROR_FILE_CORRUPT.
Arguments:
lpFileName -> Same As CreateFile
dwDesiredAccess -> Same As CreateFile, except can't be 0
dwShareMode -> Same As CreateFile except, can't be FILE_SHARE_DELETE
dwCreationDistribution -> Same As CreateFile
dwFlagsAndAttributes -> Same As CreateFile
lpPcmWaveFormat -> Pointer to a wave format structure
lplpInfo -> Pointer to a pointer that will hold the info data.
Return Value:
If function is successful then ERROR_SUCCESS is returned.
If the specified file exists before the function call
and dwCreationDistribution is CREATE_ALWAYS or OPEN_ALWAYS, then
ERROR_ALREADY_EXISTS (even though the function has succeeded).
If the file does not exist before the call, function returns zero.
If the function fails, the file creation error is returned.
Notes:
Most file CreateFile options are supported with the limitation that an
open WAVE-RIFF file must be able to played and/or recorded.
Also, security and templetes are not supported.
If the file is new and lplpInfo isn't NULL then it is assumed that
lplpInfo contains a valid LIST-INFO chunk and that chunk is writen
to the file.
--*/
DWORD CWaveFile::Create( LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwCreationDistribution,
DWORD dwFlagsAndAttributes,
LPVOID *lplpWaveFormat,
LPVOID *lplpInfo )
{
hWaveData = CreateFile( lpFileName,
dwDesiredAccess,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
dwCreationDistribution,
dwFlagsAndAttributes,
NULL );
if( INVALID_HANDLE_VALUE == hWaveData )
{
RETAILMSG(1, (TEXT("ERROR: CREATE: hWaveData == INVALID_HANDLE_VALUE")));
return dwLastError = GetLastError();
}
/* --------------------------------------------------------------------
NOTE: hRiffSize and hWaveSize are write optimizations. They are
not needed for read only files and there for will not be used for
read only files.
-------------------------------------------------------------------- */
if( dwDesiredAccess & GENERIC_WRITE )
{
hRiffSize = CreateFile( lpFileName,
dwDesiredAccess,
FILE_SHARE_READ | FILE_SHARE_WRITE,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -