//-----------------------------------------------------------------------------
//-------- Copyright (c)2003-2013. All Rights Reserved. -----------------------
//-----------------------------------------------------------------------------
// file implementing MIDI controller 'American Audio 10MXR'
// Author: Brandon James
//----------------------------------------------------------------------------- 
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 "AA10MXR";
}
//----------------------------------------------------------------------------- 
// 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 "000170030001";
	
	//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

var Shift=0 
//----------------------------------------------------------------------------- 
// 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 AA10MXR ['+uniqueId+']');// this outputs a string to DebugView app on Win
	for(var i=0; i<2; i++)
	{
		
		
		//deck 1
		sendMidi(uniqueId,0x90,0,0x0D,(getValue('play', 0) ? 0x7F : 0x00));// play-pause
		sendMidi(uniqueId,0x90,0,0x0C,(getValue('cue', 0)? 0x7F : 0x00));// Cue
		sendMidi(uniqueId,0x90,0,0x12,(getValue('cuepos1', 0) ? 0x7F : 0x00));// cue 1
		sendMidi(uniqueId,0x90,0,0x13,(getValue('cuepos2', 0) ? 0x7F : 0x00));// cue 2		
		sendMidi(uniqueId,0x90,0,0x15,(getValue('onbeat', 0) ? 0x7F : 0x00));// sync
		
		//deck2
		
		sendMidi(uniqueId,0x90,0,0x2F,(getValue('play', 1) ? 0x7F : 0x00));// play-pause
		sendMidi(uniqueId,0x90,0,0x2E,(getValue('cue', 1)? 0x7F : 0x00));// Cue		
		sendMidi(uniqueId,0x90,0,0x34,(getValue('cuepos1', 1) ? 0x7F : 0x00));// cue 1
		sendMidi(uniqueId,0x90,0,0x35,(getValue('cuepos2', 1) ? 0x7F : 0x00));// cue 2
		sendMidi(uniqueId,0x90,0,0x37,(getValue('onbeat', 1) ? 0x7F : 0x00));// sync
		
		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 AA10MXR ['+uniqueId+']');
	
	for(var i=0; i<2; i++)
	{
		

	 	//global	
		
		//deck1
		sendMidi(uniqueId,0x90,0,0x0D,0x00); //play
		sendMidi(uniqueId,0x90,0,0x0C,0x00); //cue
		sendMidi(uniqueId,0x90,0,0x12,0x00); //cue 1
		sendMidi(uniqueId,0x90,0,0x13,0x00); //cue 2
		sendMidi(uniqueId,0x90,0,0x15,0x00); //sync
		
		//deck 2
		sendMidi(uniqueId,0x90,0,0x2F,0x00); //play
		sendMidi(uniqueId,0x90,0,0x2E,0x00); //cue
		sendMidi(uniqueId,0x90,0,0x34,0x00); //cue 1
		sendMidi(uniqueId,0x90,0,0x35,0x00); //cue 2
		sendMidi(uniqueId,0x90,0,0x37,0x00); //sync		
		

		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
		case 0x34: call('lbMoveCursor', data2==0x3F ? 1:-1); break; //library curser up down		
		case 0x1F: call('setCrossfader', data2); break;
		

		// decks
		case 0x04: call('setDeckVolume', 0, data2); break; //volume slider deck 1
		case 0x09: call('setDeckVolume', 1, data2); break; //volume slider deck 2

		
		//deck 1 knobs
		case 0x00: call('setDeckGain', 0, data2); break; // deck gain
		case 0x01: call('setDeckEqHigh', 0, data2); break; // eq high
		case 0x02: call('setDeckEqMid', 0, data2); break; // eq mid
		case 0x03: call('setDeckEqLow', 0, data2); break; // eq low
		case 0x25: call('setDeckCutoff', 0, data2); break; // HP filter

		
		//deck 2 knobs
		case 0x05: call('setDeckGain', 1, data2); break; // deck gain
		case 0x06: call('setDeckEqHigh', 1, data2); break; // eq high
		case 0x07: call('setDeckEqMid', 1, data2); break; // eq mid
		case 0x08: call('setDeckEqLow', 1, data2); break; // eq low
		case 0x27: call('setDeckCutoff', 1, data2); break; // HP filter

		
			

		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	// note on/off: buttons, switches ('pressing' things)
	case 0x80:
	case 0x90:
		//if(status==0x90) data2 = 0x7F;
		//if(status==0x40) data2 = 0;
		if((status==0x80)&&(data2==0x40)) data2 = 0;
		switch(data1)
		{
		// global
		case 0x67:  if(channel==0) call('lbBrowserSelect', data2); break;
		case 0x68:  if(channel==0) call('lbListSelect', data2); break;
		case 0x6B:  if(channel==0) call('lbToggleSelect', data2); break;
		case 0x6D:  if(data2==0x7F) Shift==1;
					if(data2==0x40) Shift==0;
					break;
		//case 0x00: if((status==0x90)&&(data2>0x3F)) call('lbToggleSelect', data2); break;
		//case 0x59: if(data2>0x3F) call('lbBrowserSelect', data2); break;
		//case 0x5A: if(data2>0x3F) call('lbListSelect', data2); break;

		
		// decks

		//deck 1
		case 0x65: if(channel==0) call('deckLBLoad', 0, data2); break;
		case 0x0D: if(channel==0) call('deckPlayPause', 0, data2); break;
		case 0x0C: if(channel==0) call('deckCue', 0, data2); break;
		case 0x12: if((channel==0)&&(Shift==0)) call('deckCuePos1', 0, data2);else
				   call('deckBendDown', 0, data2); break; //cue 1 / bend down		
		
		case 0x13: if((channel==0)&&(Shift==0)) call('deckCuePos2', 0, data2);else
				   call('deckResetPitch', 0, data2); break; //cue 2 / reset pitch		
		
		case 0x15: if((channel==0)&&(Shift==0)) call('deckSync', 0, data2);else
				   call('deckBendUp', 0, data2); break; //sync / Bend Up

		
		
		//deck 2
		case 0x66: if(channel==0) call('deckLBLoad', 1, data2); break;
		case 0x2F: if(channel==0) call('deckPlayPause', 1, data2); break;
		case 0x2E: if(channel==0) call('deckCue', 1, data2); break;				
		case 0x34: if((channel==0)&&(Shift==0)) call('deckCuePos1', 1, data2);else
				   call('deckBendDown', 1, data2); break; //cue 1 / bend down
		
		case 0x35: if((channel==0)&&(Shift==0)) call('deckCuePos2', 1, data2);else
				   call('deckResetPitch', 1, data2); break; //cue 2 / bend up
		
		case 0x37: if((channel==0)&&(Shift==0)) call('deckSync', 1, data2);else
				   call('deckBendUp', 1, data2); break; //sync / bend up
		
		
		

		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<2)
	{
		if(name=='cue') 		 sendMidi(uniqueId,0x90,0,(deck==0)?0x0C:0x2E,(param1 ? 0x7F : 0x00)); //cue 
		if(name=='play') 		 sendMidi(uniqueId,0x90,0,(deck==0)?0x0D:0x2F,(param1 ? 0x7F : 0x00)); //play
		if(name=='cuepos1') 	 sendMidi(uniqueId,0x90,0,(deck==0)?0x12:0x34,(param1 ? 0x7F : 0x00)); //cue 1
		if(name=='cuepos2') 	 sendMidi(uniqueId,0x90,0,(deck==0)?0x13:0x35,(param1 ? 0x7F : 0x00)); //cue 2
		if(name=='onbeat') 	 	 sendMidi(uniqueId,0x90,0,(deck==0)?0x15:0x37,(param1 ? 0x7F : 0x00)); //sync
		//if(name=='monitor') 	 sendMidi(uniqueId,0x90,0,(deck==0)?0x65:0x61,(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<2; i++)
	{
	
	}
}
//----------------------------------------------------------------------------- 
