aboutsummaryrefslogtreecommitdiffstats
path: root/BowelyzerConfig.sc
blob: 4af229df29324c9324abdfff29dea7a73827a340 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
BowelyzerConfig{

  var <config,
  <defaultConfig,
  <defaultControls,
  <changed = false,
  <hasToBeString,
  <hasToBeInteger,
  <hasToBeArray,
  <hasToBeDictionary,
  <hasToBeFloat;


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

  init{
    arg configFile;
    hasToBeString = [
      \forwardAddress,
      \hubAddress,
      \synthServerAddress
    ];
    hasToBeInteger = [
      \forwardPort,
      \hubPort,
      \synthServerPort,
      \pitchMedian,
      \pitchDownSample
    ];
    hasToBeArray = [
      \names,
      \channelOffset
    ];
    hasToBeDictionary = [
      \controls,
      \left,
      \right
    ];
    hasToBeFloat = [
      \amplitudeAttackTime,
      \amplitudeReleaseTime,
      \hfHainsworth,
      \hfFoote,
      \hfThreshold,
      \hfWaitTime,
      \pitchInitFreq,
      \pitchMinFreq,
      \pitchMaxFreq,
      \pitchExecFreq,
      \pitchMaxBinsPerOctave,
      \pitchAmpThreshold,
      \pitchPeakThreshold,
      \sendReplyFreq
    ];
    config = this.createDefaultConfig;
    if(configFile.notNil,{
      this.readConfigurationFile(configFile)
    },{
      ("No configuration file provided. Using default.").postln;
    });
    this.showConfig;
  }

// create the default configuration
  createDefaultConfig{
    defaultControls = Dictionary.with(*[
      "amplitudeAttackTime" -> 0.01,
      "amplitudeReleaseTime" -> 0.1,
      "hfHainsworth" -> 1.0,
      "hfFoote" -> 0.0,
      "hfThreshold" -> 0.3,
      "hfWaitTime" -> 0.04,
      "pitchInitFreq" -> 440,
      "pitchMinFreq" -> 60,
      "pitchMaxFreq" -> 4000,
      "pitchExecFreq" -> 100,
      "pitchMaxBinsPerOctave" -> 16,
      "pitchMedian" -> 1,
      "pitchAmpThreshold" -> 0.01,
      "pitchPeakThreshold" -> 0.5,
      "pitchDownSample" -> 1,
      "sendReplyFreq" ->20
    ]);
    defaultConfig = Dictionary.with(*[
      "channelOffset" -> [0, 0],
      "names" -> ["left", "right"],
      "synthServerAddress" -> "127.0.0.1",
      "synthServerPort"->57110,
      "hubAddress" -> "127.0.0.1",
      "hubPort" -> 57120,
      "forwardAddress" -> "127.0.0.1",
      "forwardPort" -> 9999,
      "controls" -> Dictionary.with(*[
        "left" -> defaultControls,
        "right" -> defaultControls
      ])
    ]);
    ^defaultConfig;
  }

// read configuration from file
  readConfigurationFile{
    arg configFile;
    var configFromFile, reader, path;
    path = PathName.new(configFile.asString);
    if(path.isFile,{
      postln("Reading configuration file: "++configFile);
      try{
        configFromFile = (configFile.asString).parseYAMLFile;
        configFromFile.keysValuesDo({|key, value|
          if(config.includesKey(key),{
            config.put(key,value);
          });
        });
        this.validateConfig;
      }{
        ("Failed parsing the file: "++configFile).error;
        ("Make sure its JSON syntax is valid!").error;
      };
    }, {
      error("File not readable:"++configFile.asString);
    });
  }

  //check common mistakes in configuration and try fixing them
  validateConfig{
    var broken = false;
    //check for correct types
    config.keysValuesDo({|key, value|
      if(key.isKindOf(String).not,{
        error("Key is no string: "++key);
        broken = true;
      });
      // check if the strings are associated with the correct keys
      if(hasToBeString.includes(key.asSymbol) && value.isKindOf(String).not, {
        config.put(key, value.asString);
      });
      // check if the integers are associated with the correct keys
      if(hasToBeInteger.includes(key.asSymbol) && value.isKindOf(Integer).not, {
        config.put(key, value.asInteger);
      });
      // check if the floats are associated with the correct keys
      if(hasToBeFloat.includes(key.asSymbol) && value.isKindOf(Float).not, {
        config.put(key, value.asFloat);
      });
      // check if the dictionaries are associated with the correct keys
      if(hasToBeDictionary.includes(key.asSymbol) && value.isKindOf(Dictionary).not, {
        error("Value ("++value++") of key ("++key++") should be of type Dictionary.");
        broken = true;
      });
      //check if arrays are associated with the correct keys
      if(hasToBeArray.includes(key.asSymbol),{
        if(value.isArray.not,{
          error("Value ("++value++") of key ("++key++") should be Array.");
          broken = true;
        },{
          //setting correct types in Arrays
          if((key == "channelOffset") && (value.size >= 1) && value[0].isKindOf(Integer).not,{
            for(0, value.size-1, {|i|
              config.at(key)[i] = value[i].asInteger;
            });
          });
          if((key == "names") && (value.size >= 1) && value[0].isKindOf(String).not,{
            for(0, value.size-1, {|i|
              config.at(key)[i] = value[i].asString;
            });
          });
        });
      });
      // check if controls dictionaries are setup correctly
      if(key == "controls",{
        value.keysValuesDo({|name, controls|
          if(hasToBeDictionary.includes(name.asSymbol) && controls.isKindOf(Dictionary).not, {
            error("Value ("++controls++") of key ("++name++") should be of type Dictionary.");
            broken = true;
          },{
            config.at("controls").at(name).keysValuesDo({|controlKey, controlValue|
              // check if the integers are associated with the correct keys
              if(hasToBeInteger.includes(controlKey.asSymbol) && controlValue.isKindOf(Integer).not, {
                config.at(key).at(name).put(controlKey, controlValue.asInteger);
              });
              // check if the floats are associated with the correct keys
              if(hasToBeFloat.includes(controlKey.asSymbol) && controlValue.isKindOf(Float).not, {
                config.at(key).at(name).put(controlKey, controlValue.asFloat);
              });
            });
          });
        });
      });
    });
    // if not completely broken, fix stuff
    if(broken,{
      error("There were errors. Reverting to default config.");
      config = this.createDefaultConfig;
    },{
      postln("Fixing stuff");
      // zero-pad channel offsets
      if(config.at("names").size > config.at("channelOffset").size,{
        var difference = config.at("names").size - config.at("channelOffset").size;
        config.at("channelOffset").asArray = config.at("channelOffset")++Array.fill(difference, {arg i; i*0});
      });
      //TODO: add defaultControls for channels that have none
      //TODO: fill non-existing keys with default values (in case config file is incomplete)
    });
  }

  //print the current config
  showConfig{
    postln("Configuration is:");
    config.keysValuesDo{|key, value|
      if(key == "controls",{
        postln(key++" ("++key.class++") ->");
        value.keysValuesDo{|key, value|
          postln(  key++" ("++key.class++"):");
          value.keysValuesDo{|key, value|
            postln("    "++key++" ("++key.class++") -> "++value++" ("++value.class++")");
          }
        }
      },{
        postln(key++" ("++key.class++") -> "++value++" ("++value.class++")");
      });
    };
  }


}