CPI_Equaliser_Basic.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:7k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "globals.h"
  3. #include "CPI_Player.h"
  4. #include "CPI_Equaliser.h"
  5. typedef struct _CPs_EqualiserContext_Basic
  6. {
  7.     BOOL m_bEnabled;
  8.     int m_aryLevels[10];
  9.     BOOL m_bStreamIsCapable;
  10.     unsigned int m_aryHistory[256];
  11.     unsigned int m_aryFuture[256];
  12.     unsigned int m_iCursor;
  13.     int m_arySum_left[9];
  14.     int m_arySum_right[9];
  15. } CPs_EqualiserContext_Basic;
  16. #define CIC_WRAPSAMPLE(expr) (expr&0xFF)
  17. #define CIC_DECODESAMPLE_LEFT(expr) (*(short*)&(expr))
  18. #define CIC_DECODESAMPLE_RIGHT(expr) (*((short*)&(expr) + 1))
  19. #define CIC_FPMULTIPLY(expr1, expr2) ( ( (expr1) * (expr2) )>>8 )
  20. #define CIC_TRUNCATESHORT(expr)    (((expr) > SHRT_MAX) ? SHRT_MAX : (((expr) < SHRT_MIN) ? SHRT_MIN : (expr)))
  21. const int glb_iEQOffsets[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };
  22. const int glb_iEQOffsets_his[] = { -1, -2, -4, -8, -16, -32, -64, -128, -256 };
  23. void CPP_EBSC_Initialise(CPs_EqualiserModule* pModule, const int iFrequency, const BOOL b16bit);
  24. void CPP_EBSC_Uninitialise(CPs_EqualiserModule* pModule);
  25. void CPP_EBSC_ApplySettings(CPs_EqualiserModule* pModule, const CPs_EQSettings* pSettings, BOOL* pbEnableStateChanged);
  26. void CPP_EBSC_ApplyEQToBlock_Inplace(CPs_EqualiserModule* pModule, void* pPCMBlock, const DWORD dwBlockSize);
  27. //
  28. void CPI_Player_Equaliser_Initialise_Basic(CPs_EqualiserModule* pModule)
  29. {
  30.     CPs_EqualiserContext_Basic* pContext;
  31.     pModule->Initialise = CPP_EBSC_Initialise;
  32.     pModule->Uninitialise = CPP_EBSC_Uninitialise;
  33.     pModule->ApplySettings = CPP_EBSC_ApplySettings;
  34.     pModule->ApplyEQToBlock_Inplace = CPP_EBSC_ApplyEQToBlock_Inplace;
  35.     pContext = (CPs_EqualiserContext_Basic*)malloc(sizeof(CPs_EqualiserContext_Basic));
  36.     pModule->m_pModuleCookie = pContext;
  37.     memset(pContext->m_aryLevels, 0, sizeof(pContext->m_aryLevels));
  38.     pContext->m_bEnabled = FALSE;
  39. }
  40. //
  41. void CPP_EBSC_Initialise(CPs_EqualiserModule* pModule, const int iFrequency, const BOOL b16bit)
  42. {
  43.     CPs_EqualiserContext_Basic* pContext = (CPs_EqualiserContext_Basic*)pModule->m_pModuleCookie;
  44.     CP_TRACE0("Equaliser (re)initialising");
  45.     memset(pContext->m_aryHistory, 0, sizeof(pContext->m_aryHistory));
  46.     memset(pContext->m_aryFuture, 0, sizeof(pContext->m_aryFuture));
  47.     memset(pContext->m_arySum_left, 0, sizeof(pContext->m_arySum_left));
  48.     memset(pContext->m_arySum_right, 0, sizeof(pContext->m_arySum_right));
  49.     pContext->m_iCursor = 0;
  50.     if(iFrequency == 44100 && b16bit == TRUE)
  51.         pContext->m_bStreamIsCapable = TRUE;
  52.     else
  53.         pContext->m_bStreamIsCapable = FALSE;
  54. }
  55. //
  56. void CPP_EBSC_Uninitialise(CPs_EqualiserModule* pModule)
  57. {
  58.     CPs_EqualiserContext_Basic* pContext = (CPs_EqualiserContext_Basic*)pModule->m_pModuleCookie;
  59.     CP_CHECKOBJECT(pContext);
  60.     CP_TRACE0("Equaliser shutting down");
  61.     free(pContext);
  62.     pModule->m_pModuleCookie = NULL;
  63. }
  64. //
  65. void CPP_EBSC_ApplySettings(CPs_EqualiserModule* pModule, const CPs_EQSettings* pSettings, BOOL* pbEnableStateChanged)
  66. {
  67.     CPs_EqualiserContext_Basic* pContext = (CPs_EqualiserContext_Basic*)pModule->m_pModuleCookie;
  68.     int iBandIDX = 0;
  69.     for(iBandIDX=0; iBandIDX < 8; iBandIDX++)
  70.         pContext->m_aryLevels[9-iBandIDX] = (pSettings->m_aryBands[iBandIDX]<<1) + 256;
  71.     pContext->m_aryLevels[0] = pContext->m_aryLevels[2];
  72.     pContext->m_aryLevels[1] = pContext->m_aryLevels[2];
  73.     if(pContext->m_bEnabled == pSettings->m_bEnabled)
  74.         *pbEnableStateChanged = FALSE;
  75.     else
  76.         *pbEnableStateChanged = TRUE;
  77.     pContext->m_bEnabled = pSettings->m_bEnabled;
  78. }
  79. //
  80. void CPP_EBSC_ApplyEQToBlock_Inplace(CPs_EqualiserModule* pModule, void* _pPCMBlock, const DWORD dwBlockSize)
  81. {
  82.     CPs_EqualiserContext_Basic* pContext = (CPs_EqualiserContext_Basic*)pModule->m_pModuleCookie;
  83.     const int iNumSamples = dwBlockSize >> 2;
  84.     int* pSampleBase = (int*)_pPCMBlock;
  85.     int iSampleIDX, iPointIDX;
  86.     unsigned int iThisSample, iThisSample_EQ;
  87.     int iThisSample_left, iThisSample_right;
  88.     int aryCoefficient_left[9], aryCoefficient_right[9];
  89.     int iClips = 0;
  90.     if(pContext->m_bStreamIsCapable == FALSE || pContext->m_bEnabled == FALSE)
  91.         return;
  92.     for(iSampleIDX =0; iSampleIDX < iNumSamples; iSampleIDX++)
  93.     {
  94.         iThisSample = pContext->m_aryFuture[pContext->m_iCursor];
  95.         pContext->m_aryFuture[pContext->m_iCursor] = pSampleBase[iSampleIDX];
  96.         iThisSample_left = CIC_DECODESAMPLE_LEFT(iThisSample);
  97.         iThisSample_right = CIC_DECODESAMPLE_RIGHT(iThisSample);
  98.         for(iPointIDX =0; iPointIDX < 9; iPointIDX++)
  99.         {
  100.             pContext->m_arySum_left[iPointIDX] += CIC_DECODESAMPLE_LEFT(pContext->m_aryFuture[CIC_WRAPSAMPLE(pContext->m_iCursor+glb_iEQOffsets[iPointIDX])]);
  101.             pContext->m_arySum_right[iPointIDX] += CIC_DECODESAMPLE_RIGHT(pContext->m_aryFuture[CIC_WRAPSAMPLE(pContext->m_iCursor+glb_iEQOffsets[iPointIDX])]);
  102.         }
  103.         for(iPointIDX =0; iPointIDX < 9; iPointIDX++)
  104.         {
  105.             aryCoefficient_left[iPointIDX] = pContext->m_arySum_left[iPointIDX] >> (iPointIDX+1);
  106.             aryCoefficient_right[iPointIDX] = pContext->m_arySum_right[iPointIDX] >> (iPointIDX+1);
  107.         }
  108.         iThisSample_left = CIC_FPMULTIPLY(iThisSample_left - aryCoefficient_left[0], pContext->m_aryLevels[0]);
  109.         iThisSample_right = CIC_FPMULTIPLY(iThisSample_right - aryCoefficient_right[0], pContext->m_aryLevels[0]);
  110.         for(iPointIDX =0; iPointIDX < 8; iPointIDX++)
  111.         {
  112.             iThisSample_left += CIC_FPMULTIPLY(aryCoefficient_left[iPointIDX] - aryCoefficient_left[iPointIDX+1], pContext->m_aryLevels[iPointIDX+1]);
  113.             iThisSample_right += CIC_FPMULTIPLY(aryCoefficient_right[iPointIDX] - aryCoefficient_right[iPointIDX+1], pContext->m_aryLevels[iPointIDX+1]);
  114.         }
  115.         iThisSample_left += CIC_FPMULTIPLY(aryCoefficient_left[8], pContext->m_aryLevels[9]);
  116.         iThisSample_right += CIC_FPMULTIPLY(aryCoefficient_right[8], pContext->m_aryLevels[9]);
  117.         for(iPointIDX =0; iPointIDX < 9; iPointIDX++)
  118.         {
  119.             pContext->m_arySum_left[iPointIDX] -= CIC_DECODESAMPLE_LEFT(pContext->m_aryHistory[CIC_WRAPSAMPLE(pContext->m_iCursor+glb_iEQOffsets_his[iPointIDX])]);
  120.             pContext->m_arySum_right[iPointIDX] -= CIC_DECODESAMPLE_RIGHT(pContext->m_aryHistory[CIC_WRAPSAMPLE(pContext->m_iCursor+glb_iEQOffsets_his[iPointIDX])]);
  121.         }
  122.         *(short*)&iThisSample_EQ = CIC_TRUNCATESHORT(iThisSample_left);
  123.         *((short*)&iThisSample_EQ + 1) = CIC_TRUNCATESHORT(iThisSample_right);
  124.         pSampleBase[iSampleIDX] = iThisSample_EQ;
  125.         pContext->m_aryHistory[pContext->m_iCursor] = iThisSample;
  126.         pContext->m_iCursor = CIC_WRAPSAMPLE(pContext->m_iCursor+1);
  127.     }
  128. }
  129. //