Custom module: "MIDI note message at x velocity" → "widget value"

I'd like to use a custom velocity note message to set the value of a modal widget to 1. I used the Math.abs() so that every time the message is received, the value of "AppleScript - KB1" modal alternates between 0 and 1. What am I doing wrong?

oscInFilter: function(data) {
        if (data.host === 'midi' && data.port === 'SessionKiano' && address == '/note' && args === [2, 94, 3]) {
        set('AppleScript - KB1', Math.abs(get('AppleScript - KB1')-1))
        }
}

That's wrong because a) args contains {type, value} objects, not values, and b) How to Compare Two JavaScript Arrays

1 Like

I rewrote the input filter and now it looks like this:


oscInFilter: function(data) {

        if (data.host === 'midi' && data.port === 'SessionKiano') {
            
            var {address, args, host, port} = data
            
            if (args[0].value == 2 && args[1].value == 94 && args[2].value == 3) {
            
            // In the scripting property of the button push widget I would write: set('AppleScript - KB1', Math.abs(get('AppleScript - KB1')-1))
            receive('AppleScript - KB1', 1, 1) // should change the value of the widget to 1, but it's not...
            console.log("Hello!")
            return           

            }
        }
        return data       
}

The console prints the message "Hello!", but the widget in the client doesn't receive the value change.

(widget - inspector screenshot)

receive takes an address as first argument, not an id.

1 Like

1) About the custom module

Isn't the variable aaa supposed to be stored somewhere? I now it's not stored because everytime the console.log() function is triggered, just ones are displayed in the console (trigger 1 = 1, trigger 2 = 1, trigger 3 = 1, etc).

if (data.host === 'midi' && data.port === 'SessionKiano') {
            
            var {address, args, host, port} = data
            var aaa = 0
            
            if (args[0].value == 2 && args[1].value == 94 && args[2].value == 3) {
            
            aaa = Math.abs(aaa-1)
            receive('/AppleScript - KB1', 1, aaa)
            console.log(aaa) // every time the MIDI message is received, the console prints only ones (1, 1, 1, 1, ...)
            
            return
           
            }
        }
(GIF)

Absolute value

2) About the documentation

You wrote (here):

Shouldn't be {type, value} instead?

It looks to me me you're not doing anything with the aaa variable after declaring it. That's why it just prints the same thing over.

But take what I say with a pinch of salt!

1 Like

But I am!

I'm doing this:

The problem is that aaa resets itself (it returns to 0).

I mean it doesn't look you're updating the aaa variable at all.

First console print (first trigger):
0 (initial value of aaa) minus 1 equals -1.
Absolute value of -1 is 1, so here aaa is 1 (first trigger should print 1).

Second console print (second trigger):
Now aaa is 1 (it should be, at least, if it was somehow stored / made persistent).
So, this time I should have 1 (new value of aaa) minus 1.
Absolute value of 0 is 0, isn't it? If it is, the second trigger should print 0 (not 1 again).

This is my source of the concept (11:23):

You're reinitialising the aaa variable to 0 each time though.

Try initialising the variable above the first if statement?

1 Like

You're right! I've tried at first to initialise the variable in a different place, but I was always doing it inside the module.exports code block. Now it works! Hurray! :partying_face:

(because I've initialised the variable before the module.export block)

It's all about scope: var - JavaScript | MDN

Shouldn't be {type, value} instead?

There's no difference between {type:"i", value: 1} and {value: 1, type:"i"}

1 Like