⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encoder.c

📁 Intel开发的IPP库的应用实例
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
//
//                  INTEL CORPORATION PROPRIETARY INFORMATION
//     This software is supplied under the terms of a license agreement or
//     nondisclosure agreement with Intel Corporation and may not be copied
//     or disclosed except in accordance with the terms of that agreement.
//          Copyright(c) 2003-2006 Intel Corporation. All Rights Reserved.
//
//     Intel(R) Integrated Performance Primitives Aurora Sample for Windows*
//
//  By downloading and installing this sample, you hereby agree that the
//  accompanying Materials are being provided to you under the terms and
//  conditions of the End User License Agreement for the Intel(R) Integrated
//  Performance Primitives product previously accepted by you. Please refer
//  to the file ippEULA.rtf located in the root directory of your Intel(R) IPP
//  product installation for more information.
//
//  ES 201 108 v1.1.3 is the international standard promoted by ETSI
//   and other organizations. Implementations of these standards, or the standard
//   enabled platforms may require licenses from various entities, including
//  Intel Corporation.
//
*/

#include "encoderapi.h"
#include <malloc.h>
#include <stdio.h>
#include <math.h>

#include "tab_8kHz.inc"
#include "tab_16kHz.inc"
#include "mframe.h"

#define EMPHASIS_COEFF           0.97f

#define NUM_CHANNELS             23
#define STARTING_FREQ            64.0

typedef struct EncoderStruct{
    IppsFBankState_16s* pFBank;
    IppsDCTLifterState_16s* pDCTLifter;
    IppsCdbkState_16s  **ppCdbkState;
    int FFTOrder;
    int FrameLength;
    int FrameShift;
    int FFTLength;
    int numCepCoeff;
    int numChannels;
    short Dst0;
    short pSrc0;
    short preFloat;
    short *workBuffer;
    short *pWwork;
    int curPosition;
    AuroraDataType EncoderOutput;
    AuroraDataType EncoderInput;
    AuroraRate freq;
    double weight_c0;
    double weight_logE;
    unsigned char pIndexVQBuffer[2*NUM_CODEBOOK];
    short *pFeatBuffer;
    short *HammingWindow;
    int iCountFrame;
    int NumberFrame;
    int mframeCounter;
    MFrame *pFrame;
    int crc;
    int BufferData;
    int Curr;
    int Prev;
    int Last;
    int Start;
    int BufferSize;
    int done;

};


void ResetAuroraEncoder(AuroraEncoder *pCodec){
   pCodec->Dst0=0;
   pCodec->preFloat=0;
   pCodec->pSrc0=0;
   pCodec->curPosition = 0;
   pCodec->iCountFrame = 0;
   pCodec->mframeCounter = 1;
   pCodec->pFrame->pos=6;
   pCodec->NumberFrame = 0;
}

