~mixxxdevelopers/mixxx/engine-control-refactor

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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/****************************************************************/
/*      Reloop Digital Jockey 2 controller script v 1.0         */
/*          Copyright (C) 2010, Tobias Rafreider  		*/
/*      but feel free to tweak this to your heart's content!    */
/*      For Mixxx version 1.8.x                                 */
/****************************************************************/

function DigitalJockey2Controller() {}

DigitalJockey2Controller.ledOn = 0x7F;
DigitalJockey2Controller.ledOff = 0x00;
DigitalJockey2Controller.keyPressed = 0x7F;
DigitalJockey2Controller.keyUp = 0x00;

DigitalJockey2Controller.scratchModeChannel1 = false;
DigitalJockey2Controller.scratchModeChannel2 = false;

DigitalJockey2Controller.searchModeChannel1 = false;
DigitalJockey2Controller.searchModeChannel2 = false;

//boolean value that indicated if CUP LED is active
DigitalJockey2Controller.CUP_Button1_IsActive = false;
DigitalJockey2Controller.CUP_Button2_IsActive = false;

DigitalJockey2Controller.init = function(id){
    //print ("Initalizing Reloop Digital Jockey 2 Controler Edition.");
	DigitalJockey2Controller.resetLEDs();

	engine.connectControl("[Channel1]","play","DigitalJockey2Controller.isChannel1_Playing");
	engine.connectControl("[Channel2]","play","DigitalJockey2Controller.isChannel2_Playing");
	
	engine.connectControl("[Channel1]","cue_default","DigitalJockey2Controller.isChannel1_Cue_Active");
	engine.connectControl("[Channel2]","cue_default","DigitalJockey2Controller.isChannel2_Cue_Active");
	
	engine.connectControl("[Channel1]","filterHighKill","DigitalJockey2Controller.OnFilterHigh_KillButton1");
	engine.connectControl("[Channel1]","filterLowKill","DigitalJockey2Controller.OnFilterLow_KillButton1");
	engine.connectControl("[Channel1]","filterMidKill","DigitalJockey2Controller.OnFilterMid_KillButton1");
	
	engine.connectControl("[Channel2]","filterHighKill","DigitalJockey2Controller.OnFilterHigh_KillButton2");
	engine.connectControl("[Channel2]","filterLowKill","DigitalJockey2Controller.OnFilterLow_KillButton2");
	engine.connectControl("[Channel2]","filterMidKill","DigitalJockey2Controller.OnFilterMid_KillButton2");
	
	engine.connectControl("[Channel1]","pfl","DigitalJockey2Controller.OnPFL_Button1");
	engine.connectControl("[Channel2]","pfl","DigitalJockey2Controller.OnPFL_Button2");
	
	//Looping
	engine.connectControl("[Channel1]","loop_enabled","DigitalJockey2Controller.LoopActiveLED1");
	engine.connectControl("[Channel2]","loop_enabled","DigitalJockey2Controller.LoopActiveLED2");
	//Key Lock
	engine.connectControl("[Channel1]","keylock","DigitalJockey2Controller.OnKeyLockChange1");
	engine.connectControl("[Channel2]","keylock","DigitalJockey2Controller.OnKeyLockChange2");
	//Flanger
	engine.connectControl("[Channel1]","flanger","DigitalJockey2Controller.OnFlangerChange1");
	engine.connectControl("[Channel2]","flanger","DigitalJockey2Controller.OnFlangerChange2");
}
DigitalJockey2Controller.resetLEDs = function(){

	//Turn all LEDS off 
	midi.sendShortMsg(0x90, 0x19, DigitalJockey2Controller.ledOff);   // Turn on the Play LED1 off
	midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOff); //Turn CUP LED1 off
	midi.sendShortMsg(0x90, 0x18, DigitalJockey2Controller.ledOff); //Turn CUE LED1 off
	midi.sendShortMsg(0x90, 0x05, DigitalJockey2Controller.ledOff); //Turn PFL LED off
	midi.sendShortMsg(0x90, 0x14, DigitalJockey2Controller.ledOff); //HighFilterKill
	midi.sendShortMsg(0x90, 0x15, DigitalJockey2Controller.ledOff); //MidFilterKill
	midi.sendShortMsg(0x90, 0x16, DigitalJockey2Controller.ledOff); //LowFilterKill
	midi.sendShortMsg(0x90, 0x1B, DigitalJockey2Controller.ledOff); //disable scratch control
	midi.sendShortMsg(0x90, 0x1A, DigitalJockey2Controller.ledOff); //disable search control
	midi.sendShortMsg(0x90, 0x1C, DigitalJockey2Controller.ledOff); //disable fx dry/wet control
	midi.sendShortMsg(0x90, 0x02, DigitalJockey2Controller.ledOff); //Turn KeyLock off
	midi.sendShortMsg(0x90, 0x12, DigitalJockey2Controller.ledOff); //Loop1 LED Off
	midi.sendShortMsg(0x90, 0x0e, DigitalJockey2Controller.ledOff); //Flanger
	midi.sendShortMsg(0x90, 0x1A, DigitalJockey2Controller.ledOff); //Search
	midi.sendShortMsg(0x90, 0x1b, DigitalJockey2Controller.ledOff);//Scratch
	
	
	midi.sendShortMsg(0x90, 0x55, DigitalJockey2Controller.ledOff);   // Turn on the Play LED2 off
	midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOff); //Turn CUP LED2 off
	midi.sendShortMsg(0x90, 0x54, DigitalJockey2Controller.ledOff); //Turn CUE LED2 off
	midi.sendShortMsg(0x90, 0x41, DigitalJockey2Controller.ledOff); //Turn PFL LED off
	midi.sendShortMsg(0x90, 0x50, DigitalJockey2Controller.ledOff); //HighFilterKill
	midi.sendShortMsg(0x90, 0x51, DigitalJockey2Controller.ledOff); //MidFilterKill
	midi.sendShortMsg(0x90, 0x52, DigitalJockey2Controller.ledOff); //LowFilterKill
	midi.sendShortMsg(0x90, 0x57, DigitalJockey2Controller.ledOff); //disable scratch control
	midi.sendShortMsg(0x90, 0x56, DigitalJockey2Controller.ledOff); //disable search control
	midi.sendShortMsg(0x90, 0x58, DigitalJockey2Controller.ledOff); //disable fx dry/wet control
	midi.sendShortMsg(0x90, 0x3E, DigitalJockey2Controller.ledOff); //Turn KeyLock2 off
	midi.sendShortMsg(0x90, 0x4E, DigitalJockey2Controller.ledOff); //Loop2 LED Off
	midi.sendShortMsg(0x90, 0x45, DigitalJockey2Controller.ledOff); //Flanger
	midi.sendShortMsg(0x90, 0x56, DigitalJockey2Controller.ledOff); //Search
	midi.sendShortMsg(0x90, 0x57, DigitalJockey2Controller.ledOff);//Scratch
}
DigitalJockey2Controller.shutdown = function(id){
 //Turn all LEDs off by using init function
 DigitalJockey2Controller.resetLEDs();
}

