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.

243 lines
6.6 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: MixerUtils.cpp
  6. * Content: Utility functions for mixing audio
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/06/99 rodtoll Created It
  12. *
  13. ***************************************************************************/
  14. #include "dxvoicepch.h"
  15. #define DPV_MAX_SHORT ((SHORT)32767)
  16. #define DPV_MIN_SHORT ((SHORT)-32768)
  17. #define DPV_MAX_BYTE 127
  18. #define DPV_MIN_BYTE 0
  19. // "Mixer Buffer"
  20. //
  21. // Throughout this module we refer to a "mixer buffer". A mixer buffer is a
  22. // buffer of higher resolution then a traditional audio buffer which is used
  23. // for mixing audio. In the case of this module a mixer buffer promotes
  24. // each sample to a DWORD. So audio is mixed in a "mixer buffer" and then
  25. // converted back to the approrpriate sample size.
  26. #undef DPF_MODNAME
  27. #define DPF_MODNAME "FillBufferWithSilence"
  28. // FillBufferWithSilence
  29. //
  30. // This function fills a mixer buffer with the appropriate bytes to make it
  31. // equivalent to silence.
  32. //
  33. // Parameters:
  34. // LONG *buffer -
  35. // Pointer to the mixer buffer which will be filled with silence.
  36. // BOOL eightBit -
  37. // Is the audio we're mixing 8 bit? (Set to TRUE for 8 bit)
  38. // LONG frameSize -
  39. // The number of samples the mixer buffer consists of.
  40. //
  41. // Returns:
  42. // N/A
  43. //
  44. void FillBufferWithSilence( LONG *buffer, BOOL eightBit, LONG frameSize )
  45. {
  46. LONG mixerSize = frameSize;
  47. // If we're working with 16 bit then the number of samples is half the
  48. // number of bytes in a frame.
  49. if( !eightBit )
  50. {
  51. mixerSize >>= 1;
  52. }
  53. BYTE silenceByte = (eightBit) ? 0x80 : 0x00;
  54. // Set the mixer buffer to silence
  55. memset( buffer, silenceByte, mixerSize << 2 );
  56. }
  57. #undef DPF_MODNAME
  58. #define DPF_MODNAME "MixIn8BitBuffer"
  59. // MixIn8BitBuffer
  60. //
  61. // This function mixes an 8-bit buffer with an existing mixer buffer.
  62. //
  63. // Parameters:
  64. // LONG *mixerBuffer -
  65. // Pointer to the mixer buffer
  66. // BYTE *sourceBuffer -
  67. // Pointer to the buffer which will be mixed into the mixer buffer
  68. // LONG frameSize -
  69. // The size, in bytes of the source Buffer. (Also = # of samples)
  70. //
  71. // Returns:
  72. // N/A
  73. //
  74. void MixIn8BitBuffer( LONG *mixerBuffer, const BYTE *sourceBuffer, LONG frameSize )
  75. {
  76. LONG mixerSize = frameSize;
  77. for( int index = 0; index < mixerSize; index++ )
  78. {
  79. mixerBuffer[index] += sourceBuffer[index];
  80. }
  81. }
  82. #undef DPF_MODNAME
  83. #define DPF_MODNAME "MixIn16BitBuffer"
  84. // MixIn16BitBuffer
  85. //
  86. // This function mixes a 16-bit buffer with an existing mixer buffer.
  87. //
  88. // Parameters:
  89. // LONG *mixerBuffer -
  90. // Pointer to the mixer buffer
  91. // SHORT *sourceBuffer -
  92. // Pointer to the buffer which will be mixed into the mixer buffer
  93. // LONG frameSize -
  94. // The size, in bytes of the source Buffer. (Since the sourceBuffer
  95. // is 16 bit, the number of samples is # of bytes / 2).
  96. //
  97. // Returns:
  98. // N/A
  99. //
  100. void MixIn16BitBuffer( LONG *mixerBuffer, const SHORT *sourceBuffer, LONG frameSize )
  101. {
  102. LONG mixerSize = frameSize >> 1;
  103. for( int index = 0; index < mixerSize; index++ )
  104. {
  105. mixerBuffer[index] += sourceBuffer[index];
  106. }
  107. }
  108. #undef DPF_MODNAME
  109. #define DPF_MODNAME "MixInBuffer"
  110. // MixInBitBuffer
  111. //
  112. // This function mixes an 8 or 16-bit buffer with an existing mixer buffer.
  113. //
  114. // Parameters:
  115. // LONG *mixerBuffer -
  116. // Pointer to the mixer buffer
  117. // BYTE *sourceBuffer -
  118. // Pointer to the buffer which will be mixed into the mixer buffer
  119. // LONG frameSize -
  120. // The size, in bytes of the source Buffer.
  121. //
  122. // Returns:
  123. // N/A
  124. //
  125. void MixInBuffer( LONG *mixerBuffer, const BYTE *sourceBuffer, BOOL eightBit, LONG frameSize )
  126. {
  127. if( eightBit )
  128. {
  129. MixIn8BitBuffer( mixerBuffer, sourceBuffer, frameSize );
  130. }
  131. else
  132. {
  133. MixIn16BitBuffer( mixerBuffer, (SHORT *) sourceBuffer, frameSize );
  134. }
  135. }
  136. #undef DPF_MODNAME
  137. #define DPF_MODNAME "Normalize16BitBuffer"
  138. // Normalize16BitBuffer
  139. //
  140. // This function takes the "mixer buffer" and transfers the result of
  141. // the mix back to a 16-bit audio buffer.
  142. //
  143. // Parameters:
  144. // SHORT *targetBuffer -
  145. // Pointer to the buffer which where the mixed audio will be placed
  146. // LONG *mixerBuffer -
  147. // Pointer to the mixer buffer
  148. // LONG frameSize -
  149. // The size, in bytes of the target buffer.
  150. //
  151. // Returns:
  152. // N/A
  153. //
  154. void Normalize16BitBuffer( SHORT *targetBuffer, const LONG *mixerBuffer, LONG frameSize )
  155. {
  156. LONG mixerSize = frameSize >> 1;
  157. for( int index = 0; index < mixerSize; index++ )
  158. {
  159. // Clip mixed audio, ensure it does not exceed range
  160. if( mixerBuffer[index] >= DPV_MAX_SHORT )
  161. targetBuffer[index] = DPV_MAX_SHORT;
  162. else if( mixerBuffer[index] <= DPV_MIN_SHORT )
  163. targetBuffer[index] = DPV_MIN_SHORT;
  164. else
  165. targetBuffer[index] = (SHORT) mixerBuffer[index]; // / noiseCount;
  166. }
  167. }
  168. #undef DPF_MODNAME
  169. #define DPF_MODNAME "Normalize8BitBuffer"
  170. // Normalize8BitBuffer
  171. //
  172. // This function takes the "mixer buffer" and transfers the result of
  173. // the mix back to an 8-bit audio buffer.
  174. //
  175. // Parameters:
  176. // BYTE *targetBuffer -
  177. // Pointer to the buffer which where the mixed audio will be placed
  178. // LONG *mixerBuffer -
  179. // Pointer to the mixer buffer
  180. // LONG frameSize -
  181. // The size, in bytes of the target buffer.
  182. //
  183. // Returns:
  184. // N/A
  185. //
  186. void Normalize8BitBuffer( BYTE *targetBuffer, const LONG *mixerBuffer, LONG frameSize )
  187. {
  188. LONG mixerSize = frameSize;
  189. for( int index = 0; index < mixerSize; index++ )
  190. {
  191. targetBuffer[index] = (BYTE) mixerBuffer[index]; /// noiseCount;
  192. }
  193. }
  194. #undef DPF_MODNAME
  195. #define DPF_MODNAME "NormalizeBuffer"
  196. // NormalizeBuffer
  197. //
  198. // This function takes the "mixer buffer" and transfers the result of
  199. // the mix back to an 8-bit or 160bit audio buffer.
  200. //
  201. // Parameters:
  202. // BYTE *targetBuffer -
  203. // Pointer to the buffer which where the mixed audio will be placed
  204. // LONG *mixerBuffer -
  205. // Pointer to the mixer buffer
  206. // BOOL eightBit -
  207. // If the buffer is 8-bit, set this to TRUE, set to FALSE for 16-bit
  208. // LONG frameSize -
  209. // The size, in bytes of the target buffer.
  210. //
  211. // Returns:
  212. // N/A
  213. //
  214. void NormalizeBuffer( BYTE *targetBuffer, const LONG *mixerBuffer, BOOL eightBit, LONG frameSize )
  215. {
  216. if( eightBit )
  217. {
  218. Normalize8BitBuffer( targetBuffer, mixerBuffer, frameSize );
  219. }
  220. else
  221. {
  222. Normalize16BitBuffer( (SHORT *) targetBuffer, mixerBuffer, frameSize );
  223. }
  224. }