int InitAuroraEncoder(AuroraEncoder **pCodec, AuroraRate SamplingFrequency,AuroraDataType EncoderInput,AuroraDataType EncoderOutput){
   IppStatus status;
   int FrameShift=0;
   int FrameLength=0;
   float startingFeq=0;
   int i;
   float freq;
   short *qCoeffLog, *qCoeff;
   float pLiftCoeff[NUM_CEP_COEFF-1] = {1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,};
/*   float pLiftCoeff[NUM_CEP_COEFF-1] = {0.740585f,1.18330f,1.60780f,2.00544f,2.36814f,2.68850f,
      2.96001f,3.17714f,3.33547f,3.43178f,3.46410f,3.43178f};*/


   pCodec[0] = malloc(sizeof(AuroraEncoder));
   if (pCodec[0]==NULL) return -1;
   status = ippStsNoErr;
   pCodec[0]->pFBank = NULL;
   pCodec[0]->pDCTLifter = NULL;
   pCodec[0]->workBuffer = NULL;
   pCodec[0]->ppCdbkState = NULL;
   pCodec[0]->pWwork = NULL;
   pCodec[0]->pFeatBuffer = NULL;
   pCodec[0]->HammingWindow = NULL;
   pCodec[0]->pFeatBuffer = ippsMalloc_16s(NUM_CEP_COEFF+1);
   pCodec[0]->iCountFrame = 0;
   pCodec[0]->mframeCounter = 1;
   pCodec[0]->NumberFrame = 0;
   pCodec[0]->EncoderInput = EncoderInput;
   pCodec[0]->pFrame = (MFrame*)ippsMalloc_8u(sizeof(MFrame));
   if ( !((SamplingFrequency == r8KHz) || (SamplingFrequency == r11KHz) || (SamplingFrequency == r16KHz))){
      free(pCodec[0]);
      return -1;
   }

   if (SamplingFrequency == r8KHz){
      FrameLength = FRAME_LEN_8;
      FrameShift = FRAME_SHIFT_8;
      startingFeq = STARTING_FREQ;
      pCodec[0]->freq = r8KHz;
      freq = 8000.0f;
      pCodec[0]->weight_c0 = w8kHz_C0;
      pCodec[0]->weight_logE = w8kHz_LE;
      qCoeff = (short*)qCoeff8kHz;
      qCoeffLog = (short*)qCoeff8kHz_Log;
      pCodec[0]->pFrame->sampRate = 0;
   }
   else if (SamplingFrequency == r11KHz){
      FrameLength = FRAME_LEN_11;
      FrameShift = FRAME_SHIFT_11;
      startingFeq = STARTING_FREQ;
      pCodec[0]->freq = r11KHz;
      freq = 11000.0f;
      pCodec[0]->weight_c0 = w8kHz_C0;
      pCodec[0]->weight_logE = w8kHz_LE;
      qCoeff = (short*)qCoeff8kHz;
      qCoeffLog = (short*)qCoeff8kHz_Log;
      pCodec[0]->pFrame->sampRate = 1;
   }
   else if (SamplingFrequency == r16KHz){
      FrameLength = FRAME_LEN_16;
      FrameShift = FRAME_SHIFT_16;
      startingFeq = STARTING_FREQ;
      pCodec[0]->freq = r16KHz;
      freq = 16000.0f;
      pCodec[0]->weight_c0 = w16kHz_C0;
      pCodec[0]->weight_logE = w16kHz_LE;
      qCoeff = (short*)(qCoeff16kHz);
      qCoeffLog = (short*)qCoeff16kHz_Log;
      pCodec[0]->pFrame->sampRate = 3;
   }

   pCodec[0]->FrameLength = FrameLength;
   pCodec[0]->FrameShift = FrameShift;
   pCodec[0]->Dst0 = 0;
   pCodec[0]->pSrc0 = 0;
   pCodec[0]->preFloat = 0;
   pCodec[0]->numChannels = NUM_CHANNELS;
   pCodec[0]->numCepCoeff = NUM_CEP_COEFF;
   if (EncoderInput==WAVEFORM){
      pCodec[0]->HammingWindow = ippsMalloc_16s(FrameLength);
      ippsZero_16s(pCodec[0]->HammingWindow,FrameLength);
      for (i = 0; i < FrameLength / 2; i++)
           pCodec[0]->HammingWindow[i] = (short)((0.54 - 0.46 * cos (IPP_2PI * i / (FrameLength - 1)))*16384);
      for (i = FrameLength / 2; i < FrameLength; i++)
           pCodec[0]->HammingWindow[i] = pCodec[0]->HammingWindow[FrameLength - 1 - i];
   }
   pCodec[0]->pFrame->pos=6;
   pCodec[0]->curPosition = 0;
   pCodec[0]->EncoderOutput = EncoderOutput;

   if (EncoderInput==WAVEFORM){
      status = ippsMelFBankInitAlloc_16s(&pCodec[0]->pFBank,&pCodec[0]->FFTOrder,FrameLength,freq,
         startingFeq, freq/2,NUM_CHANNELS,2595.0,700,(IppMelMode)(IPP_FBANK_FREQWGT | IPP_POWER_SPECTRUM));

   pCodec[0]->FFTLength = 1<<pCodec[0]->FFTOrder;
   pCodec[0]->workBuffer = ippsMalloc_16s(pCodec[0]->FFTLength);
   pCodec[0]->pWwork = ippsMalloc_16s (pCodec[0]->FFTLength);

      status = ippsDCTLifterInitAlloc_Mul_16s(&pCodec[0]->pDCTLifter,NUM_CHANNELS,pLiftCoeff,NUM_CEP_COEFF-1);

   if(status!=ippStsNoErr){
       if(pCodec[0]->pFBank) ippsFBankFree_16s(pCodec[0]->pFBank);
          pCodec[0]->pFBank=NULL;
          return -1;
   }
   }
   if (EncoderOutput == QUANTIZED || EncoderOutput == MULTIFRAME){
      pCodec[0]->ppCdbkState = (IppsCdbkState_16s  **)ippsMalloc_8u (sizeof(IppsCdbkState_16s  *)*NUM_CODEBOOK);
      if (pCodec[0]->ppCdbkState==NULL){
         printf("Memory not allocated");
         return -1;
      }

      for (i=0; i < 6; i++)
         ippsCdbkInitAlloc_L2_16s(&(pCodec[0]->ppCdbkState[i]),(const short*)(qCoeff+i*128),2,2,64,64,IPP_CDBK_FULL);
      for (i=0; i<256;i++){
         qCoeffLog[i*2+0] = (short)(qCoeffLog[i*2+0] * pCodec[0]->weight_c0);
         qCoeffLog[i*2+1] = (short)(qCoeffLog[i*2+1] * pCodec[0]->weight_logE);
      }
      ippsCdbkInitAlloc_L2_16s(&(pCodec[0]->ppCdbkState[6]),(const short*)qCoeffLog,2,2,256,256,IPP_CDBK_FULL);
   }
   return 0;
}


