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

FEAPIExamplePluginFindMax.cpp

Go to the documentation of this file.
00001 
00002 //     /*! \file FEAPIExamplePluginFindMax.cpp: \brief implementation of the CFindAbsoluteMax 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 
00038 #include <string>
00039 #include <iostream>
00040 #include <math.h>
00041 #include <cstdlib>
00042 
00043 #include "FEAPI.h"
00044 #include "FEAPIExamplePluginFindMax.h"
00045 #include "FEAPIEntryPoints.h"
00046 
00047 #ifndef FLT_MAX
00048 #define FLT_MAX 3.402823466e+38F
00049 #endif
00050 
00051 
00053 #define _MY_MAJOR_VERSION       0x00000001
00054 #define _MY_MINOR_VERSION       0x00000000
00055 #define _MY_SUB_VERSION         0x00000001
00056 
00057 
00058 // defines for plug in name etc.
00059 #define _MY_PLUGIN_NAME        "zMaxFind"
00060 #define _MY_PLUGIN_VENDOR      "zplane.development"
00061 #define _MY_PLUGIN_DESCRIPTION "This PlugIn searches each input audio data block for its maximum value per channel"
00062 #define _MY_PLUGIN_COPYRIGHT   "(c) 2004-2005 by zplane.development"
00063 #define _MY_PLUGIN_ID          "MaxFindId"
00064 
00065 
00066 // defines for description of result
00067 #define _FEATURE_NAME           "Absolute Maximum Value"
00068 #define _FEATURE_UNIT           "dBFS"
00069 #define _FEATURE_DESCRIPTION    "The maximum absolute value of channel: %d per input block"
00070 #define _FEATURE_RANGE_MIN      -740
00071 #define _FEATURE_RANGE_MAX      0.0F
00072 #define _FEATURE_QUANTIZED      -1  // value is not quantized
00073 #define _FEATURE_SAMPLERATE     -1  // output sample rate equals input block length
00074 
00075 
00076 CFindAbsoluteMax::CFindAbsoluteMax () : CFeatureExtractBase()
00077 {
00078     // set of strings that will be returned by the default methods
00079     m_cPluginName           = _MY_PLUGIN_NAME;
00080     m_cPluginVendor         = _MY_PLUGIN_VENDOR;
00081     m_cPluginDescription    = _MY_PLUGIN_DESCRIPTION;
00082     m_cPluginId             = _MY_PLUGIN_ID;
00083     m_cPluginCopyRight      = _MY_PLUGIN_COPYRIGHT;
00084 
00085     // set plug in version info
00086     m_iMajorVersion     = _MY_MAJOR_VERSION;
00087     m_iMinorVersion     = _MY_MINOR_VERSION;
00088     m_iSubVersion       = _MY_SUB_VERSION;
00089     
00090     m_pfMaxValue        = 0;
00091     m_pbIHaveResults    = 0;
00092     m_ptTimeStamp       = 0;
00093 }
00094 
00095 
00096 CFindAbsoluteMax::~CFindAbsoluteMax ()
00097 {
00098     // destroy feature vector
00099     if (m_pfMaxValue)
00100         delete [] m_pfMaxValue;
00101     m_pfMaxValue        = 0;
00102 
00103     // destroy time stamp vector
00104     if (m_ptTimeStamp)
00105         delete [] m_ptTimeStamp;
00106     m_ptTimeStamp       = 0;
00107 
00108     // destroy bool vector
00109     if (m_pbIHaveResults)
00110         delete [] m_pbIHaveResults;
00111     m_pbIHaveResults    = 0;
00112 }
00113 
00114 
00115 FEAPI_Error_t      CFindAbsoluteMax::InitializePlugin (float               fInputSampleRate, 
00116                                                         int                 iNumberOfAudioChannels,
00117                                                         int                 iHostApiMajorVersion,
00118                                                         FEAPI_UserData_t     *pstUserData)
00119 {
00120     CFeatureExtractBase::InitializePlugin ( fInputSampleRate, 
00121                                             iNumberOfAudioChannels,
00122                                             iHostApiMajorVersion,
00123                                             pstUserData);
00124 
00125     // Set information about the inputs
00126     char acChannelStr[FEAPI_kMaxDescriptionLength];
00127     for (int h = 0; h < iNumberOfAudioChannels; ++h) {
00128         sprintf(acChannelStr, "Channel: %d", h);
00129         SetPluginInputPinInfo(CPin::SetIndex(h)
00130             .SetName(acChannelStr)
00131             .SetUnit("")
00132             .SetDescription(acChannelStr)
00133             .SetRangeMin(-1.0f)
00134             .SetRangeMax(1.0f)
00135             .SetSampleRate(fInputSampleRate)
00136         );
00137     }
00138         
00139     // Set information about the results
00140     char acDescription[FEAPI_kMaxDescriptionLength];
00141     for (int k = 0; k < iNumberOfAudioChannels; ++k) {
00142         sprintf(acDescription, _FEATURE_DESCRIPTION, k);
00143         SetPluginResultPinInfo(CPin::SetIndex(k)
00144             .SetName(_FEATURE_NAME)
00145             .SetUnit(_FEATURE_UNIT)
00146             .SetDescription(acDescription)
00147             .SetRangeMin(_FEATURE_RANGE_MIN)
00148             .SetRangeMax(_FEATURE_RANGE_MAX)
00149             .SetQuantizedTo(_FEATURE_QUANTIZED)
00150             .SetSampleRate(_FEATURE_SAMPLERATE)
00151         );
00152     }
00153 
00154     // This plugin does not have any parameter
00155 
00156     // allocate one dimensional result/feature vector
00157     int iNumberOfResults = GetPluginNumOfResults();
00158     if (m_pfMaxValue)
00159         delete m_pfMaxValue;
00160     m_pfMaxValue        = new float [iNumberOfResults];
00161     if (m_ptTimeStamp)
00162         delete m_ptTimeStamp;
00163     m_ptTimeStamp       = new FEAPI_TimeStamp_t [iNumberOfResults];
00164     
00165     // allocate one dimensional result/feature vector
00166     if (m_pbIHaveResults)
00167         delete m_pbIHaveResults;
00168     m_pbIHaveResults    = new bool [iNumberOfResults];
00169 
00170     // initialize bool vector
00171     for (int i = 0; i < iNumberOfResults; i++)
00172         m_pbIHaveResults[i] = false;
00173 
00174     return FEAPI_kNoError;
00175 }
00176 
00177 FEAPI_Error_t      CFindAbsoluteMax::SetPluginParameter (int iParameterIndex, float fValue)
00178 {
00179     // we don't have any parameters in this plugin
00180     return FEAPI_kUnknownError;
00181 }
00182 
00183 
00184 float       CFindAbsoluteMax::GetPluginParameter (int iParameterIndex)
00185 {
00186     // we don't have any parameters in this plugin
00187     return -1;
00188 }
00189 
00190 
00191 int         CFindAbsoluteMax::GetPluginResultLatency (int iResultIndex)
00192 {
00193     //<! \todo format???
00194     return 0;
00195 }
00196 
00197 
00198 FEAPI_Error_t      CFindAbsoluteMax::ProcessPluginDone ()
00199 {
00200     // we don't have to do anything special here
00201     return FEAPI_kNoError;
00202 }
00203 
00204 FEAPI_Error_t      CFindAbsoluteMax::ProcessPlugin (const float **ppfInputBuffer, const FEAPI_TimeStamp_t *ptTimeStamps, int iNumberOfFrames)
00205 {
00206     int iCh,
00207         iIdx;
00208 
00209     if (!m_bIsInitialized)
00210         return FEAPI_kUnspecifiedError;
00211 
00212     for (iCh = 0; iCh < GetPluginNumOfInputs(); iCh++)
00213     {
00214         m_pfMaxValue[iCh]   = 0.0F;
00215         for (iIdx = 0; iIdx < iNumberOfFrames; iIdx++)
00216         {
00217             float fAbsValue = fabsf (ppfInputBuffer[iCh][iIdx]);
00218             if (fAbsValue > m_pfMaxValue[iCh])
00219             {
00220                 m_pfMaxValue[iCh]   = fAbsValue;
00221                 m_ptTimeStamp[iCh]  = ptTimeStamps[iCh];
00222             }
00223         }
00224         m_pbIHaveResults[iCh]       = true;
00225     }
00226 
00227     return FEAPI_kNoError;
00228 }
00229 
00230 int         CFindAbsoluteMax::GetPluginSizeOfResult (int iResultIndex)
00231 {
00232     if (iResultIndex >= GetPluginNumOfResults())
00233         return -1;
00234 
00235     if (m_pbIHaveResults[iResultIndex])
00236         return (sizeof(m_pfMaxValue[iResultIndex])/sizeof(float));
00237     else
00238         return 0;
00239 }
00240 
00241 
00242 FEAPI_Error_t      CFindAbsoluteMax::GetPluginResult (int iResultIndex, float *pfResult, FEAPI_TimeStamp_t *ptTimeStamp)
00243 {
00244     if ((iResultIndex >= GetPluginNumOfResults()) || (!m_pbIHaveResults[iResultIndex]))
00245         return FEAPI_kUnknownError;
00246 
00247     if (m_pfMaxValue[iResultIndex] < 1e-37)
00248         m_pfMaxValue[iResultIndex]  = 1e-37F;
00249     *pfResult       = 20*logf(m_pfMaxValue[iResultIndex])/logf(10.0F);
00250  
00251     *ptTimeStamp    = m_ptTimeStamp[iResultIndex];
00252 
00253     m_pbIHaveResults[iResultIndex]  = false;
00254     
00255     return FEAPI_kNoError;
00256 }
00257 
00258 
00259 FEAPI_Error_t      CFindAbsoluteMax::ResetPlugin ()
00260 {
00261     int i;
00262 
00263     for (i = 0; i < GetPluginNumOfResults(); i++)
00264     {
00265         m_pfMaxValue[i] = 0.0F;
00266         m_ptTimeStamp[i]= 0.0F;
00267     }
00268     return FEAPI_kNoError;
00269 }
00270 
00271 
00272 float        CFindAbsoluteMax::GetPluginProperty ( FEAPI_PluginProperty_t ePluginProperty)
00273 {
00274     switch (ePluginProperty)
00275     {
00276     case FEAPI_kMinSampleRate:
00277         {
00278             return 1;
00279         }
00280     case FEAPI_kMaxSampleRate:
00281         {
00282             return 1e38F;
00283         }
00284     case FEAPI_kMinChannels:
00285         {
00286             return 1;
00287         }
00288     case FEAPI_kMaxChannels:
00289         {
00290             return (float)((1<<30)-1);
00291         }
00292     case FEAPI_kMinFrameSize:
00293         {
00294             return 1;
00295         }
00296     case FEAPI_kMaxFrameSize:
00297         {
00298             return (float)((1<<30)-1);
00299         }
00300     case FEAPI_kOptFrameSize:
00301         {
00302             return floorf(0.01F * m_fSampleRate + .5F);
00303         }
00304     default:
00305         {
00306             // this shall never happen ...
00307             return -1;
00308         }
00309     }
00310     return -1;
00311 }
00312 
00313 
00315 // entry points
00316 #ifdef __cplusplus                                                      
00317 extern "C" {                                                            
00318 #endif                                                              
00319 
00320 FEAPI_ENTRY_POINTS(CFindAbsoluteMax)
00321 
00322 #ifdef __cplusplus                                                  
00323 }                                                               
00324 #endif
00325 
00326 

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