/*

"MIDI_ChannelRemapper+Router" - by: Sjoerd van Geffen (banned)
v0.01 (2009-06-07)

Remaps / routes MIDI events by channel.

____

(C) Copyright 2009, Sjoerd van Geffen
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are not permitted 
unless explicitly specified by mandatory provisions of applicable law.

*/

////////////////////////////////////////////////////////////////////////////////////////////
desc: MIDI: channel remapper/filter.

////////////////////////////////////////////////////////////////////////////////////////////
slider1:1<0,16,1{BLOCK,Pass Thru on Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 1 Input >
slider2:2<0,16,1{BLOCK,Route to Channel 1,Pass Thru on Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 2 Input >
slider3:3<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Pass Thru on Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 3 Input >
slider4:4<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Pass Thru on Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 4 Input >
slider5:5<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Pass Thru on Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 5 Input >
slider6:6<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Pass Thru on Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 6 Input >
slider7:7<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Pass Thru on Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 7 Input >
slider8:8<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Pass Thru on Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 8 Input >
slider9:9<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Pass Thru on Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 9 Input >
slider10:10<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Pass Thru on Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 10 Input >
slider11:11<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Pass Thru on Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 11 Input >
slider12:12<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Pass Thru on Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 12 Input >
slider13:13<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Pass Thru on Channel 13,Route to Channel 14,Route to Channel 15,Route to Channel 16}>Channel 13 Input >
slider14:14<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Pass Thru on Channel 14,Route to Channel 15,Route to Channel 16}>Channel 14 Input >
slider15:15<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Pass Thru on Channel 15,Route to Channel 16}>Channel 15 Input >
slider16:16<0,16,1{BLOCK,Route to Channel 1,Route to Channel 2,Route to Channel 3,Route to Channel 4,Route to Channel 5,Route to Channel 6,Route to Channel 7,Route to Channel 8,Route to Channel 9,Route to Channel 10,Route to Channel 11,Route to Channel 12,Route to Channel 13,Route to Channel 14,Route to Channel 15,Pass Thru on Channel 16}>Channel 16 Input >

////////////////////////////////////////////////////////////////////////////////////////////
@init

ext_noinit = 1.0; //Set this variable to 1.0 in your @init section if you do not wish for @init to be called (and variables/RAM to be possibly cleared) on every transport start.

ext_nodenorm = 1.0; //Set this variable to 1.0 in your @init section if you do not wish to have anti-denormal noise added to input.

//MIDI messages:
//statNoteOff = 128; // (= 8 * 16)
//statNoteOn = 144; // (= 9 * 16)
//statControlChange = 176; // (= 11 * 16)
//statPitch = 224; // (=14 * 16)
//ccAllNotesOff = 123;

////////////////////////////////////////////////////////////////////////////////////////////
@slider

MIDIChannel1Output = slider1 - 1;
MIDIChannel2Output = slider2 - 1;
MIDIChannel3Output = slider3 - 1;
MIDIChannel4Output = slider4 - 1;
MIDIChannel5Output = slider5 - 1;
MIDIChannel6Output = slider6 - 1;
MIDIChannel7Output = slider7 - 1;
MIDIChannel8Output = slider8 - 1;
MIDIChannel9Output = slider9 - 1;
MIDIChannel10Output = slider10 - 1;
MIDIChannel11Output = slider11 - 1;
MIDIChannel12Output = slider12 - 1;
MIDIChannel13Output = slider13 - 1;
MIDIChannel14Output = slider14 - 1;
MIDIChannel15Output = slider15 - 1;
MIDIChannel16Output = slider16 - 1;

////////////////////////////////////////////////////////////////////////////////////////////
@block
while
(
	midirecv(offset,msg1,msg23) ?
	(
		//Check status byte
		status = msg1 & 240;  //High four bits is message type (240 == 11110000)
		//Check MIDI channel
		channel = msg1 & 15;  //Low four bits is channel number (15 == 00001111)
		
		//Filter/route MIDI events for each channel
		(channel == 0 && MIDIChannel1Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel1Output,msg23); //Pass thru.
		);
		(channel == 1 && MIDIChannel2Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel2Output,msg23); //Pass thru.
		);
		(channel == 2 && MIDIChannel3Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel3Output,msg23); //Pass thru.
		);
		(channel == 3 && MIDIChannel4Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel4Output,msg23); //Pass thru.
		);
		(channel == 4 && MIDIChannel5Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel5Output,msg23); //Pass thru.
		);
		(channel == 5 && MIDIChannel6Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel6Output,msg23); //Pass thru.
		);
		(channel == 6 && MIDIChannel7Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel7Output,msg23); //Pass thru.
		);
		(channel == 7 && MIDIChannel8Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel8Output,msg23); //Pass thru.
		);
		(channel == 8 && MIDIChannel9Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel9Output,msg23); //Pass thru.
		);
		(channel == 9 && MIDIChannel10Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel10Output,msg23); //Pass thru.
		);
		(channel == 0 && MIDIChannel11Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel11Output,msg23); //Pass thru.
		);
		(channel == 1 && MIDIChannel12Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel12Output,msg23); //Pass thru.
		);
		(channel == 2 && MIDIChannel13Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel13Output,msg23); //Pass thru.
		);
		(channel == 3 && MIDIChannel14Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel14Output,msg23); //Pass thru.
		);
		(channel == 4 && MIDIChannel15Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel15Output,msg23); //Pass thru.
		);
		(channel == 5 && MIDIChannel16Output >= 0) ?
		(
			midisend(offset,status + MIDIChannel16Output,msg23); //Pass thru.
		);
	);
);