//-----------------------------------------------------------------------------
//-------- Copyright (c)2003-2011. All Rights Reserved. -----------------------
//-----------------------------------------------------------------------------
// file implementing MIDI controller ' Pioneer DDJ SB-2
// Date: 2016 01 06
// Author: 
//----------------------------------------------------------------------------- 
function getLanguageVersion()
{
	return 1;// do not modify this, this should be 1 for all scripts
}
//----------------------------------------------------------------------------- 
function getFileVersion()
{
	return 1;// update this as needed
}
//----------------------------------------------------------------------------- 
// return a user friendly name for the controller supported
function getControllerUserFriendlyName()
{
	return "SLATE Audio";
}
//----------------------------------------------------------------------------- 
// return the communication type that the controller supports 
// this can be either 'midi' or 'hid' (please return only one of them in case the controller supports both)
function getControllerType()
{
	return 'midi';
}
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// preferred way to identify the controller (using the USB device VID and PID -- vendor and product id)
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedHidDeviceVID(osId)
{
	return false;
}
//----------------------------------------------------------------------------- 
function getSupportedHidDevicePID(osId)
{
	return false;
}
//----------------------------------------------------------------------------- 
// this is NOT the preferred way to identify the controller but you should use
// it if the controller does not support standard USB VID and PID identification
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedHidDeviceName(osId)
{
	return false;
}

//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// preferred way to identify the controller (using the USB device VID and PID -- vendor and product id)
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedMidiDeviceVID(osId)
{	
	return 0x08E4;
}
//----------------------------------------------------------------------------- 
function getSupportedMidiDevicePID(osId)
{	
	return 0x0185;	
}
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// preferred way to identify the controller (if it supports MIDI SysEx)
// return false if the controller does not support this kind of identification
function getSupportedMidiSysExIdString(osId)
{
	// sysex response to 'F0 7E 7F 06 01 F7' (identification request sysex)
	// please note that the response contains 5 bytes in hex format containing
	// the Manufacturer ID (3 bytes) and Model Number (2 bytes)
	// ATTN: the returned value should omit the standard 'F0 7E 00 06 02' message start
	//if(osId==1) return false;
	
	return "00207908E4";
	
}
//----------------------------------------------------------------------------- 
// this is NOT the preferred way to identify the controller but you should use
// it if the controller does not support standard MIDI SysEx identification
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedMidiDeviceNameForInput(osId)
{
	return "PIONEER DDJ-WeGO2";// !!! 
	
}
//----------------------------------------------------------------------------- 
function getSupportedMidiDeviceNameForOutput(osId)
{
	return "PIONEER DDJ-WeGO2";
	//return false;
}
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// some variables necessary only for this particular script
var jogWheelsInScratchMode	=[0,0,0,0];
//var jogWheelsWasDeckPlaying	=[0,0,0,0];
//var shiftButton				=[0,0,0,0];
//var deckMode				=[0,0,0,0]; //0=hot cue 1=roll 2=slicer 3=sampler

var samplerBlinkLedStatus	= [0,0,0,0,0,0,0,0];// 2 decks leds, each with 4 samplers // 0 means off; 1 means on; 2 means blink on; 3 means blink off;
var samplerBlinkState 		= [0,0];
var shiftPressed = 0;
var bankFxIsOn 				= [[0x00,0x00,0x00], [0x00,0x00,0x00], [0x00,0x00,0x00], [0x00,0x00,0x00]];
var fxLevel                 = [0x00,0x00,0x00,0x00];

var deckSelect = [0x00,0x00];

