hxmixer.cpp
Upload User: dangjiwu
Upload Date: 2013-07-19
Package Size: 42019k
Code Size: 8k
Category:

Symbian

Development Platform:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: hxmixer.cpp,v 1.6.36.1 2004/07/09 02:01:10 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. #include "hlxclib/memory.h"
  50.  
  51. #include "hxresult.h"
  52. #include "hxtypes.h"
  53. #include "hxmixer.h"
  54. #include "hxcom.h"
  55. #include "hxheap.h"
  56. #ifdef _DEBUG
  57. #undef HX_THIS_FILE
  58. static const char HX_THIS_FILE[] = __FILE__;
  59. #endif
  60. /************************************************************************
  61.  *  Method:
  62.  *              CHXMixer::MixBuffer()
  63.  *      Purpose:
  64.  * NOTE: This routine may change the Source Buffer contents in order 
  65.  *       to multiple it by volume
  66.  */
  67. ULONG32 CHXMixer::MixBuffer
  68. UCHAR* pSrc
  69. , UCHAR* pDst
  70. , ULONG32 ulNumBytes
  71. , BOOL bChannelConvert
  72. , UINT16 uVolume
  73. , UINT16  uBitsPerSample
  74. , BOOL& bIsDestBufferDirty
  75. )
  76. {
  77.     if (!(bIsDestBufferDirty || bChannelConvert || uVolume != 100))
  78.     {
  79. ::memcpy(pDst, pSrc, ulNumBytes); /* Flawfinder: ignore */
  80. bIsDestBufferDirty = TRUE;
  81. return ulNumBytes;
  82.     }
  83.     ULONG32 nSamples = 0;
  84.     ULONG32 i,j;
  85.     LONG32 lFixedVolume = (uVolume*256)/100;
  86.     if (bIsDestBufferDirty)
  87.     {
  88. if ( uBitsPerSample == 16 )
  89. {
  90.     short   *dst = (short*)pDst;
  91.     short   *src = (short*)pSrc;
  92.     LONG32  lTmp = 0;
  93.     if ( !bChannelConvert )
  94.     {
  95. nSamples = ulNumBytes / 2; 
  96. for(i = 0, j=0; i < nSamples; i++,j++)
  97. {
  98.     lTmp = *(dst+j) + (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  99.     //lTmp =   *(dst+j) +   (*(src+i));
  100.     // clip if need to
  101.     if ( lTmp > 32767 )
  102. *(dst+j) = 32767;
  103.     else if ( lTmp < -32768 )
  104. *(dst+j) = -32768;
  105.     else
  106. *(dst+j) = (short)lTmp;
  107. }
  108.     }
  109.     // Add the sample in again if we are converting this from
  110.     // mono to stereo.
  111.     else if ( bChannelConvert )
  112.     {
  113. short itmp = 0;
  114. nSamples = ulNumBytes / 2; 
  115. for(i = 0, j=0; i < nSamples; i++,j++)
  116. {
  117.     lTmp = *(dst+j) + (LONG32) (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  118.     //lTmp = *(dst+j) + (*(src+i));
  119.     // clip if need to
  120.     if ( lTmp > 32767 )
  121. (*(dst+j)) = 32767;
  122.     else if ( lTmp < -32768 )
  123. (*(dst+j)) = -32768;
  124.     else
  125. (*(dst+j)) = (short)lTmp;
  126.     // Add this sample in twice for stereo
  127.     itmp = (*(dst+j));
  128.     j++;
  129.     (*(dst+j)) = itmp;
  130. }    
  131.     }
  132. }
  133. else if ( uBitsPerSample == 8 )
  134. {
  135.     UCHAR* dst = pDst;
  136.     UCHAR* src = pSrc; 
  137.     LONG32 uTmp = 0;
  138.     for(i = 0; i < ulNumBytes; i++)
  139.     {
  140. uTmp = (LONG32) *(dst+i) + (LONG32) (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  141. //uTmp = *(dst+i) +  (*(src+i)) ;
  142. // clip if needed
  143. if ( uTmp > 255 )
  144.     *(dst+i) = 255;
  145. else
  146.     *(dst+i) = (UCHAR)uTmp;
  147.     }
  148. }
  149.     }
  150.     else
  151.     {
  152. if ( uBitsPerSample == 16 )
  153. {
  154.     short   *dst = (short*)pDst;
  155.     short   *src = (short*)pSrc;
  156.     LONG32  lTmp = 0;
  157.     if ( !bChannelConvert )
  158.     {
  159. nSamples = ulNumBytes / 2; 
  160. for(i = 0, j=0; i < nSamples; i++,j++)
  161. {
  162.     lTmp = (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  163.     // clip if need to
  164.     if ( lTmp > 32767 )
  165. *(dst+j) = 32767;
  166.     else if ( lTmp < -32768 )
  167. *(dst+j) = -32768;
  168.     else
  169. *(dst+j) = (short)lTmp;
  170. }
  171.     }
  172.     // Add the sample in again if we are converting this from
  173.     // mono to stereo.
  174.     else if ( bChannelConvert )
  175.     {
  176. short itmp = 0;
  177. nSamples = ulNumBytes / 2; 
  178. for(i = 0, j=0; i < nSamples; i++,j++)
  179. {
  180.     lTmp = (LONG32) (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  181.     //lTmp = *(dst+j) + (*(src+i));
  182.     // clip if need to
  183.     if ( lTmp > 32767 )
  184. (*(dst+j)) = 32767;
  185.     else if ( lTmp < -32768 )
  186. (*(dst+j)) = -32768;
  187.     else
  188. (*(dst+j)) = (short)lTmp;
  189.     // Add this sample in twice for stereo
  190.     itmp = (*(dst+j));
  191.     j++;
  192.     (*(dst+j)) = itmp;
  193. }    
  194.     }
  195. }
  196. else if ( uBitsPerSample == 8 )
  197. {
  198.     UCHAR* dst = pDst;
  199.     UCHAR* src = pSrc; 
  200.     LONG32 uTmp = 0;
  201.     for(i = 0; i < ulNumBytes; i++)
  202.     {
  203. uTmp = (LONG32) (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  204. //uTmp = *(dst+i) +  (*(src+i)) ;
  205. // clip if needed
  206. if ( uTmp > 255 )
  207.     *(dst+i) = 255;
  208. else
  209.     *(dst+i) = (UCHAR)uTmp;
  210.     }
  211. }
  212.     }
  213.     bIsDestBufferDirty = TRUE;
  214.     return bChannelConvert ? ulNumBytes*2 : ulNumBytes;
  215. }
  216. /************************************************************************
  217.  *  Method:
  218.  *              CHXMixer::ApplyVolume()
  219.  *      Purpose:
  220.  * NOTE: This routine simply multiplies input buffer by volume 
  221.  */
  222. void CHXMixer::ApplyVolume
  223. UCHAR* pSrc
  224. , ULONG32 ulNumBytes
  225. , UINT16 uVolume
  226. , UINT16  uBitsPerSample
  227. )
  228. {
  229.     ULONG32 nSamples = 0;
  230.     ULONG32 i;
  231.     LONG32 lFixedVolume = (uVolume*256)/100;
  232.     if ( uBitsPerSample == 16 )
  233.     {
  234. short   *dst = (short*)pSrc;
  235. short   *src = (short*)pSrc;
  236. LONG32  lTmp = 0;
  237. nSamples = ulNumBytes / 2; 
  238. for(i = 0; i < nSamples; i++)
  239. {
  240.     lTmp = (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  241.     //lTmp =   *(dst+j) +   (*(src+i));
  242.     // clip if need to
  243.     if ( lTmp > 32767 )
  244. *(dst+i) = 32767;
  245.     else if ( lTmp < -32768 )
  246. *(dst+i) = -32768;
  247.     else
  248. *(dst+i) = (short)lTmp;
  249. }
  250.     }
  251.     else if ( uBitsPerSample == 8 )
  252.     {
  253. UCHAR* dst = pSrc;
  254. UCHAR* src = pSrc; 
  255. LONG32 uTmp = 0;
  256. for(i = 0; i < ulNumBytes; i++)
  257.         {
  258.     uTmp = (LONG32) (((LONG32)(lFixedVolume * (*(src+i)))) >> 8);
  259.     //uTmp = *(dst+i) +  (*(src+i)) ;
  260.     // clip if needed
  261.     if ( uTmp > 255 )
  262. *(dst+i) = 255;
  263.     else
  264. *(dst+i) = (UCHAR)uTmp;
  265.         }
  266.     }
  267.     return;
  268. }