//-----------------------------------------------------------------------------
//-------- Copyright (c)2003-2011 XYLIO INFO SRL. All Rights Reserved. --------
//-----------------------------------------------------------------------------
// file implementing MIDI controller 'Beamz Player'
//----------------------------------------------------------------------------- 
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 "Beamz Player";
}
//----------------------------------------------------------------------------- 
// 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 (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 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)
{
	if(osId==1) return "Beamz";
	return "Beamz Virtual MIDI";
}
//----------------------------------------------------------------------------- 
function getSupportedMidiDeviceNameForOutput(osId)
{
	if(osId==1) return "Beamz";
	return "Beamz Virtual MIDI";
}
//----------------------------------------------------------------------------- 
// 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 Beamz ['+uniqueId+']');// this outputs a string to DebugView app on Win
	//sendMidi(uniqueId, status, channel, data1, data2);
}
//----------------------------------------------------------------------------- 
// 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 Beamz ['+uniqueId+']');
}	
//----------------------------------------------------------------------------- 
// 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 0x38: call('setCrossfader', data2); break;
		//case 0x3D: call('setCuePgmMix', data2); break;
		case 0x00: break;
		
		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	// note on/off: buttons, switches ('pressing' things)
	case 0x80:
	case 0x90:
		switch(data1)
		{
		// global
		case 0x36: call('mixNext', data2); break;// from profile 1
		case 0x2F: call('mixNext', data2); break;// from profile 2
		case 0x28: call('mixNext', data2); break;// from profile 3
		
		// per deck
		
		// deck A
		//Profile 1 (Transport)
		case 0x3C: call('deckCuePlay', 0, data2); break;
		case 0x3A: call('deckSync', 0, data2); break;
		case 0x38: call('deckPlayPause', 0, data2); break;
		
		//Profile 2 (Loops)
		// 64 = 1 beat; 32 = 1/2 beat; 16 = 1/4 beat; 8 = 1/8 beat; 4 = 1/16 beat;
		case 0x35: 
			var deck=0;
			if(data2==0x00)
			{
				// simulate a fast press
				call('deckLoopExit', deck, 0x7F);
				call('deckLoopExit', deck, 0x00);
			}else
			{
				// simulate a fast press
				call('deckLoop1_4', deck, 0x7F);
				call('deckLoop1_4', deck, 0x00);
			}
			break;
		case 0x33: 
			var deck=0;
			if(data2==0x00)
			{
				// simulate a fast press
				call('deckLoopExit', deck, 0x7F);
				call('deckLoopExit', deck, 0x00);
			}else
			{
				// simulate a fast press
				call('deckLoop1', deck, 0x7F);
				call('deckLoop1', deck, 0x00);
			}
			break;			
		case 0x31: call('deckReverse', 0, data2); break;
		
		//Profile 3 (Effects)
		case 0x2E: call('deckFxFlanger', 0, data2); break;
		case 0x2C: call('deckFxBeatwaw', 0, data2); break;
		case 0x2A: call('deckBrake', 0, data2); break;

		// deck B
		//Profile 1 (Transport)
		case 0x3B: call('deckCuePlay', 1, data2); break;
		case 0x39: call('deckSync', 1, data2); break;
		case 0x37: call('deckPlayPause', 1, data2); break;
		
		//Profile 2 (Loops)
		// 64 = 1 beat; 32 = 1/2 beat; 16 = 1/4 beat; 8 = 1/8 beat; 4 = 1/16 beat;
		case 0x34: 
			var deck=1;
			if(data2==0x00)
			{
				// simulate a fast press
				call('deckLoopExit', deck, 0x7F);
				call('deckLoopExit', deck, 0x00);
			}else
			{
				// simulate a fast press
				call('deckLoop1_4', deck, 0x7F);
				call('deckLoop1_4', deck, 0x00);
			}
			break;
		case 0x32: 
			var deck=1;
			if(data2==0x00)
			{
				// simulate a fast press
				call('deckLoopExit', deck, 0x7F);
				call('deckLoopExit', deck, 0x00);
			}else
			{
				// simulate a fast press
				call('deckLoop1', deck, 0x7F);
				call('deckLoop1', deck, 0x00);
			}
			break;			
		case 0x30: call('deckReverse', 1, data2); break;
		
		//Profile 3 (Effects)
		case 0x2D: call('deckFxFlanger', 1, data2); break;
		case 0x2B: call('deckFxBeatwaw', 1, data2); break;
		case 0x29: call('deckBrake', 1, 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);
}
//----------------------------------------------------------------------------- 
// 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+']');
}
//----------------------------------------------------------------------------- 
