Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

270 lines
7.9 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgWaveFile.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgWaveFile.cpp
//----------------------------------------------------------------------------
#include "StdAfx.h"
#include "LgWaveFile.h"
CLgWaveFile::CLgWaveFile()
{
m_pWaveFormatEx = NULL;
m_pWaveData = NULL;
m_nWaveDataLength = 0;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgWaveFile::CLgWaveFile" );
}
CLgWaveFile::~CLgWaveFile()
{
Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgWaveFile::~CLgWaveFile" );
}
BOOL CLgWaveFile::Load( LPCTSTR pFileName )
{
// destroy old data
Destroy();
HMMIO hMMIO = NULL;
__try
{
// open wave file
hMMIO = mmioOpen( (LPTSTR)pFileName, NULL, MMIO_READ | MMIO_ALLOCBUF );
// parse wave file
MMCKINFO mmckinfoParent; // parent chunk info struct
MMCKINFO mmckinfoSubchunk; // sub chunk
// verify that the file is of type "WAVE"
mmckinfoParent.fccType = MAKEFOURCC('W', 'A', 'V', 'E');
if(mmioDescend(hMMIO, (LPMMCKINFO)&mmckinfoParent, NULL, MMIO_FINDRIFF))
return FALSE;
// find the "fmt " chunk (must be a subchunk of the "WAVE" chunk)
mmckinfoSubchunk.ckid = MAKEFOURCC('f', 'm', 't', ' ');
if(mmioDescend(hMMIO, (LPMMCKINFO)&mmckinfoSubchunk, (LPMMCKINFO)&mmckinfoParent, MMIO_FINDCHUNK))
return FALSE;
// get the size of the "fmt " chunk
INT cbFormat = mmckinfoSubchunk.cksize;
// allocate waveformat
m_pWaveFormatEx = (LPWAVEFORMATEX)malloc( cbFormat );
if(!m_pWaveFormatEx)
return FALSE;
// read the "fmt " chunk
if(mmioRead(hMMIO, (HPSTR)m_pWaveFormatEx, cbFormat) != (LONG)cbFormat)
return FALSE;
// ascend out o' the "fmt " chunk
mmioAscend(hMMIO, &mmckinfoSubchunk, 0);
// and find the data subchunk. (current file pos should be at the beginning of the
// data chunk, but when you assume...)
mmckinfoSubchunk.ckid = MAKEFOURCC('d', 'a', 't', 'a');
if(mmioDescend(hMMIO, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK))
return FALSE;
// get the size of the "data" subchunk
m_nWaveDataLength = mmckinfoSubchunk.cksize;
if( 0L == m_nWaveDataLength )
return FALSE;
// read data
m_pWaveData = new CHAR[ m_nWaveDataLength ];
if( !m_pWaveData )
return FALSE;
// read data chunk
m_nWaveDataLength = mmioRead( hMMIO, m_pWaveData, m_nWaveDataLength );
}
__finally
{
if( hMMIO )
mmioClose( hMMIO, 0 );
}
return TRUE;
}
void CLgWaveFile::Destroy()
{
if( m_pWaveFormatEx )
{
free( m_pWaveFormatEx );
m_pWaveFormatEx = NULL;
}
if( m_pWaveData )
{
free( m_pWaveData );
m_pWaveData = NULL;
}
m_nWaveDataLength = NULL;
}
BOOL CLgWaveFile::ConvertToPCM()
{
if( !m_pWaveData || !m_pWaveFormatEx )
{
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "No data for conversion" );
return FALSE;
}
HACMSTREAM hAcmStream = NULL;
LPWAVEFORMATEX pWaveFormatEx = NULL;
LPBYTE pWaveData = NULL;
DWORD dwOutputBytes = 0;
__try
{
pWaveFormatEx = (LPWAVEFORMATEX)malloc( sizeof(WAVEFORMATEX) );
if( !pWaveFormatEx )
{
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "Can't allocate memory for WAVEFORMATEX" );
return FALSE;
}
pWaveFormatEx->wFormatTag = WAVE_FORMAT_PCM;
pWaveFormatEx->nChannels = 1;
pWaveFormatEx->nSamplesPerSec = 8000;
pWaveFormatEx->wBitsPerSample = 16;
pWaveFormatEx->nBlockAlign = pWaveFormatEx->nChannels * pWaveFormatEx->wBitsPerSample / 8;
pWaveFormatEx->nAvgBytesPerSec = pWaveFormatEx->nSamplesPerSec * pWaveFormatEx->nBlockAlign;
pWaveFormatEx->cbSize = 0;
MMRESULT mm[] = { ACMERR_NOTPOSSIBLE, MMSYSERR_INVALFLAG, MMSYSERR_INVALHANDLE, MMSYSERR_INVALPARAM, MMSYSERR_NOMEM };
// open stream
// MMRESULT mmResult = acmStreamOpen( &hAcmStream, NULL, m_pWaveFormatEx, pWaveFormatEx, NULL, (DWORD)StreamConvertCallback, (DWORD)this, CALLBACK_FUNCTION | ACM_STREAMOPENF_ASYNC | ACM_STREAMOPENF_NONREALTIME );
MMRESULT mmResult = acmStreamOpen( &hAcmStream, NULL, m_pWaveFormatEx, pWaveFormatEx, NULL, NULL, 0, ACM_STREAMOPENF_NONREALTIME );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamOpen fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
// calculate stream size
mmResult = acmStreamSize( hAcmStream, m_nWaveDataLength, &dwOutputBytes, ACM_STREAMSIZEF_DESTINATION );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamSize fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
// create output buffer
pWaveData = (LPBYTE)malloc( dwOutputBytes );
if( !pWaveData )
return FALSE;
// prepare conversion
ACMSTREAMHEADER StreamHeader;
StreamHeader.cbStruct = sizeof(StreamHeader);
StreamHeader.fdwStatus = 0;
StreamHeader.dwUser = (DWORD)this;
StreamHeader.pbSrc = (PBYTE)m_pWaveData;
StreamHeader.cbSrcLength = m_nWaveDataLength;
StreamHeader.cbSrcLengthUsed = m_nWaveDataLength;
StreamHeader.dwSrcUser = (DWORD)this;
StreamHeader.pbDst = pWaveData;
StreamHeader.cbDstLength = dwOutputBytes;
StreamHeader.cbDstLengthUsed = 0;
StreamHeader.dwDstUser = (DWORD)this;
// prepare converter
mmResult = acmStreamPrepareHeader( hAcmStream, &StreamHeader, 0 );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamPrepareHeader fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
// convert data
mmResult = acmStreamConvert( hAcmStream, &StreamHeader, ACM_STREAMCONVERTF_END | ACM_STREAMCONVERTF_START );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamConvert fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
// unprepare converter
mmResult = acmStreamUnprepareHeader( hAcmStream, &StreamHeader, 0 );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamUnprepareHeader fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
dwOutputBytes = StreamHeader.cbDstLengthUsed;
}
__finally
{
// close converter
MMRESULT mmResult = acmStreamClose( hAcmStream, 0 );
if( mmResult )
{
// translate error
TCHAR pErrorText[MAX_PATH] = {0};
waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
// log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamClose fails with 0x%08X <%s>", mmResult, pErrorText );
return FALSE;
}
}
// free old data
free( m_pWaveFormatEx );
free( m_pWaveData );
// store new format
m_pWaveFormatEx = pWaveFormatEx;
m_pWaveData = (LPSTR)pWaveData;
m_nWaveDataLength = dwOutputBytes;
return TRUE;
}
void CALLBACK CLgWaveFile::StreamConvertCallback( HACMSTREAM has, UINT uMsg, DWORD dwInstance, LPARAM lParam1, LPARAM lParam2 )
{
}