ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
803 stars 128 forks source link

add functionality for shreds to set child stack size hint #365

Closed gewang closed 1 year ago

gewang commented 1 year ago

test program (not part of the PR)

// test many simultaneous shreds
// each sporked shred is relatively lightweight
// NOTE there is a sine wave 'foo' signal; to hear when
//   real-time audio breaks down either due to the sheer # of
//   shreds running or from adding a new shred when there is
//   a lot of shreds in the system; it is possible that audio
//   clicks when adding shreds (e.g., beyond 10000) but that
//   audio is actually smooth once no more shreds are added
//   (e.g., when N is reached); this is also a function of the
//   rate that the shreds are waking up and doing something,
//   controlled in the `randomizer()` function
// NOTE by default, each shred allocates a 65K 
int N;

// check arguments
if( me.args() ) me.arg(0) => Std.atoi => N;
// default value
if( N <= 0 ) 25000 => N;

// how many to spork at a time
N / 250 => int INC;
// make sure at least 1
if( INC == 0 ) 1 => INC;

// let's keep this one at constant freq
// for different values of N, does this sound stable?
SinOsc foo => dac;
440 => foo.freq;
.1 => foo.gain;

// make another one to randomizer
SinOsc bar => dac;
.1 => bar.gain;

// set child stack sizes to be much smaller
// so we can spork A LOT of shreds with less memory
me.childMemSize( 1024 );
me.childRegSize( 256 );

// gonna be sporking a lot of shreds and keep them running
<<< "sporking", N, "shreds..." >>>;

int sporked;
while( sporked < N )
{
    // spork away
    repeat( INC ) spork ~ randomizer( bar );
    // increment
    INC +=> sporked;
    // print
    <<< "sporked", sporked, "shreds so far..." >>>;
    // wait
    50::ms => now;
}

// a long time
eon => now;

// entry point for shreds
fun void randomizer( Osc oscil )
{
    while( true )
    {
        // random frequency
        Math.random2f(100,4000) => oscil.freq;
        // wait some randomized amount
        20*Math.random2f(.9,1.1)::second => now;
    }
}