void ReleaseAuroraEncoder(AuroraEncoder *pCodec){
   int i;

   if (pCodec){
        if(pCodec->pFrame)ippsFree(pCodec->pFrame);
        if(pCodec->pFeatBuffer)ippsFree(pCodec->pFeatBuffer);
        if(pCodec->HammingWindow)ippsFree(pCodec->HammingWindow);
        if(pCodec->workBuffer) ippsFree(pCodec->workBuffer);
        if(pCodec->pWwork) ippsFree(pCodec->pWwork);
        if(pCodec->pFBank) ippsFBankFree_16s(pCodec->pFBank);
        if(pCodec->pDCTLifter) ippsDCTLifterFree_16s(pCodec->pDCTLifter);
        if(pCodec->ppCdbkState){
           for (i=0; i<NUM_CODEBOOK; i++){
             if (pCodec->ppCdbkState[i])ippsCdbkFree_16s(pCodec->ppCdbkState[i]);
           }
           ippsFree(pCodec->ppCdbkState);
        }
        free(pCodec);
   }
}

/*   WAVEFORM -> FEATURE   */
int ApplyAuroraEncoder_WF(AuroraEncoder *pCodec,short *pSrc,int InputLength,
                       short * pDst, int StreamEnd){

   int i,iCount;
   short *pFwork,*WaveBuffer;
   int FrameCounter;
   int tailSample;
   int step;
   float LogEnergy;
   int pFBBuffer[NUM_CHANNELS];
   float tmp,C0;

   if (InputLength<=0){
      FrameCounter=0;
      ResetAuroraEncoder(pCodec);
      return FrameCounter;
   }

   if (pCodec->EncoderOutput == FEATURE){
      pFwork =(short*) pDst/*+FeatureSize*pCodec->NumberFrame*/;
   }
   else {
      printf("\n!!! ERROR: encoder was initialized with another output flag !!!");
      return 0;
   }

   WaveBuffer = pSrc;
   FrameCounter = InputLength;
   if (pCodec->EncoderInput==WAVEFORM){
      if (pCodec->curPosition+InputLength >= pCodec->FrameLength){
            FrameCounter = (pCodec->curPosition+InputLength-(pCodec->FrameLength-pCodec->FrameShift)) / pCodec->FrameShift;
            tailSample = (pCodec->curPosition+InputLength-(pCodec->FrameLength-pCodec->FrameShift)) % pCodec->FrameShift;
            step = pCodec->FrameLength - pCodec->curPosition;
      }
      else{
            FrameCounter = 0;
            tailSample = InputLength;
      }
   }
   else {
      printf("\n!!! ERROR: encoder was initialized with another input flag !!!");
      return 0;
   }
   /*   calculate feature   */
   for (iCount=0;iCount<FrameCounter;iCount++){
      /* Offset Compensation*/
      ippsRShiftC_16s(WaveBuffer,1,pCodec->pWwork+pCodec->curPosition,step);
      ippsCompensateOffset_16s_I(pCodec->pWwork+pCodec->curPosition,
         step,&(pCodec->pSrc0),pCodec->Dst0,0.999f);

      pCodec->pSrc0 = WaveBuffer[step-1]>>1;
      pCodec->Dst0 = pCodec->pWwork[pCodec->FrameLength-1];

      WaveBuffer+=step;

      pCodec->curPosition = pCodec->FrameLength - pCodec->FrameShift;
      step = pCodec->FrameShift;

      /* logE computation */
      LogEnergy = 0.0;

      ippsDotProd_16s32f(pCodec->pWwork,pCodec->pWwork,pCodec->FrameLength,&LogEnergy);
      LogEnergy*=4;
      if (LogEnergy <= 0)
         LogEnergy = (float)-50.0f;
      else{
         LogEnergy = (float) log ((double) LogEnergy);
      }

      /* Pre-emphasis */
      ippsCopyWithPadding_16s(pCodec->pWwork, pCodec->FrameLength, pCodec->workBuffer,pCodec->FFTLength);

      ippsPreemphasize_16s(pCodec->workBuffer, pCodec->FrameLength, EMPHASIS_COEFF);

      pCodec->workBuffer[0] = (short)(pCodec->pWwork[0] - EMPHASIS_COEFF * pCodec->preFloat);

      /* Replace WorkBuffer*/
      pCodec->preFloat = pCodec->pWwork[pCodec->FrameShift-1];
      ippsMove_16s(pCodec->pWwork+pCodec->FrameShift, pCodec->pWwork,
         pCodec->FrameLength-pCodec->FrameShift);

      /* Windowing */
      ippsMul_16s_ISfs(pCodec->HammingWindow,pCodec->workBuffer,pCodec->FrameLength,15);

      /* Mel filtering with FFT and Magnitude spectrum*/
      ippsEvalFBank_16s32s_Sfs(pCodec->workBuffer,pFBBuffer,pCodec->pFBank,-2);

      /* Natural logarithm computation */
      C0=0;
      for (i = 0; i < pCodec->numChannels; i++){
         tmp = (float)pFBBuffer[i];
         if (tmp <=0)
            pFBBuffer[i] = (int)-50*1024;
         else
            tmp = (float) log ((double) tmp);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -