summaryrefslogtreecommitdiff
path: root/RHMixxx1280x800/MixxController.js
blob: 0a26df2ca6caab6c220911d2008eede8404f6498 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function MixxxController(id) {
  this.midiMappings = new Object();
  this.namedMappings = new Object();
  this.controls = new Object();
  this.id = id;

  /*
  * Convenience method that returns a Mixxx value for the given control
  */
  this.getMixxxValue = function(control) {
    return engine.getValue(control["group"]["name"], control["name"]);
  }

  /*
  * Sets a Mixxx value for the given control. If a coversion has been 
  * registered, it is taken into account. This method also takes care
  * of softTakeover, if enabled.
  */
  this.setMixxxValue = function(control, value) {
    var conversion = control["conversion"];
    if (conversion) {
      if (conversion["name"] != "none") {
        value = script[conversion["name"]](value, conversion["min"], conversion["max"], conversion["mid"]);
      }
    }
    else {
      value = script["absoluteNonLin"](value, 0, 1, 4);
    }
    if (control["softTakeover"] && (Math.abs(this.getMixxxValue(control) - value) < 0.1)) 
    { 
      control["softTakeover"] = false;
    }
    else if (!control["softTakeover"]) { 
      engine.setValue(control["group"]["name"], control["name"], value);
      control["value"] = value;
    }
  }

  /*
  * Convenience method that returns a control based on its midi number.
  */
  this.getByMidiNo = function (midiNo)
  {
    return this.midiMappings[midiNo];
  }

  /*
  * Convenience method that returns a control based on its group and control.
  */
  this.getByNames = function(group, control)
  {
    return this.namedMappings[group+control];
  }

  /*
  * Creates a multi button that supports klicking and turning.
  * Parameters are:
  * # Mixxx value that is set if button is klicked (may be empty if button cannot be pressed)
  * # Midi number
  * # Mixxx value that is set if button is turned left
  * # Mixxx value that is set if button is turned right
  */
  this.createMultiButton = function(name, id, left, right)
  {
    var button = this.createControl(name, id);
    button.setConversion("none");
    button["isMultiButton"] = true;
    button["left"] = left;
    button["right"] = right;
    return button;
  }

  /*
  * Creates a button that executed a Mixxx function without having a concrete
  * state (e.g. the beatsync button).
  * Parameters are:
  * # Mixxx value that is set if button is klicked
  * # Midi number
  */
  this.createButton = function(name, id)
  {
    var button = this.createControl(name, id);
    button.setConversion("none");
    button["isButton"] = true;
    return button;
  }

  /*
  * Creates a switch that can be switches on and off (e.g. the play button).
  * Parameters are:
  * # Mixxx value that is toggled if switch is pressed
  * # Midi number
  */
  this.createSwitch = function(name, id)
  {
    var button = this.createControl(name, id);
    button.setConversion("none");
    button["activated"] = false;
    button["isSwitch"] = true;
    return button;
  }

  /*
  * Creates a control that can be used for volume sliders, filter knobs, etc.
  * Parameters are:
  * # Mixxx value that is changed if the control is used
  * # Midi number
  * Various behaviours can be enabled on a control:
  * # enableFireOnKeyUp() (control is fired if released, not if pressed. Useful for buttons/switches)
  * # enableSoftTakeover() (custom softTakeover function that is some smarter than Mixxx's default implementation)
  * # addCallback() (add a MixxxController callback. Currently available is only "kill" to emulate a kill switch for equalizers)
  * # addExternalCallback() (add a callback to a custom function specific for a certain controller)
  * # setConversion(name, min, max, mid) (supported: absoluteLin, absoluteNonLin)
  * headVolume, Master volume, crossfader, headMix and volume are automatically enhanced with a fitting conversion.
  */
  this.createControl = function(name, id)
  {
    var control = new Object();
    control["name"] = name;
    control["midiNo"] = id;
    control["ledMidiNo"] = id;
    control["callbacks"] = new Object();
    control["fireOnKeyUp"] = false;
    this.midiMappings[id] = control;
    control.enableFireOnKeyUp = function() {
      control["fireOnKeyUp"] = true;
      return control;
    }
    control.enableSoftTakeover = function() {
      control["softTakeover"] = true;
      return control;
    }
    control.addCallback = function(name) {
      control["callbacks"][name] = name;
      return control;
    }
    control.addCallback = function(name) {
      control["callbacks"][name] = name;
      return control;
    }
    control.addExternalCallback = function(name) {
      control["externalCallbacks"][name] = name;
      return control;
    }
    control.setConversion = function(name, min, max, mid) {
      control["conversion"] = new Object();
      control["conversion"]["name"] = name;
      control["conversion"]["min"] = min;
      control["conversion"]["max"] = max;
      control["conversion"]["mid"] = mid;
      return control;
    }
    control.setLEDMidiNo = function(id) {
      control["ledMidiNo"] = id;
      return control;
    }
    return control;
  }
  
  /*
  * Create a new control group to which multiple controls can be added. A control must have a name that matches
  * the Mixxx group ([Master], [Channel1], ...) 
  */
  this.createGroup = function(name, id)
  {
    var group = new Object();
    group["name"] = name;
    if (name.indexOf("[Channel") == 0) {
      group["id"] = name.substring(8, 9);
    }
    group["controller"] = this;
    group["scratching"] = false;
    group["scratchingEnabled"] = false;
    group.addControl = function(control) {
      group[control["name"]] = control;
      control["group"] = this;
      group["controller"].namedMappings[group["name"]+control["name"]] = control;
  
      if (control["softTakeover"]) {
        control["value"] = group["controller"].getMixxxValue(control);
        engine.connectControl(group["name"], control["name"], this["controller"].id+".softTakeover");
      }
      if (control["name"] == "play") {
        engine.connectControl(group["name"], control["name"], this["controller"].id+".playListener");
      }
      for (var callback in control["callbacks"]) {
        engine.connectControl(group["name"], control["name"], this["controller"].id+"."+callback);
      }
      for (var callback in control["externalCallbacks"]) {
        engine.connectControl(group["name"], control["name"], callback);
      }

      var name = control["name"];
      if (name == "headVolume") {
        control.setConversion("absoluteNonLin", 0, 5, 1);
      } else if ((group["name"] == "[Master]") && (name == "volume")) {
        control.setConversion("absoluteNonLin", 0, 5, 1);
      } else if (name == "crossfader") {
        control.setConversion("absoluteLin", -1, 1);
      } else if (name == "headMix") {
        control.setConversion("absoluteLin", -1, 1);
      } else if (name == "volume") {
        control.setConversion("absoluteLin", 0, 1);
      }

      return group;
    }
    this[name] = group;
    return group;
  }

  /*
  * Enabled softTakeover on a control if the controllers value differs to much from Mixxx's state.
  * This can be the case on startup of mix or if a control has been modified inside of Mixxx, 
  * if beatsync has been used, ...
  */
  this.softTakeover = function(value, group, control) {
    var controller = this["controller"];
    var control = controller.getByNames(group, control);
    if (Math.abs(control["value"] - value) > 0.1) { 
      control["softTakeover"] = true;
    }
  }
  
  /*
  * If a controller has no eq kill switch, this method can be used to kill frequencies
  * if a knob has been moved to zero. To enable this feature, use .addCallback("kill") 
  * on the created control before adding it to its group.
  */
  this.kill = function(value, group, control) {
    var controller = this["controller"];
    var control = controller.getByNames(group, control);
    if (controller.getMixxxValue(control) == 0) {
      engine.setValue(control["group"]["name"], control["name"]+"Kill", 1);
    } else if (engine.getValue(control["group"]["name"], control["name"]+"Kill") == 1) {
      engine.setValue(control["group"]["name"], control["name"]+"Kill", 0);
    }
  }
  
  /*
  * This callback is added to the play state and toggles of the play buttons led if needed.
  */  
  this.playListener = function (value, group, control) {
    var controller = this["controller"];
    var control = controller.getByNames(group, control);
    controller.led(controller.getMixxxValue(control), control["group"]["name"], control["name"]);
    control["activated"] = (controller.getMixxxValue(control) == 1);
  }
  
  /*
  * Switch a led for a control on or off.
  */
  this.led = function (value, group, control) {
    var led = this.getByNames(group, control)["ledMidiNo"];
    if (value == 1) {
      midi.sendShortMsg(0x90, led, 0x7F);
    } else if (value == 0) {
      midi.sendShortMsg(0x90, led, 0x0);
    }
  }
  
  /*
  * This method must be called from a concrete controller script to get the cow flying.
  */
  this.dispatch = function (c, midi, value, status) {
    var control = this.getByMidiNo(midi);
    if (control["isButton"]) {
      this.handleButton(control, value);
    } else if (control["isSwitch"]) {
      this.handleSwitch(control, value);
    } else if (control["isMultiButton"]) {
      this.handleMultiButton(control, value);
    } else if (control["name"] == "jogWheel") {
      this.wheelTurn(c, midi, value, status);
    } else {
      this.setMixxxValue(control, value);
    }
  }

  /*
  * This method handles multiButtons.
  */
  this.handleMultiButton = function (control, value) {
    if (value == 127) {
      engine.setValue(control["group"]["name"], control["name"], 1);
    } else if (value == 0x3f) {
      engine.setValue(control["group"]["name"], control["left"], 1);
    } else {
      engine.setValue(control["group"]["name"], control["right"], 1);
    }
  }

  /*
  * This method handles buttons.
  */
  this.handleButton = function (control, value) {
    if (value == 0) {
      this.led(0, control["group"]["name"], control["name"]);
    } else {
      this.led(1, control["group"]["name"], control["name"]);
    }
    if ((control["fireOnKeyUp"] && (value == 0)) || (!control["fireOnKeyUp"] && (value == 127))) {  
      this.setMixxxValue(control, 1);
    } else {
      this.setMixxxValue(control, 0);
    }
  }
  
  /*
  * This method handles switches.
  */
  this.handleSwitch = function (control, value) {
    if ((control["fireOnKeyUp"] && (value == 0)) || (!control["fireOnKeyUp"] && (value == 127))) {
      if (control["name"] == "scratch") {
        var group = control["group"];
        group["scratchingEnabled"] = !group["scratchingEnabled"];
        if (group["scratchingEnabled"]) {
          this.led(1, control["group"]["name"], control["name"]); 
        }  else {
          this.led(0, control["group"]["name"], control["name"]); 
        }
      } else if (control["activated"]) {
        this.setMixxxValue(control, 0);
        this.led(0, control["group"]["name"], control["name"]);
      } else {
        this.setMixxxValue(control, 1);
        this.led(1, control["group"]["name"], control["name"]);
      }
      control["activated"] = !control["activated"];
    }
  }

  /*
  * This method handles a jog wheel for scratching.
  */
  this.wheelTurn = function (channel, control, value, status) {
    var control = Mixage.controls.getByMidiNo(control);
    var group = control["group"];
    // See if we're scratching. If not, just return.
    if (!group["scratchingEnabled"]) return;
  
    if (!group["scratching"]) {
      var alpha = 1.0/8;
      var beta = alpha/32;
      engine.scratchEnable(group["id"], 128, 100, alpha, beta);
    }
  
    // Register the movement
    var ramp = (value - 64); 
    engine.scratchTick(group["id"], ramp);
    group["scratching"] = true;
    // This two timers check if you are still scratching and disable
    // the function if not. 
    engine.beginTimer(20, function() {
          group["scratching"] = false;
          engine.beginTimer(20, function() {
            if (!group["scratching"]) {
              engine.scratchDisable(group["id"]);
            } 
          }, true);
    }, true);
  }
}