thraudioproc.c

来自「DSP体系结构实现与应用源代码」· C语言 代码 · 共 191 行

C
191
字号
/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* "@(#) ReferenceFrameworks 2.20.00.08 07-18-03 (swat-f02)" */
/*
 *  ======== thrAudioproc.c ========
 *
 *  Static and dynamic initialization of thread Audioproc,
 *  and its runtime procedure
 */
#include <std.h>
#include <pip.h>

#include <utl.h>            /* debug/diagnostics utility functions */

#include "appResources.h"   /* application-wide common info */
#include "appThreads.h"     /* thread-wide common info */
#include "vol.h"            /* interface for VOL algorithm */
#include "thrAudioproc.h"   /* definition of thrAudioproc objects */

/*
 *  Static part of thread initialization
 */

/* 
 *  Initialization of the thread resources structure:
 *  NULL for algorithm handles, addresses of appropriate 
 *  pipe objects for input and output pipes,
 *  NULL for globally visible temporary pointers to pipe frames,
 *  addresses of intermediate buffers for this thread,
 *  and everything else thread-specific.
 */
ThrAudioproc thrAudioproc[ NUMCHANNELS ] = {
    { /* channel #0 */
        /* algorithm handle(s) (to be initialized in runtime) */
        NULL,                   /* algVOL */
    
        /* input pipe(s) */
        &pipRx,                /* pipIn */

        /* output pipe(s) */
        &pipTx,                /* pipOut */

        /* buffer(s) */
        bufAudioproc,           /* bufInterm */
    
        /* everything else private for the thread */
        
    }, /* end channel # 0 */

};


/*
 *  Dynamic part of thread initialization
 */

/*
 *  ========= thrAudioprocInit ========
 *  Initialization of data structures for the thread(s), called from 
 *  appThreads.c:thrInit() at init time.
 *
 *  Here we create one instance of FIR algorithm per channel and
 *  one instance of VOL algorithm per channel. In a loop, we create
 *  parameters for algorithm instance for each channel by using the 
 *  default parameters and modifying fields that are different.
 *  (If the parameters are the same across channels, they can be
 *  created outside of the loop.)
 */
Void thrAudioprocInit( Void ) 
{
    /* declaration volume parameter structures */
    VOL_Params volParams;
    Int i;

    for (i = 0; i < NUMCHANNELS; i++) {  
        /* 
         *  Set the parameters structure to the default, i.e. 
         *  the one used in i<alg>.c, and modify fields that are different.
         */
        volParams = VOL_PARAMS;             /* default parameters */
        volParams.frameSize  = FRAMELEN;    /* size in samples */
        volParams.gainPercentage = 100;     /* default gain */
    
        /* create instance, confirm creation success, show memory usage */
        thrAudioproc[i].algVOL = VOL_create( &VOL_IVOL, &volParams );
        UTL_assert( thrAudioproc[i].algVOL != NULL );
        UTL_showAlgMem( thrAudioproc[i].algVOL );
    }
}    

/*
 *  Runtime thread code, invoked by the appropriate SWI object
 *  every time the object is posted
 */

/*
 *  ========= thrAudioprocRun ========
 *  The "body" of the swiAudioproc0, swiAudioproc1,... threads.
 *
 *  The single argument of this function is the channel number:
 *  0, 1, 2 etc. up to NUM_CHANNELS - 1. All the SWI objects
 *  that invoke this function pass the channel number as the
 *  argument. 
 *
 *  Based on the channel number, the thread -- the procedure --
 *  decides which thread resource object to access.
 */
Void thrAudioprocRun( Arg aChan )
{
    Sample *src, *dst;
    Int     size;       /* in samples */
    Int     chan;
    Int		i;

    /*
     *  cast 'Arg' types to 'Int'. This is required on 55x large data model
     *  since Arg is not the same size as Int and Ptr in that model.
     *  On all other devices (54x, 55x small, 6x) ArgToInt is a simple cast
     */
    chan = ArgToInt( aChan );

    /*
     *  Check that the preconditions are met, that is the in-pipe 
     *  has a ready-to-consume buffer of data and the out-pipe 
     *  has a free buffer, in other words that this thread has
     *  not been posted in error.
     */
    UTL_assert( PIP_getReaderNumFrames( thrAudioproc[chan].pipIn  ) > 0 );
    UTL_assert( PIP_getWriterNumFrames( thrAudioproc[chan].pipOut ) > 0 );

    /* get the full buffer from the input pipe */
    PIP_get( thrAudioproc[chan].pipIn );
    src = (Sample *)PIP_getReaderAddr( thrAudioproc[chan].pipIn );
    /* get the size in samples (the function below returns it in words) */
    size = sizeInSamples( PIP_getReaderSize( thrAudioproc[chan].pipIn ) );

    /* get the empty buffer from the out-pipe */
    PIP_alloc( thrAudioproc[chan].pipOut );
    dst = (Sample *)PIP_getWriterAddr( thrAudioproc[chan].pipOut );

    /* amplify the signal in the interm. buffer and store result in dst */
    VOL_apply( thrAudioproc[chan].algVOL, 
               src, dst );
               
    for(i=0; i<FRAMELEN; i++) {
    	dst[i] = dst[i] & 0xfffe;
    }
        
    /* Record the amount of actual data being sent */
    PIP_setWriterSize( thrAudioproc[chan].pipOut, sizeInWords( size ) );

    /* Free the receive buffer, put the transmit buffer */
    PIP_free( thrAudioproc[chan].pipIn  );
    PIP_put ( thrAudioproc[chan].pipOut );
}

/*
 *  ========= thrAudioprocSetOutputVolume ========
 *  Procedure that changes output volume for an Audioproc thread
 *
 *  This procedure is called from the likes of the Control thread.
 *  Instead of having the control thread directly write to
 *  thread Audioproc's variables, it calls this function which
 *  changes the state of the VOL algorithm instance for the
 *  given channel.
 */
Void thrAudioprocSetOutputVolume( Int chan, Int volume )
{
    /* VOL algorithm control structure */
    IVOL_Status status;

    UTL_assert( chan < NUMCHANNELS );   /* sanity checking */
    
    /* 
     *  Apply volume gain information to the appropriate alg. instance.
     *  Retrieve the current parameters, change the fields that need
     *  to be changed, and apply the new parameters. Volume gain is
     *  a percentage, a number from 0 to 200, 100 being the normal
     *  volume (100%).
     */
    VOL_control( thrAudioproc[ chan ].algVOL, IVOL_GETSTATUS, &status );
    status.gainPercentage = volume;
    VOL_control( thrAudioproc[ chan ].algVOL, IVOL_SETSTATUS, &status );
}

⌨️ 快捷键说明

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