aboutsummaryrefslogtreecommitdiffstats
path: root/Bowelyzer.sc
blob: 30715886a16ad356672adeb91598b615441ba23c (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Bowelyzer{

  var <gui, <server, <analyzer, <config, <hub;

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

  init{
    //initialize with configuration, if available (else use default)
    arg configFile;
    config = BowelyzerConfig.new(configFile);
    server = Server.new(
      \bowelyzer,
      BowelyzerOSCHub.getNetAddr(
        config.config.at(\synthServerAddress),
        config.config.at(\synthServerPort)
      )
    );
    Server.default = server;
    server.waitForBoot({
      hub = BowelyzerOSCHub.new(config.config);
      analyzer = BowelyzerAnalyzer.new(config.config);
      gui = BowelyzerGUI.new(config.config);
      this.addGUIListeners;
   },
   5,
   {"scsynth failed to start!".postln});
  }

  addGUIListeners{
    OSCdef.newMatching(
      key: \controls,
      func: {|msg, time, addr, recvPort|
        postln("Received: "++msg);
        // if the control exists, change it
        if(msg[1].notNil && msg[2].notNil && msg[3].notNil,{
          if(config.config.at(\controls).includesKey(msg[1]),{
            if(config.config.at(\controls).at(msg[1]).includesKey(msg[2]),{
              config.config.at(\controls).at(msg[1]).put(msg[2], config.getControlValue(msg[2], msg[3]));
            });
          });
        });
      },
      path: "/controls",
      srcID: NetAddr.new("127.0.0.1", NetAddr.langPort)
    );
    OSCdef.newMatching(
      key: \inputs,
      func: {|msg, time, addr, recvPort|
        postln("Received: "++msg);
        if(msg[1].notNil && msg[2].notNil && msg[3].notNil,{
          if(config.config.at(\inputs).includesKey(msg[1]) && config.config.at(\controls).includesKey(msg[1]),{
            switch(msg[2],
              \name,{
                //if the name exists and the new name is not empty, change it
                if(msg[3]!= "",{
                  //move the controls
                  config.config.at(\controls).put(msg[3].asSymbol, config.config.at(\controls).at(msg[1]));
                  config.config.at(\controls).removeAt(msg[1].asSymbol);
                  // rename the input
                  config.config.at(\inputs).put(msg[3].asSymbol, config.config.at(\inputs).at(msg[1]));
                  config.config.at(\inputs).removeAt(msg[1]);
                  // rename the GUI element in channelView
                  Routine{
                    gui.channels.keysValuesDo({|name, channelView|
                      postln(name++"->"++channelView.name);
                      if(channelView.name.asSymbol == msg[1].asSymbol, {
                        channelView.name = msg[3].asSymbol;
                      });
                    });
                    //TODO: rename the LevelListener
                  }.play(AppClock);
                  // free the synth on the server and start a new one according to the config
                  analyzer.freeAnalysisSynth(msg[1].asSymbol);
                  analyzer.addSynthWithName(msg[3]);
                  Routine{
                    1.wait;
                    analyzer.startSynthWithNameAndControls(msg[3].asSymbol, config.config.at(\controls).at(msg[3]), config.config.at(\inputs).at(msg[3]));
                  }.play;
                  hub.freeSynthListener(msg[1]);
                  hub.addSynthListener(msg[3]);
                  hub.startSynthListener(msg[3]);
                });
              },
              \input,{
                // if the input name exists and the new is an Integer and greater/equal 0
                if(((msg[3].isInteger) && (msg[3].asInteger >= 0)),{
                  // change the input in configuration
                  config.config.at(\inputs).put(msg[1], msg[3].asInteger);
                  analyzer.setSynthControl(msg[1].asSymbol, \inputChannel, msg[3].asInteger);
                });
              }
            );
          });
        });
      },
      path: "/inputs",
      srcID: NetAddr.new("127.0.0.1", NetAddr.langPort)
    );
    OSCdef.newMatching(
      key: \toggle,
      func: {|msg, time, addr, recvPort|
        postln("Received: "++msg);
        if(msg[1].notNil && msg[2].notNil && msg[3].notNil,{
          if(config.config.at(\inputs).includesKey(msg[1]),{
            switch(msg[3],
              0,{analyzer.startAnalysisSynth(msg[1])},
              1,{analyzer.stopAnalysisSynth(msg[1])}
            );
          });
        });
      },
      path: "/toggle",
      srcID: NetAddr.new("127.0.0.1", NetAddr.langPort)
    );
  }
  //TODO: add SimpleController
  //TODO: delegate changes to BowelyzerConfig.config and update analyzer and GUI with it
}