[chuck-users] function for creating 8 bit waveforms from expressions

Steve Morris steve at judgement.com
Sat Dec 28 16:30:47 EST 2013

See my post about polymorphism. To "pass in an expression in on the fly"
you need to wrap the function in a polymorphic class and "on the fly" pass
a reference to that class instead. For example if you are doing this in a
ChuGen you can create a class variable to hold a reference to the "current"
expression object. At the appropriate time you can assign a new class to
the reference variable in much the same way you would change the gain or
frequency on the fly. The tick() method would call the current function
through the reference variable. You can then put the common code directly
in the ChuGen and code you want to change at run time in the referenced

This is all simpler if all classes are defined in the same file and thread
but I believe that if you are clever the new code doesn't even need to be
defined when the ChuGen is started. All it needs is for the base class to
be defined. You could connect and start up the ChuGen and then weeks later
write new code and put it into the reference variable of the running
ChuGen. The tricky part is how to pass the new object from a different
thread. This requires using static class variables which is a subject for a
different post.


PS all the helper functions you can imagine are probably already written
and available in the LiCK Library for ChucK. If you don't have it already
you should get it immediately. Half the things I was originally planning of
writing in ChucK I found already implemented in LiCK. It is also a great
thing to read if you want to see how complex things can be done in ChuCK.

On Fri, Dec 27, 2013 at 7:16 PM, Joel Matthys <jwmatthys at yahoo.com> wrote:

> Of course this isn't exactly what you asked about, since it takes in an
> 8-bit number and you want to take in a number [0-8000], but maybe this will
> get you started.
> Maybe Steve can help with how to pass an expression as a reference, which
> would make it possible to change the expression on the fly.
> Joel
> On 12/27/2013 06:13 PM, Joel Matthys wrote:
>> On 12/27/2013 05:08 PM, Steve Morris wrote:
>>> It is a mystery to many of us exactly why users are not allowed to know
>>> how to do this. Questions to this list are basically ignored except by
>>> other users. I suspect that ChucK developers don't read this list and the
>>> real point of this list is to hope users will support each other and stop
>>> bothering the developers. It is also possible that there are no real
>>> developers, that ChucK development consists of an occasional  grad student
>>> who shows interest for a while then moves on to more productive endeavors.
>> There is a ChuGen example in the examples/extend folder; it's extremely
>> straight-forward. I know the devs read this list, but honestly this is
>> exactly the kind of question we as a community should be able to help with.
>> Let's light some candles rather than curse the darkness.
>> Now, ChuGen takes floats in the range [-1,1] for its calculations, but
>> you want to work on 8-bit ints, so you'll have to have functions that
>> convert itof and ftoi. Then you can write your expression to work on those
>> samples directly. A Phasor UGen then provides the 0-1 samples in. Try this:
>> --
>> // ChuGen
>> // Create new UGens by performing audio-rate processing in ChucK
>> class MyFunc extends Chugen
>> {
>>     8 => int _b; // default 8 bits
>>     fun float tick(float in)
>>     {
>>         ftoi (in, _b) => int ival; // convert in sample to int
>>         my_expression(ival) % (1 << _b) => int fval; //calculate and mod
>> into range
>>         return (itof (fval, _b)); // convert back to float
>>     }
>>     // your custom expression goes here
>>     fun int my_expression (int t)
>>     {
>>         return t * ( t >> 8 * ( t >>15 | t >> 8) & 20 | ( t >>19) * 5 >>
>> t | t >> 3);
>>     }
>>     // helper function to convert float [0,1] to b bits
>>     fun int ftoi (float in, int b)
>>     {
>>         return (in * (1 << b)) $ int;
>>     }
>>     // helper function to convert b bit integer to float [-1,1]
>>     fun float itof (int in, int b)
>>     {
>>         return (in * 1.0 / (1 << (b-1))) - 1;
>>     }
>>     // allow user to set number of bits
>>     fun void bits (int b)
>>     {
>>         b => _b;
>>     }
>> }
>> // phasor powers the function with numbers ascending 0-1
>> Phasor p => MyFunc f => dac;
>> 1 => p.freq;
>> while(true) 1::second => now;
>> /// END ///
>> Joel
>> _______________________________________________
>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20131228/45cbad2b/attachment.html>

More information about the chuck-users mailing list