//-----------------------------------------------------------------------------
//------------ Copyright (c)2003-2018 Xylio. All Rights Reserved. -------------
//-----------------------------------------------------------------------------
// file implementing MIDI controller 'Hercules DJ4Set'
//-----------------------------------------------------------------------------
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 "Hercules DJ 4Set";
}
//-----------------------------------------------------------------------------
// 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 getSupportedMidiDeviceVID(osId)
{
	return 0x06F8;
}
//-----------------------------------------------------------------------------
function getSupportedMidiDevicePID(osId)
{
	return 0xB10C;
}
//-----------------------------------------------------------------------------
/*
function getSupportedMidiDeviceVID2(osId)
{
	return 0x06F8;
}
//-----------------------------------------------------------------------------
function getSupportedMidiDevicePID2(osId)
{
	return 0xB10C;
}
*/
//-----------------------------------------------------------------------------
// 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 "00014E02000A";
	// F0 7E 00 06 02 00 01 4E 02 00 02 00 01 00 00 00 F7 -- Hercules DJ Console MK2 complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 03 00 01 00 00 00 F7 -- Hercules DJ Control MP3 complete sysex
	// F0 7E 00 06 02 00 01 4E 02 00 04 00 01 00 00 00 F7 -- Hercules DJ Console RMX complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 05 00 01 00 00 00 F7 -- Hercules DJ Control Steel complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 07 00 01 00 00 00 F7 -- Hercules DJ Control MP3e2 complete sysex
	// F0 7E 00 06 02 00 01 4E 02 00 08 00 01 00 00 00 F7 -- Hercules DJ Console MK4 complete sysex
	// F0 7E 00 06 02 00 01 4E 02 00 0A 00 01 00 00 00 F7 -- Hercules DJ4Set complete sysex
}
//-----------------------------------------------------------------------------
// 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)
{
	//if(osId==1) return "DJ4Set";
	//return "DJ 4Set";// macOS
	//return "Hercules DJ 4Set";
	//return "Hercules DJ 4Set MIDI";// Windows
	return false;
}
//-----------------------------------------------------------------------------
function getSupportedMidiDeviceNameForOutput(osId)
{
	//if(osId==1) return "DJ4Set";
	//return "DJ 4Set";// macOS
	//return "Hercules DJ 4Set";
	//return "Hercules DJ 4Set MIDI";// Windows
	return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// some variables necessary only for this particular script
var jogWheelsInScratchMode=0;
var loopEffectsMode=[0,0];// 0x00 loop; 0x7F effects
var previousAutoLoopLightLed=[-1,-1];// 2 decks leds
//-----------------------------------------------------------------------------
// 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 ['+uniqueId+']');// this outputs a string to DebugView app on Win
	// setting audio (the actual routing is done automatically for this controller so
	// we don't need to manually set "RoutingOutHeadphones", "RoutingOutDeckA", etc
	// ATTN: we first try to set ASIO mode, and only then regular CoreAudio, DirectSound audio/WDM/Wasapi
	if(setAudioDevice("Hercules DJ 4Set ASIO|||Hercules DJ 4Set|||Hercules DJ 4SET Aggregate|||DJ 4Set|||Speakers (Hercules DJ 4Set)|||Speakers (DJ 4Set)"))
	{
		//debug('AudioDevice was set OK');
		setSetting('AudioDevice2', "NO AUDIO");
	}
	//sendMidi(uniqueId, status, channel, data1, data2);
	sendMidi(uniqueId,0x90,0,0x3D,0x00);// scratch mode off
	sendMidi(uniqueId,0x90,0,0x3C,0x00);// record
	//------------------------------------
	callSendAllNotifications(uniqueId, true);// send all notifications to the controller (so that we don't have to manually init all leds/lcd screens)
}
//-----------------------------------------------------------------------------
// 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 ['+uniqueId+']');
	// set leds off
	sendMidi(uniqueId, 0x90, 0, 0x3D, 0x00);// scratch mode
	sendMidi(uniqueId, 0x90, 0, 0x3C, 0x00);// record
	//------------------------------------
	callSendAllNotifications(uniqueId, false);// send all notifications to the controller (so that we don't have to manually de-init all leds/lcd screens)
}
//-----------------------------------------------------------------------------
// 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)
{
	var messageNotProcessed=false;
	switch(status)
	{
	//-----------------------------------------------
	// controllers: sliders, knobs, jog-wheels, encoders ('moving' things)
	case 0xB0:
		switch(data1)
		{
		// global
		case 0x21: call('setMasterVolume', data2); break;
		case 0x22: call('setCrossfader', data2); break;
		case 0x23: call('setCuePgmMix', data2); break;
		case 0x04: call('setMonitorVolume', data2); break;
		case 0x0C: call('setMicGain', data2); break;
		// per deck
		// deck A
		case 0x08: call('setDeckEqLow', 0, data2); break;
		case 0x07: call('setDeckEqMid', 0, data2); break;
		case 0x06: call('setDeckEqHigh', 0, data2); break;
		case 0x03: call('setDeckVolume', 0, data2); break;
		case 0x05: call('setDeckPitch', 0, data2); break;// increment mode
		case 0x01:
		case 0x24:// params: (deck, increment, total positions per complete revolution) (24)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 0, val1, 250);else call('moveDeckJogWheel', 0, val1, 250);
			break;

		// deck B
		case 0x10: call('setDeckEqLow', 1, data2); break;
		case 0x0F: call('setDeckEqMid', 1, data2); break;
		case 0x0E: call('setDeckEqHigh', 1, data2); break;
		case 0x0B: call('setDeckVolume', 1, data2); break;
		case 0x0D: call('setDeckPitch', 1, data2); break;// increment mode
		case 0x09:
		case 0x25:// params: (deck, increment, total positions per complete revolution) (25)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 1, val1, 250);else call('moveDeckJogWheel', 1, val1, 250);
			break;

			// deck C
		case 0x18: call('setDeckEqLow', 2, data2); break;
		case 0x17: call('setDeckEqMid', 2, data2); break;
		case 0x16: call('setDeckEqHigh', 2, data2); break;
		case 0x13: call('setDeckVolume', 2, data2); break;
		case 0x15: call('setDeckPitch', 2, data2); break;// increment mode
		case 0x11:
		case 0x26:// params: (deck, increment, total positions per complete revolution) (26)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 2, val1, 250);else call('moveDeckJogWheel', 2, val1, 250);
			break;

		// deck D
		case 0x20: call('setDeckEqLow', 3, data2); break;
		case 0x1F: call('setDeckEqMid', 3, data2); break;
		case 0x1E: call('setDeckEqHigh', 3, data2); break;
		case 0x1B: call('setDeckVolume', 3, data2); break;
		case 0x1D: call('setDeckPitch', 3, data2); break;// increment mode
		case 0x19:
		case 0x27:// params: (deck, increment, total positions per complete revolution) (27)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 3, val1, 250);else call('moveDeckJogWheel', 3, val1, 250);
			break;

		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	// note on/off: buttons, switches ('pressing' things)
	case 0x80:
	case 0x90:
		switch(data1)
		{
		// global
		case 0x40: if(data2>0x3F) call('lbMoveCursor', -1); break;
		case 0x41: if(data2>0x3F) call('lbMoveCursor', +1); break;
		case 0x3E: if(data2>0x3F) call('lbListSelect', 1); break;
		case 0x3F: if(data2>0x3F) call('lbBrowserSelect', 1); break;
		case 0x3D: if(data2>0x3F){ jogWheelsInScratchMode = (jogWheelsInScratchMode+1)%2; sendMidi(uniqueId,0x90,0,0x3D,(jogWheelsInScratchMode ? 0x7F : 0x00));} break;
		case 0x53: call('micOnAir', data2); /*call('micTalkOver', data2);*/ break;
		case 0x3C: call('record', data2); break;
		// per deck
		// deck A/C
		case 0x14: call('deckLoopInEnd', 0+channel*2, data2); break;
		case 0x13: call('deckLoopExit', 0+channel*2, data2); break;
		case 0x01: call('deckCuePos1', 0+channel*2, data2); break;
		case 0x02: call('deckCuePos2', 0+channel*2, data2); break;
		case 0x03: call('deckCuePos3', 0+channel*2, data2); break;
		case 0x07: call('deckFxFlanger', 0+channel*2, data2); break;
		case 0x08: call('deckFxReverb', 0+channel*2, data2); break;
		case 0x09: call('deckFxEcho', 0+channel*2, data2); break;
		case 0x1A: call('holdDeckJogWheel', 0+channel*2, data2); break;
		case 0x1D: call('deckBendDown', 0+channel*2, data2); break;
		case 0x1E: call('deckBendUp', 0+channel*2, data2); break;
		case 0x1F: call('deckSearchLeft', 0+channel*2, data2); break;
		case 0x20: call('deckSearchRight', 0+channel*2, data2); break;
		case 0x0D: call('deckCue', 0+channel*2, data2); break;
		case 0x0E: call('deckPlayPause', 0+channel*2, data2); break;
		case 0x0F: call('deckMonitor', 0+channel*2, data2); break;
		case 0x4A: call('deckLBLoad', 0+channel*2, data2); break;
		case 0x11: call('deckSync', 0+channel*2, data2); break;
		case 0x1C:
				if(channel==0) loopEffectsMode[0] = data2;
				if(channel==1) loopEffectsMode[2] = data2;
				break;

		// deck B/D
		case 0x34: call('deckLoopInEnd', 1+channel*2, data2); break;
		case 0x33: call('deckLoopExit', 1+channel*2, data2); break;
		case 0x21: call('deckCuePos1', 1+channel*2, data2); break;
		case 0x22: call('deckCuePos2', 1+channel*2, data2); break;
		case 0x23: call('deckCuePos3', 1+channel*2, data2); break;
		case 0x27: call('deckFxFlanger', 1+channel*2, data2); break;
		case 0x28: call('deckFxReverb', 1+channel*2, data2); break;
		case 0x29: call('deckFxEcho', 1+channel*2, data2); break;
		case 0x3A: call('holdDeckJogWheel', 1+channel*2, data2); break;
		case 0x43: call('deckBendDown', 1+channel*2, data2); break;
		case 0x44: call('deckBendUp', 1+channel*2, data2); break;
		case 0x45: call('deckSearchLeft', 1+channel*2, data2); break;
		case 0x46: call('deckSearchRight', 1+channel*2, data2); break;
		case 0x2D: call('deckCue', 1+channel*2, data2); break;
		case 0x2E: call('deckPlayPause', 1+channel*2, data2); break;
		case 0x2F: call('deckMonitor', 1+channel*2, data2); break;
		case 0x4B: call('deckLBLoad', 1+channel*2, data2); break;
		case 0x31: call('deckSync', 1+channel*2, data2); break;
		case 0x42:
				if(channel==0) loopEffectsMode[1] = data2;
				if(channel==1) loopEffectsMode[3] = data2;
				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)
{
	//debug('notif ['+uniqueId+'] name='+name + ' deck='+deck + ' p1(int)='+param1 + ' p2(float)='+param2 + ' p3(str)='+param3);
	var ch=0;// for decks A,B
	if(deck>=2) ch=1;// for decks C,D
	if(name=='cuepos1') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x01:0x21,(param1 ? 0x7F : 0x00));
	if(name=='cuepos2') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x02:0x22,(param1 ? 0x7F : 0x00));
	if(name=='cuepos3') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x03:0x23,(param1 ? 0x7F : 0x00));
	if(name=='fxFlanger') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x07:0x27,(param1 ? 0x7F : 0x00));
	if(name=='fxReverb') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x08:0x28,(param1 ? 0x7F : 0x00));
	if(name=='fxEcho')  	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x09:0x29,(param1 ? 0x7F : 0x00));
	if(name=='cue') 		sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x0D:0x2D,(param1 ? 0x7F : 0x00));
	if(name=='play') 		sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x0E:0x2E,(param1 ? 0x7F : 0x00));
	if(name=='monitor') 	sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x0F:0x2F,(param1 ? 0x7F : 0x00));
	if(name=='onbeat') 		sendMidi(uniqueId,0x90,ch,(deck%2==0)?0x11:0x31,(param1 ? 0x7F : 0x00));
	if(name=='recording')   sendMidi(uniqueId,0x90,0,0x3C,(param1 ? 0x7F : 0x00));
}
//-----------------------------------------------------------------------------
// 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)
{
	//debug('timerTick ['+uniqueId+']');
}
//-----------------------------------------------------------------------------
