Source code of Windows XP (NT5)
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.

298 lines
9.0 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: wirecs.cpp
  6. * Content:
  7. * This module contains the implementation of the WaveInException class
  8. * the recording format db.
  9. *
  10. * History:
  11. * Date By Reason
  12. * ==== == ======
  13. * 07/16/99 rodtoll Created
  14. * 08/25/99 rodtoll General Cleanup/Modifications to support new
  15. * compression sub-system.
  16. * 09/03/99 rodtoll Fixed WaveFormatToString
  17. * 09/20/99 rodtoll Updated to check for memory allocation failures
  18. * 10/05/99 rodtoll Added DPF_MODNAMES
  19. * 03/28/2000 rodtoll Removed code which was no longer used
  20. * 04/14/2000 rodtoll Fix: Bug #32498 - Updating format list to ensure that 8Khz formats are
  21. * tried first to reduce compression overhead / quality loss
  22. *
  23. ***************************************************************************/
  24. #include "dxvutilspch.h"
  25. #undef DPF_SUBCOMP
  26. #define DPF_SUBCOMP DN_SUBCOMP_VOICE
  27. #define __VOXWARE
  28. // NUM_RECORD_FORMATS
  29. //
  30. // This define determines the number of recording formats which
  31. // will be present in the recording db. (Since they are currently
  32. // hard-coded.
  33. #define NUM_RECORD_FORMATS 16
  34. #define MODULE_ID WAVEINUTILS
  35. // g_waveInDBInitialized
  36. //
  37. // This flag is used to report when the recording db has been initialized.
  38. BOOL g_waveInDBInitialized = FALSE;
  39. // g_pwfRecordFormats
  40. //
  41. // This is the actual record format db. It contains a list of the formats
  42. // that are tried when attempting to find a format which will allow
  43. // full duplex operation. They are listed in the order in which they
  44. // should be tried.
  45. WAVEFORMATEX **g_pwfRecordFormats;
  46. #undef DPF_MODNAME
  47. #define DPF_MODNAME "GetRecordFormat"
  48. // GetRecordFormat
  49. //
  50. // This function returns the recording format at the index specified
  51. // by index in the recording format DB.
  52. //
  53. // The recording format db must be initialized before this can be called.
  54. //
  55. // Parameters:
  56. // UINT index -
  57. // The 0-based index into the recording format db that the user
  58. // wishes to retrieve.
  59. //
  60. // Returns:
  61. // WAVEFORMATEX * -
  62. // A pointer to a WAVEFORMATEX structure describing the format
  63. // at the given index in the recording db. This will be NULL
  64. // if index >= NUM_RECORD_FORMATS or if the recording db has
  65. // not been initialized.
  66. //
  67. // WARNING:
  68. // The pointer returned is to the actual entry in the recording db and
  69. // is owned by it. Therefore the caller should not modify or free
  70. // the memory returned by the pointer.
  71. //
  72. WAVEFORMATEX *GetRecordFormat( UINT index )
  73. {
  74. if( !g_waveInDBInitialized )
  75. return NULL;
  76. if( index >= NUM_RECORD_FORMATS )
  77. {
  78. return NULL;
  79. }
  80. else
  81. {
  82. return g_pwfRecordFormats[index];
  83. }
  84. }
  85. #undef DPF_MODNAME
  86. #define DPF_MODNAME "GetNumRecordFormats"
  87. // GetNumRecordFormats
  88. //
  89. // This function returns the number of recording formats stored
  90. // in the recording format db.
  91. //
  92. // Parameters:
  93. // N/A
  94. //
  95. // Returns:
  96. // UINT -
  97. // The number of formats in the recording format db.
  98. //
  99. UINT GetNumRecordFormats()
  100. {
  101. if( !g_waveInDBInitialized )
  102. return 0;
  103. return NUM_RECORD_FORMATS;
  104. }
  105. #undef DPF_MODNAME
  106. #define DPF_MODNAME "InitRecordFormats"
  107. // InitRecordFormats
  108. //
  109. // This function initializes the recording format db with the formats which
  110. // should be tried when initializing recording. This should be the first
  111. // function called from the recording format db.
  112. //
  113. // Parameters:
  114. // N/A
  115. //
  116. // Returns:
  117. // N/A
  118. //
  119. void InitRecordFormats()
  120. {
  121. if( g_waveInDBInitialized )
  122. return;
  123. DPFX(DPFPREP, DVF_ENTRYLEVEL, "- WDB: Init End" );
  124. g_pwfRecordFormats = new WAVEFORMATEX*[NUM_RECORD_FORMATS];
  125. if( g_pwfRecordFormats == NULL )
  126. {
  127. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to init recordb, memory alloc failure" );
  128. return;
  129. }
  130. g_pwfRecordFormats[0] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 8000, 16 );
  131. g_pwfRecordFormats[1] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 8000, 8 );
  132. g_pwfRecordFormats[2] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 11025, 16 );
  133. g_pwfRecordFormats[3] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 22050, 16 );
  134. g_pwfRecordFormats[4] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 44100, 16 );
  135. g_pwfRecordFormats[5] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 11025, 8 );
  136. g_pwfRecordFormats[6] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 22050, 8 );
  137. g_pwfRecordFormats[7] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 44100, 8 );
  138. g_pwfRecordFormats[8] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 8000, 16 );
  139. g_pwfRecordFormats[9] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 8000, 8 );
  140. g_pwfRecordFormats[10] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 11025, 16 );
  141. g_pwfRecordFormats[11] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 22050, 16 );
  142. g_pwfRecordFormats[12] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 44100, 16 );
  143. g_pwfRecordFormats[13] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 11025, 8 );
  144. g_pwfRecordFormats[14] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 22050, 8 );
  145. g_pwfRecordFormats[15] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 44100, 8 );
  146. /*
  147. g_pwfRecordFormats[0] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 22050, 8 );
  148. g_pwfRecordFormats[1] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 22050, 8 );
  149. g_pwfRecordFormats[2] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 22050, 16 );
  150. g_pwfRecordFormats[3] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 22050, 16 );
  151. g_pwfRecordFormats[4] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 11025, 8 );
  152. g_pwfRecordFormats[5] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 11025, 8 );
  153. g_pwfRecordFormats[6] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 11025, 16 );
  154. g_pwfRecordFormats[7] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 11025, 16 );
  155. g_pwfRecordFormats[8] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 44100, 8 );
  156. g_pwfRecordFormats[9] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 44100, 8 );
  157. g_pwfRecordFormats[10] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 44100, 16 );
  158. g_pwfRecordFormats[11] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 44100, 16 );
  159. g_pwfRecordFormats[12] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 8000, 16 );
  160. g_pwfRecordFormats[13] = CreateWaveFormat( WAVE_FORMAT_PCM, FALSE, 8000, 8 );
  161. g_pwfRecordFormats[14] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 8000, 16 );
  162. g_pwfRecordFormats[15] = CreateWaveFormat( WAVE_FORMAT_PCM, TRUE, 8000, 8 );
  163. */
  164. g_waveInDBInitialized = TRUE;
  165. DPFX(DPFPREP, DVF_ENTRYLEVEL, "- WDB: Init End" );
  166. }
  167. #undef DPF_MODNAME
  168. #define DPF_MODNAME "DeInitRecordFormats"
  169. // DeInitRecordFormats
  170. //
  171. // This function releases the memory associated with the recording
  172. // format DB.
  173. //
  174. // Parameters:
  175. // N/A
  176. //
  177. // Returns:
  178. // N/A
  179. //
  180. void DeInitRecordFormats()
  181. {
  182. if( g_waveInDBInitialized )
  183. {
  184. DPFX(DPFPREP, DVF_INFOLEVEL, "- WDB: DeInit Begin" );
  185. for( int index = 0; index < NUM_RECORD_FORMATS; index++ )
  186. {
  187. delete g_pwfRecordFormats[index];
  188. }
  189. delete [] g_pwfRecordFormats;
  190. DPFX(DPFPREP, DVF_INFOLEVEL, "- WDB: DeInit End" );
  191. g_waveInDBInitialized = FALSE;
  192. }
  193. }
  194. #undef DPF_MODNAME
  195. #define DPF_MODNAME "CreateWaveFormat"
  196. // CreateWaveFormat
  197. //
  198. // This utility function is used to allocate and fill WAVEFORMATEX
  199. // structures for the various formats used. This function
  200. // currently supports the following formats:
  201. //
  202. // WAVE_FORMAT_ADPCM
  203. // WAVE_FORMAT_DSPGROUP_TRUESPEECH
  204. // WAVE_FORMAT_GSM610
  205. // WAVE_FORMAT_LH_CODEC
  206. // WAVE_FORMAT_PCM
  207. //
  208. // The function will allocate the required memory for the sturcture
  209. // (including extra bytes) as required by the format and will fill
  210. // in all the members of the sturcture. The structure which is
  211. // returned belongs to the caller and must be deallocated by the
  212. // caller.
  213. //
  214. // Parameters:
  215. // short formatTag -
  216. // The format tag for the wav format.
  217. //
  218. // BOOL stereo -
  219. // Specify TRUE for stereo, FALSE for mono
  220. //
  221. // int hz -
  222. // Specify the sampling rate of the format. E.g. 22050
  223. //
  224. // int bits -
  225. // Specify the number of bits / sample. E.g. 8 or 16
  226. //
  227. // Returns:
  228. // WAVEFORMATEX * -
  229. // A pointer to a newly allocated WAVEFORMATEX structure
  230. // for the specified format, or NULL if format is not supported
  231. //
  232. WAVEFORMATEX *CreateWaveFormat( short formatTag, BOOL stereo, int hz, int bits ) {
  233. switch( formatTag ) {
  234. case WAVE_FORMAT_PCM:
  235. {
  236. WAVEFORMATEX *format = new WAVEFORMATEX;
  237. if( format == NULL )
  238. {
  239. goto EXIT_MEMALLOC_CREATEWAV;
  240. }
  241. format->wFormatTag = WAVE_FORMAT_PCM;
  242. format->nSamplesPerSec = hz;
  243. format->nChannels = (stereo) ? 2 : 1;
  244. format->wBitsPerSample = (WORD) bits;
  245. format->nBlockAlign = (bits * format->nChannels / 8);
  246. format->nAvgBytesPerSec = format->nSamplesPerSec * format->nBlockAlign;
  247. format->cbSize = 0;
  248. return format;
  249. }
  250. break;
  251. default:
  252. DNASSERT( TRUE );
  253. }
  254. EXIT_MEMALLOC_CREATEWAV:
  255. DPFX(DPFPREP, DVF_ERRORLEVEL, "Unable to alloc buffer for waveformat, or invalid format" );
  256. return NULL;
  257. }