[chuck-users] Load UGen if condition is met

Jean Menezes da Rocha jean at menezesdarocha.info
Mon Oct 5 02:18:12 EDT 2015


Thank you very much, Casper!
Although I got another solution myself (my code became really big so I
abstracted some classes),
it is good to know that I can use this method when trying something quicker.

Best regards!

On Mon, Oct 5, 2015 at 3:01 AM, Casper Schipper <casper.schipper at gmail.com>
wrote:

> 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 <jean at menezesdarocha.info>
> 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]:line(38): undefined variable/member 'sh' in class/namespace
> 'Pixel'...
> [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
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
>
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
>


-- 
Jean Menezes da Rocha
Compositor
Professor -- Faculdades Est
Mestre e Doutorando em Composição pela Universidade Federal da Bahia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20151005/56928979/attachment-0001.html>


More information about the chuck-users mailing list