Debian Codegraph @

src/host-examples/ck/test-4-shreds.ck

raw
1 //---------------------------------------------------------------------
2 // name: test-4-shreds.ck
3 // desc: working with shreds and custom Event
4 // (adapted from examples/event/event-extend2.ck)
5 // (used by host-examples/example-4-shreds.cpp)
6 //
7 // the "main" shred sporks four child shreds, each of which
8 // sets an concurrent voice (using FrencHrn). the main shred
9 // then signals the child shreds one by one, passing along
10 // pitch and velocity info using the custom event we defined.
11 //---------------------------------------------------------------------
12
13 // define a custom event class
14 class TheEvent extends Event
15 {
16 int pitch;
17 float velocity;
18 }
19
20 // instantiate the event
21 TheEvent e;
22
23 // shared reverb
24 NRev reverb => dac;
25 // set mix
26 .15 => reverb.mix;
27
28 // handler
29 fun void hi( TheEvent e, int id )
30 {
31 // voice
32 FrencHrn f => reverb;
33
34 while( true )
35 {
36 // wait on event
37 e => now;
38 // set the pitch
39 e.pitch => Std.mtof => f.freq;
40 // play it
41 e.velocity => f.noteOn;
42 // wait a bit
43 150::ms => now;
44 // note off
45 f.noteOff( 0 );
46 }
47 }
48
49 // spork
50 spork ~ hi( e, 1 );
51 spork ~ hi( e, 2 );
52 spork ~ hi( e, 3 );
53 spork ~ hi( e, 4 );
54
55 // yield to let sporked shreds to have a chance to run and wait on the event
56 me.yield();
57
58 // infinite time loop
59 while( true )
60 {
61 // set pitch
62 Math.random2( 48, 84 ) => e.pitch;
63 // set velocity
64 Math.random2f( .5,1 ) => e.velocity;
65 // signal one waiting shred
66 e.signal();
67 // advance time
68 600::ms => now;
69 }