Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

FEAPIExamplePluginFindRms.cpp

Go to the documentation of this file.
00001 
00002 //     /*! \file FEAPIExamplePluginFindRms.cpp: \brief implementation of the CFindAbsoluteRms class. */
00003 //
00004 //        Copyright (c) 2004-2007, Alexander Lerch, zplane.development GbR
00005 //        All rights reserved.
00006 //
00007 //        Redistribution and use in source and binary forms, with or without 
00008 //        modification, are permitted provided that the following conditions 
00009 //        are met:
00010 //
00011 //        *   Redistributions of source code must retain the above copyright 
00012 //            notice, this list of conditions and the following disclaimer. 
00013 //        *   Redistributions in binary form must reproduce the above 
00014 //            copyright notice, this list of conditions and the following 
00015 //            disclaimer in the documentation and/or other materials 
00016 //            provided with the distribution. 
00017 //        *   Neither the name of the FEAPI development team nor the names 
00018 //            of its contributors may be used to endorse or promote products 
00019 //            derived from this software without specific prior written 
00020 //            permission. 
00021 //
00022 //        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 //        "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 //        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00025 //        FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00026 //        COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00027 //        INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00028 //        BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00029 //        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00030 //        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031 //        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00032 //        ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00033 //        POSSIBILITY OF SUCH DAMAGE.
00034 //
00036 
00037 #include <string>
00038 #include <iostream>
00039 #include <math.h>
00040 #include <cstdlib>
00041 
00042 #include "FEAPI.h"
00043 #include "FEAPIExamplePluginFindRms.h"
00044 #include "FEAPIEntryPoints.h"
00045 
00046 #ifndef FLT_MAX
00047 #define FLT_MAX 3.402823466e+38F
00048 #endif
00049 
00050 // defines for plug in name etc.
00051 #define _MY_PLUGIN_NAME        "RmsFind"
00052 #define _MY_PLUGIN_VENDOR      "remy muller"
00053 #define _MY_PLUGIN_DESCRIPTION "This PlugIn searches each input audio data block for its RMS value per channel"
00054 #define _MY_PLUGIN_COPYRIGHT   "(c) 2006 by remy muller"
00055 #define _MY_PLUGIN_ID          "RmsFindId"
00056 #define _MY_NUM_PARAMETERS     0    // no parameters to adjust here....
00057 
00058 // defines for description of result
00059 #define _FEATURE_NAME           "Rms Value"
00060 #define _FEATURE_UNIT           "dBFS"
00061 #define _FEATURE_DESCRIPTION    "The rms value of channel: %d per input block"
00062 #define _FEATURE_RANGE_MIN      -740
00063 #define _FEATURE_RANGE_MAX      0.0F
00064 #define _FEATURE_QUANTIZED      -1  // value is not quantized
00065 #define _FEATURE_SAMPLERATE     -1  // output sample rate equals input block length
00066 
00067 
00068 CFindAbsoluteRms::CFindAbsoluteRms () : CFeatureExtractBase()
00069 {   
00070     // set of strings that will be returned by the default methods
00071     m_cPluginName           = _MY_PLUGIN_NAME;
00072     m_cPluginVendor         = _MY_PLUGIN_VENDOR;
00073     m_cPluginDescription    = _MY_PLUGIN_DESCRIPTION;
00074     m_cPluginId             = _MY_PLUGIN_ID;
00075     m_cPluginCopyRight      = _MY_PLUGIN_COPYRIGHT;
00076 
00077     // set plug in version info
00078     m_iMajorVersion     = 0x00000001;
00079     m_iMinorVersion     = 0x00000001;
00080     m_iSubVersion       = 0x00000000;
00081     
00082     m_pfRmsValue        = 0;
00083     m_pbIHaveResults    = 0;
00084     m_ptTimeStamp       = 0;
00085 
00086 }
00087 
00088 
00089 CFindAbsoluteRms::~CFindAbsoluteRms ()
00090 {
00091     // destroy feature vector
00092     if (m_pfRmsValue)
00093         delete [] m_pfRmsValue;
00094     m_pfRmsValue        = 0;
00095 
00096     if (m_ptTimeStamp)
00097         delete [] m_ptTimeStamp;
00098     m_ptTimeStamp        = 0;
00099 
00100     // destroy bool vector
00101     if (m_pbIHaveResults)
00102         delete [] m_pbIHaveResults;
00103     m_pbIHaveResults    = 0;
00104 }
00105 
00106 
00107 FEAPI_Error_t CFindAbsoluteRms::InitializePlugin (float               fInputSampleRate, 
00108                                                   int                 iNumberOfAudioChannels,
00109                                                   int                 iHostApiMajorVersion,
00110                                                   FEAPI_UserData_t     *pstUserData)
00111 {
00112     CFeatureExtractBase::InitializePlugin ( fInputSampleRate, 
00113                                             iNumberOfAudioChannels,
00114                                             iHostApiMajorVersion,
00115                                             pstUserData);
00116 
00117 
00118     // Set information about the inputs
00119     char acChannelStr[FEAPI_kMaxDescriptionLength];
00120     for (int h = 0; h < iNumberOfAudioChannels; ++h) {
00121         sprintf(acChannelStr, "Channel: %d", h);
00122         SetPluginInputPinInfo(CPin::SetIndex(h)
00123             .SetName(acChannelStr)
00124             .SetUnit("")
00125             .SetDescription(acChannelStr)
00126             .SetRangeMin(-1.0f)
00127             .SetRangeMax(1.0f)
00128             .SetSampleRate(fInputSampleRate)
00129         );
00130     }
00131         
00132     // note that in this special case, the number of results equals the number of channels, 
00133     // Set information about the results
00134     char acDescription[FEAPI_kMaxDescriptionLength];
00135     for (int k = 0; k < iNumberOfAudioChannels; ++k) {
00136         sprintf(acDescription, _FEATURE_DESCRIPTION, k);
00137         SetPluginResultPinInfo(CPin::SetIndex(k)
00138             .SetName(_FEATURE_NAME)
00139             .SetUnit(_FEATURE_UNIT)
00140             .SetDescription(acDescription)
00141             .SetRangeMin(_FEATURE_RANGE_MIN)
00142             .SetRangeMax(_FEATURE_RANGE_MAX)
00143             .SetQuantizedTo(_FEATURE_QUANTIZED)
00144             .SetSampleRate(_FEATURE_SAMPLERATE)
00145         );
00146     }
00147 
00148     // allocate one dimensional result/feature vector
00149     if (m_pfRmsValue)
00150         delete [] m_pfRmsValue;
00151     m_pfRmsValue        = new float [iNumberOfAudioChannels];
00152     if (m_ptTimeStamp)
00153         delete [] m_ptTimeStamp;
00154     m_ptTimeStamp       = new FEAPI_TimeStamp_t [iNumberOfAudioChannels];
00155     
00156     // allocate one dimensional result/feature vector
00157     if (m_pbIHaveResults)
00158         delete [] m_pbIHaveResults;
00159     m_pbIHaveResults    = new bool [iNumberOfAudioChannels];
00160 
00161     // initialize bool vector
00162     for (int i = 0; i < iNumberOfAudioChannels; i++)
00163         m_pbIHaveResults[i] = false;
00164 
00165     return FEAPI_kNoError;
00166 }
00167 
00168 FEAPI_Error_t CFindAbsoluteRms::SetPluginParameter (int iParameterIndex, float fValue)
00169 {
00170     // we don't have any parameters in this plugin
00171     return FEAPI_kUnknownError;
00172 }
00173 
00174 
00175 float CFindAbsoluteRms::GetPluginParameter (int iParameterIndex)
00176 {
00177     // we don't have any parameters in this plugin
00178     return -1;
00179 }
00180 
00181 
00182 int CFindAbsoluteRms::GetPluginResultLatency (int iResultIndex)
00183 {
00184     return 0;
00185 }
00186 
00187 
00188 FEAPI_Error_t CFindAbsoluteRms::ProcessPluginDone ()
00189 {
00190     // we don't have to do anything special here
00191     return FEAPI_kNoError;
00192 }
00193 
00194 FEAPI_Error_t CFindAbsoluteRms::ProcessPlugin (const float **ppfInputBuffer, const FEAPI_TimeStamp_t *ptTimeStamps, int iNumberOfFrames)
00195 {
00196     int iNumOfInputs    = this->GetPluginNumOfInputs ();
00197 
00198     if (!m_bIsInitialized)
00199         return FEAPI_kUnspecifiedError;
00200 
00201     if (iNumberOfFrames <= 0)
00202         return FEAPI_kNoError;
00203 
00204     for (int iCh = 0; iCh < iNumOfInputs; ++iCh)
00205     {
00206         m_pfRmsValue[iCh]   = 0.0F;
00207         float rms = 0.f;
00208         for (int iIdx = 0; iIdx < iNumberOfFrames; ++iIdx)
00209         {
00210             const float x = ppfInputBuffer[iCh][iIdx];
00211             rms += x*x;
00212         }
00213         m_pfRmsValue[iCh]       = sqrt(rms/float(iNumberOfFrames));
00214         m_ptTimeStamp[iCh]      = ptTimeStamps[iCh];
00215         m_pbIHaveResults[iCh]   = true;
00216     }
00217 
00218     return FEAPI_kNoError;
00219 }
00220 
00221 int CFindAbsoluteRms::GetPluginSizeOfResult (int iResultIndex)
00222 {
00223     if (iResultIndex >= this->GetPluginNumOfResults ())
00224         return -1;
00225 
00226     if (m_pbIHaveResults[iResultIndex])
00227         return (sizeof(m_pfRmsValue[iResultIndex])/sizeof(float));
00228     else
00229         return 0;
00230 }
00231 
00232 
00233 FEAPI_Error_t CFindAbsoluteRms::GetPluginResult (int iResultIndex, float *pfResult, FEAPI_TimeStamp_t *ptTimeStamp)
00234 {
00235     if ((iResultIndex >= this->GetPluginNumOfResults ()) || (!m_pbIHaveResults[iResultIndex]))
00236         return FEAPI_kUnknownError;
00237 
00238     if (m_pfRmsValue[iResultIndex] < 1e-37)
00239         m_pfRmsValue[iResultIndex]  = 1e-37F;
00240     *pfResult       = 20*logf(m_pfRmsValue[iResultIndex])/logf(10.0F);
00241     *ptTimeStamp    = m_ptTimeStamp[iResultIndex];
00242     
00243     m_pbIHaveResults[iResultIndex]  = false;
00244     
00245     return FEAPI_kNoError;
00246 }
00247 
00248 
00249 FEAPI_Error_t CFindAbsoluteRms::ResetPlugin ()
00250 {
00251     for (int i = 0; i < this->GetPluginNumOfResults (); i++)
00252     {
00253         m_pfRmsValue[i] = 0.0F;
00254         m_ptTimeStamp[i]= 0.0F;
00255     }
00256 
00257     return FEAPI_kNoError;
00258 }
00259 
00260 
00261 float CFindAbsoluteRms::GetPluginProperty ( FEAPI_PluginProperty_t ePluginProperty)
00262 {
00263     switch (ePluginProperty)
00264     {
00265     case FEAPI_kMinSampleRate:
00266         {
00267             return 1;
00268         }
00269     case FEAPI_kMaxSampleRate:
00270         {
00271             return 1e38F;
00272         }
00273     case FEAPI_kMinChannels:
00274         {
00275             return 1;
00276         }
00277     case FEAPI_kMaxChannels:
00278         {
00279             return (float)((1<<30)-1);
00280         }
00281     case FEAPI_kMinFrameSize:
00282         {
00283             return 1;
00284         }
00285     case FEAPI_kMaxFrameSize:
00286         {
00287             return (float)((1<<30)-1);
00288         }
00289     case FEAPI_kOptFrameSize:
00290         {
00291             return floorf(0.01F * m_fSampleRate + .5F);
00292         }
00293     default:
00294         {
00295             // this shall never happen ...
00296             return -1;
00297         }
00298     }
00299     return -1;
00300 }
00301 
00303 // entry points
00304 #ifdef __cplusplus                                                      
00305 extern "C" {                                                            
00306 #endif                                                              
00307 
00308 FEAPI_ENTRY_POINTS(CFindAbsoluteRms)
00309 
00310 #ifdef __cplusplus                                                  
00311 }                                                               
00312 #endif
00313 
00314 

Generated on Fri Mar 23 10:28:54 2007 for FEAPI Plugin Documentation by  doxygen 1.3.9.1