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

📄 vad_exmp.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 contains the example dispatcher for an VAD example library.
//  It reads data from a 16khz,mono,16bit PCM binary file
//  and runs the voice activity detector on the data using API calls
//  to the VAD sample library. It writes two output files - one PCM binary file
//  and one ASCII file. The output PCM binary file has the same format and size
//  as the input file. It contains the unaltered input data between the beginning
//  and end points of an utterance as determined by the VAD algorithm and zeroed
//  sample values otherwise. The ASCII file contains a set of endpoints
//  (beginning and end) per utterance as determined by the VAD algorithm. Each
//  endpoint is further reported as a frame/sample number pair.
//
*/

/* headers */
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <ipps.h>
#include <ippsr.h>
#include "VAD.h"
#include "VADout.h"

/* Error checking macro */
#define ABORT -1
#define ASSERT_MSGBOX(cond, msg, title, exitcode)                    \
{                                                                    \
  if (!(cond))                                                       \
  {                                                                  \
    MessageBox(NULL, _T(msg), _T(title), MB_OK);                     \
    exit(exitcode);                                                  \
  }                                                                  \
}

/* defines */
/* input/output filenames */
#define INPUT_PCM_FN            "VADtest.pcm"         /* input PCM filename */
#define OUTPUT_PCM_FN           "VADtest_output.pcm"  /* output PCM filename */
#define OUTPUT_TEXT_FN          "VADtest_output.txt"  /* output ASCII filename. File contains frame/sample numbers of endpoints */

/* parameters */
const int SAMPLING_FREQUENCY_HZ = 16000;              /* sampling frequency of the input data in Hz */
#define DC_RMV_COEF             0.99f                 /* dc removal filter coefficient (0.99) */
#define WINDOW_LEN_MSEC         32                    /* length of a window/frame of speech in msec */
#define FRAME_SHIFT_MSEC        16                    /* frame shift in msec for overlapping frames */
#define MAX_OUTPUT_LATENCY_MSEC 400                   /* maximum latency of the output data. Also, implies size of output buffer */
#define CONV_FACTOR_SEC_MSEC    1000

/* conversion of lengths defined above from msec to samples */
#define WINDOW_LEN_SAMPS        WINDOW_LEN_MSEC * SAMPLING_FREQUENCY_HZ / CONV_FACTOR_SEC_MSEC
#define FRAME_SHIFT_SAMPS       FRAME_SHIFT_MSEC * SAMPLING_FREQUENCY_HZ / CONV_FACTOR_SEC_MSEC
#define OUT_BUF_SIZE_SAMPS      MAX_OUTPUT_LATENCY_MSEC * SAMPLING_FREQUENCY_HZ / CONV_FACTOR_SEC_MSEC