// Play button deck 1
DigitalJockey2Controller.playButton1 = function (channel, control, value) {
	DigitalJockey2Controller.playTrack(1, control, value);	
}
// Play Button deck 2
DigitalJockey2Controller.playButton2 = function (channel, control, value) {
	DigitalJockey2Controller.playTrack(2, control, value);
}

DigitalJockey2Controller.playTrack = function (channel, control, value) {
	/*
	 * We immediately want to start and stop playing as soon as play button has been pressed
	 * KeyUp events are out of interest in this case
	 */
	if(value == DigitalJockey2Controller.keyPressed){
		//If no song is loaded
		if (engine.getValue("[Channel"+channel+"]", "duration") == 0) { 
				return; 
		};
		var currentlyPlaying = engine.getValue("[Channel"+channel+"]","play");
		if (currentlyPlaying == 1) {    // If currently playing
			engine.setValue("[Channel"+channel+"]","play",0);    // Stop
			if(channel == 1) {
				midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOff); //Turn CUP LED off
				DigitalJockey2Controller.CUP_Button1_IsActive = false;
			}
			else if(channel == 2){
				midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOff); //Turn CUP LED off
				DigitalJockey2Controller.CUP_Button2_IsActive = false;
			}
		}
		else {    // If not currently playing,
			engine.setValue("[Channel"+channel+"]","play",1);    // Start
		}
	}
}

DigitalJockey2Controller.CueButton1 = function (channel, control, value) {
	DigitalJockey2Controller.Cue(1, control, value);	
}
DigitalJockey2Controller.CueButton2 = function (channel, control, value) {
	DigitalJockey2Controller.Cue(2, control, value);	
}
DigitalJockey2Controller.Cue = function (channel, control, value) {
	//If no song is loaded
	if (engine.getValue("[Channel"+channel+"]", "duration") == 0) { 
			return; 
	};
	// As soon as we press CUE, execute CUE Logic
	if(value == DigitalJockey2Controller.keyPressed){
		engine.setValue("[Channel"+channel+"]","cue_default",1);
		if(channel == 1) {
			midi.sendShortMsg(0x90, 0x19, DigitalJockey2Controller.ledOff);   // Turn on the Play LED off
			midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOff); //Turn CUP LED off
			DigitalJockey2Controller.CUP_Button1_IsActive = false;
		}
		else if(channel == 2){
			midi.sendShortMsg(0x90, 0x55, DigitalJockey2Controller.ledOff);   // Turn on the Play LED off
			midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOff); //Turn CUP LED off
			DigitalJockey2Controller.CUP_Button2_IsActive = false;
		}
	
	}
	else if(value == DigitalJockey2Controller.keyUp){
		engine.setValue("[Channel"+channel+"]","cue_default",0);
	}
	
}
DigitalJockey2Controller.CuePlayButton1 = function (channel, control, value) {
	DigitalJockey2Controller.CuePlay(1, control, value);	
}
DigitalJockey2Controller.CuePlayButton2 = function (channel, control, value) {
	DigitalJockey2Controller.CuePlay(2, control, value);	
}

