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

Joel Matthys jwmatthys at yahoo.com
Fri Dec 27 19:13:02 EST 2013

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