Joined: May 20, 2008
|Posted: Mon Mar 05, 2012 3:34 pm Post subject:
Subject description: include code from another file
|I want to make a library of functions that I can call from any chuck script. If I put those functions in one file, how do I access them from another? It would be great if I could make them into a new namespace, akin to Std.X or Math.X (I am using miniAudicle 0.2.0, not command line.)
(I checked the language specification for the words "include" and "import" and found nothing relevant.)
PS is there a popular/built-in solution for making exponentially distributed, normally distributed, etc random variables? Standard algorithms for these tasks are easily available, and I can certainly implement those, but if someone already has, so much the better.
PPS If no such "library" exists already, I'd be happy to share my work - is there a common way for people to share code like this?
Joined: May 20, 2008
|Posted: Wed Mar 07, 2012 8:33 am Post subject:
|Here are the responses from the chuck-users list:
|Jordan Orelli wrote: |
|use Machine.add. The docs aren't descriptive, but when you Machine.add a ChucK program, any classes it defines will be available in the runtime for any future chuck programs you add to the VM. The ChucK compiler is single-pass and has no concept of make files or a build system, though, so if you want one file to include another file, you have to have a third file that defines the order to add the files to the VM.
e.g., here's an example of a ChucK program that I invoke that imports a bunch of other utility files: https://github.com/jordanorelli/wii-chuck-synth/blob/master/resin.ck
if you look in resin_run.ck, I utilize some of the stuff that is imported in resin.ck. resin_run.ck will fail to compile if those other files aren't already loaded into the VM, so it's resin that I run from the command line.
|Michael Heuer wrote: |
|> I want to make a library of functions that I can call from any chuck script.
> If I put those functions in one file, how do I access them from another?
> It would be great if I could make them into a new namespace, akin to Std.X
> or Math.X (I am using miniAudicle 0.2.0, not command line.)
> (I checked the language specification for the words "include" and "import"
> and found nothing relevant.)
Machine.add("filename.ck"); loads filename.ck on a separate shred.
There is no include/import or namespace mechanism in ChucK.
> PS is there a popular/built-in solution for making exponentially
> distributed, normally distributed, etc random variables?
No. I would love to see such. I ported this just recently but
haven't tested it yet
// adapted from org.apache.commmons.math3.random.AbstractRandomGenerator,
// which implements the Polar Method, due to G.E.P. Box, M.E.
Muller and G. Marsaglia,
// as described in D. Knuth, _The Art of Computer Programming_, 3.4.1C.
fun static float nextGaussian()
0.0 => float v1;
0.0 => float v2;
1.0 => float s;
while (s >= 1.0)
2.0 * Std.randf() - 1.0 => v1;
2.0 * Std.randf() - 1.0 => v2;
s = v1 * v1 + v2 * v2;
if (s > 0.0)
s = Math.sqrt(-2.0 * Math.log(s)/s);
v2 * s => cached;
return v1 * s;
> PPS If not, I'd be happy to share my work - is there a common way for people
> to share code like this?
This mailing list, or the ChucK wiki
or e.g. github
|Kassen wrote: |
> I want to make a library of functions that I can call from any chuck script.
> If I put those functions in one file, how do I access them from another? It
> would be great if I could make them into a new namespace, akin to Std.X or
> Math.X (I am using miniAudicle 0.2.0, not command line.)
Right now I'd say make a public class called -say- "Lib" and outfit it
with static member functions that would be your "x". Then you'd add it
using Machine.add(file_with_class.ck) at the start of your session.
That way Lib.x() might be exactly equivalent to Math.x() in usage.
The downside is the way of loading it, but after that it'd be
essentially identical in usage.
> (I checked the language specification for the words "include" and "import" and
> found nothing relevant.)
Yes, clearly the above is a workaround. People were working on exactly
this functionality for library style functions. It hasn't been very
busy on that front for a while, but recently things are picking up a
bit again, I think.
> PS is there a popular/built-in solution for making exponentially distributed,
> normally distributed, etc random variables?
There was something like that, I seem to remember, but I can't
remember who did it. Clearly good tools for randomness would be a good
idea, maybe even as a build-in library as randomness is quite
fundamental to computer-music and synthesis and just having
white-noise is a bit limited. We used to collect stuff at the
Princeton CS wiki, where (generously) non-students (or students of
other insitutions) could also have accounts, then there was a wave of
spam and new accounts can no longer be casually created.
> PPS If not, I'd be happy to share my work - is there a common way for people to
> share code like this?
Erm... anyone? This sounds worthwhile.
|Robert Poor wrote: |
I love github as a shared repository of code. To answer your first
questions -- if you have the stomach -- look in:
The _loadup.ck file loads all the requisite files in the proper order,
and finally calls:
Machine.add(HOME + "_main.ck");
which has the main performance loop. _main's last line is:
1::week => now;
so the main shred runs for a week -- the PatchManager (q.v.) sporks
and kills patch-specific shreds as required.
As for gaussian random numbers, look up
... any random number generator plus a couple of trig calls does the trick.
(cross posting here and the mail-list won't be a habit. I was having trouble getting on the list.)
So it looks like the thing to do is to import all your importables in one "startup.ck" file every session. I have to say that this is a rather disappointing state of affairs. It seems a little perverse to have a robust OO framework without proper code reuse facilities.
Joined: Jan 18, 2005
Audio files: 355
G2 patch files: 100
|Posted: Wed Mar 07, 2012 2:19 pm Post subject:
|ChucK runs well, usually doesn't crash, and fun and very useful, but I'm not sure I'd call it "robust"...
I was going to give a go at mentioning the include file "trick", but forgot about it. I wouldn't have been able to explain it as well as those mailing list posts anyway.
I too would welcome a proper way to include files. It may be that the work involved in making that happen isn't as fun as messing around with FFT and oscillators, which I can totally sympathize with. Part of the charm with ChucK (as I choose to see it) is that focus is on the interesting stuff, and the rest is done with a shrug and a laugh. The ChucK operator is a bit of brilliant computer science comedy actually.
@soundcloud @Flattr home - you can't explain music