DigitalJockey2Controller.CuePlayButton1 = function (channel, control, value) {
	DigitalJockey2Controller.CuePlay(1, control, value);	
}
DigitalJockey2Controller.CuePlayButton2 = function (channel, control, value) {
	DigitalJockey2Controller.CuePlay(2, control, value);	
}
DigitalJockey2Controller.CuePlay = function (channel, control, value) {
	// As soon as we press CUP, execute CUP Logic
	if(value == DigitalJockey2Controller.keyPressed){
		//If no song is loaded
		if (engine.getValue("[Channel"+channel+"]", "duration") == 0) { 
				return; 
		};
		var currentlyPlaying = engine.getValue("[Channel"+channel+"]","play");
		//print ("isPlaying" + currentlyPlaying);
		if(currentlyPlaying == 1){
			if(channel == 1) {
				midi.sendShortMsg(0x90, 0x19, DigitalJockey2Controller.ledOff);   // Turn on the Play LED off
			}
			else if(channel == 2){
				midi.sendShortMsg(0x90, 0x55, DigitalJockey2Controller.ledOff);   // Turn on the Play LED off
			}
		}

		engine.setValue("[Channel"+channel+"]","cue_default",1);
		engine.setValue("[Channel"+channel+"]","cue_default",0);
		engine.setValue("[Channel"+channel+"]","play",1);

		if(channel == 1) {
			DigitalJockey2Controller.CUP_Button1_IsActive = true;
		}
		else if(channel == 2){
			DigitalJockey2Controller.CUP_Button2_IsActive = true;
		}
		
	}
}
DigitalJockey2Controller.EnableHeadPhone1 = function (channel, control, value) {
	DigitalJockey2Controller.EnableHeadPhone(1, control, value);
}
DigitalJockey2Controller.EnableHeadPhone2 = function (channel, control, value) {
	DigitalJockey2Controller.EnableHeadPhone(2, control, value);
}
DigitalJockey2Controller.EnableHeadPhone = function (channel, control, value) {
	var isHeadPhoneActive = engine.getValue("[Channel"+channel+"]","pfl");
	//print("Channel"+channel+" isHeadPhoneActive: " + isHeadPhoneActive);
	if(value == DigitalJockey2Controller.keyPressed){
		if(isHeadPhoneActive == 1){
			engine.setValue("[Channel"+channel+"]","pfl",0);
		}
		else{
			engine.setValue("[Channel"+channel+"]","pfl",1);
		}
	}
}

DigitalJockey2Controller.BassKillChannel1 = function (channel, control, value){
	var deck = 1;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterLowKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterLowKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterLowKill",1);
		}
	}
}
DigitalJockey2Controller.MidKillChannel1 = function (channel, control, value){
	var deck = 1;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterMidKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterMidKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterMidKill",1);
		}
	}
}

DigitalJockey2Controller.HighKillChannel1 = function (channel, control, value){
	var deck = 1;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterHighKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterHighKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterHighKill",1);
		}
	}
}
DigitalJockey2Controller.BassKillChannel2 = function (channel, control, value){
	var deck = 2;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterLowKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterLowKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterLowKill",1);
		}
	}
}
DigitalJockey2Controller.MidKillChannel2 = function (channel, control, value){
	var deck = 2;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterMidKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterMidKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterMidKill",1);
		}
	}
}
DigitalJockey2Controller.HighKillChannel2 = function (channel, control, value){
	var deck = 2;
	if(value == DigitalJockey2Controller.keyPressed){
		var isKillButtonIsActive = engine.getValue("[Channel"+deck+"]","filterHighKill");
		if(isKillButtonIsActive == true){
			engine.setValue("[Channel"+deck+"]","filterHighKill",0);
		}
		else{
			engine.setValue("[Channel"+deck+"]","filterHighKill",1);
		}
	}
}
DigitalJockey2Controller.Scratch1 = function (channel, control, value){
	DigitalJockey2Controller.Scratch(1, control, value);
}
DigitalJockey2Controller.Scratch2 = function (channel, control, value){
	DigitalJockey2Controller.Scratch(2, control, value);
}
DigitalJockey2Controller.Scratch = function (channel, control, value){
	if(value == DigitalJockey2Controller.keyPressed){
		if(channel == 1){
		    //print("Scratch 1: " + DigitalJockey2Controller.scratchModeChannel1);
			if(DigitalJockey2Controller.scratchModeChannel1 == true){
				DigitalJockey2Controller.scratchModeChannel1 = false;
				midi.sendShortMsg(0x90, control, DigitalJockey2Controller.ledOff);
			}
			else{
				DigitalJockey2Controller.scratchModeChannel1 = true;
				midi.sendShortMsg(0x90, control, DigitalJockey2Controller.ledOn);
			}
		}
		if(channel == 2){
		    //print("Scratch 2: " + DigitalJockey2Controller.scratchModeChannel2);
			if(DigitalJockey2Controller.scratchModeChannel2 == true){
				DigitalJockey2Controller.scratchModeChannel2 = false;
				midi.sendShortMsg(0x90, control, DigitalJockey2Controller.ledOff);
			}
			else{
				DigitalJockey2Controller.scratchModeChannel2 = true;
				midi.sendShortMsg(0x90, control, DigitalJockey2Controller.ledOn);		
			}
		}
	}
}

