summaryrefslogtreecommitdiffstats
path: root/res/controllers/DJ-Tech-CDJ-101-scripts.js
blob: 9d09e7a5c063a66657ca824894e370cebdd0d918 (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
/****************************************************************/
/*      DJ-Tech CDJ101 controller script                        */
/*      For Mixxx version 1.11                                  */
/*      Author: zestoi                                          */
/****************************************************************/
//
// pitch slider works as you'd expect
// holding down the push button works as a "shift" to activate secondary functions
// needs the cdj101 to be on midi channel 1 for deck1 and 2 for deck2
//
// track playing:
//
// * surface of jog wheel: scratch
// * edge of jog wheel: tempo bend
// * shift + edge of jog wheel: fine tempo bend
//
// track not playing:
//
// * click push button: load a track
// * surface of jog wheel: scratch thru track
// * shift + surface of jog wheel: scan through track quickly
// * edge of jog wheel: jog though track slowly
// * shift + cue: move beatgrid
//
// track playing or not:
//
// * rotate push button: navigate tracks
// * shift + rotate push button: switch between playlists
// * cue button: default cue behaviour
// * play: toggle play
// * shift + play: sync
//

DJTechCDJ101 = {};
DJTechCDJ101.pushmaxtime = 200;
DJTechCDJ101.pushed = false;
DJTechCDJ101.pushedon = 0;
DJTechCDJ101.outer2inner = false;
DJTechCDJ101.scratch_timeout = 100;

DJTechCDJ101.init = function(id) {
    engine.connectControl("[Channel1]", "play", "DJTechCDJ101.play_feedback_deck1");
    engine.connectControl("[Channel2]", "play", "DJTechCDJ101.play_feedback_deck2");
    engine.connectControl("[Channel1]", "cue_default", "DJTechCDJ101.cue_feedback_deck1");
    engine.connectControl("[Channel2]", "cue_default", "DJTechCDJ101.cue_feedback_deck2");
}

DJTechCDJ101.shutdown = function() {}

//
// utility function
//

DJTechCDJ101.mtime = function()
{
    var t = new Date();
    return t.getTime();
}

//
// cue+play led feedback
//

DJTechCDJ101.play_feedback_deck1 = function(value)
{
    midi.sendShortMsg(0x90, 0x2a, value > 0 ? 0x7f : 0);
}

DJTechCDJ101.play_feedback_deck2 = function(value)
{
    midi.sendShortMsg(0x91, 0x2a, value > 0 ? 0x7f : 0);
}

DJTechCDJ101.cue_feedback_deck1 = function(value)
{
    midi.sendShortMsg(0x90, 0x2b, value > 0 ? 0x7f : 0);
}

DJTechCDJ101.cue_feedback_deck2 = function(value)
{
    midi.sendShortMsg(0x91, 0x2b, value > 0 ? 0x7f : 0);
}

//
// translate 14bit pitchbend message into pitch control
//

DJTechCDJ101.pitch = function(channel, lsb, msb, status, group)
{
    engine.setValue(group, "rate", script.pitch(lsb, msb, status)); // not working for some reason
    //engine.setValue(group, "rate", (8192 - (msb << 7 | lsb)) / 8192);
}

DJTechCDJ101.beatjump = function(group, jump)
{
    jump = jump * 120 / engine.getValue(group, "bpm") / engine.getValue(group, "track_samples") * engine.getValue(group, "track_samplerate");
    engine.setValue(group, "playposition", engine.getValue(group, "playposition") + jump);
}

//
// hold down encoder and turn to change playlists
// turn while not pushed in to scroll through tracks
// click to load track
//

DJTechCDJ101.browse = function(channel, control, value, status, group)
{
    if (DJTechCDJ101.pushed) {
        engine.setValue("[Playlist]", value == 0x41 ? "SelectNextPlaylist" : "SelectPrevPlaylist", 1);
    }
    else {
        engine.setValue("[Playlist]", value == 0x41 ? "SelectNextTrack" : "SelectPrevTrack", 1);
    }
}

DJTechCDJ101.push = function(channel, control, value, status, group)
{
    if (value > 0) {
        DJTechCDJ101.pushed = true;
        DJTechCDJ101.pushedon = DJTechCDJ101.mtime();
    }
    else {

        //
        // load selected track if released quickly enough
        //

        if (DJTechCDJ101.mtime() - DJTechCDJ101.pushedon < DJTechCDJ101.pushmaxtime) {
            engine.setValue(group, "LoadSelectedTrack", 1);
        }

        DJTechCDJ101.pushed = false;
        DJTechCDJ101.pushedon = 0;
    }
}

//
// when deck is not playing and the push button is held, pressing cue will move the beatgrid
//

DJTechCDJ101.cue = function(channel, control, value, status, group)
{
    if (DJTechCDJ101.pushed && !engine.getValue(group, "play")) {
        engine.setValue(group, "beats_translate_curpos", value > 0 ? 1 : 0);
    }
    else {
        engine.setValue(group, "cue_default", value > 0 ? 1 : 0);
    }
}

//
// when the push button is held, pressing play will sync
//

DJTechCDJ101.play = function(channel, control, value, status, group)
{
    if (DJTechCDJ101.pushed) {
        engine.setValue(group, "beatsync", value > 0 ? 1 : 0);
    }
    else if (value > 0) {
        //
        // we want play to toggle
        //

        engine.setValue(group, "play", !engine.getValue(group, "play"));
    }
}

//
// when deck is playing either enable or disable scratch mode, no action otherwise
//

DJTechCDJ101.jogtouch = function(channel, control, value, status, group)
{
    var deck = parseInt(group.substring(8,9));

    if (value > 0) {
        engine.scratchEnable(deck, 143, 45, 0.125, 0.125/32);
    }
    else {
        //
        // don't disable right away in case of spin backs if playing and also morph
        // outer jog movements into top jog movements during this time
        //

        if (!engine.getValue(group, "play")) {
            DJTechCDJ101.finishScratch(deck, false);
        }
        else {
            DJTechCDJ101.outer2inner = true;
            DJTechCDJ101.scratch_timer = engine.beginTimer(DJTechCDJ101.scratch_timeout, 'DJTechCDJ101.finishScratch(' + deck + ', true)');
        }
    }
}

DJTechCDJ101.finishScratch = function(deck, stop_timer)
{
    if (stop_timer) {
        engine.stopTimer(DJTechCDJ101.scratch_timer);
    }
    engine.scratchDisable(deck);
    DJTechCDJ101.outer2inner = false;
}

//
// use outer part of jog to pitchbend when in play mode and fine track seek when not
//

DJTechCDJ101.jogouter = function(channel, control, value, status, group)
{
    //
    // when touch surface has been release for a short time convert
    // outer jog movements into surface jog ones for spinbacks etc
    //

    if (DJTechCDJ101.outer2inner) {
        return DJTechCDJ101.jogtop(channel, control, value, status, group);
    }

    //
    // scale down based on whether we are playing and the shift is held down
    //

    value = (value - 0x40) / 2;
    if (DJTechCDJ101.pushed) value /= 2.5;
    if (!engine.getValue(group, "play")) value /= 2.5;
    engine.setValue(group, "jog", value);
}

//
// track playing: top of jog scratches
// track not playing: seek thru track (using same scratch ticks and gives more positive response) or seek faster through track when push button held down
//

DJTechCDJ101.jogtop = function(channel, control, value, status, group)
{
    value -= 0x40;
    if (!engine.getValue(group, "play") && DJTechCDJ101.pushed) {
        DJTechCDJ101.beatjump(group, value);
    }
    else {
        engine.scratchTick(parseInt(group.substring(8,9)), value);
    }
}