desc:Chalisque MIDI velocity to Modwheel
//tags: MIDI processing

@init

@block

// mod wheel is cc1
// so when we receive a noteon,
// echo the noteon to output,
// either echoing the velocity value, or setting
// it to a standard value
// rather than use controls
// I prefer to have multiple scripts
// so the only controls are the ones I want to automate
while (
	midirecv(mpos,msg1,msg23) ? (
		// message type
		m=msg1&240; // picks out the high nibble (the F in 0xFE)
		// channel
		c=msg1&15; // picks out the low nibble (the E in 0xFE)
		// velocity
		vel=(msg23/256)|0;
		// midi pitch
		note=msg23&127;
		// is it a note on (note on with vel=0 is note off)
		m == 9*16 && vel > 0 ? (
			// note on event
			// we want to change the message from
			// 9XYYZZ (note on, where X is channel, YY is pitch, ZZ velocity)
			// to
			// BX01ZZ (cc, X is channel, 1 is modwheel, ZZ value)
			ccmsg1=0xB0|c; // construct first byte
			ccmsg23=(msg23&0xff00)|1; // construct last 2 bytes
			midisend(mpos,msg1,msg23); // echo note on
			midisend(mpos,0xB0|c,ccval); // output modwheel
		) : (
			midisend(mpos,msg1,msg23); // passthru
		);
	);
);