Is it possible to wait to run a midi trigger until the value has stopped changing?

Hello folks!

I'm using a simple 4 knob midi device to trigger a Shortcut (the capital S macOS built-in Shortcuts app kind) with the intention of adjusting volumes in Rogue Amoeba's SoundSource app, which has Shortcuts support. Basically I use a midi trigger in BTT which calls some applescript I wrote, which itself calls the Shortcut.

Here's the trigger:

Here's the applescript:

on bttMIDIValuechanged(midiValue)
	set shortcutName to "Firefox Volume"
	
	if (midiValue < 15) then
		set volumeLevel to 0
	else if (midiValue > 112) then
		set volumeLevel to 100
	else
		set volumeLevel to (midiValue / 127) * 100
	end if
	
	tell application "Shortcuts Events"
		activate
		run the shortcut shortcutName with input volumeLevel
	end tell
end bttMIDIValuechanged

The problem I'm running into is that Shortcuts is kind of slow, so basically it will sometimes adjust the volume out of order. I've mitigated this slightly by setting the trigger to only run when the value is changed by 15 or more, but this is often still too fast for Shortcuts to keep up, and will result in my volume jumping around wildly. Here's a gif of what it looks like when I turn the knob from 0 to 100 to see what I'm talking about:

Capture 2022-06-07T124905

You can see in this particular example that it looks like Shortcuts processed the set volume to 27% command last, so while I tried to adjust the volume to 100%, that didn't work out. I think this is what programmers who actually know what they are doing may call a race condition :slight_smile:

Can anyone think of a better way for me to set this up to? It would be nice if BTT had an option to sort of wait for the midi value to stay constant for a second, but maybe there is something I could do in my applescript to improve this.

Interesting use case!

One approach you could try is instead of passing volumeLevel to Shortcuts, just save its value inside BTT as an internal variable.

Then from inside the Shortcut, use a Run AppleScript or JavaScript to retrieve the current value of the variable (you'd need to enable external scripting in BTT) and use the result in the SoundSource Set Source Volume action.

Then if the knob has changed in the time taken to start running the Shortcut, it should at least use a more recent value.

1 Like

Alright, that makes sense, I'll try that.

Thanks!

I'm trying to do something similar (using MIDI fader to control system volume). Did you solve the problem?

Unfortunately, no I never quite got it working. I ended up getting a Stream Deck plus when that came out instead, and I use that with the wave link software.

That being said if you want to control system volume I think you wouldn't need to roundtrip things through Shortcuts like I was trying to do. I believe you should be able to have BTT adjust system volume directly which should be faster and less error prone I bet.

In theory something like this should work for triggering after a certain timeout: