Debian Codegraph @

examples/event/event-extend2.ck

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