Counter Strike : Global Offensive Source Code
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.

217 lines
6.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /* This product contains Speex software. The license terms of the Speex
  9. software, distributed with this product, are as follows:
  10. 2002-2003, Jean-Marc Valin/Xiph.Org Foundation
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. - Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or
  18. other materials provided with the distribution.
  19. Neither the name of the Xiph.org Foundation nor the names of its
  20. contributors may be used to endorse or promote products derived from this
  21. software without specific prior written permission.
  22. This software is provided by the copyright holders and contributors "as is"
  23. and any express or implied warranties, including, but not limited to, the
  24. implied warranties of merchantability and fitness for a particular purpose
  25. are disclaimed. In no event shall the foundation or contributors be liable
  26. for any direct, indirect, incidental, special, exemplary, or consequential
  27. damages (including, but not limited to, procurement of substitute goods or
  28. services; loss of use, data, or profits; or business interruption) however
  29. caused and on any theory of liability, whether in contract, strict
  30. liability, or tort (including negligence or otherwise) arising in any way
  31. out of the use of this software, even if advised of the possibility of such
  32. damage. */
  33. #include "ivoicecodec.h"
  34. #include "VoiceEncoder_Speex.h"
  35. #include <stdio.h>
  36. // NOTE: This has to be the last file included!
  37. #include "tier0/memdbgon.h"
  38. #define SAMPLERATE 8000 // get 8000 samples/sec
  39. #define RAW_FRAME_SIZE 160 // in 160 samples per frame
  40. // each quality has a differnt farme size
  41. const int ENCODED_FRAME_SIZE [11] = {6,6,15,15,20,20,28,28,38,38,38};
  42. /* useful Speex voice qualities are 0,2,4,6 and 8. each quality level
  43. has a diffrent encoded frame size and needed bitrate:
  44. Quality 0 : 6 bytes/frame, 2400bps
  45. Quality 2 : 15 bytes/frame, 6000bps
  46. Quality 4 : 20 bytes/frame, 8000bps
  47. Quality 6 : 28 bytes/frame, 11200bps
  48. Quality 8 : 38 bytes/frame, 15200bps */
  49. extern IVoiceCodec* CreateVoiceCodec_Frame(IFrameEncoder *pEncoder);
  50. void* CreateSpeexVoiceCodec()
  51. {
  52. IFrameEncoder *pEncoder = new VoiceEncoder_Speex;
  53. return CreateVoiceCodec_Frame( pEncoder );
  54. }
  55. EXPOSE_INTERFACE_FN(CreateSpeexVoiceCodec, IVoiceCodec, "vaudio_speex")
  56. //////////////////////////////////////////////////////////////////////
  57. // Construction/Destruction
  58. //////////////////////////////////////////////////////////////////////
  59. VoiceEncoder_Speex::VoiceEncoder_Speex()
  60. {
  61. m_EncoderState = NULL;
  62. m_DecoderState = NULL;
  63. m_Quality = 0;
  64. }
  65. VoiceEncoder_Speex::~VoiceEncoder_Speex()
  66. {
  67. TermStates();
  68. }
  69. bool VoiceEncoder_Speex::Init(int quality, int &rawFrameSize, int &encodedFrameSize)
  70. {
  71. if ( !InitStates() )
  72. return false;
  73. rawFrameSize = RAW_FRAME_SIZE * BYTES_PER_SAMPLE;
  74. // map gerneral voice quality 1-5 to speex quality levels
  75. switch ( quality )
  76. {
  77. case 1 : m_Quality = 0; break;
  78. case 2 : m_Quality = 2; break;
  79. case 3 : m_Quality = 4; break;
  80. case 4 : m_Quality = 6; break;
  81. case 5 : m_Quality = 8; break;
  82. default : m_Quality = 0; break;
  83. }
  84. encodedFrameSize = ENCODED_FRAME_SIZE[m_Quality];
  85. speex_encoder_ctl( m_EncoderState, SPEEX_SET_QUALITY, &m_Quality);
  86. speex_decoder_ctl( m_DecoderState, SPEEX_SET_QUALITY, &m_Quality);
  87. int postfilter = 1; // Set the perceptual enhancement on
  88. speex_decoder_ctl( m_DecoderState, SPEEX_SET_ENH, &postfilter);
  89. int samplerate = SAMPLERATE;
  90. speex_decoder_ctl( m_DecoderState, SPEEX_SET_SAMPLING_RATE, &samplerate );
  91. speex_encoder_ctl( m_EncoderState, SPEEX_SET_SAMPLING_RATE, &samplerate );
  92. return true;
  93. }
  94. void VoiceEncoder_Speex::Release()
  95. {
  96. delete this;
  97. }
  98. void VoiceEncoder_Speex::EncodeFrame(const char *pUncompressedBytes, char *pCompressed)
  99. {
  100. float input[RAW_FRAME_SIZE];
  101. short * in = (short*)pUncompressedBytes;
  102. /*Copy the 16 bits values to float so Speex can work on them*/
  103. for (int i=0;i<RAW_FRAME_SIZE;i++)
  104. {
  105. input[i]=(float)*in;
  106. in++;
  107. }
  108. /*Flush all the bits in the struct so we can encode a new frame*/
  109. speex_bits_reset( &m_Bits );
  110. /*Encode the frame*/
  111. speex_encode( m_EncoderState, input, &m_Bits );
  112. /*Copy the bits to an array of char that can be written*/
  113. int size;
  114. size = speex_bits_write(&m_Bits, pCompressed, ENCODED_FRAME_SIZE[m_Quality] );
  115. // char text[255]; _snprintf(text, 255, "outsize %i,", size ); OutputDebugStr( text );
  116. }
  117. void VoiceEncoder_Speex::DecodeFrame(const char *pCompressed, char *pDecompressedBytes)
  118. {
  119. float output[RAW_FRAME_SIZE];
  120. short * out = (short*)pDecompressedBytes;
  121. if (pCompressed == NULL)
  122. {
  123. for (int i=0;i<RAW_FRAME_SIZE;i++)
  124. {
  125. *out = (short)0;
  126. out++;
  127. }
  128. return;
  129. }
  130. /*Copy the data into the bit-stream struct*/
  131. speex_bits_read_from(&m_Bits, (char *)pCompressed, ENCODED_FRAME_SIZE[m_Quality] );
  132. /*Decode the data*/
  133. speex_decode(m_DecoderState, &m_Bits, output);
  134. /*Copy from float to short (16 bits) for output*/
  135. for (int i=0;i<RAW_FRAME_SIZE;i++)
  136. {
  137. *out = (short)output[i];
  138. out++;
  139. }
  140. }
  141. bool VoiceEncoder_Speex::ResetState()
  142. {
  143. speex_encoder_ctl(m_EncoderState, SPEEX_RESET_STATE , NULL );
  144. speex_decoder_ctl(m_DecoderState, SPEEX_RESET_STATE , NULL );
  145. return true;
  146. }
  147. bool VoiceEncoder_Speex::InitStates()
  148. {
  149. speex_bits_init(&m_Bits);
  150. m_EncoderState = speex_encoder_init( &speex_nb_mode ); // narrow band mode 8kbp
  151. m_DecoderState = speex_decoder_init( &speex_nb_mode );
  152. return m_EncoderState && m_DecoderState;
  153. }
  154. void VoiceEncoder_Speex::TermStates()
  155. {
  156. if(m_EncoderState)
  157. {
  158. speex_encoder_destroy( m_EncoderState );
  159. m_EncoderState = NULL;
  160. }
  161. if(m_DecoderState)
  162. {
  163. speex_decoder_destroy( m_DecoderState );
  164. m_DecoderState = NULL;
  165. }
  166. speex_bits_destroy( &m_Bits );
  167. }