//----------------------------------------------------------------------------- 
// this is called when the controller is initialized and the control is passed to this script
// use this to initialize any variables you might need, the controller' leds, etc
// uniqueId is a unique integer between 0..15 that is guaranteed not to change for 
// a certain opened controller -- please note that there can be many controllers
// of the same type in use at the same time ! Use uniqueId to identify between them
function controllerInit(uniqueId)
{
	debug('controllerInit Pioneer WeGO 2 ['+uniqueId+']');// this outputs a string to DebugView app on Win
	//callSendAllNotifications(uniqueId, true);
	sendMidi(uniqueId,0x90,2,0x72,0x00);
	sendMidi(uniqueId,0x90,3,0x72,0x00);
	
	//disable fx
	sendMidi(uniqueId,0x90,4,0x43,0x00);
	sendMidi(uniqueId,0x90,4,0x44,0x00);
	sendMidi(uniqueId,0x90,4,0x45,0x00);
	
	sendMidi(uniqueId,0x90,5,0x43,0x00);
	sendMidi(uniqueId,0x90,5,0x44,0x00);
	sendMidi(uniqueId,0x90,5,0x45,0x00);
	
	sendMidi(uniqueId,0x90,4,0x48,0x00);
	sendMidi(uniqueId,0x90,4,0x49,0x00);
	sendMidi(uniqueId,0x90,4,0x4A,0x00);
	
	sendMidi(uniqueId,0x90,5,0x48,0x00);
	sendMidi(uniqueId,0x90,5,0x49,0x00);
	sendMidi(uniqueId,0x90,5,0x4A,0x00);
	//disable monitor
	
	sendMidi(uniqueId,0x90,0x06,0x54,0x00);
	sendMidi(uniqueId,0x90,0x06,0x55,0x00);
	sendMidi(uniqueId,0x90,0x06,0x56,0x00);
	sendMidi(uniqueId,0x90,0x06,0x57,0x00);
	
}
//----------------------------------------------------------------------------- 
// called just before the controller will be deinitialized and the control will go away from the script
// use to power off any powered leds, etc
function controllerDeinit(uniqueId)
{
	//debug('controllerDeinit PioneerDDJSX ['+uniqueId+']');
	callSendAllNotifications(uniqueId, false);
	//disable deck select
	sendMidi(uniqueId,0x90,2,0x72,0x00);
	sendMidi(uniqueId,0x90,3,0x72,0x00);
	
	//disable fx
	sendMidi(uniqueId,0x90,4,0x43,0x00);
	sendMidi(uniqueId,0x90,4,0x44,0x00);
	sendMidi(uniqueId,0x90,4,0x45,0x00);
	
	sendMidi(uniqueId,0x90,5,0x43,0x00);
	sendMidi(uniqueId,0x90,5,0x44,0x00);
	sendMidi(uniqueId,0x90,5,0x45,0x00);
	
	sendMidi(uniqueId,0x90,4,0x48,0x00);
	sendMidi(uniqueId,0x90,4,0x49,0x00);
	sendMidi(uniqueId,0x90,4,0x4A,0x00);
	
	sendMidi(uniqueId,0x90,5,0x48,0x00);
	sendMidi(uniqueId,0x90,5,0x49,0x00);
	sendMidi(uniqueId,0x90,5,0x4A,0x00);
	//disable monitor
	
	sendMidi(uniqueId,0x90,0x06,0x54,0x00);
	sendMidi(uniqueId,0x90,0x06,0x55,0x00);
	sendMidi(uniqueId,0x90,0x06,0x56,0x00);
	sendMidi(uniqueId,0x90,0x06,0x57,0x00);
	
	
	
	
	//sendMidi(uniqueId,0x90,0x0B,0x09,0x00); //dj app not connected
	
}
//----------------------------------------------------------------------------- 
// this is your main target function to implement the controller -- this is called 
// for each and every MIDI message that comes from the controller itself
// return true if the message was processed by the script and false to let it 
// pass thru the software (eg. so it can be assigned by the user using the LEARN function)
function messageReceived(uniqueId,status,channel,data1,data2)
{
	//debug('messageReceived ['+uniqueId+'] - status:'+status+' channel:'+channel+' data1:'+data1+' data2:'+data2);
	
	var messageNotProcessed=false;
	switch(status)
	{
	//-----------------------------------------------
	// controllers: sliders, knobs, jog-wheels, encoders ('moving' things)
	case 0xB0:
		switch(data1)
		{		
		// global
		case 0x00:
		call('setDeckPitch', channel, 0x7F-data2);
		break;
		case 0x01://done
			call('setCuePgmMix', data2);
			break;
		case 0x02: //deck 1/2 fx 1 jog
			switch(channel){
				case 4:
				call('deckFxFlangerDW', channel-4, data2);
				break;
				case 5:
				call('deckFxFlangerDW', channel-4, data2);
				break;
			}
		break;
		case 0x03://done
			
				
			break;
		case 0x04: //done
			switch(channel){
				case 4:				
				call('deckFxEchoDW', channel-4, data2);
				break;
				case 5:
				call('deckFxEchoDW', channel-4, data2);
				break;
			}
			break;
		case 0x05: //done
			//wetdry fx 2
			
			break;
		case 0x06: //done
			//wetdry fx 3
			switch(channel){
				case 4:
				call('deckFxReverbDW', channel-4, data2);
				break;
				case 5:
				call('deckFxReverbDW', channel-4, data2);
				break;
			}
			
			break;
		case 0x07: //done
		
			call('setDeckEqHigh', 0, data2);
		break;
		case 0x08: //done
		
			call('setDeckEqHigh', 1, data2);
		break;
		case 0x09: //done
			call('setDeckEqHigh', 2, data2);
			break;
		case 0x0A: //done
			switch(channel){
				case 4:
				call('deckFxFlangerDW', channel-2, data2);
				break;
				case 5:
				call('deckFxFlangerDW', channel-2, data2);
				break;
				default:
				call('setDeckEqHigh', 3, data2);	
			}
			
			break;
		case 0x0B: //done
			call('setDeckEqMid', 0, data2);
			break;
		case 0x0C: //done
			switch(channel){
				case 4:
				call('deckFxEchoDW', channel-2, data2);
				break;
				case 5:
				call('deckFxEchoDW', channel-2, data2);
				break;
				default:
				call('setDeckEqMid', 1, data2);
			}
			
			break;
		case 0x0D: //done
			call('setDeckEqMid', 2, data2);
			break;
		case 0x0E: //done	
			switch(channel){
				case 4:
				call('deckFxReverbDW', channel-2, data2);
				break;
				case 5:
				call('deckFxReverbDW', channel-2, data2);
				break;
				default:
				call('setDeckEqMid', 3, data2);
			}
			
			break;
		case 0x0F: //done
			call('setDeckEqLow', 0, data2);
			break;
		case 0x10: //done
			call('setDeckEqLow', 1, data2);
			break;
		case 0x11: //done
			call('setDeckEqLow', 2, data2);
			break;
		case 0x12: //done
			call('setDeckEqLow', 3, data2);
			break;
		case 0x13: //done
			call('setDeckVolume', 0, data2);
			break;
		case 0x14: //done
			
			break;
		case 0x15: //done
			call('setDeckVolume', 1, data2);
			break;
		case 0x16: //done
			
			break;
		case 0x17: //done
			call('setDeckVolume', 2, data2);
			break;
		case 0x18: //done
			
			break;
		case 0x19:
			call('setDeckVolume', 3, data2);
			break;
		case 0x1A:
			
			break;
		case 0x1B:
			
			break;
		case 0x1C:break;
		case 0x1D:break;
		case 0x1E:
			
			break;
		case 0x1F:
			call('setCrossfader', data2);
		break;
		case 0x20:
			
		break;
		case 0x20:break;
		case 0x21:
			var val1 = data2-0x40;
			call('moveDeckJogWheel', channel, val1, 600);	
		break;
		case 0x22:
			var val1 = data2-0x40;
			call('scratchDeckJW', channel, val1, 120);
		break;
		case 0x23:break;
		case 0x24:break;
		case 0x25:break;
		case 0x26:break;
		case 0x27:
		break;
		case 0x28:break;
		case 0x29:break;
		case 0x2A:break;
		case 0x2B:break;
		case 0x2C:break;
		case 0x2D:break;
		case 0x2E:break;
		case 0x2F:break;
		case 0x30:break;
		case 0x31:break;
		case 0x32:break;
		case 0x33:break;
		case 0x34:break;
		case 0x35:break;
		case 0x36:break;
		case 0x37:break;
		case 0x38:break;
		case 0x39:break;
		case 0x3A:break;
		case 0x3B:break;
		case 0x3C:break;
		case 0x3D:break;
		case 0x3E:break;
		case 0x3F:break;
		case 0x40:
		var val1 = 0;
		if(data2 > 0x40){
			val1 = data2 - 0x80;
		} else {
			val1 = data2;
		}
		//(data2>0x40 ? -1 : 1);
					call('lbMoveCursor', val1);
		break;
		case 0x41:break;
		case 0x42:break;
		case 0x43:break;
		case 0x44:break;
		case 0x45:break;
		case 0x46:break;
		case 0x47:break;
		case 0x48:break;
		case 0x49:break;
		case 0x4A:break;
		case 0x4B:break;
		case 0x4C:break;
		case 0x4D:break;
		case 0x4E:break;
		case 0x4F:break;
		case 0x50:break;
		case 0x51:break;
		case 0x52:break;
		case 0x53:break;
		case 0x54:break;
		case 0x55:break;
		case 0x56:break;
		case 0x57:break;
		case 0x58:break;
		case 0x59:break;
		case 0x5A:break;
		case 0x5B:break;
		case 0x5C:break;
		case 0x5D:break;
		case 0x5E:break;
		case 0x5F:break;
		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	// note on/off: buttons, switches ('pressing' things)
	case 0x80:
	case 0x90:	
	//Deck A
	//sendMidi(uniqueId,0x90,0x0C,0x28,0x3F);//deck a first LED
	//sendMidi(uniqueId,0x90,0x0C,0x29,0x3F);//deck a second LED
	//sendMidi(uniqueId,0x90,0x0C,0x2A,0x3F);//deck a third LED
	//sendMidi(uniqueId,0x90,0x0C,0x2B,0x3F);//deck a forth LED
	//sendMidi(uniqueId,0x90,0x0C,0x2C,0x3F);//deck a fifth LED
	
	//Deck B
	//sendMidi(uniqueId,0x90,0x0C,0x2D,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x2E,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x3F,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x30,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x31,0x3F);//deck a fifth LED
	
	//Deck c
	//sendMidi(uniqueId,0x90,0x0C,0x32,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x33,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x34,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x35,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x36,0x3F);//deck a fifth LED
	
	//Deck d
	//sendMidi(uniqueId,0x90,0x0C,0x37,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x38,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x39,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x3A,0x3F);//deck a fifth LED
	//sendMidi(uniqueId,0x90,0x0C,0x3B,0x3F);//deck a fifth LED
	
	//sendMidi(uniqueId,0xB0,0x06,0x24,0x01);
	//sendMidi(uniqueId,0xB0,0x06,0x24,0x01);
	
		debug('button press');
		switch(data1)
		{
		case 0x01: //done
			
		break;
		case 0x02: //done
			
		break;
		case 0x03:
			
			
		break;
		case 0x04: //done
			
			break;
		case 0x05: //done
			
			break;
		case 0x06: //done
			
			break;
		case 0x07: //done
			
			break;
		case 0x08: //done
			
			break;
		case 0x09: //done
			
			break;
		case 0x0A: //done
			
			break;
		case 0x0B: //done
			if(data2)
				call('deckPlayPause', channel, data2);
			break;
		case 0x0C: //done
			//cue button
			//debug('cue pressed');
			//sendMidi(uniqueId,0x90,channel,0x0C,data2);
			call('deckCue', channel, data2);
			break;
		case 0x0D: //done
					
			break;
		case 0x0E: //done
			
			break;
		case 0x0F: //done
			
			break;
		case 0x10: //done
			
				
			break;
		case 0x11: //done
			
			break;
		case 0x12: //done
			
			break;
		case 0x13:break;
		case 0x14: //done
			
			break;
		case 0x15: //done
			
			break;
		case 0x16:break;
		case 0x17: //done
		
			break;
		case 0x18: //done
			
			break;
		case 0x19:break;
		case 0x1A: //done
			
			break;
		case 0x1B: //done
			
		break;
		case 0x1C:break;
		case 0x1D: //done
			
			break;
		case 0x1E:
			
			break;
		case 0x1F:break;
		case 0x20: //done
				
			break;		
		case 0x21:
			
			break;
		case 0x22:break;
		case 0x23: //done
				
			break;
		case 0x24:
			
			break;
		case 0x25:break;
		case 0x26: //done
				
			break;
		case 0x27:
			
			break;
		case 0x28:break;
		case 0x29: //done
				
			break;
		case 0x2A:
			
			break;
		case 0x2B:break;
		case 0x2C: //done
				
			break;
		case 0x2D:
			
			break;
		case 0x2E:
		//if(shiftPressed[channel]){
		//	call('deckCuePos1Clear', channel, data2);
			//debug();
		//} else {
			call('deckCuePos1', channel, data2);
			//debug('setting cue 1');
		//}
		break;
		case 0x2F:
		//if(shiftPressed[channel]){
		//	call('deckCuePos2Clear', channel, data2);
			//debug();
		//} else {
			call('deckCuePos2', channel, data2);										
		//}
		break;
		case 0x30:
			//if(shiftPressed[channel]){
			//	call('deckCuePos3Clear', channel, data2);										
			//} else {
				call('deckCuePos3', channel, data2);
			//}
			break;
		case 0x31:
		//if(shiftPressed[channel]){
		//	call('deckCuePos4Clear', channel, data2);										
		//} else {
			call('deckCuePos4', channel, data2);
		//}
		break;
		case 0x32:break;
		case 0x33:
			
			break;
		case 0x34:break;
		case 0x35:break;
		case 0x36: //done
			call('holdDeckJogWheel', channel, data2);
		break;
		case 0x37:break;
		case 0x38: //done
	
		break;
		case 0x39: //done
		
			break;
		case 0x3A:break;
		case 0x3B:break;
		case 0x3C: //done
			if(data2){
				if(channel == 0 || channel == 2)
					call('samplerPlay', 1);
				else 
					call('samplerPlay', 5);
			}
			
			break;
		case 0x3D:break;
		case 0x3E: //done
			if(data2){
				if(channel == 0 || channel == 2)
					call('samplerPlay', 2);
				else 
					call('samplerPlay', 6);
			}
			break;
		case 0x3F:break;
		case 0x40:
			if(data2){
				if(channel == 0 || channel == 2)
					call('samplerPlay', 3);
				else 
					call('samplerPlay', 7);
			}
		break;
		case 0x41:
			if(data2)
				call('lbToggleSelect', data2);
			break;
		case 0x42:
			if(data2){
				switch(channel){
					case 4:
					break;
					case 5:
					break;
					default:
					if(channel == 0 || channel == 2)
						call('samplerPlay', 4);
					else 
						call('samplerPlay', 8);
				}
			}
				
		break;
		case 0x43:
		if(data2){
			switch(channel){
				case 4:
				bankFxIsOn[channel-4][0] = (bankFxIsOn[channel-4][0] ? 0x00 : 0x7F);
				call('deckFxFlangerDW', channel-4, bankFxIsOn[channel-4][0]);
				sendMidi(uniqueId,0x90,channel,0x43,bankFxIsOn[channel-4][0]);
				break;
				case 5:
				bankFxIsOn[channel-4][0] = (bankFxIsOn[channel-4][0] ? 0x00 : 0x7F);
				call('deckFxFlangerDW', channel-4, bankFxIsOn[channel-4][0]);
				sendMidi(uniqueId,0x90,channel,0x43,bankFxIsOn[channel-4][0]);
				break;				
				
			}
			//bankFxIsOn[channel-4][0] = (bankFxIsOn[channel-4][0] ? 0x00 : 0x7F);
			//call('deckFxFlangerDW', channel-4, bankFxIsOn[channel-4][0]);
		}
		
		break;
		case 0x44:
		if(data2){
			switch(channel){
				case 4:
					bankFxIsOn[channel-4][1] = (bankFxIsOn[channel-4][1] ? 0x00 : 0x7F);
					call('deckFxEchoDW', channel-4, bankFxIsOn[channel-4][1]);
					sendMidi(uniqueId,0x90,channel,0x44,bankFxIsOn[channel-4][1]);
				break;
				case 5:
					bankFxIsOn[channel-4][1] = (bankFxIsOn[channel-4][1] ? 0x00 : 0x7F);
					call('deckFxEchoDW', channel-4, bankFxIsOn[channel-4][1]);
					sendMidi(uniqueId,0x90,channel,0x44,bankFxIsOn[channel-4][1]);
				break;
				
			}
			//bankFxIsOn[channel-4][1] = (bankFxIsOn[channel-4][1] ? 0x00 : 0x7F);
			//call('deckFxEchoDW', channel-4, bankFxIsOn[channel-4][1]);
		}
		break;
		case 0x45:
		if(data2){
			switch(channel){
				case 4:
				bankFxIsOn[channel-4][2] = (bankFxIsOn[channel-4][2] ? 0x00 : 0x7F);
				call('deckFxReverbDW', channel-4, bankFxIsOn[channel-4][2]);
				sendMidi(uniqueId,0x90,channel,0x45,bankFxIsOn[channel-4][2]);
				break;
				case 5:
				bankFxIsOn[channel-4][2] = (bankFxIsOn[channel-4][2] ? 0x00 : 0x7F);
				call('deckFxReverbDW', channel-4, bankFxIsOn[channel-4][2]);
				sendMidi(uniqueId,0x90,channel,0x45,bankFxIsOn[channel-4][2]);
				break;
			}
			//bankFxIsOn[channel-4][2] = (bankFxIsOn[channel-4][2] ? 0x00 : 0x7F);
			//call('deckFxReverbDW', channel-4, bankFxIsOn[channel-4][2]);
		}
		break;
		case 0x46:
		if(data2){
			switch(channel){
				case 4:
				break;
				case 5:
				break;
				default:
				call('deckLBLoad', 0, data2);
			}
			
		}
		break;
		case 0x47:
		if(data2){
			switch(channel){
				case 4:
				break;
				case 5:
				break;
				default:
				call('deckLBLoad', 1, data2);
			}
			
		}
		break;
		case 0x48:
		if(data2){
			switch(channel){
				case 6:
				call('deckLBLoad', 2, data2);
				break;
				case 4: //fx1 deck 3
					bankFxIsOn[channel-2][0] = (bankFxIsOn[channel-2][0] ? 0x00 : 0x7F);
					call('deckFxFlangerDW', channel-2, bankFxIsOn[channel-2][0]);
					sendMidi(uniqueId,0x90,channel,0x48,bankFxIsOn[channel-2][0]);
				break;
				case 5://fx1 deck 4
					bankFxIsOn[channel-2][0] = (bankFxIsOn[channel-2][0] ? 0x00 : 0x7F);
					call('deckFxFlangerDW', channel-2, bankFxIsOn[channel-2][0]);
					sendMidi(uniqueId,0x90,channel,0x48,bankFxIsOn[channel-2][0]);
				break;
				//default:
				//bankFxIsOn[channel-2][0] = (bankFxIsOn[channel-2][0] ? 0x00 : 0x7F);
				//call('deckFxFlangerDW', channel-2, bankFxIsOn[channel-2][0]);
			}
			
		}
		break;
		case 0x49:
		if(data2){
			switch(channel){
				case 6:
				call('deckLBLoad', 3, data2);
				break;
				case 4:
					bankFxIsOn[channel-2][1] = (bankFxIsOn[channel-2][1] ? 0x00 : 0x7F);
					call('deckFxEchoDW', channel-2, bankFxIsOn[channel-2][1]);
					sendMidi(uniqueId,0x90,channel,0x49,bankFxIsOn[channel-2][1]);
				break;
				case 5:
					bankFxIsOn[channel-2][1] = (bankFxIsOn[channel-2][1] ? 0x00 : 0x7F);
					call('deckFxEchoDW', channel-2, bankFxIsOn[channel-2][1]);
					sendMidi(uniqueId,0x90,channel,0x49,bankFxIsOn[channel-2][1]);
				break;
				//default:
				//bankFxIsOn[channel-2][1] = (bankFxIsOn[channel-2][1] ? 0x00 : 0x7F);
				//call('deckFxEchoDW', channel-2, bankFxIsOn[channel-2][1]);
			}
			
		}
		break;
		case 0x4A:
		if(data2){
			switch(channel){
				case 4:
				bankFxIsOn[channel-2][2] = (bankFxIsOn[channel-2][2] ? 0x00 : 0x7F);
				call('deckFxReverbDW', channel-2, bankFxIsOn[channel-2][2]);
				sendMidi(uniqueId,0x90,channel,0x4A,bankFxIsOn[channel-2][2]);
				break;
				case 5:
				bankFxIsOn[channel-2][2] = (bankFxIsOn[channel-2][2] ? 0x00 : 0x7F);
				call('deckFxReverbDW', channel-2, bankFxIsOn[channel-2][2]);
				sendMidi(uniqueId,0x90,channel,0x4A,bankFxIsOn[channel-2][2]);
				break;
			}
			//bankFxIsOn[channel-2][2] = (bankFxIsOn[channel-2][2] ? 0x00 : 0x7F);
			//call('deckFxReverbDW', channel-2, bankFxIsOn[channel-2][2]);
		}
		break;
		case 0x4B:break;
		case 0x4C:break;
		case 0x4D:break;
		case 0x4E:break;
		case 0x4F:
			shiftPressed = data2;
		break;
		case 0x50:break;
		case 0x51:break;
		case 0x52:break;
		case 0x53:break;
		case 0x54:
			if(channel == 6){
				if(data2)					
					call('deckMonitor', 0, data2);
			}
		break;
		case 0x55:
			if(channel == 6){
				if(data2)					
					call('deckMonitor', 1, data2);
			}
		break;
		case 0x56:
		if(channel == 6){
				if(data2)					
					call('deckMonitor', 2, data2);
			}
		break;
		case 0x57:
		if(channel == 6){
				if(data2)					
					call('deckMonitor', 3, data2);
			}
		break;
		case 0x58:
		if(data2)
			call('deckSync', channel, data2);
		break;
		case 0x59:break;
		case 0x5A:break;
		case 0x5B:break;
		case 0x5C:break;
		case 0x5D:break;
		case 0x5E:break;
		case 0x5F:
		call('deckCuePos1Clear', channel, data2);
		break;
		
		case 0x60:
		call('deckCuePos2Clear', channel, data2);
		break;
		
		case 0x61:
		call('deckCuePos3Clear', channel, data2);
		break;
		case 0x62:
		call('deckCuePos4Clear', channel, data2);
		break;
		
		case 0x72:
		if(data2){
		//debug('messageReceived ['+uniqueId+'] - status:'+status+' channel:'+channel+' data1:'+data1+' data2:'+data2);	
		//debug((deckSelect).toString());
			deckSelect[channel-2] = (deckSelect[channel-2] ? 0x00 : 0x7F);
			sendMidi(uniqueId,0x90,channel,0x72,deckSelect[channel-2]);
		}
		
				
		break;
		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	default: messageNotProcessed = true;
	}
	if(messageNotProcessed) debug('messageReceived ['+uniqueId+'] - status:'+status+' channel:'+channel+' data1:'+data1+' data2:'+data2);
	return !messageNotProcessed;
}
//----------------------------------------------------------------------------- 
// this is called by the software whenever something important changes (eg. play status, song changed, etc)
// depending on the notification type it may refer to a global event (deck is -1) or a specific deck (deck >= 0)
// and can contain any/all of the parameters: param 1 is an integer, param 2 is a float, param 3 is a string
function notificationReceived(uniqueId,name,deck,param1,param2,param3)
{
	var nameLen = name.length;
	
	if(name=='play')
		{
			sendMidi(uniqueId,0x90,deck,0x0B,(param1 ? 0x7F : 0x00)); //play button
			
	
		}
		
	if(name=='cue') 		 
	{
		sendMidi(uniqueId,0x90,deck,0x0C,(param1 ? 0x7F : 0x00)); // cue			
	}
	
	if(name=='onbeat') 	 	 sendMidi(uniqueId,0x90,deck,0x58,(param1 ? 0x7F : 0x00)); //sync
	
	
	if(name=='monitor') 	 sendMidi(uniqueId,0x90,0x06,deck + 0x54,(param1 ? 0x7F : 0x00)); //pfl
	
	
	if(name=='cuepos1')		sendMidi(uniqueId,0x90,deck,0x2E,(param1 ? 0x7F : 0x00));
	if(name=='cuepos2')		sendMidi(uniqueId,0x90,deck,0x2F,(param1 ? 0x7F : 0x00));
	if(name=='cuepos3')		sendMidi(uniqueId,0x90,deck,0x30,(param1 ? 0x7F : 0x00));
	if(name=='cuepos4')		sendMidi(uniqueId,0x90,deck,0x31,(param1 ? 0x7F : 0x00));
	
	if(nameLen==8)
	{
		var name1 = name.substr(0, nameLen-1);
		if(name1=='sampler')
			{				
				var samplerIdx = parseInt(name.charAt(nameLen-1))-1;		
				if(!param1) 
				{
					samplerBlinkLedStatus[samplerIdx] = 0;
					if(name=='sampler1'){ sendMidi(uniqueId,0x90,0,0x3C,0x00); sendMidi(uniqueId,0x90,2,0x3C,0x00)};
					if(name=='sampler2'){ sendMidi(uniqueId,0x90,0,0x3E,0x00); sendMidi(uniqueId,0x90,2,0x3E,0x00)};
					if(name=='sampler3'){ sendMidi(uniqueId,0x90,0,0x40,0x00); sendMidi(uniqueId,0x90,2,0x40,0x00)};
					if(name=='sampler4'){ sendMidi(uniqueId,0x90,0,0x42,0x00); sendMidi(uniqueId,0x90,2,0x42,0x00)};
					if(name=='sampler5'){ sendMidi(uniqueId,0x90,1,0x3C,0x00); sendMidi(uniqueId,0x90,3,0x3C,0x00)};
					if(name=='sampler6'){ sendMidi(uniqueId,0x90,1,0x3E,0x00); sendMidi(uniqueId,0x90,3,0x3E,0x00)};
					if(name=='sampler7'){ sendMidi(uniqueId,0x90,1,0x40,0x00); sendMidi(uniqueId,0x90,3,0x40,0x00)};
					if(name=='sampler8'){ sendMidi(uniqueId,0x90,1,0x42,0x00); sendMidi(uniqueId,0x90,3,0x42,0x00) };
					// instead of Math.floor(19/4), parseInt(19/4) = 4 can also be used
				}else
				{
					if(name=='sampler1'){ sendMidi(uniqueId,0x90,0,0x3C,0x7F); sendMidi(uniqueId,0x90,2,0x3C,0x7F)};
					if(name=='sampler2'){ sendMidi(uniqueId,0x90,0,0x3E,0x7F); sendMidi(uniqueId,0x90,2,0x3E,0x7F)};
					if(name=='sampler3'){ sendMidi(uniqueId,0x90,0,0x40,0x7F); sendMidi(uniqueId,0x90,2,0x40,0x7F)};
					if(name=='sampler4'){ sendMidi(uniqueId,0x90,0,0x42,0x7F); sendMidi(uniqueId,0x90,2,0x42,0x7F)};
					if(name=='sampler5'){ sendMidi(uniqueId,0x90,1,0x3C,0x7F); sendMidi(uniqueId,0x90,3,0x3C,0x7F)};
					if(name=='sampler6'){ sendMidi(uniqueId,0x90,1,0x3E,0x7F); sendMidi(uniqueId,0x90,3,0x3E,0x7F)};
					if(name=='sampler7'){ sendMidi(uniqueId,0x90,1,0x40,0x7F); sendMidi(uniqueId,0x90,3,0x40,0x7F)};
					if(name=='sampler8'){ sendMidi(uniqueId,0x90,1,0x42,0x7F); sendMidi(uniqueId,0x90,3,0x42,0x7F)};
					if(param2>0) samplerBlinkLedStatus[samplerIdx] = 2;else samplerBlinkLedStatus[samplerIdx] = 1;
				}				
			}
	}
}
//----------------------------------------------------------------------------- 
function getMIDIValueForVU(vu_val)
{
	// translating VU-meter signal from decibels (dB) to 0..127 MIDI range
	var val = 0;
	var step = 12.7;
	if(vu_val>-44) val =  1 * step;
	if(vu_val>-36) val =  2 * step;
	if(vu_val>-28) val =  3 * step;
	if(vu_val>-20) val =  4 * step;
	if(vu_val>-16) val =  5 * step;
	if(vu_val>-12) val =  6 * step;
	if(vu_val> -8) val =  7 * step;
	if(vu_val> -4) val =  8 * step;
	if(vu_val> -2) val =  9 * step;
	if(vu_val> -1) val = 10 * step;
	return Math.floor(val);
}
//----------------------------------------------------------------------------- 
// this is called from time to time by the software (several times per second)
// (it is guaranteed that 'timerTick' will get executed ALWAYS right after notificationReceived)
function timerTick(uniqueId)
{
	for(var i=0; i<4; i++)
	{	
		var vuDeck = getValue('vumeterDeck', i);// float dB
		var newValue = getMIDIValueForDeckVU(vuDeck);
		
		sendMidi(uniqueId,0x90,0x0C,(40 + (5 * i) + newValue),newValue ? 0x3F : 0x00);
		//sendMidi(uniqueId,0xB0,0x03,(40 + (5 * i) + newValue),0x7F);
	}
}
//----------------------------------------------------------------------------- 
function getMIDIValueForDeckVU(vu_val)
{
	// translating VU-meter signal from decibels (dB) to 0..127 MIDI range
	//values are between 1 and 10
	var val = 0;
	var step = 12.7;
	if(vu_val>-44) val =  0;
	if(vu_val>-36) val =  1;
	//if(vu_val>-28) val =  3 * step;
	if(vu_val>-20) val =  2;
	//if(vu_val>-16) val =  5 * step;
	if(vu_val>-12) val =  3;
	//if(vu_val> -8) val =  7 * step;
	if(vu_val> -4) val =  4;
	//if(vu_val> -2) val =  9 * step;
	if(vu_val> -1) val = 5;
	return Math.floor(val);
}