/********************************************************************************
// Name:                WinMain
// Description:         This is the entry-point function for the VAD example, it reads
//                      data from an input mono wave file, handles overlapping of frames,
//                      marshalls the data into calls to a VAD example API
//                      which calls the Intel(R) IPP functions. It also calls functions to write data and
//                      endpoint frame/sample number to output files.
//
// Input Arguments:     None of the arguments are used by the example code.  Refer
//                      to the WinMain description in the Microsoft Visual Studio
//                      online help for complete details.
********************************************************************************/

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR  lpCmdLine,int nShowCmd)
{
    FILE*              pInputPCMFile;         /* file pointer to input PCM file */
    FILE*              pOutputPCMFile;        /* file pointer to output PCM file */
    FILE*              pOutputTextFile;       /* file pointer to output ASCII file */
    int                cSampsRead;            /* actual number of samples read from input file */
    Ipp16s*            pReadBuf;              /* buffer to hold read input data */
    Ipp32f*            pReadBuf32f;           /* buffer to hold read input data */
    int                cWinSamps;             /* window/frame size in samples */
    int                cFrameShiftSamps;      /* frame shift in samples for overlapping frames */
    int                cReuseSamps;           /* number of samples from previous frame to be reused for next frame */
    VADStateStruct32f* pState;                /* pointer to the internal VAD state structure */
    int                cStateBytes;           /* size in bytes of the VAD state */
    VADDecisionState   curDecisionState;      /* the decision state of VAD for the current frame */
    VADDecisionState   prevDecisionState;     /* decision state of VAD from previous frames */
    int                decisionFrameNum;      /* the frame number of the endpoint determined by VAD */
    int                decisionSampNum;       /* the sample number of the endpoint deteremined by VAD */
    CircBufStruct16s   outBuf;                /* circular buffer containing data to be written to the output */
    int                outBufSzSamps;         /* size of the output circular buffer in samples */
    Ipp32f             prevInputSample;       /* previous input sample for DC removal filter */
    Ipp32f             prevOutputSample;      /* previous output sample for DC removal filter */

    /* open input and output files */
    pInputPCMFile   = fopen(INPUT_PCM_FN,  "rb");
    ASSERT_MSGBOX(NULL!=pInputPCMFile, "VAD example. Cannot open input file!\n", "Error", ABORT);

    pOutputPCMFile  = fopen(OUTPUT_PCM_FN, "wb");
    ASSERT_MSGBOX(NULL!=pOutputPCMFile, "VAD example. Cannot open output file!\n", "Error", ABORT);

    pOutputTextFile = fopen(OUTPUT_TEXT_FN,"wt");
    ASSERT_MSGBOX(NULL!=pOutputTextFile, "VAD example. Cannot open output file!\n", "Error", ABORT);

    /* compute size in samples for a frame , frame shift and output buffer */
    cWinSamps        = WINDOW_LEN_SAMPS;
    cFrameShiftSamps = FRAME_SHIFT_SAMPS;
    cReuseSamps      = cWinSamps - cFrameShiftSamps;
    outBufSzSamps    = OUT_BUF_SIZE_SAMPS;

    /* compute the size in bytes required for the VAD Internal State */
    VAD_GetStateSizeBytes(cWinSamps, &cStateBytes);

    /* allocate memory for local buffers & VAD state */
    pReadBuf = (Ipp16s *) malloc(cWinSamps*sizeof(Ipp16s));
    ASSERT_MSGBOX(NULL!=pReadBuf, "VAD example. Cannot allocate memory!\n", "Error", ABORT);

    pReadBuf32f = (Ipp32f *) malloc(cWinSamps*sizeof(Ipp32f));
    ASSERT_MSGBOX(NULL!=pReadBuf, "VAD example. Cannot allocate memory!\n", "Error", ABORT);

    outBuf.pBuf = (Ipp16s *) malloc (outBufSzSamps * sizeof(Ipp16s));
    ASSERT_MSGBOX(NULL!=outBuf.pBuf, "VAD example. Cannot allocate memory!\n", "Error", ABORT);

    pState = (VADStateStruct32f *) malloc(cStateBytes);
    ASSERT_MSGBOX(NULL!=pState, "VAD example. Cannot allocate memory!\n", "Error", ABORT);

    /* Initializations - output circular structure, VAD state and VAD decision states */
    OUT_Init(outBufSzSamps, &outBuf);
    VAD_Init(pState, FRAME_SHIFT_MSEC, cWinSamps, SAMPLING_FREQUENCY_HZ);

    curDecisionState = NODECISION;
    prevDecisionState = INACTIVE;

    /* read one frame and one lookahead frame */
    cSampsRead = fread(pReadBuf, sizeof(Ipp16s), cWinSamps, pInputPCMFile);
    ippsConvert_16s32f(pReadBuf,pReadBuf32f,cWinSamps);

    /* copy data to the output buffer */
    OUT_AppendSamples(pReadBuf32f,cSampsRead,&outBuf);

    /* remove DC */
    prevInputSample = 0.0;
    ippsCompensateOffset_32f_I(pReadBuf32f, cSampsRead, &prevInputSample, 0, DC_RMV_COEF);
    prevOutputSample = pReadBuf32f[cSampsRead-1];

    /* Main Loop. Process each frame of input data until input file exhausted. */
    while(cSampsRead >= cFrameShiftSamps)
    {
        /* perform VAD */
        VAD_ProcessFrame(pState, pReadBuf32f, cWinSamps, &curDecisionState, &decisionFrameNum);

        ippsConvert_32f16s_Sfs(pReadBuf32f,pReadBuf,cWinSamps,ippRndNear,0);


        /* write data to the output files based on the outcome of VAD */
        decisionSampNum = decisionFrameNum * cFrameShiftSamps;
        OUT_ConditionalFlushBuffer(&outBuf, decisionSampNum, curDecisionState, &prevDecisionState, cFrameShiftSamps, pOutputPCMFile);
        OUT_WriteEndPoints(decisionFrameNum, decisionSampNum, curDecisionState, pOutputTextFile);

        /* save the samples to be re-used from the recently processed frame and read another (lookahead) frame shift of data. */
        ippsCopy_16s(pReadBuf+cFrameShiftSamps,pReadBuf,cReuseSamps);
        cSampsRead = fread(pReadBuf+cReuseSamps, sizeof(Ipp16s), cFrameShiftSamps, pInputPCMFile);
        ippsConvert_16s32f(pReadBuf,pReadBuf32f,cWinSamps);

        /* copy data to the output buffer */
        OUT_AppendSamples(pReadBuf32f+cReuseSamps,cSampsRead,&outBuf);

        /* remove DC */
        ippsCompensateOffset_32f_I(pReadBuf32f+cReuseSamps, cSampsRead, &prevInputSample, prevOutputSample, DC_RMV_COEF);
        prevOutputSample = pReadBuf32f[cReuseSamps+cSampsRead-1];
    }

    /*
    // End of file processing
    */

    /* check to see if VAD was in the process of determining teh edn of an utterance */
    VAD_ProcessEndOfInput(pState,&curDecisionState, &decisionFrameNum);

    /* write data to the output files based on the outcome of VAD */
    decisionSampNum = decisionFrameNum * cFrameShiftSamps;
    OUT_ConditionalFlushBuffer(&outBuf, decisionSampNum, curDecisionState, &prevDecisionState,0, pOutputPCMFile);
    OUT_WriteEndPoints(decisionFrameNum,decisionSampNum,curDecisionState, pOutputTextFile);

    /* close input/ouput file pointers */
    fclose(pInputPCMFile);
    fclose(pOutputPCMFile);
    fclose(pOutputTextFile);

    /* free allocated memory */
    free(pReadBuf);
    free(pReadBuf32f);
    free(outBuf.pBuf);
    free(pState);

//    ASSERT_MSGBOX(0, "Finished executing VAD example\n", "Success", 0);
}

/* EOF */

⌨️ 快捷键说明

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