DigitalJockey2Controller.Search1 = function (channel, control, value, status, group){
    if(value == DigitalJockey2Controller.keyPressed){
	    if(DigitalJockey2Controller.searchModeChannel1 == false){
	        DigitalJockey2Controller.searchModeChannel1 = true;
		    midi.sendShortMsg(0x90, 0x1A, DigitalJockey2Controller.ledOn); //Turn LED on
	    }
	    else{
	        DigitalJockey2Controller.searchModeChannel1 = false;
		    midi.sendShortMsg(0x90, 0x1A, DigitalJockey2Controller.ledOff); //Turn LED off
	    }
	    //print("Search 1: " + DigitalJockey2Controller.searchModeChannel1);
	}
}
DigitalJockey2Controller.Search2 = function (channel, control, value, status, group){
    if(value == DigitalJockey2Controller.keyPressed){
	    if(DigitalJockey2Controller.searchModeChannel2 == false){
	        DigitalJockey2Controller.searchModeChannel2 = true;
		    midi.sendShortMsg(0x90, 0x56, DigitalJockey2Controller.ledOn); //Turn LED on
	    }
	    else{
	        DigitalJockey2Controller.searchModeChannel2 = false;
		    midi.sendShortMsg(0x90, 0x56, DigitalJockey2Controller.ledOff); //Turn LED off
	    }
	    //print("Search 2: " + DigitalJockey2Controller.searchModeChannel2);
	}
}

DigitalJockey2Controller.JogWheelHelper = function (channel, control, value){
	//print("control: " + control);
	//print("value: " + value);
	if (control == 29) {
		if (DigitalJockey2Controller.scratchModeChannel1 == true && DigitalJockey2Controller.searchModeChannel1 == true) {
			var currentlyPlaying = engine.getValue("[Channel1]","play");
			if (currentlyPlaying) {
				if (value == 0) {
					if (engine.getValue("[Channel1]", "rate_temp_up_small"))
					    engine.setValue("[Channel1]", "rate_temp_up_small", 0);
					if (engine.getValue("[Channel1]", "rate_temp_down_small"))
					    engine.setValue("[Channel1]", "rate_temp_down_small", 0);
				}
			}
			else {
				if (value == 127)
					engine.scratchEnable(1, 128, 33+1/3, 1.0/8, (1.0/8)/32);
				else if (value == 0)
					engine.scratchDisable(1);
			}
		}
		else if (DigitalJockey2Controller.scratchModeChannel1 == true && DigitalJockey2Controller.searchModeChannel1 == false) {
			if (value == 127)
				engine.scratchEnable(1, 128, 33+1/3, 1.0/8, (1.0/8)/32);
			else if (value == 0)
				engine.scratchDisable(1);
		}
		else if (DigitalJockey2Controller.scratchModeChannel1 == false && DigitalJockey2Controller.searchModeChannel1 == true) {
			if (value == 0) {
				if (engine.getValue("[Channel1]", "fwd"))
				    engine.setValue("[Channel1]", "fwd", 0);
				if (engine.getValue("[Channel1]", "back"))
				    engine.setValue("[Channel1]", "back", 0);
			}
		}
	}
	else if (control == 89) {
		if (DigitalJockey2Controller.scratchModeChannel2 == true && DigitalJockey2Controller.searchModeChannel2 == true) {
			var currentlyPlaying = engine.getValue("[Channel2]","play");
			if (currentlyPlaying) {
				if (value == 0) {
					if (engine.getValue("[Channel2]", "rate_temp_up_small"))
					    engine.setValue("[Channel2]", "rate_temp_up_small", 0);
					if (engine.getValue("[Channel2]", "rate_temp_down_small"))
					    engine.setValue("[Channel2]", "rate_temp_down_small", 0);
				}
			}
			else {
				if (value == 127)
					engine.scratchEnable(2, 128, 33+1/3, 1.0/8, (1.0/8)/32);
				else if (value == 0)
					engine.scratchDisable(2);
			}
		}
		else if (DigitalJockey2Controller.scratchModeChannel2 == true && DigitalJockey2Controller.searchModeChannel2 == false) {
			if (value == 127)
				engine.scratchEnable(2, 128, 33+1/3, 1.0/8, (1.0/8)/32);
			else if (value == 0)
				engine.scratchDisable(2);
		}
		else if (DigitalJockey2Controller.scratchModeChannel2 == false && DigitalJockey2Controller.searchModeChannel2 == true) {
			if (value == 0) {
				if (engine.getValue("[Channel2]", "fwd"))
				    engine.setValue("[Channel2]", "fwd", 0);
				if (engine.getValue("[Channel2]", "back"))
				    engine.setValue("[Channel2]", "back", 0);
			}
		}
	}
}

DigitalJockey2Controller.JogWheel1 = function (channel, control, value){
	DigitalJockey2Controller.JogWheel(1, control, value);
}

DigitalJockey2Controller.JogWheel2 = function (channel, control, value){
	DigitalJockey2Controller.JogWheel(2, control, value);
}

DigitalJockey2Controller.PitchBendSensitivity = 0.00001;
DigitalJockey2Controller.SearchSensitivity = 0.01;

