//-----------------------------------------------------------------------------
//-------- Copyright (c)2003-2011. All Rights Reserved. -----------------------
//-----------------------------------------------------------------------------
// file implementing MIDI controller ' Numark 4trak FX unit. The Main Unit is another script'
//Author: Brandon James 2013 08 15 initial. 
//----------------------------------------------------------------------------- 
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 "Numark 4TrakFX";
}
//----------------------------------------------------------------------------- 
// 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 (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 "00013F440019";
	
	
		//full string:
//00  F0 7E 00 06 02 00 01 3F  44 00 19 00 00 00 09 00  | ~     ?D       |
//10  7F 7F 7F 7F 00 4E 01 01  03 00 02 08 07 01 01 00  |     N          |
//20  00 00 06 06 F7 
	//return false;
}
//----------------------------------------------------------------------------- 
// 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 "Midi port name ";// !!! 
	return false;
}
//----------------------------------------------------------------------------- 
function getSupportedMidiDeviceNameForOutput(osId)
{
	//return " midi port name ";// !!! MAKE SURE THIS IS INDEED OK !!!
	return false;
}
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
// some variables necessary only for this particular script

//----------------------------------------------------------------------------- 
// 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 4TrackFX ['+uniqueId+']');// this outputs a string to DebugView app on Win
	callSendAllNotifications(uniqueId, true);
	for(var i=0; i<4; i++)
	{
		
		//sendMidi(uniqueId,0x90,i,0x22,(getValue('play', i) ? 0x7F : 0x00));// play-pause		

		
		
		
		call('sleep', 10);// delay for 10ms to allow the MIDI out buffer to flush
	}
}
//----------------------------------------------------------------------------- 
// 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 4TrakFX ['+uniqueId+']');
	callSendAllNotifications(uniqueId, false);
	for(var i=0; i<4; i++)
	{
		

		
		
		
		

		call('sleep', 10);// delay for 10ms to allow the MIDI out buffer to flush
	}
}
//----------------------------------------------------------------------------- 
// 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
	


		// decks
		case 0x15: if(channel==7) call('setDeckCutoff', 2, data2); break; //Filter Deck C
		case 0x16: if(channel==7) call('setDeckCutoff', 0, data2); break; //Filter Deck A
		case 0x17: if(channel==7) call('setDeckCutoff', 1, data2); break; //Filter Deck B
		case 0x18: if(channel==7) call('setDeckCutoff', 3, data2); break; //Filter Deck B
		
		case 0x11: if(channel==7) call('deckFxFlangerDW', 0, data2); break; //delay DW Deck A
		case 0x1A: if(channel==7) call('deckFxFlangerDW', 1, data2); break; //delay DW Deck B		
		
		case 0x12: if(channel==7) call('deckFxEchoDW', 0, data2); break; //Flanger DW Deck A
		case 0x1B: if(channel==7) call('deckFxEchoDW', 1, data2); break; //Flanger DW Deck B	
		
		case 0x13: if(channel==7) call('deckFxPhaserDW', 0, data2); break; //Phazer DW Deck A
		case 0x1C: if(channel==7) call('deckFxPhaserDW', 1, data2); break; //Phazer DW Deck B	
		
		case 0x14: if(channel==7) call('deckFxBeatwawDW', 0, data2); break; //beatwaw DW Deck A
		case 0x1D: if(channel==7) call('deckFxBeatwawDW', 1, data2); break; //beatwaw DW Deck B		

		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	// note on/off: buttons, switches ('pressing' things)
	case 0x80:
	case 0x90:
		//if(status==0x90) data2 = 0x7F;
		//if(status==0x80) data2 = 0;
		switch(data1)
		{
		// global
		

		// decks
		case 0x11: if(channel==7) call('deckFxFlanger', 0, data2); break; //deck A flanger on / off
		case 0x1A: if(channel==7) call('deckFxFlanger', 1, data2); break; //deck B flanger on / off			

		case 0x12: if(channel==7) call('deckFxEcho', 0, data2); break; //deck A echo on / off
		case 0x1B: if(channel==7) call('deckFxEcho', 1, data2); break; //deck B echo on / off	

		case 0x13: if(channel==7) call('deckFxPhaser', 0, data2); break; //deck A phaser on / off
		case 0x1C: if(channel==7) call('deckFxPhaser', 1, data2); break; //deck B phaser on / off		

		case 0x14: if(channel==7) call('deckFxBeatwaw', 0, data2); break; //deck A beatwaw on / off
		case 0x1D: if(channel==7) call('deckFxBeatwaw', 1, data2); break; //deck B beatwaw on / off		


		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);
	if(deck<4)
	{

		if(name=='fxFlanger') 
			{	 
				if(deck==0){
					sendMidi(uniqueId,0x90,7,0x11,(param1 ? 0x7F : 0x00));
				}
				if(deck==1){
					sendMidi(uniqueId,0x90,7,0x1A,(param1 ? 0x7F : 0x00));
				}
			}
			
		if(name=='fxEcho') 
			{	 
				if(deck==0){
					sendMidi(uniqueId,0x90,7,0x12,(param1 ? 0x7F : 0x00));
				}
				if(deck==1){
					sendMidi(uniqueId,0x90,7,0x1B,(param1 ? 0x7F : 0x00));
				}
			}
			
		if(name=='fxPhaser') 
			{	 
				if(deck==0){
					sendMidi(uniqueId,0x90,7,0x13,(param1 ? 0x7F : 0x00));
				}
				if(deck==1){
					sendMidi(uniqueId,0x90,7,0x1C,(param1 ? 0x7F : 0x00));
				}
			}

		if(name=='fxBeatwaw') 
			{	 
				if(deck==0){
					sendMidi(uniqueId,0x90,7,0x14,(param1 ? 0x7F : 0x00));
				}
				if(deck==1){
					sendMidi(uniqueId,0x90,7,0x1D,(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+']');


	for(var i=0; i<4; i++)
	{
	
	}
}
//----------------------------------------------------------------------------- 
