Gamma: synth2 deconstructed

From cmwiki
Jump to: navigation, search

Disclaimer

The official website for Gamma is http://mat.ucsb.edu/gamma.

This is NOT an official gamma website. Everything HERE is just random scribblings by people trying to understand gamma--and might be inaccurate, out of date, or just plain wrong. You have been warned.

In S12 MAT276AI, JoAnn Kuchera-Morin provided the following example file for doing synthesis with basic sine waves:

Media:synth2.cpp

This page is an attempt to deconstruct and understand the code in this file.

First, I will not repeat any material from Gamma: synth1 deconstructed, so read that first. I'm going to concentrate on the stuff that's new.

ArrayPow2

In synth2.cpp, we find:

 ArrayPow2<float>
                tbSaw(2048), tbSqr(2048), tbImp(2048), tbSin(2048), tbPls(2048),
                tb__1(2048), tb__2(2048), tb__3(2048), tb__4(2048);

ArrayPow2 is defined in Gamma/Containers.h and it is not at all clear on first glance what it is for. But from context, it appears to be something you can use to build a complex waveform out of partials, when combined together with AddSinesPow<1> which is found in Gamma/tbl.h:

        addSinesPow<1>(tbSaw, 9,1);
        addSinesPow<1>(tbSqr, 9,2);
        addSinesPow<0>(tbImp, 9,1);

It's a bit tough to find the definition, because if you are looking for a template with one parameter, you wont find one. Instead, you are looking for this: a template with one parameter that comes from the number in the <>, and several others that are inferred from the type of the first parameter (e.g. tbSaw, tbSqr, tbImp, each of which is an ArrayPow2<float>.)

template <int InvPower, class Alloc, class T, template<class,class> class ArrayType>
inline void addSinesPow(
        ArrayType<T,Alloc>& dst, int numh,
	double hmul=1, double hshf=1, double amp=1, double hphs=0, double wphs=0
){
        addSinesPow<InvPower>(&dst[0], dst.size(), numh,hmul,hshf,amp,hphs,wphs);
}

The documentation for this function suggests the following:

/// Add multiple sine waves to array                                                      

/// \tparam InvPower    amplitudes will be set to 1 / h^InvPower                          
/// @param[out] dst             destination array                                         
/// @param[in] len              length of destination array                               
/// @param[in] numh             total number of harmonics                                 
/// @param[in] hmul             harmonic number multiplication factor                     
/// @param[in] hshf             harmonic number shift amount                              
/// @param[in] amp              overall amplitude scaling factor                          
/// @param[in] hphs             phase of (sine) harmonics, in [0,1]                       
/// @param[in] wphs             phase of composite waveform, in [0,1]