Send a specific Midi command from a button based on the value selected in a Switch

I have tried to write a custom module for this but cannot get the button to send.

I want to select a variable from a switch that is inside a modal widget, and based on the switch value chosen, use this value to “prime” a button with a midi command that will be sent when the button is pressed.

Credit must go to @DMDComposer and JoeHidden at ViControl for posting their code and templates to get me started but I just can’t get this bit to work. I have commented out the if statements for copy or not on the transpose just for testing but regardless of what I do the b and # buttons just don’t do anything!

// Variables for Transpose Module

var transposeInterval = 0;
var transposeCopy = false;
var oscHost = '127.0.0.1';
var oscPort = '8080';

        module.exports = {
            init: function () {
                  //sendOsc({address: '/bg1', args: [{type: 's', value: 'ff0000'}], host: oscHost, port: oscPort})
                //receive('/EDIT', 'bg1', { value: 'ff0000' })
            },

            oscOutFilter: function (data) {
                // Filter incomming osc messages
                var { address, args, host, port } = data
      
                if (address === '/transpose') {
                    switch (args[0].value) {
                        case 'up':
                           // if (transposeCopy) {
                              //  sendOsc({ address: '/control', args: [{ type: 'i', value: 8 }, { type: 'i', value: 24 + transposeInterval }, { type: 'i', value: 127 }], host: 'midi', port: 'oscIn' });
                           // } else 
                           {
                                sendOsc({ address: '/control', args: [{ type: 'i', value: 8 }, { type: 'i', value: 0 + transposeInterval }, { type: 'i', value: 127 }], host: 'midi', port: 'oscIn' });
                            }
                            break;
                        case 'down':
                           // if (transposeCopy) {
                             //   sendOsc({ address: '/control', args: [{ type: 'i', value: 8 }, { type: 'i', value: 36 + transposeInterval }, { type: 'i', value: 127 }], host: 'midi', port: 'oscIn' });

                           // } else 
                           {
                                sendOsc({ address: '/control', args: [{ type: 'i', value: 8 }, { type: 'i', value: 12 + transposeInterval }, { type: 'i', value: 127 }], host: 'midi', port: 'oscIn' });
                            }

                            break;  
                    }
                    return
                }
        
                if (address === '/transposeInterval') {

                    transposeInterval = args[0].value;
                    new_label = ["minor Second", "Major Second", "minor Third", "Major 3", "Perfect 4th", "TT", "Perfect 5", "minor 6", "Major 6", "minor 7", "Major 7", "Octave"];
                   receive('/EDIT', 'modal_transpose_interval', { type: 's', value: { 'label': new_label[args[0].value] } });
                    receive('/modal_transpose_interval', 0);
                    return;
                }

               // if (address === '/transposeCopy') { transposeCopy = args[0].value; console.log('Transpose Copy = ' + args[0].value); return; }
                return data
            }
        }

I think already following some more investigations - this may be because I am using the beta version? Maybe?

The code seems fine, you need to make sure of 2 things for it to work:

  • that the button sending “/transpose up|downactually sends a message otherwise it won’t be catched by the custom module.
  • that the midi device “oscIn” has been properly declared when setting up the server

Using the console.log function can help debugging custom modules, and enabling the server’s debug option is useful to check what’s actually sent/received by OSC.

On a side note, since you are using v1, you might be able to use the button’s script property to do that.

Thanks for coming back to me

I think that this may well be the problem - I have put the console.log line to the end of the code again and now the console gives the following:
(DEBUG, OSC) In: {
address: '/EDIT',
args: [ 'modal_transpose_interval', { label: 'M6' } ]
} From: undefined:undefined
(DEBUG, OSC) In: { address: '/modal_transpose_interval', args: 0 } From: undefined:undefined
(DEBUG, OSC) Out: { address: '/transposeDown', args: [ { type: 'i', value: NaN } ] } To: 192.198.0.23:8080
Transpose Copy = NaN

As you can see I get NaN as the value returned. Now I suspect this may be because I don't think the widget is sending a number but a true/false condition, but if I run this in the earlier OSC version (ie the non-beta) then this doesn't happen.

Very interested in this, not only for this bit of my conroller but also another area - are there any tutorials or examples for doing this?

Thanks again for all your help - I'm trying not to be the sort of person who jsts comes and says do this for me I can't be bothered to learn, or RTFM but my scripting skills are severely limited!

Casting a non-number argument to an integer automatically is not something you should rely on, instead you could convert it easily to avoid any trouble.

Very interested in this, not only for this bit of my conroller but also another area - are there any tutorials or examples for doing this?

There are some on the forums but not much, I started working on small examples but haven't finished them yet.

1 Like