summaryrefslogtreecommitdiffstats
path: root/res/controllers/Behringer-CMDStudio4a-scripts.js
blob: 211bf30491e63d00619df95159707a058df9df35 (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
// ****************************************************************************
// * Mixxx mapping script file for the Behringer CMD Studio 4a.
// * Author: Craig Easton
// * Version 1.4 (Jan 2016)
// * Forum: http://www.mixxx.org/forums/viewtopic.php?f=7&t=7868
// * Wiki: http://www.mixxx.org/wiki/doku.php/behringer_cmd_studio_4a
// ****************************************************************************

////////////////////////////////////////////////////////////////////////
// JSHint configuration                                               //
////////////////////////////////////////////////////////////////////////
/* global engine                                                      */
/* global script                                                      */
/* global print                                                       */
/* global midi                                                        */
////////////////////////////////////////////////////////////////////////

// Master function definition.
function BehringerCMDStudio4a() {}


// ***************************** Global Vars **********************************

// Shift/mode state variables.
BehringerCMDStudio4a.delButtonState = [false,false,false,false];
BehringerCMDStudio4a.scratchButtonState = [false,false,false,false];

// Button push/release state variables.
BehringerCMDStudio4a.pitchPushed = [[false,false,false,false], [false,false,false,false]];
BehringerCMDStudio4a.delPushed = false;
BehringerCMDStudio4a.delShiftUsed = false;
BehringerCMDStudio4a.fxAssignPushed = false;
BehringerCMDStudio4a.fxAssignShiftUsed = false;
BehringerCMDStudio4a.fxAssignLastGroup = "";

// ************************ Initialisation stuff. *****************************

BehringerCMDStudio4a.vuMeterUpdate = function (value, group, control){
    value = (value*15)+48;
    switch(control) {
    case "VuMeterL":
        midi.sendShortMsg(0xB0, 0x7E, value);
        break;
    case "VuMeterR":
        midi.sendShortMsg(0xB0, 0x7F, value);
        break;
    }
}

BehringerCMDStudio4a.initLEDs = function () {
    // (re)Initialise any LEDs that are direcctly controlled by this script.
    // DEL buttons (one for each virtual deck).
    midi.sendShortMsg(0x90, 0x2A, 0x00);
    midi.sendShortMsg(0x91, 0x4A, 0x00);
    midi.sendShortMsg(0x92, 0x2A, 0x00);
    midi.sendShortMsg(0x93, 0x4A, 0x00);
    // Scratch buttons (one for each virtual deck).
    midi.sendShortMsg(0x90, 0x16, 0x00);
    midi.sendShortMsg(0x91, 0x36, 0x00);
    midi.sendShortMsg(0x92, 0x16, 0x00);
    midi.sendShortMsg(0x93, 0x36, 0x00);
}

BehringerCMDStudio4a.init = function () {
    // Initialise anything that might not be in the correct state.
    BehringerCMDStudio4a.initLEDs();
    // Connect the VUMeters
    engine.connectControl("[Master]","VuMeterL","BehringerCMDStudio4a.vuMeterUpdate");
    engine.connectControl("[Master]","VuMeterR","BehringerCMDStudio4a.vuMeterUpdate");
}

BehringerCMDStudio4a.shutdown = function() {
    // Leave the deck in a properly initialised state.
    BehringerCMDStudio4a.initLEDs();

    // Disconnect the VUMeters.
// Maybe not! It seems you don't have to do this even though the connection
// in done in init(), in fact if you try it throws an error.
//  engine.connectControl("[Master]","VuMeterL","BehringerCMDStudio4a.vuMeterUpdate",true);
//  engine.connectControl("[Master]","VuMeterR","BehringerCMDStudio4a.vuMeterUpdate",true);
}


// *************************** Control Stuff. *********************************
// The code below is primarily "shift/mode" key functionality as there is no
// native support for this in Mixxx at the moment.
// I suspect that the vast majority of controller mappings could be completed
// with little or no scripting if Mixxx supported shift/mode buttons in the
// XML (together with standard wheel/scratching functionality).

// Function to deal with the DEL "shift/mode" buttons.
BehringerCMDStudio4a.del = function (channel, control, value, status, group) {
    if (value == 127) {
        // Button pushed.
        BehringerCMDStudio4a.delPushed = true;
        BehringerCMDStudio4a.delShiftUsed = false;
    } else {
        // Button released.
        BehringerCMDStudio4a.delPushed = false;
        // Only toggle the DEL-mode if the "shift" function wasn't used.
        if (!BehringerCMDStudio4a.delShiftUsed) {
            BehringerCMDStudio4a.delButtonState[channel] = !BehringerCMDStudio4a.delButtonState[channel];
            midi.sendShortMsg(0x90 + channel, control, BehringerCMDStudio4a.delButtonState[channel] ? 0x01 : 0x00);
        }
    }
}

// Function to deal with the play buttons, (because they have a DEL-mode behaviour).
BehringerCMDStudio4a.play = function (channel, control, value, status, group) {
    if (BehringerCMDStudio4a.delButtonState[channel]) {
        // DEL-mode is active, do reverse-roll (slip).
        engine.setValue(group, "reverseroll", (value == 127) ? 1 : 0);
    } else {
        // DEL-mode is not active, just toggle play (on push only).
        if (value == 127) {
            script.toggleControl(group,"play");
        }
    }
}

// Function to deal with the cue buttons, (because they have a DEL-mode behaviour).
BehringerCMDStudio4a.cue = function (channel, control, value, status, group) {
    if (BehringerCMDStudio4a.delButtonState[channel]) {
        // DEL-mode is active, do reverse play.
        engine.setValue(group, "reverse", (value == 127) ? 1 : 0);
    } else {
        // DEL-mode is not active so just cue.
        engine.setValue(group, "cue_default", (value == 127) ? 1 : 0);
    }
}

// Function to deal with the scratch mode buttons.
BehringerCMDStudio4a.scratch = function (channel, control, value, status, group) {
    BehringerCMDStudio4a.scratchButtonState[channel] = !BehringerCMDStudio4a.scratchButtonState[channel];
    midi.sendShortMsg(status, control, BehringerCMDStudio4a.scratchButtonState[channel] ? 0x01 : 0x00);
}

// Function to deal with the FX Assign buttons, (because they also act as "shift" buttons).
BehringerCMDStudio4a.fxAssign = function (channel, control, value, status, group) {
    // FX Assign buttons start at 0x52.
    var fxAssignButton = (control - 0x52) & 1;  // Either 0 or 1 depending on button (1 or 2).
    if (value == 127) {
        // Button pushed.
        BehringerCMDStudio4a.fxAssignPushed = true;
        BehringerCMDStudio4a.fxAssignShiftUsed = false;
        BehringerCMDStudio4a.fxAssignLastGroup = group;
    }
    else
    {
        // Button released.
        BehringerCMDStudio4a.fxAssignPushed = false;
        // Only toggle the effect on release if the "shift" function wasn't used.
        if (!BehringerCMDStudio4a.fxAssignShiftUsed) {
            script.toggleControl(group,"group_[Channel"+(channel+1)+"]_enable");
        }
    }
}

// Function to deal with the browse left/right buttons, (because they have an "FX Assign mode" behaviour).
BehringerCMDStudio4a.browseLR = function (channel, control, value, status, group) {
    if (BehringerCMDStudio4a.fxAssignPushed) {
        BehringerCMDStudio4a.fxAssignShiftUsed = true;
        if (control == 0x2) {
            // Left.
            engine.setValue(BehringerCMDStudio4a.fxAssignLastGroup,"prev_chain", 1);
        } else {
            // Right.
            engine.setValue(BehringerCMDStudio4a.fxAssignLastGroup,"next_chain", 1);
        }
    } else {
        if (control == 0x2) {
            // Left.
            engine.setValue(group, "SelectPrevPlaylist",1)
        } else {
            // Right.
            engine.setValue(group, "SelectNextPlaylist",1)
        }
    }
}

// Functions to deal with the hot-cue buttons, (because they have a DEL-mode behaviour).
BehringerCMDStudio4a.hotcue = function (channel, control, value, status, group) {
    // Translate the button to the actual hotcue.
    var hotcue = control-0x21;  // Hotcue buttons on left deck go from 0x22 to 0x29
    if (hotcue>8) {
        // Right deck, buttons are 0x20 higher so we need to compensate.
        hotcue = hotcue-0x20;
    }
    if (BehringerCMDStudio4a.delPushed) {
        // DEL button is being held so delete the hotcue.
        engine.setValue(group, "hotcue_"+hotcue+"_clear", 1);
        BehringerCMDStudio4a.delShiftUsed = true;
    } else {
        // DEL button is not being held down.
        if (BehringerCMDStudio4a.delButtonState[channel]) {
            // DEL-mode is active, lets do auto-loops.
            engine.setValue(group, "beatloop_"+(1/8)*Math.pow(2,hotcue-1)+"_toggle", 1);
            if (value == 0) {
                // Button is being released. Disable then re-enable slip if it
                // is active. This "re-syncs" the playback after every
                // auto-loop in slip-mode which is a nice effect and probably
                // what you want most of the time if slip is on.
                if (engine.getValue(group, "slip_enabled") == 1) {
                    engine.setValue(group, "slip_enabled", 0);
                    // It seems we can't just flip a param off and on in the
                    // same call! Since we've just turned slip off, I can't now
                    // turn it on directly here, the only work-around I could
                    // think of was to create a (very short) timed call-back
                    // to turn it off!
                    // Raised bug about this:
                    // https://bugs.launchpad.net/mixxx/+bug/1538200
                    // Changed timer from 50 to 100 after the pathology of this
                    // bug was explined in the bug report.
                    engine.beginTimer(100, function() { engine.setValue(group, "slip_enabled", 1); }, 1);
                }
            }
        } else {
            // DEL-mode is not active so do the set/jump-to hotcue function as normal.
            engine.setValue(group, "hotcue_"+hotcue+"_activate", (value == 127) ? 1 : 0);
        }
    }
}

// Functions to deal with the pitch inc/dec buttons, (because they have a DEL-mode behaviour).
BehringerCMDStudio4a.pitch = function (channel, control, value, status, group) {
    // Work out the direction.
    var direction = ((control & 0x01) == 0) ? "down" : "up";
    // Work out the type (and join) by looking at the DEL button state.
    var type = BehringerCMDStudio4a.delButtonState[channel] ? "pitch" : "rate";
    var join = BehringerCMDStudio4a.delButtonState[channel] ? "" : "_perm";
    // Pushed or released?
    if (value