Author |
Message |
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Tue Mar 18, 2008 2:04 pm Post subject:
UGens and parameters as arguments? |
|
|
I would like to use a UGen as an argument to a function.
I would like to be able to assign a different parameter of a different UGen every time I call the function.
When I try to run something like this then I get an error stating that "UGen has no member .freq".
what would be the best way to approach this?
like this, for instance:
Code: |
fun void do_something (UGen u) {
Std.rand2f( 100, 200) => u.freq;
}
//call function
while(true) {
do_something( LPF );
100::ms => now;
} |
|
|
Back to top
|
|
|
Frostburn
Joined: Dec 12, 2007 Posts: 255 Location: Finland
Audio files: 9
|
Posted: Wed Mar 19, 2008 1:40 am Post subject:
|
|
|
I recommend creating a base class with the desired member functions and extending it. Then you can write functions that take the base class as an argument and do their thing to it.
Code: | //This is the base class
class MyOscillator{
Gain out;
//These are supposed to be overriden
fun float freq(){
return 0.0;
}
fun float freq(float _freq){
return _freq;
}
}
//Let's extend it:
class MySawtooth extends MyOscillator{
BlitSaw blitsaw => out;
//Override:
fun float freq(){
return blitsaw.freq();
}
fun float freq(float _freq){
_freq => blitsaw.freq;
0 => blitsaw.harmonics; //Calculate harmonics up to nyquist
return blitsaw.freq();
}
}
class MyFilteredBlit extends MyOscillator{
Blit blit => LPF lpf => out;
//You can also set some basic parameters in the constructor
4.0 => lpf.Q;
//Even call the member functions
220.0 => freq;
//Override:
fun float freq(){
return blit.freq();
}
fun float freq(float _freq){
_freq => blit.freq;
3.0*_freq => lpf.freq;
10 => blit.harmonics;
return blit.freq();
}
}
//Here's our function using the base class
fun void do_something (MyOscillator mo) {
Std.rand2f( 100, 200) => mo.freq;
}
//create an instance:
MyFilteredBlit myOsc;
//MySawtooth myOsc; //You can try this one too
myOsc.out => dac; //connect it
//call function
while(true) {
do_something( myOsc );
500::ms => now;
} |
As an added bonus you'll be able to make neat packages out of your patches and you're not limited to chucks native ugens. _________________ To boldly go where no man has bothered to go before. |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Thu Mar 20, 2008 9:10 am Post subject:
|
|
|
Hey thanks Frostburn. Now i'm cruisin' right along. |
|
Back to top
|
|
|
|