DigitalJockey2Controller.JogWheel = function (channel, control, value){
	/*
	 * The JogWheels of the controler work as follows.
	 * Spinning around in reverse order produces decimal values of 63 or lower
	 * depending on the the speed you drag the wheel.
	 * 
	 * Spinning around in a forward manner produces values of 65 or higher.
	 */
	var jogValue = (value - 64); //DigitalJockey2Controller.WheelSensitivity;
	
	//Functionality of Jog Wheel if we're in scratch mode 
	if(channel == 1){
	    if (DigitalJockey2Controller.scratchModeChannel1 == true && DigitalJockey2Controller.searchModeChannel1 == true) {
	        var currentlyPlaying = engine.getValue("[Channel1]","play");
	        if (currentlyPlaying) {
		    var rtu = engine.getValue("[Channel1]", "rate_temp_up_small");
		    var rtd = engine.getValue("[Channel1]", "rate_temp_down_small");
	            if (jogValue > 0) {
	                if (rtd)
			    engine.setValue("[Channel1]", "rate_temp_down_small", 0);
			if (rtu != 1)
			    engine.setValue("[Channel1]", "rate_temp_up_small", 1);
	                
	            }
	            else if (jogValue < 0) {
	                if (rtu)
			    engine.setValue("[Channel1]", "rate_temp_up_small", 0);
	                if (rtd != 1)
			    engine.setValue("[Channel1]", "rate_temp_down_small", 1);
	            }
	            else
	                return;
	        }
	        else {
	            engine.scratchTick(1,jogValue);
	        }
	    }
	    else if (DigitalJockey2Controller.scratchModeChannel1 == true && DigitalJockey2Controller.searchModeChannel1 == false) {
		    engine.scratchTick(1,jogValue);
	    }
	    else if (DigitalJockey2Controller.scratchModeChannel1 == false && DigitalJockey2Controller.searchModeChannel1 == true) {
		var currentlyPlaying = engine.getValue("[Channel1]","play");
	        if (currentlyPlaying) {
		    if (jogValue == 0)
			return;
		    var fwd = engine.getValue("[Channel1]", "fwd");
		    var back = engine.getValue("[Channel1]", "back");
		    if (jogValue > 0) {
			if (back)
			    engine.setValue("[Channel1]", "back", 0);
			if (fwd != 1)
			    engine.setValue("[Channel1]", "fwd", 1);
		    }
		    else if (jogValue < 0) {
			if (fwd)
			    engine.setValue("[Channel1]", "fwd", 0);
			if (back != 1)
			    engine.setValue("[Channel1]", "back", 1);
		    }
		}
		else {
		    var playpos = engine.getValue("[Channel1]", "playposition");
		    if (jogValue > 0) {
		        if (playpos < 1 - DigitalJockey2Controller.SearchSensitivity)
		            playpos += DigitalJockey2Controller.SearchSensitivity;
		        else
		            playpos = 1;
		    }
		    else if (jogValue < 0) {
			if (playpos > DigitalJockey2Controller.SearchSensitivity)
			    playpos -= DigitalJockey2Controller.SearchSensitivity;
			else
			    playpos = 0;
		    }
		    engine.setValue("[Channel1]", "playposition", playpos);
		}
	    }		
	}
	if(channel == 2){
	    if (DigitalJockey2Controller.scratchModeChannel2 == true && DigitalJockey2Controller.searchModeChannel2 == true) {
	        var currentlyPlaying = engine.getValue("[Channel2]","play");
	        if (currentlyPlaying) {
	            var rtu = engine.getValue("[Channel2]", "rate_temp_up_small");
		    var rtd = engine.getValue("[Channel2]", "rate_temp_down_small");
	            if (jogValue > 0) {
	                if (rtd)
			    engine.setValue("[Channel2]", "rate_temp_down_small", 0);
			if (rtu != 1)
			    engine.setValue("[Channel2]", "rate_temp_up_small", 1);
	                
	            }
	            else if (jogValue < 0) {
	                if (rtu)
			    engine.setValue("[Channel2]", "rate_temp_up_small", 0);
	                if (rtd != 1)
			    engine.setValue("[Channel2]", "rate_temp_down_small", 1);
	            }
	            else
	                return;
	        }
	        else {
	            engine.scratchTick(2,jogValue);
	        }
	    }
	    else if (DigitalJockey2Controller.scratchModeChannel2 == true && DigitalJockey2Controller.searchModeChannel2 == false) {
		    engine.scratchTick(2,jogValue);
	    }
	    else if (DigitalJockey2Controller.scratchModeChannel2 == false && DigitalJockey2Controller.searchModeChannel2 == true) {
		var currentlyPlaying = engine.getValue("[Channel2]","play");
	        if (currentlyPlaying) {
		    if (jogValue == 0)
			return;
		    var fwd = engine.getValue("[Channel2]", "fwd");
		    var back = engine.getValue("[Channel2]", "back");
		    if (jogValue > 0) {
			if (back)
			    engine.setValue("[Channel2]", "back", 0);
			if (fwd != 1)
			    engine.setValue("[Channel2]", "fwd", 1);
		    }
		    else if (jogValue < 0) {
			if (fwd)
			    engine.setValue("[Channel2]", "fwd", 0);
			if (back != 1)
			    engine.setValue("[Channel2]", "back", 1);
		    }
		}
		else {
		    var playpos = engine.getValue("[Channel2]", "playposition");
		    if (jogValue > 0) {
		        if (playpos < 1 - DigitalJockey2Controller.SearchSensitivity)
		            playpos += DigitalJockey2Controller.SearchSensitivity;
		        else
		            playpos = 1;
		    }
		    else if (jogValue < 0) {
			if (playpos > DigitalJockey2Controller.SearchSensitivity)
			    playpos -= DigitalJockey2Controller.SearchSensitivity;
			else
			    playpos = 0;
		    }
		    engine.setValue("[Channel2]", "playposition", playpos);
		}
	    }		
	}
}

