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

📄 vad.c

📁 Intel开发的IPP库的应用实例
💻 C
字号:
/*
//
//                  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) 2002-2006 Intel Corporation. All Rights Reserved.
//
//  Intel(R) Integrated Performance Primitives Audio Processing
//  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.
//
//  Description:
//  This source file is the main file that contains the implementation of a
//  Voice Activity Detector based upon the Intel Integrated Performance Primitives.
//
*/

/* headers */
#include "VADcommon.h"

/********************************************************************************
// Name:             VAD_GetStateSizeBytes
// Description:      Calculate and return the size in bytes required by the VAD
//                   internal state structure based on the input parameters
// Input Arguments:
//                   cWinSamps      - size of an input data frame in samples
// Output Arguments:
//                   pNumStateBytes - pointer to output variable containing the
//                                    calculated state size
// Returns:          None
// Notes:
********************************************************************************/
void VAD_GetStateSizeBytes(int cWinSamps, int* pNumStateBytes)
{
    int cTmpStateBytes; /* size of the intermediate states in bytes */

    /* initialize with the size of the static components of the VADStateStruct */
    *pNumStateBytes = sizeof(VADStateStruct);

    /* add the size of the energy state structure */
    E_GetStateSizeBytes(&cTmpStateBytes);
    *pNumStateBytes += cTmpStateBytes;

    /* add the size of the periodicity state structure */
    PER_GetStateSizeBytes(cWinSamps, &cTmpStateBytes);
    *pNumStateBytes += cTmpStateBytes;

    /* add the size of the state-machine structure */
    SM_GetStateSizeBytes(&cTmpStateBytes);
    *pNumStateBytes += cTmpStateBytes;
}

/********************************************************************************
// Name:            VAD_Init
// Description:     Initialize the VAD state structure with initial values. Also,
//                  assign the externally allocated memory to the internal variables
//                  of the VADStateStruct.
//
// Input Arguments:
//                  frameShiftMsec - frame shift for overlapping frames in msec
//                  cWinSamps      - size of an input data frame in samples
//                  sampFreqHz     - sampling frequency of the input data in Hz
// Input/Output Arguments:
//                  pState         - pointer to an VADState structure
//
// Returns:         None
********************************************************************************/
void VAD_Init(VADStateStruct* pState, int frameShiftMsec, int cWinSamps, int sampFreqHz)
{
    char *pMemory;      /* pointer to current memory block to be assigned */
    int  cStateBytes;  /* size of the intermediate states in bytes */

    /* start memory pointer just after the memory for the VADStateStruct */
    pMemory = (char*)(&pState->pMemoryBlock + 1);

    /* assign memory and initialize the energy state structure */
    pState->pEState = (EStateStruct *) pMemory;
    E_Init(pState->pEState, cWinSamps, frameShiftMsec);
    E_GetStateSizeBytes(&cStateBytes);
    pMemory += cStateBytes;

    /* assign memory and initialize the periodicity state structure */
    pState->pPerState = (PERStateStruct *) pMemory;
    PER_Init(pState->pPerState, frameShiftMsec, cWinSamps, sampFreqHz);
    PER_GetStateSizeBytes(cWinSamps, &cStateBytes);
    pMemory += cStateBytes;

    /* assign memory and initialize the state-machine structure */
    pState->pSM = (SMStruct *) pMemory;
    SM_Init(pState->pSM, frameShiftMsec);
    SM_GetStateSizeBytes(&cStateBytes);
    pMemory += cStateBytes;

    /* initialize the frame count */
    pState->frameNum = 0;
}

/********************************************************************************
// Name:             VAD_ProcessFrame
// Description:      Main VAD function.  This function receives a frame of data
//                   and computes different measures (energy, periodicity)
//                   that are used for determining the state of the VAD
//                   state machine. The algorithm is as described in the VAD Example
//                   guide.
//
// Input Arguments:
//                  pInFrame  - pointer to the input frame of data
//                  len       - number of samples in the input data
// Input/Output Arguments:
//                  pState         - pointer to VAD state structure
// Output Arguments:
//                  pDecisionState - pointer to output variable that contains the decision
//                                   made by VAD regarding the current frame
//                  pDecisionFrame - pointer to output variable that contains the frame number
//                                   (counted from zero)  of the determined endpoint
// Returns:         None
********************************************************************************/
void VAD_ProcessFrame(
  VADStateStruct*    pState,
  const Ipp16s*      pInFrame,
  int                len,
  VADDecisionState*  pDecisionState,
  int*               pDecisionFrame)
{
    /* update frame number count */
    pState->frameNum++;

    /* Energy-Based state update */
    E_UpdateEnergyState(pInFrame, pState->frameNum, pState->pEState);

    /* Periodicity-Based state update */
    PER_UpdatePerState(pInFrame, pState->frameNum, len, pState->pPerState);

    /* State Machine update */
    SM_UpdateState(pState);

    /* Update the output decision variables based on the VAD internal state */
    if (pState->pSM->uttHasStartedFlag)
    {
        /* if utterance start detected */
        *pDecisionState = ACTIVE;
        *pDecisionFrame = pState->pSM->uttBegFrameNum;

        /* reset the start flag for the next utterance */
        pState->pSM->uttHasStartedFlag=0;
    }
    else if (pState->pSM->uttHasEndedFlag)
    {
        /* if utterance end detected */
        *pDecisionState = INACTIVE;
        *pDecisionFrame = pState->pSM->uttEndFrameNum;

        /* reset the end flag for the next utterance */
        pState->pSM->uttHasEndedFlag = 0;
    }
    else
    {
        /* if neither start or end of utterance was detected */
        *pDecisionState = NODECISION;
        *pDecisionFrame = -1;
    }
}

/********************************************************************************
// Name:             VAD_ProcessEndOfInput
// Description:      This function is called at the end of input data stream to check
//                   if the VAD state machine was already in the HANG state when the stream
//                   ended. If so, utterance endpoint is flagged accordingly.
//
// Input/Output Arguments:
//                  pState         - pointer to the VAD state structure
// outputArguments:
//                  pDecisionState - pointer to output variable that contains the decision
//                                   made by VAD
//                  pDecisionFrame - pointer to output variable that contains the frame number
//                                   (counted from zero)  of the determined endpoint
// Returns:         None
********************************************************************************/
void VAD_ProcessEndOfInput(
  VADStateStruct*    pState,
  VADDecisionState*  pDecisionState,
  int*               pDecisionFrame)
{
   /* Update the output decision variables based on the VAD internal state */
    if (pState->pSM->uttHasEndedFlag)
    {
        /* if utterance end detected */
        *pDecisionState = INACTIVE;
        *pDecisionFrame = pState->pSM->uttEndFrameNum;

        /* reset the end flag for the next utterance */
        pState->pSM->uttHasEndedFlag = 0;
    }
    else
    {
        /* declare end of stream to flush the complete output buffer */
        *pDecisionState = END_OF_STREAM;
        *pDecisionFrame = -1;
    }

}

/* EOF */

⌨️ 快捷键说明

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