Hello Jean,

You can solve your problem by declaring an empty variable (Osc is the 
superclass of both SinOsc and TriOsc):

fun void playForPixels(int totalpixels, string osc) {
    Osc @ sh;
    if ( osc == "sin" ) { new SinOsc @=> sh; }
    else {
        if ( osc == "tri" ) { new TriOsc @=>  sh; }
        else { new SawOsc @=> sh; }
    }

    440 => osc.freq; // Still allows you to control frequency...

Best,
Casper


> On Oct 5, 2015, at 04:19, Jean Menezes da Rocha <[email protected]> 
> wrote:
> 
> Hello, guys,
> 
> I am trying to choose which UGen to load (SinOsc, TriOsc or SawOsc) based on 
> a given condition. This sample code
> 
>     fun void playForPixels(int totalpixels, string osc) {
>         if ( osc == "sin" ) { SinOsc sh; }
>         else {
>             if ( osc == "tri" ) { TriOsc sh; }
>             else { SawOsc sh; }
>         }
> 
>         Envelope e => Gain g => dac;
> 
>         ( ( s + v) * 100 )::ms => dur dura;
>         s * v => g.gain;
>         ( dura / 40 ) / second => e.time;
> 
>         h * 2000 => sh.freq;
> 
>         sh => e;
> 
> (code goes on...)
> 
> gives me the following error:
> 
> [listener.ck <http://listener.ck/>]:line(38): undefined variable/member 'sh' 
> in class/namespace 'Pixel'...
> [listener.ck <http://listener.ck/>]: ...in function 'playForPixels'
> 
> (i am working inside a class named 'Pixel', in case it is important).
> 
> How can I load my UGen and make it persist outside the scope of the if clause?
> 
> Thanks in advance!
> -- 
> Jean Menezes da Rocha
> Compositor
> Professor -- Faculdades Est
> Mestre e Doutorando em Composição pela Universidade Federal da Bahia
> _______________________________________________
> chuck-users mailing list
> [email protected]
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users

_______________________________________________
chuck-users mailing list
[email protected]
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users

Reply via email to