DigitalJockey2Controller.JogWheel1_Hold = function (channel, control, value){
	
}

DigitalJockey2Controller.JogWheel2_Hold = function (channel, control, value){
	
}
/*****************************************************
 * Put functions here to handle controlobjets functions
 ******************************************************/
DigitalJockey2Controller.isChannel1_Playing = function (value){
		if(value == 0){
			midi.sendShortMsg(0x90, 0x19, DigitalJockey2Controller.ledOff);   // Turn on the Play LED1 off
			//midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOff); //Turn CUP LED1 off
			midi.sendShortMsg(0x90, 0x18, DigitalJockey2Controller.ledOff); //Turn CUE LED1 off
		}
		else{ //if deck is playing but not in CUE modus
			if(DigitalJockey2Controller.CUP_Button1_IsActive == false){
				midi.sendShortMsg(0x90, 0x19, DigitalJockey2Controller.ledOn);   // Turn on the Play LED1 on
			}
		}	
}
DigitalJockey2Controller.isChannel2_Playing = function (value){
		if(value == 0){
			midi.sendShortMsg(0x90, 0x55, DigitalJockey2Controller.ledOff);   // Turn on the Play LED2 off
			//midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOff);  //Turn CUP LED2 off
			midi.sendShortMsg(0x90, 0x54, DigitalJockey2Controller.ledOff); //Turn CUE LED2 off
		}
		else{
			if(DigitalJockey2Controller.CUP_Button2_IsActive == false)
				midi.sendShortMsg(0x90, 0x55, DigitalJockey2Controller.ledOn);   // Turn on the Play LED2 on
		}	
}
DigitalJockey2Controller.isChannel1_Cue_Active = function (value){
	if(value == 0){
		if(DigitalJockey2Controller.CUP_Button1_IsActive == true)
			midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOn); //Turn CUP LED1 on
		midi.sendShortMsg(0x90, 0x18, DigitalJockey2Controller.ledOff); //Turn CUE LED1 off
	}
	else{
		//if CUP LED is active leave, we can switch off CUE Botton
		if(DigitalJockey2Controller.CUP_Button1_IsActive == true){
			midi.sendShortMsg(0x90, 0x18, DigitalJockey2Controller.ledOff); //Turn CUE LED1 off
			midi.sendShortMsg(0x90, 0x17, DigitalJockey2Controller.ledOn); // Turn CUP LED1 on
		}
		else
			midi.sendShortMsg(0x90, 0x18, DigitalJockey2Controller.ledOn); //Turn CUE LED1 on
		
	}
}
DigitalJockey2Controller.isChannel2_Cue_Active = function (value){
	
	if(value == 0){
		if(DigitalJockey2Controller.CUP_Button2_IsActive == true)
			midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOn);  //Turn CUP LED2 on
		midi.sendShortMsg(0x90, 0x54, DigitalJockey2Controller.ledOff); //Turn CUE LED2 off
	}
	else{
		//if CUP LED is active leave, we can switch off CUE Botton
		if(DigitalJockey2Controller.CUP_Button2_IsActive == true){
			midi.sendShortMsg(0x90, 0x54, DigitalJockey2Controller.ledOff); //Turn CUE LED2 off
			midi.sendShortMsg(0x90, 0x53, DigitalJockey2Controller.ledOn);  //Turn CUP LED2 on
		}
		else
			midi.sendShortMsg(0x90, 0x54, DigitalJockey2Controller.ledOn); //Turn CUE LED2 on
		
	}
}
DigitalJockey2Controller.SelectNextTrack_or_prevTrack = function (channel, control, value, status){
	if(value == 65)
		engine.setValue("[Playlist]","SelectNextTrack",1);
	else
		engine.setValue("[Playlist]","SelectPrevTrack",1);
	
}
/*
 * Toggles LED status light on/off if you press kill buttons through Mixxx
 */
DigitalJockey2Controller.OnFilterHigh_KillButton2 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x50,DigitalJockey2Controller.ledOn); //HighFilterKill
	else
		midi.sendShortMsg(0x90, 0x50,DigitalJockey2Controller.ledOff); //HighFilterKill
}
DigitalJockey2Controller.OnFilterMid_KillButton2 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x51, DigitalJockey2Controller.ledOn); //MidFilterKill
	else
		midi.sendShortMsg(0x90, 0x51, DigitalJockey2Controller.ledOff); //MidFilterKill
}
DigitalJockey2Controller.OnFilterLow_KillButton2 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x52, DigitalJockey2Controller.ledOn); //LowFilterKill
	else
		midi.sendShortMsg(0x90, 0x52, DigitalJockey2Controller.ledOff); //LowFilterKill
}

