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

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgWaveFile.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgWaveFile.cpp
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "LgWaveFile.h"
  16. CLgWaveFile::CLgWaveFile()
  17. {
  18. m_pWaveFormatEx = NULL;
  19. m_pWaveData = NULL;
  20. m_nWaveDataLength = 0;
  21. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgWaveFile::CLgWaveFile" );
  22. }
  23. CLgWaveFile::~CLgWaveFile()
  24. {
  25. Destroy();
  26. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgWaveFile::~CLgWaveFile" );
  27. }
  28. BOOL CLgWaveFile::Load( LPCTSTR pFileName )
  29. {
  30. // destroy old data
  31. Destroy();
  32. HMMIO hMMIO = NULL;
  33. __try
  34. {
  35. // open wave file
  36. hMMIO = mmioOpen( (LPTSTR)pFileName, NULL, MMIO_READ | MMIO_ALLOCBUF );
  37. // parse wave file
  38. MMCKINFO mmckinfoParent; // parent chunk info struct
  39. MMCKINFO mmckinfoSubchunk; // sub chunk
  40. // verify that the file is of type "WAVE"
  41. mmckinfoParent.fccType = MAKEFOURCC('W', 'A', 'V', 'E');
  42. if(mmioDescend(hMMIO, (LPMMCKINFO)&mmckinfoParent, NULL, MMIO_FINDRIFF))
  43. return FALSE;
  44. // find the "fmt " chunk (must be a subchunk of the "WAVE" chunk)
  45. mmckinfoSubchunk.ckid = MAKEFOURCC('f', 'm', 't', ' ');
  46. if(mmioDescend(hMMIO, (LPMMCKINFO)&mmckinfoSubchunk, (LPMMCKINFO)&mmckinfoParent, MMIO_FINDCHUNK))
  47. return FALSE;
  48. // get the size of the "fmt " chunk
  49. INT cbFormat = mmckinfoSubchunk.cksize;
  50. // allocate waveformat
  51. m_pWaveFormatEx = (LPWAVEFORMATEX)malloc( cbFormat );
  52. if(!m_pWaveFormatEx)
  53. return FALSE;
  54. // read the "fmt " chunk
  55. if(mmioRead(hMMIO, (HPSTR)m_pWaveFormatEx, cbFormat) != (LONG)cbFormat)
  56. return FALSE;
  57. // ascend out o' the "fmt " chunk
  58. mmioAscend(hMMIO, &mmckinfoSubchunk, 0);
  59. // and find the data subchunk. (current file pos should be at the beginning of the
  60. // data chunk, but when you assume...)
  61. mmckinfoSubchunk.ckid = MAKEFOURCC('d', 'a', 't', 'a');
  62. if(mmioDescend(hMMIO, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK))
  63. return FALSE;
  64. // get the size of the "data" subchunk
  65. m_nWaveDataLength = mmckinfoSubchunk.cksize;
  66. if( 0L == m_nWaveDataLength )
  67. return FALSE;
  68. // read data
  69. m_pWaveData = new CHAR[ m_nWaveDataLength ];
  70. if( !m_pWaveData )
  71. return FALSE;
  72. // read data chunk
  73. m_nWaveDataLength = mmioRead( hMMIO, m_pWaveData, m_nWaveDataLength );
  74. }
  75. __finally
  76. {
  77. if( hMMIO )
  78. mmioClose( hMMIO, 0 );
  79. }
  80. return TRUE;
  81. }
  82. void CLgWaveFile::Destroy()
  83. {
  84. if( m_pWaveFormatEx )
  85. {
  86. free( m_pWaveFormatEx );
  87. m_pWaveFormatEx = NULL;
  88. }
  89. if( m_pWaveData )
  90. {
  91. free( m_pWaveData );
  92. m_pWaveData = NULL;
  93. }
  94. m_nWaveDataLength = NULL;
  95. }
  96. BOOL CLgWaveFile::ConvertToPCM()
  97. {
  98. if( !m_pWaveData || !m_pWaveFormatEx )
  99. {
  100. // log error
  101. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "No data for conversion" );
  102. return FALSE;
  103. }
  104. HACMSTREAM hAcmStream = NULL;
  105. LPWAVEFORMATEX pWaveFormatEx = NULL;
  106. LPBYTE pWaveData = NULL;
  107. DWORD dwOutputBytes = 0;
  108. __try
  109. {
  110. pWaveFormatEx = (LPWAVEFORMATEX)malloc( sizeof(WAVEFORMATEX) );
  111. if( !pWaveFormatEx )
  112. {
  113. // log error
  114. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "Can't allocate memory for WAVEFORMATEX" );
  115. return FALSE;
  116. }
  117. pWaveFormatEx->wFormatTag = WAVE_FORMAT_PCM;
  118. pWaveFormatEx->nChannels = 1;
  119. pWaveFormatEx->nSamplesPerSec = 8000;
  120. pWaveFormatEx->wBitsPerSample = 16;
  121. pWaveFormatEx->nBlockAlign = pWaveFormatEx->nChannels * pWaveFormatEx->wBitsPerSample / 8;
  122. pWaveFormatEx->nAvgBytesPerSec = pWaveFormatEx->nSamplesPerSec * pWaveFormatEx->nBlockAlign;
  123. pWaveFormatEx->cbSize = 0;
  124. MMRESULT mm[] = { ACMERR_NOTPOSSIBLE, MMSYSERR_INVALFLAG, MMSYSERR_INVALHANDLE, MMSYSERR_INVALPARAM, MMSYSERR_NOMEM };
  125. // open stream
  126. // MMRESULT mmResult = acmStreamOpen( &hAcmStream, NULL, m_pWaveFormatEx, pWaveFormatEx, NULL, (DWORD)StreamConvertCallback, (DWORD)this, CALLBACK_FUNCTION | ACM_STREAMOPENF_ASYNC | ACM_STREAMOPENF_NONREALTIME );
  127. MMRESULT mmResult = acmStreamOpen( &hAcmStream, NULL, m_pWaveFormatEx, pWaveFormatEx, NULL, NULL, 0, ACM_STREAMOPENF_NONREALTIME );
  128. if( mmResult )
  129. {
  130. // translate error
  131. TCHAR pErrorText[MAX_PATH] = {0};
  132. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  133. // log error
  134. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamOpen fails with 0x%08X <%s>", mmResult, pErrorText );
  135. return FALSE;
  136. }
  137. // calculate stream size
  138. mmResult = acmStreamSize( hAcmStream, m_nWaveDataLength, &dwOutputBytes, ACM_STREAMSIZEF_DESTINATION );
  139. if( mmResult )
  140. {
  141. // translate error
  142. TCHAR pErrorText[MAX_PATH] = {0};
  143. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  144. // log error
  145. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamSize fails with 0x%08X <%s>", mmResult, pErrorText );
  146. return FALSE;
  147. }
  148. // create output buffer
  149. pWaveData = (LPBYTE)malloc( dwOutputBytes );
  150. if( !pWaveData )
  151. return FALSE;
  152. // prepare conversion
  153. ACMSTREAMHEADER StreamHeader;
  154. StreamHeader.cbStruct = sizeof(StreamHeader);
  155. StreamHeader.fdwStatus = 0;
  156. StreamHeader.dwUser = (DWORD)this;
  157. StreamHeader.pbSrc = (PBYTE)m_pWaveData;
  158. StreamHeader.cbSrcLength = m_nWaveDataLength;
  159. StreamHeader.cbSrcLengthUsed = m_nWaveDataLength;
  160. StreamHeader.dwSrcUser = (DWORD)this;
  161. StreamHeader.pbDst = pWaveData;
  162. StreamHeader.cbDstLength = dwOutputBytes;
  163. StreamHeader.cbDstLengthUsed = 0;
  164. StreamHeader.dwDstUser = (DWORD)this;
  165. // prepare converter
  166. mmResult = acmStreamPrepareHeader( hAcmStream, &StreamHeader, 0 );
  167. if( mmResult )
  168. {
  169. // translate error
  170. TCHAR pErrorText[MAX_PATH] = {0};
  171. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  172. // log error
  173. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamPrepareHeader fails with 0x%08X <%s>", mmResult, pErrorText );
  174. return FALSE;
  175. }
  176. // convert data
  177. mmResult = acmStreamConvert( hAcmStream, &StreamHeader, ACM_STREAMCONVERTF_END | ACM_STREAMCONVERTF_START );
  178. if( mmResult )
  179. {
  180. // translate error
  181. TCHAR pErrorText[MAX_PATH] = {0};
  182. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  183. // log error
  184. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamConvert fails with 0x%08X <%s>", mmResult, pErrorText );
  185. return FALSE;
  186. }
  187. // unprepare converter
  188. mmResult = acmStreamUnprepareHeader( hAcmStream, &StreamHeader, 0 );
  189. if( mmResult )
  190. {
  191. // translate error
  192. TCHAR pErrorText[MAX_PATH] = {0};
  193. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  194. // log error
  195. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamUnprepareHeader fails with 0x%08X <%s>", mmResult, pErrorText );
  196. return FALSE;
  197. }
  198. dwOutputBytes = StreamHeader.cbDstLengthUsed;
  199. }
  200. __finally
  201. {
  202. // close converter
  203. MMRESULT mmResult = acmStreamClose( hAcmStream, 0 );
  204. if( mmResult )
  205. {
  206. // translate error
  207. TCHAR pErrorText[MAX_PATH] = {0};
  208. waveOutGetErrorText( mmResult, pErrorText, MAX_PATH );
  209. // log error
  210. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgWaveFile::ConvertToPCM", "acmStreamClose fails with 0x%08X <%s>", mmResult, pErrorText );
  211. return FALSE;
  212. }
  213. }
  214. // free old data
  215. free( m_pWaveFormatEx );
  216. free( m_pWaveData );
  217. // store new format
  218. m_pWaveFormatEx = pWaveFormatEx;
  219. m_pWaveData = (LPSTR)pWaveData;
  220. m_nWaveDataLength = dwOutputBytes;
  221. return TRUE;
  222. }
  223. void CALLBACK CLgWaveFile::StreamConvertCallback( HACMSTREAM has, UINT uMsg, DWORD dwInstance, LPARAM lParam1, LPARAM lParam2 )
  224. {
  225. }