electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
import/include
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
flies



Joined: May 20, 2008
Posts: 33
Location: NJ

PostPosted: Mon Mar 05, 2012 3:34 pm    Post subject: import/include
Subject description: include code from another file
Reply with quote  Mark this post and the followings unread

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?
Back to top
View user's profile Send private message
flies



Joined: May 20, 2008
Posts: 33
Location: NJ

PostPosted: Wed Mar 07, 2012 8:33 am    Post subject: Reply with quote  Mark this post and the followings unread

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.

http://chuck.cs.princeton.edu/doc/language/spork.html

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

Code:

   //
   // 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

http://wiki.cs.princeton.edu/forums.html/ChucK

or e.g. github

https://github.com/heuermh/lick


Kassen wrote:
> Hi,

Hey George!
>
> 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:
George:

I love github as a shared repository of code. To answer your first
questions -- if you have the stomach -- look in:

https://github.com/rdpoor/chuck_performance_setup

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

http://en.wikipedia.org/wiki/Box-Muller_transform

... any random number generator plus a couple of trig calls does the trick.

- Rob


(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.
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Wed Mar 07, 2012 2:19 pm    Post subject: Reply with quote  Mark this post and the followings unread

ChucK runs well, usually doesn't crash, and fun and very useful, but I'm not sure I'd call it "robust"... Wink

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. Laughing

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use