Endless MIDI knob

Hi there Supercollider list, long time no see.

Has anyone written some code that turns a MIDI endless knob into an endlessly increasing (and decreasing) counter?

Etienne

you can use an action function that keeps the previous value and adds the difference:

var prev;
var current = 0;
f = { |val|
	var diff;
	if(prev.notNil) {
		diff = val - prev;
		prev = val;
		current = current + diff;
	};
	
};

One note – and the main reason why I recognize this bug is because I’ve replicated it accidentally in my own code likely a hundred times – prev must be non-nil before it’s assigned a value, but it will never be non-nil until it’s assigned a value. So this function will never do anything.

Should move prev = val to be after the if block, not within it.

I’ve done this myself way too many times! And it’s a smack-my-head feeling, every time :laughing:

hjh

2 Likes

Here, & let us know if you have any more questions.

((
	x = 0;
	
	MIDIdef 
	(
		key:
		(
			\x
		)
		,func:
		{
			|val n| // Post <<<* [ val , n ] << $\n ;
			
			postln
			
			(
				\x -> 
				
				(
					x = x +
					
					(
						if
						
						(
							val > 0
						)
						
						/* -> */
						
						{
							1		
						}
						
						/* <- */ 
						
						{
							-1			
						}
					)
				)
			)
		}
		,msgNum: 
		(
			..127
		)  
		,msgType:
		(
			\control
		)
	)
	.permanent_ ( true )  // CmdPeriod removes def if false by default
))  

// MIDIIn.doControlAction(num: 11 , val: 127)

// fork { doControlAction( MIDIIn , num: 11 , val: 127 ) ; yieldAndReset ( 0.01 ) }