[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] [APPROVE] Adding a RunningAverage UGen



Is it Ok for me to add a RunningAverage UGen (to
filters.cpp? any better place?). This supports some
time domain onset detectors based on RMS levels, and
is probably generally useful for RMS calcs and
possibly further stuff?  

The code would look like the below, except I'll take
out the squaring and rooting for RMS since this could
be done as 

//RMS Power over 60 samples
RunningAverage.ar(input.squared, 60).sqrt

I'd welcome any comments...

/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights
reserved.
	http://www.audiosynth.com

    This program is free software; you can
redistribute it and/or modify
    it under the terms of the GNU General Public
License as published by
    the Free Software Foundation; either version 2 of
the License, or
    (at your option) any later version.

    This program is distributed in the hope that it
will be useful,
    but WITHOUT ANY WARRANTY; without even the implied
warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General
Public License
    along with this program; if not, write to the Free
Software
    Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA  02111-1307  USA
*/

//RMS UGen implemented by Nick Collins
(sicklincoln.org)

#include "SC_PlugIn.h"
    
        static InterfaceTable *ft; 
        
        struct RMSPower : public Unit 
        {
        int msamp, mcount;     
        float msum;
        float mmeanmult;
        float* msquares;
        };

        extern "C" {  
  
            void RMSPower_next_k(RMSPower *unit, int
inNumSamples);
            void RMSPower_Ctor(RMSPower* unit);
            void RMSPower_Dtor(RMSPower* unit);
        }
    
        void RMSPower_Ctor( RMSPower* unit ) {
                         
        SETCALC(RMSPower_next_k);
        
        unit->msamp= (int) ZIN0(1);
     
        unit->mmeanmult= 1.0f/(unit->msamp);
        unit->msum=0.0f;
        unit->mcount=0; //unit->msamp-1;
        unit->msquares= (float*)RTAlloc(unit->mWorld,
unit->msamp * sizeof(float));
        
        //initialise to zeroes
        for(int i=0; i<unit->msamp; ++i)
        unit->msquares[i]=0.f;
        
        }
    
        void RMSPower_Dtor(RMSPower *unit)
        {
	RTFree(unit->mWorld, unit->msquares);
       }
    
        //RMS is easy because convolution kernel can
be updated just by deleting oldest
        //sample and adding newest-
        //half hanning window convolution etc requires
updating values for all samples in memory
        //on each iteration 
        void RMSPower_next_k( RMSPower *unit, int
inNumSamples ) {
        
        float *in = ZIN(0);
        float *out = ZOUT(0);
        
        int count= unit->mcount;
        int samp= unit->msamp;

        float * data= unit->msquares;
        float sum= unit->msum;
        float meanmult= unit->mmeanmult;
  
        LOOP(inNumSamples, 
        
        //update sum
        sum -=data[count];
        float next= ZXP(in);
        next *= next;
        data[count]= next;
        sum += next;
         
        count=(count+1)%samp;
        
                ZXP(out) = sum*meanmult;
//sqrt((sum/samp));	//could ditch the mean and sqrt
bit if don't need normalised
	);

	unit->mcount =count;
        unit->msum =  sum;      
    
        }

    
        extern "C" void load(InterfaceTable *inTable)
{
                
            ft = inTable;

            DefineDtorUnit(RMSPower);
                    }
        
        
    




	
	
		
___________________________________________________________
BT Yahoo! Broadband - Free modem offer, sign up online today and save £80 http://btyahoo.yahoo.co.uk