DigitalJockey2Controller.OnFilterHigh_KillButton1 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x14, DigitalJockey2Controller.ledOn); //HighFilterKill
	else
		midi.sendShortMsg(0x90, 0x14, DigitalJockey2Controller.ledOff); //HighFilterKill
}
DigitalJockey2Controller.OnFilterMid_KillButton1 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x15, DigitalJockey2Controller.ledOn); //HighFilterKill
	else
		midi.sendShortMsg(0x90, 0x15, DigitalJockey2Controller.ledOff); //HighFilterKill
}
DigitalJockey2Controller.OnFilterLow_KillButton1 = function (value){
	if(value == 1)
		midi.sendShortMsg(0x90, 0x16, DigitalJockey2Controller.ledOn); //HighFilterKill
	else
		midi.sendShortMsg(0x90, 0x16, DigitalJockey2Controller.ledOff); //HighFilterKill
}
DigitalJockey2Controller.OnPFL_Button1 = function (value){
	if(value == 1){
			midi.sendShortMsg(0x90, 0x05, DigitalJockey2Controller.ledOn); //Turn LED off
		}
		else{
			midi.sendShortMsg(0x90, 0x05, DigitalJockey2Controller.ledOff); //Turn LED off
		}
}
DigitalJockey2Controller.OnPFL_Button2 = function (value){
	if(value == 1){
			midi.sendShortMsg(0x90, 0x41, DigitalJockey2Controller.ledOn); //Turn LED off
		}
		else{
			midi.sendShortMsg(0x90, 0x41, DigitalJockey2Controller.ledOff); //Turn LED off
		}
}
DigitalJockey2Controller.LoopIn = function (channel, control, value, status, group) {
	if(value == DigitalJockey2Controller.keyPressed){
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOn); //Turn LED on
		engine.setValue(group,"loop_in",1);
	}
	else{
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOff); //Turn LED off
	}
}
DigitalJockey2Controller.LoopOut = function (channel, control, value, status, group) {
	if(value == DigitalJockey2Controller.keyPressed){
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOn); //Turn LED on
		engine.setValue(group,"loop_out",1);
	}
	else{
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOff); //Turn LED off
	}
}
DigitalJockey2Controller.ReloopExit = function (channel, control, value, status, group){
	//if loop is active, we exit the loop
	if(engine.getValue(group,"loop_enabled")){
		engine.setValue(group,"reloop_exit",1);
	}
	else{
		engine.setValue(group,"reloop_exit",0);
	}
	var actBeatLoop = engine.getValue(group, "beatloop");
	print("ReloopExit.actBeatLoop=" + actBeatLoop);
	if (actBeatLoop != "0") {
		print("Set BeatLoop To Zero");
		engine.setValue(group,"beatloop","0");
	}
}
DigitalJockey2Controller.BeatLoop = function (channel, control, value, status, group) {
	if(value == DigitalJockey2Controller.keyPressed){
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOn); //Turn LED on
		engine.setValue(group,"beatloop","4");
	}
	else{
		midi.sendShortMsg(status, control, DigitalJockey2Controller.ledOff); //Turn LED off
	}
}
DigitalJockey2Controller.LoopActiveLED1 = function (value){
	//if loop is active, we exit the loop
	if(value == 1){
		midi.sendShortMsg(0x90, 0x12, DigitalJockey2Controller.ledOn); //Turn LED on
	}
	else{
		midi.sendShortMsg(0x90, 0x12, DigitalJockey2Controller.ledOff); //Turn LED off
	}
	
}
DigitalJockey2Controller.LoopActiveLED2 = function (value){
	//if loop is active, we exit the loop
	if(value == 1){
		midi.sendShortMsg(0x90, 0x4E, DigitalJockey2Controller.ledOn); //Turn LED on
	}
	else{
		midi.sendShortMsg(0x90, 0x4E, DigitalJockey2Controller.ledOff); //Turn LED off
	}
	
}

DigitalJockey2Controller.Flanger1 = function (channel, control, value, status, group){
    if(value == DigitalJockey2Controller.keyPressed){
        var flanger = engine.getValue("[Channel1]", "flanger");
	    if(flanger){
	        engine.setValue("[Channel1]", "flanger", 0);
	    }
	    else{
	        engine.setValue("[Channel1]", "flanger", 1);
	    }
	}
}
DigitalJockey2Controller.Flanger2 = function (channel, control, value, status, group){
    if(value == DigitalJockey2Controller.keyPressed){
        var flanger = engine.getValue("[Channel2]", "flanger");
	    if(flanger){
	        engine.setValue("[Channel2]", "flanger", 0);
	    }
	    else{
	        engine.setValue("[Channel2]", "flanger", 1);
	    }
	}
}

DigitalJockey2Controller.LoadSelectedTrack = function (channel, control, value) {
	/* added by Jones */
	if (control == 19) {
		if(engine.getValue("[Channel1]", "play") == 0)
			engine.setValue("[Channel1]", "LoadSelectedTrack", 1);
		else
			print("no load to deck1!\n");
	}
	else if (control == 79) {
		if(engine.getValue("[Channel2]", "play") == 0)
			engine.setValue("[Channel2]", "LoadSelectedTrack", 1);
		else
			print("no load to deck2!\n");	
	}
}

