Leaked source code of windows server 2003
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.

372 lines
8.3 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999, 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: wavformat.cpp
  6. * Content:
  7. * This module contains the CWaveFormat class which is used to work with
  8. * WAVEFORMATEX structures.
  9. *
  10. * History:
  11. * Date By Reason
  12. * ==== == ======
  13. * 07/06/00 rodtoll Created
  14. *
  15. ***************************************************************************/
  16. #include "dxvutilspch.h"
  17. #undef DPF_SUBCOMP
  18. #define DPF_SUBCOMP DN_SUBCOMP_VOICE
  19. #define REGISTRY_WAVEFORMAT_RATE L"Rate"
  20. #define REGISTRY_WAVEFORMAT_BITS L"Bits"
  21. #define REGISTRY_WAVEFORMAT_CHANNELS L"Channels"
  22. #define REGISTRY_WAVEFORMAT_TAG L"Tag"
  23. #define REGISTRY_WAVEFORMAT_AVGPERSEC L"AvgPerSec"
  24. #define REGISTRY_WAVEFORMAT_BLOCKALIGN L"BlockAlign"
  25. #define REGISTRY_WAVEFORMAT_CBSIZE L"cbsize"
  26. #define REGISTRY_WAVEFORMAT_CBDATA L"cbdata"
  27. // Cleanup -- Frees memory
  28. #undef DPF_MODNAME
  29. #define DPF_MODNAME "CWaveFormat::Cleanup"
  30. void CWaveFormat::Cleanup()
  31. {
  32. if( m_pwfxFormat )
  33. {
  34. if( m_fOwned )
  35. {
  36. delete m_pwfxFormat;
  37. m_fOwned = FALSE;
  38. m_pwfxFormat = NULL;
  39. }
  40. }
  41. }
  42. // Initialize with full parameters
  43. #undef DPF_MODNAME
  44. #define DPF_MODNAME "CWaveFormat::Initialize"
  45. HRESULT CWaveFormat::Initialize( WORD wFormatTag, DWORD nSamplesPerSec, WORD nChannels, WORD wBitsPerSample,
  46. WORD nBlockAlign, DWORD nAvgBytesPerSec, WORD cbSize, void *pvExtra )
  47. {
  48. Cleanup();
  49. m_pwfxFormat = (LPWAVEFORMATEX) (new BYTE[sizeof(WAVEFORMATEX)+cbSize]);
  50. if( !m_pwfxFormat )
  51. {
  52. DPFX(DPFPREP, 0, "Error allocating memory" );
  53. return DVERR_OUTOFMEMORY;
  54. }
  55. m_fOwned = TRUE;
  56. m_pwfxFormat->wFormatTag = wFormatTag;
  57. m_pwfxFormat->nSamplesPerSec = nSamplesPerSec;
  58. m_pwfxFormat->nChannels = nChannels;
  59. m_pwfxFormat->wBitsPerSample = wBitsPerSample;
  60. m_pwfxFormat->nBlockAlign = nBlockAlign;
  61. m_pwfxFormat->nAvgBytesPerSec = nAvgBytesPerSec;
  62. m_pwfxFormat->cbSize = cbSize;
  63. if( m_pwfxFormat->cbSize )
  64. {
  65. memcpy( &m_pwfxFormat[1], pvExtra, m_pwfxFormat->cbSize );
  66. }
  67. return DV_OK;
  68. }
  69. // Initialize and copy the specified format
  70. #undef DPF_MODNAME
  71. #define DPF_MODNAME "CWaveFormat::InitializeCPY"
  72. HRESULT CWaveFormat::InitializeCPY( LPWAVEFORMATEX pwfxFormat, void *pvExtra )
  73. {
  74. Cleanup();
  75. m_pwfxFormat = (LPWAVEFORMATEX) (new BYTE[sizeof( WAVEFORMATEX ) + pwfxFormat->cbSize] );
  76. if( !m_pwfxFormat )
  77. {
  78. DPFX(DPFPREP, 0, "Error allocating memory" );
  79. return DVERR_OUTOFMEMORY;
  80. }
  81. m_fOwned = TRUE;
  82. memcpy( m_pwfxFormat, pwfxFormat, sizeof( WAVEFORMATEX ) );
  83. memcpy( &m_pwfxFormat[1], pvExtra, pwfxFormat->cbSize );
  84. return DV_OK;
  85. }
  86. // Build a standard PCM format
  87. #undef DPF_MODNAME
  88. #define DPF_MODNAME "CWaveFormat::InitializePCM"
  89. HRESULT CWaveFormat::InitializePCM( WORD wHZ, BOOL fStereo, BYTE bBitsPerSample )
  90. {
  91. Cleanup();
  92. m_pwfxFormat = new WAVEFORMATEX;
  93. if( !m_pwfxFormat )
  94. {
  95. DPFX(DPFPREP, 0, "Error allocating memory" );
  96. return DVERR_OUTOFMEMORY;
  97. }
  98. m_fOwned = TRUE;
  99. m_pwfxFormat->wFormatTag = WAVE_FORMAT_PCM;
  100. m_pwfxFormat->nSamplesPerSec = (WORD) wHZ;
  101. m_pwfxFormat->nChannels = (fStereo) ? 2 : 1;
  102. m_pwfxFormat->wBitsPerSample = (WORD) bBitsPerSample;
  103. m_pwfxFormat->nBlockAlign = (bBitsPerSample * m_pwfxFormat->nChannels / 8);
  104. m_pwfxFormat->nAvgBytesPerSec = m_pwfxFormat->nSamplesPerSec * m_pwfxFormat->nBlockAlign;
  105. m_pwfxFormat->cbSize = 0;
  106. return DV_OK;
  107. }
  108. // Create a WAVEFORMAT that is of size dwSize
  109. #undef DPF_MODNAME
  110. #define DPF_MODNAME "CWaveFormat::InitializeMEM"
  111. HRESULT CWaveFormat::InitializeMEM( DWORD dwSize )
  112. {
  113. Cleanup();
  114. m_pwfxFormat = (LPWAVEFORMATEX) new BYTE[dwSize];
  115. if( !m_pwfxFormat )
  116. {
  117. DPFX(DPFPREP, 0, "Error allocating memory" );
  118. return DVERR_OUTOFMEMORY;
  119. }
  120. m_fOwned = TRUE;
  121. return DV_OK;
  122. }
  123. // Initialize but unowned
  124. #undef DPF_MODNAME
  125. #define DPF_MODNAME "CWaveFormat::InitializeUSE"
  126. HRESULT CWaveFormat::InitializeUSE( WAVEFORMATEX *pwfxFormat )
  127. {
  128. Cleanup();
  129. m_pwfxFormat = pwfxFormat;
  130. m_fOwned = FALSE;
  131. return DV_OK;
  132. }
  133. // Set this object equal to the parameter
  134. #undef DPF_MODNAME
  135. #define DPF_MODNAME "CWaveFormat::SetEqual"
  136. HRESULT CWaveFormat::SetEqual( CWaveFormat *pwfxFormat )
  137. {
  138. Cleanup();
  139. if( pwfxFormat )
  140. {
  141. LPWAVEFORMATEX pwfxTmp = pwfxFormat->GetFormat();
  142. DNASSERT( pwfxFormat->GetFormat() );
  143. return Initialize( pwfxTmp->wFormatTag, pwfxTmp->nSamplesPerSec,
  144. pwfxTmp->nChannels, pwfxTmp->wBitsPerSample,
  145. pwfxTmp->nBlockAlign, pwfxTmp->nAvgBytesPerSec,
  146. pwfxTmp->cbSize, (pwfxTmp->cbSize) ? &pwfxTmp[1] : NULL );
  147. }
  148. return DV_OK;
  149. }
  150. // Are these two types equal?
  151. #undef DPF_MODNAME
  152. #define DPF_MODNAME "CWaveFormat::IsEqual"
  153. BOOL CWaveFormat::IsEqual( CWaveFormat *pwfxFormat )
  154. {
  155. if( !pwfxFormat )
  156. return FALSE;
  157. DNASSERT( pwfxFormat->GetFormat() );
  158. if( pwfxFormat->GetFormat()->cbSize != m_pwfxFormat->cbSize )
  159. return FALSE;
  160. if( memcmp( pwfxFormat->GetFormat(), m_pwfxFormat, sizeof( WAVEFORMATEX ) ) != 0 )
  161. return FALSE;
  162. if( memcmp( &(pwfxFormat->GetFormat())[1], &m_pwfxFormat[1], m_pwfxFormat->cbSize ) != 0 )
  163. return FALSE;
  164. return TRUE;
  165. }
  166. // Write the contained value to the registry
  167. #undef DPF_MODNAME
  168. #define DPF_MODNAME "CWaveFormat::WriteREG"
  169. HRESULT CWaveFormat::WriteREG( HKEY hKeyRoot, const WCHAR *wszPath )
  170. {
  171. CRegistry waveKey;
  172. HRESULT hr;
  173. if( !waveKey.Open( hKeyRoot, wszPath, FALSE, TRUE ) )
  174. {
  175. return E_FAIL;
  176. }
  177. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_CBSIZE, m_pwfxFormat->cbSize ) )
  178. {
  179. return E_FAIL;
  180. }
  181. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_RATE, m_pwfxFormat->nSamplesPerSec ) )
  182. {
  183. goto WRITE_FAILURE;
  184. }
  185. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_BITS, m_pwfxFormat->wBitsPerSample ) )
  186. {
  187. goto WRITE_FAILURE;
  188. }
  189. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_CHANNELS, m_pwfxFormat->nChannels ) )
  190. {
  191. goto WRITE_FAILURE;
  192. }
  193. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_TAG, m_pwfxFormat->wFormatTag ) )
  194. {
  195. goto WRITE_FAILURE;
  196. }
  197. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_AVGPERSEC, m_pwfxFormat->nAvgBytesPerSec ) )
  198. {
  199. goto WRITE_FAILURE;
  200. }
  201. if( !waveKey.WriteDWORD( REGISTRY_WAVEFORMAT_BLOCKALIGN, m_pwfxFormat->nBlockAlign ) )
  202. {
  203. goto WRITE_FAILURE;
  204. }
  205. if( !waveKey.WriteBlob( REGISTRY_WAVEFORMAT_CBDATA, (LPBYTE) &m_pwfxFormat[1], m_pwfxFormat->cbSize ) )
  206. {
  207. goto WRITE_FAILURE;
  208. }
  209. return S_OK;
  210. WRITE_FAILURE:
  211. DPFX(DPFPREP, 0, "Error writing waveformat" );
  212. return E_FAIL;
  213. }
  214. // Initialize from registry
  215. #undef DPF_MODNAME
  216. #define DPF_MODNAME "CWaveFormat::InitializeREG"
  217. HRESULT CWaveFormat::InitializeREG( HKEY hKeyRoot, const WCHAR *wszPath )
  218. {
  219. CRegistry waveKey;
  220. HRESULT hr;
  221. if( !waveKey.Open( hKeyRoot, wszPath, TRUE, FALSE ) )
  222. {
  223. return E_FAIL;
  224. }
  225. DWORD dwTmp;
  226. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_CBSIZE, &dwTmp ) )
  227. {
  228. return E_FAIL;
  229. }
  230. m_pwfxFormat = (LPWAVEFORMATEX) new BYTE[dwTmp+sizeof(WAVEFORMATEX)];
  231. if( m_pwfxFormat == NULL )
  232. {
  233. return E_OUTOFMEMORY;
  234. }
  235. m_fOwned = TRUE;
  236. m_pwfxFormat->cbSize = (BYTE) dwTmp;
  237. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_RATE, &dwTmp ) )
  238. {
  239. goto READ_FAILURE;
  240. }
  241. m_pwfxFormat->nSamplesPerSec = dwTmp;
  242. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_BITS, &dwTmp ) )
  243. {
  244. goto READ_FAILURE;
  245. }
  246. m_pwfxFormat->wBitsPerSample = (WORD) dwTmp;
  247. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_CHANNELS, &dwTmp ) )
  248. {
  249. goto READ_FAILURE;
  250. }
  251. m_pwfxFormat->nChannels = (INT) dwTmp;
  252. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_TAG, &dwTmp ) )
  253. {
  254. goto READ_FAILURE;
  255. }
  256. m_pwfxFormat->wFormatTag = (WORD) dwTmp;
  257. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_AVGPERSEC, &dwTmp ) )
  258. {
  259. goto READ_FAILURE;
  260. }
  261. m_pwfxFormat->nAvgBytesPerSec = (INT) dwTmp;
  262. if( !waveKey.ReadDWORD( REGISTRY_WAVEFORMAT_BLOCKALIGN, &dwTmp ) )
  263. {
  264. goto READ_FAILURE;
  265. }
  266. m_pwfxFormat->nBlockAlign = (INT) dwTmp;
  267. dwTmp = m_pwfxFormat->cbSize;
  268. if( !waveKey.ReadBlob( REGISTRY_WAVEFORMAT_CBDATA, (LPBYTE) &m_pwfxFormat[1], &dwTmp ) )
  269. {
  270. DPFX(DPFPREP, DVF_ERRORLEVEL, "Error reading waveformat blob" );
  271. goto READ_FAILURE;
  272. }
  273. return S_OK;
  274. READ_FAILURE:
  275. Cleanup();
  276. return E_FAIL;
  277. }