<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!-- 
Sample mapping documenting the sysex manipulation support in TouchDAW 1.5.1.

Support for changing seperate fields inside a sysex message has been requested a couple of times and I always felt it 
would make sense. However dealing with arbitrary sysex is quickly becoming super complex and making it cover each and
every possibility would probably just blow TouchDAW apart, break much of the standard MIDI support etc.
The implementation therefore is rather basic and does require manual editing of the xml file. Feedback is not fully implemented, 
incoming data will not set the controls in TouchDAW correctly in all cases. 
I do not think that this will change much in this app's context. It's all just a bit too far 
away from TouchDAW's original purpose and should better be done in a dedicated sysex app that conceptually starts thinking from 
the message onwards, not the control.

Anyway, the idea here is that you attach some sysex data to a control by means of the "midi" parameter and then give the
data field that you want to be modifiable some special value. Those values are defined as follows:

Highest bit set | 4 bit range | 3 bit modification type

That is: they are made up from OR combined values for value range and the way they change their value and always have the
highest bit set. The latter basically makes them invalid inside a sysex message or would terminate the message when interpreted
by standard MIDI rules. TouchDAW just uses these values to locate the mutable fields, though. They will not go out "on the wire". 

Modification types are defined as:

0 - Linear
1 - Endless
2 - Increment 
3 - Decrement

Value ranges:

0 - 7 bit
1 - 14 bit little endian, filling two successive array slots (the one that is marked as mutable and the one directly following it)
2 - 14 bit big endian, filling two successive array slots...
3 - 14 bit little endian with an unchanged slot between LSB and MSB (the one that is marked as mutable and the one two positions after it)
4 - 14 bit big endian with an unchanged slot between MSB and LSB...

If the modification type is 1 (endless) then value ranges have a different meaning:

0 - value will be set to either 0x01 or 0x7F depending on direction
1 - value will be set to either 0x3F or 0x41 depending on direction
2 - value will be set to either 0x01 or 0x41 depending on direction

Calculation then goes like this:

0x80 | (range << 3) | type

A 7 bit linear modifier would for example give:

0x80 | 0 | 0 = 0x80

A 14 bit big endian linear one:

0x80 | (2 << 3) | 0 = 0x90

Every message may have only one mutable field per control, but multiple controls on one channel can work on "the same" 
message if the given midi is identical besides the fields marked as being mutable. See the "UP" & "DOWN" buttons on
channel id 0 and encoder, buttons and fader on channel id 3 as an example.

All sysex data used in the example below is probably totally meaningless, it's just used to explain how the field assignment
is supposed to work.

I recommend using MIDI-Ox, MIDI Monitor or some other monitoring software when setting up something like this. TouchDAW will 
reload the xml file when switched to MIDI mode. You can push a new edit to the SD card, then exit and re-enter MIDI mode to 
test changes and do not need to relaunch the app every time.

-->
<touchdaw_midi_map>
	<mixer>
		<channel id="0">
			<!-- endless encoder, sets the messsage's first byte to 0x01 or 0x7F and sends the message -->
			<control id="0" name="v_pot" midi="0xF0, 0x81, 0x03, 0x04, 0xF7"/>
			<control id="1" name="v_pot_press"/>
			
			<control id="2" name="arm"/>
			<!-- UP button, increment value and send -->
			<control id="3" name="solo" title="UP" midi="0xF0, 0x82, 0x00, 0x00, 0xF7"/>
			<!-- DOWN button, decrement value and send -->
			<control id="4" name="mute" title="DOWN" midi="0xF0, 0x83, 0x00, 0x00, 0xF7"/>
			<control id="5" name="edit"/>
			<!-- 7 bit fader -->
			<control id="6" name="fader" midi="0xF0, 0x01, 0x02, 0x80, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="1">
			<!-- endless encoder, sets the messsage's first byte to 0x3F or 0x41 and sends the message -->
			<control id="0" name="v_pot" midi="0xF0, 0x89, 0x03, 0x04, 0xF7"/>
			<control id="1" name="v_pot_press"/>
			
			<!-- This button has a sysex message with no mutable fields assigned. It will just go out unchanged -->
			<control id="2" name="arm" title="Sysex" midi="0xF0, 0x01, 0x02, 0x03, 0x01, 0x02, 0xF7"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			
			<!-- 14 bit little endian fader -->
			<control id="6" name="fader" midi="0xF0, 0x01, 0x02, 0x88, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="2">
			<!-- endless encoder, sets the messsage's first byte to 0x01 or 0x41 and sends the message -->
			<control id="0" name="v_pot" midi="0xF0, 0x91, 0x03, 0x04, 0xF7"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			<!-- 14 bit big endian fader -->
			<control id="6" name="fader" midi="0xF0, 0x01, 0x02, 0x90, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="3">
			
			<!-- This channel uses identical sysex messages on all controls. "Identical" meaning they are identical
			apart from the fields set to be mutable. As a result all controls will write their modifications
			into all controls' messages, so that you can have multiple changing fields -->
			
			<!-- 7 bit linear potentiometer (0x80) -->
			<control id="0" name="v_pot" midi="0xF0, 0x00, 0x80, 0x00, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<!-- INC button, increment value and send -->
			<control id="3" name="solo" title="INC" midi="0xF0, 0x82, 0x00, 0x00, 0x01, 0x01, 0x02, 0xF7"/>
			<!-- DEC button, deccrement value and send -->
			<control id="4" name="mute" title="DEC" midi="0xF0, 0x83, 0x00, 0x00, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="5" name="edit"/>
			<!-- 14 bit little endian fader with spread value field -->
			<control id="6" name="fader" midi="0xF0, 0x00, 0x00, 0x98, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="7" name="meter" midi="0xB3, 0x01, 0x7F"/>
		</channel>
		<channel id="4">
			<control id="0" name="v_pot"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			<!-- 14 bit big endian fader with spread value field -->
			<control id="6" name="fader" midi="0xF0, 0x01, 0x02, 0xA0, 0x01, 0x01, 0x02, 0xF7"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="5">
			<control id="0" name="v_pot"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			<control id="6" name="fader"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="6">
			<control id="0" name="v_pot"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			<control id="6" name="fader"/>
			<control id="7" name="meter"/>
		</channel>
		<channel id="7">
			<control id="0" name="v_pot"/>
			<control id="1" name="v_pot_press"/>
			<control id="2" name="arm"/>
			<control id="3" name="solo"/>
			<control id="4" name="mute"/>
			<control id="5" name="edit"/>
			<control id="6" name="fader"/>
			<control id="7" name="meter"/>
		</channel>
		<!-- Master can also be set to MIDI mode (no NRPN though). Without this it will 
		continue to control master level in the DAW
		<master midi="0xB8, 0x01, 0x7F"/> -->
	</mixer>
</touchdaw_midi_map>