DigitalJockey2Controller.PitchControl = function (channel, control, value) {
	var pitch_value = -1.0 + 2.0 * ((value * 8 + control / 16) / 1024.0);
	//print("pitch_value: " + pitch_value);
	if (channel == 0) {
		engine.setValue("[Channel1]","rate", pitch_value);
	}
	else if (channel == 1) {
		engine.setValue("[Channel2]","rate", pitch_value);
	}
}

DigitalJockey2Controller.KeyLock1 = function (channel, control, value) {
	DigitalJockey2Controller.KeyLock(1, control, value);	
}
DigitalJockey2Controller.KeyLock2 = function (channel, control, value) {
	DigitalJockey2Controller.KeyLock(2, control, value);	
}
DigitalJockey2Controller.KeyLock = function (channel, control, value) {
	var isKeyLock = engine.getValue("[Channel"+channel+"]","keylock");
	if(value == DigitalJockey2Controller.keyPressed){
		if(isKeyLock == 1){
			engine.setValue("[Channel"+channel+"]","keylock",0);
		}
		else{
			engine.setValue("[Channel"+channel+"]","keylock",1);
		}
	}
}

DigitalJockey2Controller.OnKeyLockChange1 = function(value){
	if(value == 1){
		midi.sendShortMsg(0x90, 0x02, DigitalJockey2Controller.ledOn); //Turn Keylock on
	}
	else{
		midi.sendShortMsg(0x90, 0x02, DigitalJockey2Controller.ledOff); //Turn KeyLock off
	}
}
DigitalJockey2Controller.OnKeyLockChange2 = function(value){
	if(value == 1){
		midi.sendShortMsg(0x90, 0x3E, DigitalJockey2Controller.ledOn); //Turn KeyLock on
	}
	else{
		midi.sendShortMsg(0x90, 0x3E, DigitalJockey2Controller.ledOff); //Turn KeyLock off
	}
}

DigitalJockey2Controller.OnFlangerChange1 = function(value){
	if(value == 1){
		midi.sendShortMsg(0x90, 0x0e, DigitalJockey2Controller.ledOn); //Turn LED on
	}
	else{
		midi.sendShortMsg(0x90, 0x0e, DigitalJockey2Controller.ledOff); //Turn LED off
	}
}
DigitalJockey2Controller.OnFlangerChange2 = function(value){
	if(value == 1){
		midi.sendShortMsg(0x90, 0x45, DigitalJockey2Controller.ledOn); //Turn LED on
	}
	else{
		midi.sendShortMsg(0x90, 0x45, DigitalJockey2Controller.ledOff); //Turn LED off
	}
}

DigitalJockey2Controller.LoopPlusMinusChannel1 = function (channel, control, value, status, group) {
	DigitalJockey2Controller.LoopPlusMinus(channel, control, value, status, group);	
}
DigitalJockey2Controller.LoopPlusMinusChannel2 = function (channel, control, value, status, group) {
	DigitalJockey2Controller.LoopPlusMinus(channel, control, value, status, group);	
}

DigitalJockey2Controller.beatloops = ["0.125", "0.25", "0.5", "1", "2", "4", "8", "16"];

DigitalJockey2Controller.getPrevBeatLoop = function (beatloop) {
	var i = 0;
	for (i = 0; i < DigitalJockey2Controller.beatloops.length; i++) {
		if (DigitalJockey2Controller.beatloops[i] == beatloop) {
			if (i > 0)
				return DigitalJockey2Controller.beatloops[i - 1];
			else
				return DigitalJockey2Controller.beatloops[0];
		}
	}
	return "0";
}

DigitalJockey2Controller.getNextBeatLoop = function (beatloop) {
	var i = 0;
	for (i = 0; i < DigitalJockey2Controller.beatloops.length; i++) {
		if (DigitalJockey2Controller.beatloops[i] == beatloop) {
			if (i < DigitalJockey2Controller.beatloops.length - 1)
				return DigitalJockey2Controller.beatloops[i + 1];
			else
				return DigitalJockey2Controller.beatloops[DigitalJockey2Controller.beatloops.length - 1];
		}
	}
	return "0";
}

DigitalJockey2Controller.LoopPlusMinus = function (channel, control, value, status, group) {
	var actBeatLoop = engine.getValue(group, "beatloop");
	print("LoopPlusMinus.actBeatLoop=" + actBeatLoop);
	if (actBeatLoop != "0") {
		if (value == 0x3F) {
			var prevBeatLoop = DigitalJockey2Controller.getPrevBeatLoop(actBeatLoop);
			print("LoopPlusMinus.prevBeatLoop=" + prevBeatLoop);
			engine.setValue(group,"beatloop",prevBeatLoop);
		}
		else if (value == 0x41) {
			var nextBeatLoop = DigitalJockey2Controller.getNextBeatLoop(actBeatLoop);
			print("LoopPlusMinus.nextBeatLoop=" + nextBeatLoop );
			engine.setValue(group,"beatloop",nextBeatLoop);
		}
	}
}