[chuck-users] New to Chuck - Clases and Machine.add question

Michael Heuer heuermh at gmail.com
Fri Nov 13 11:56:15 EST 2015


Hello Oba,

I think I've ran into this before, the instance of mymidivals you create at
the bottom of midivaltest.ck goes out of scope at the end of the
Machine.add call.  When running the test score file you may notice that
Machine.add creates a new shred.

To fix this you could either move the instance declaration to the test
score file or make the midivals function static.

Or perhaps someone else might have better advice, because I still have
problems with initializers and statics.  For example, this package of
classes is all sorts of broken
https://github.com/heuermh/lick/tree/master/lick/tuning

   michael


On Fri, Nov 13, 2015 at 8:57 AM, Oba Ozai <obaozai at yahoo.com> wrote:

> Hi,
> Im stuck with something simple, i cant figure out :)
>
> This works fine =>
> // test score
> // sound chain;
> SinOsc s => dac;
> // create class with a function to return midi values
> //given and input string
> class mymidivals {
> int result;
> fun int midivals (string x){
> if (x == "c4") 60 => result;
> if (x == "d4") 62 => result;
> if (x == "e4") 64 => result;
> if (x == "f4") 65 => result;
> if (x == "g4") 67 => result;
> if (x == "a4") 69 => result;
> if (x == "b4") 71 => result;
> if (x == "c5") 72 => result;
> return result;
> } // fun
> }// class
>
> //define object
> mymidivals midivals;
> // create array with the melody notes
> ["c4","d4","e4","f4","g4","a4","b4","c5" ] @=> string song[];
> song.cap() => int songcap;
> for (0 => int i; i < songcap ; i++) {
> 0.2 =>s.gain;
> Std.mtof(midivals.midivals(song[i])) => s.freq;
> 0.5::second => now;
> ===================================
> But if i put the function mymidivals on a .ck file like this
> //midivaltest.ck
> // create class with a function to return midi values
> //given and input string
> public class mymidivals {
> int result;
> fun int midivals (string x){
> if (x == "c4") 60 => result;
> if (x == "d4") 62 => result;
> if (x == "e4") 64 => result;
> if (x == "f4") 65 => result;
> if (x == "g4") 67 => result;
> if (x == "a4") 69 => result;
> if (x == "b4") 71 => result;
> if (x == "c5") 72 => result;
> return result;
> } // fun
> }// class
> mymidivals midivals;
> =============================================
> and if call it this way, it does not work, error => undefined variable
> "midivals"
>
> // test score
> // sound chain;
> SinOsc s => dac;;
> // midivaltest.ck lives on same dir as this code
> Machine.add(me.dir() + "midivaltest.ck");
> // create array with the melody notes
> ["c4","d4","e4","f4","g4","a4","b4","c5" ] @=> string song[];
> song.cap() => int songcap;
> for (0 => int i; i < songcap ; i++) {
> 0.2 =>s.gain;
> Std.mtof(midivals.midivals(song[i])) => s.freq;
> 0.5::second => now;
> }
>
> -------------------------------------------------------
> any ideas ? Thanks
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20151113/0d399178/attachment.html>


More information about the chuck-users mailing list