aboutsummaryrefslogtreecommitdiffstats
path: root/BowelyzerOSCHub.sc
blob: 7de5b7282dbb7950de3db73c7066fb33f3a0b14f (plain)
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
BowelyzerOSCHub{
  var <hub, <forward, <synthServer;

  *new{
    arg config;
    ^super.new.init(config);
  }

  // setup a NetAddr object and return it
  *getNetAddr{
    arg server, port;
    ^NetAddr.new(server, port);
  }

  init{
    arg config;
    this.setupNetAddressesFromConfig(config);
    this.setupSynthListenersFromConfig(config);
  }

  setupNetAddressesFromConfig{
    arg config;
    forward = BowelyzerOSCHub.getNetAddr(config.at(\forwardAddress), config.at(\forwardPort));
    hub = BowelyzerOSCHub.getNetAddr(config.at(\hubAddress), config.at(\hubPort));
    synthServer = BowelyzerOSCHub.getNetAddr(config.at(\synthServerAddress), config.at(\synthServerPort));
  }

  // setup OSCdef for SynthServerAddress:SynthServerPort
  setupSynthListenersFromConfig{
    arg config;
    // listen for individual SendReply messages
    config.at(\inputs).keysValuesDo({|name, input|
      postln("Listening for messages called '/"++name++"' coming from scsynth at "++synthServer.ip++":"++synthServer.port++".");
      this.addSynthListener(name);
    });
  }

  addSynthListener{
    arg name;
    OSCdef.newMatching(
      name,
      {|msg, time, addr, recvPort| this.forwardToNetAddress(msg, time)},
      name,
      synthServer,
      hub.port
    );
  }

  startSynthListener{
    arg name;
    OSCdef(name).enable;
  }

  stopSynthListener{
    arg name;
    OSCdef(name.asSymbol).disable;
  }

  freeSynthListener{
    arg name;
    OSCdef(name.asSymbol).free;
  }

  //forward a received OSC message to the globally specified forward address
  forwardToNetAddress{
    arg msg, time;
    var name = msg[0],
    amplitude = msg[3],
    pitch = msg[4],
    hasPitch = msg[5],
    onsetDetect = msg[7];
    if(forward.isLocal && (forward.port == NetAddr.langPort), {
      postln(msg[0]++" (amplitude: "++amplitude++"; pitch: "++pitch++"; has pitch: "++hasPitch);
    },{
      forward.sendMsg(name,"/amplitude", amplitude, "/pitch", pitch, "/hasPitch", hasPitch, "/detect", onsetDetect);
    });
  }

  //TODO: add functions to modify OSC listener behavior (what data to send from which channel)

}