[chuck-users] variable of type function
Atte André Jensen
atte.jensen at gmail.com
Mon Jul 17 06:01:59 EDT 2006
Spencer Salazar wrote:
> Hey Atte,
> I dont think its possible to store references to functions, or
> function pointers (a four letter word these days :P )
Is function pointers considered bad? If so, why?
> but you can
> achieve something very similar with objects and polymorphism.
Not as far as I can see (in my case).
The function I'd like to reference to is inside an instrument class.
Each member function is an instrument, so it's something like:
public class Instruments
{
fun static void sine(float note, float dynamic, dur length){
1 => float volume;
sinosc osc => ADSR env => dac;
dynamic => osc.gain;
std.mtof(note) => osc.freq;
1 => env.keyOn;
length => now;
1 => env.keyOff;
.1::second => now;
}
}
Then I have a sequencer class that essentially steps through an array of
notes and spork ~ Instruments.sine() for every entry in the array. For
the sequencer to be able to use any of my instruments defined in
Instruments class I need to do:
public class Seq{
function instrument;
//later...
spork ~ instrument();
setFun(function new_instrument){
new_instrument => instrument;
}
}
Seq s;
s.setFun(Instruments.sine);
--
peace, love & harmony
Atte
http://www.atte.dk | quartet: http://www.anagrammer.dk
http://www.atte.dk/gps | compositions: http://www.atte.dk/compositions
More information about the chuck-users
mailing list