File: r-multi-msg.ck

package info (click to toggle)
chuck 1.5.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,056 kB
  • sloc: cpp: 123,473; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (89 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//----------------------------------------------------------------------------
// name: r-multimsg.ck
// desc: OSC example: receiver for multiple message types
// note: launch with s-multimsg.ck
//
// author: Ge Wang (https://ccrma.stanford.edu/~ge/)
// date: spring 2022
//----------------------------------------------------------------------------

// the patch
BlitSaw s => JCRev r => dac;
.5 => s.gain;
.1 => r.mix;

// spork the handlers, one for each message type
spork ~ onNotes();
spork ~ onHarmonics();

// keep alive
while( true ) 1::second => now;

// handler for incoming OSC notes messages
fun void onNotes()
{
    // create our OSC receiver
    OscIn oin;
    // create our OSC message
    OscMsg msg;
    // use port 6449 (or whatever)
    6449 => oin.port;
    // create an address in the receiver, expect an int and a float
    oin.addAddress( "/foo/notes, if" );
    
    // infinite event loop
    while( true )
    {
        // wait for event to arrive
        oin => now;
        
        // grab the next message from the queue. 
        while( oin.recv(msg) )
        { 
            // expected datatypes (note: as indicated by "i f")
            int i;
            float f;
            
            // fetch the first data element as int
            msg.getInt(0) => i => Std.mtof => s.freq;
            // fetch the second data element as float
            msg.getFloat(1) => f => s.gain;
            
            // print
            <<< "notes (via OSC):", i, f >>>;
        }
    }
}

// handler for incoming OSC harmonics messages
fun void onHarmonics()
{
    // create our OSC receiver
    OscIn oin;
    // create our OSC message
    OscMsg msg;
    // use port 6449 (or whatever)
    6449 => oin.port;
    // create an address in the receiver, expect an int
    oin.addAddress( "/foo/harmonics, i" );
    
    // infinite event loop
    while( true )
    {
        // wait for event to arrive
        oin => now;
        
        // grab the next message from the queue. 
        while( oin.recv(msg) )
        { 
            // expected datatypes (note: as indicated by "i")
            int i;
            
            // fetch the first data element as int
            msg.getInt(0) => i => s.harmonics;
            
            // print
            <<< "harmonics (via OSC):", i >>>;
        }
    }
}