Midi send from Lua

Home Forums General General MIDI discussion Midi send from Lua

Tagged: , , ,

Viewing 20 posts - 1 through 20 (of 21 total)
  • Author
    Posts
  • #70516
    fundorin
    Participant
      • Topics: 8
      • Replies: 66
      • Total: 74

      I want Lua script to send midi Control Change message when the particular OSC message is received.
      I can get the message and match it successfully, but can’t find any info of how to simply send midi from script.

      What I have now is this:

      		if oscMsg:match('/repeat') then	
      			midiOut = (CC, value, channel)
      			sendMidiNow(midiOut)
      

      What is a proper command set to send midi command to the device, which is set in midi settings?

      This is doesn’t work, either:
      panel:sendMidiMessageNow(CtrlrMidiMessage(CC,value,channel))

      #70517
      Possemo
      Participant
        • Topics: 14
        • Replies: 638
        • Total: 652
        • ★★★

        CtrlrMidiMessage just has one argument, the message. I think they have to be hex numbers. So e.g. pnr has a value range from “00” to “7f” for this program change message:
        panel:sendMidiMessageNow(CtrlrMidiMessage(“C0”..pnr))

        I convert numbers into hex strings before. Here a sysex message:

        ANr=35
        Tone=string.format(“%.2x”, ANr)
        panel:sendMidiMessageNow(CtrlrMidiMessage(“f0 41 39 00 24 30 01 1d”..Tone..”f7″))

        #70518
        fundorin
        Participant
          • Topics: 8
          • Replies: 66
          • Total: 74

          Thanks for a suggestion, but I can’t beleive that there’s no easier way to send midi messages in Ctrlr, besides hex values. I believe, there must be an easier way, like decimal values for CC,value,channel.
          I will search through source for it, tomorrow and will report back if no one would suggest an easier way.

          #70519
          Possemo
          Participant
            • Topics: 14
            • Replies: 638
            • Total: 652
            • ★★★

            I have to say that I never tried to send anything else than hex with panel:sendMidiMessageNow(CtrlrMidiMessage(…))
            as it is very easy to convert a number into a hex string with
            string.format(“%.2x”, number). It would be worth a try to look what happens when sending something else.

            you usually use hex notation for MIDI because it is closer to binary :

            https://ccrma.stanford.edu/~craig/articles/linuxmidi/misc/essenmidi.html

            #70521
            fundorin
            Participant
              • Topics: 8
              • Replies: 66
              • Total: 74

              I’ve tried this code, but it doesn’t output anything to midi-ox, though it doesn’t produce errors when script is executed.
              Am I missing some important arguments, trying to send CC message 40 (decimal) with value 1 (decimal) and channel 15 (decimal) to midi out port? Message type, maybe?

              
              midiMsg = string.format('%.2x', transport[5])	
              midiValue = string.format('%.2x', oscValue)	
              midiChannel = string.format('%.2x', channel)
              panel:sendMidiMessageNow(CtrlrMidiMessage(midiMsg..oscValue..channel))
              
              #70522
              fundorin
              Participant
                • Topics: 8
                • Replies: 66
                • Total: 74

                I’ve added this function:

                Which gives this output, when used:

                Using this function in script to send midi to output:

                Doesn’t work.

                It’s very hard for me to write a working script in Ctrlr without a proper documentation.

                #70523
                Possemo
                Participant
                  • Topics: 14
                  • Replies: 638
                  • Total: 652
                  • ★★★

                  There is a builtin midi monitor in Ctrlr. Does the monitor shows something?

                  you can show values in the Lua console too, like this (it has to be a string):

                  console("Midi Message: "..midiMsg)

                  #70524
                  proton
                  Participant
                    • Topics: 19
                    • Replies: 94
                    • Total: 113
                    • ★★

                    Sysex start and end are missing and by oscValue you mean midiValue

                    midiMsg = string.format(‘%.2x’, transport[5])
                    midiValue = string.format(‘%.2x’, oscValue)
                    midiChannel = string.format(‘%.2x’, channel)

                    panel:sendMidiMessageNow(CtrlrMidiMessage(“f0”..midiMsg..midiValue..midiChannel..”f7″))

                    • This reply was modified 7 years, 3 months ago by proton.
                    • This reply was modified 7 years, 3 months ago by proton.
                    #70525
                    fundorin
                    Participant
                      • Topics: 8
                      • Replies: 66
                      • Total: 74

                      Monitor doesn’t show anything (both input and output enabled in monitor).

                      Is it necessary to use sysex even for CC messages?

                      #70528
                      fundorin
                      Participant
                        • Topics: 8
                        • Replies: 66
                        • Total: 74

                        Thanks proton. Sysex is sent now, but I don’t want it to be a sysex message, just a regular CC message. In midi-ox blue lines show when I send regular CC via panel’s modulator and yellow/grey is the same message in sysex format, using your suggestion, though I doubt, that it’s the same. 🙂

                        View post on imgur.com

                        #70529
                        proton
                        Participant
                          • Topics: 19
                          • Replies: 94
                          • Total: 113
                          • ★★
                          #70530
                          Possemo
                          Participant
                            • Topics: 14
                            • Replies: 638
                            • Total: 652
                            • ★★★

                            I think you got the CC message format wrong. It should look like this:

                            B0 40 10

                            First “B” tells that this is a CC message, second 0 tells this is on midi channel 1, third 40 tells this is CC Nr. 40 (in hex) and last 10 is the value of the CC (in Hex)

                            #70531
                            fundorin
                            Participant
                              • Topics: 8
                              • Replies: 66
                              • Total: 74

                              Thank you. Mine is also working, but I want regular CC message type, and not Sysex (blue messages in midi-ox).

                              #70532
                              proton
                              Participant
                                • Topics: 19
                                • Replies: 94
                                • Total: 113
                                • ★★

                                — Called when a modulator value changes
                                — @mod http://ctrlr.org/api/class_ctrlr_modulator.html
                                — @value new numeric value of the modulator

                                sendTest = function(–[[ CtrlrModulator –]] mod, –[[ number –]] value, –[[ number –]] source)

                                transport = 10
                                oscValue = 10
                                channel= 10

                                CC = “B0”
                                midiMsg = string.format(“%.2x”, transport)
                                midiValue = string.format(“%.2x”, oscValue)
                                midiChannel = string.format(“%.2x”, channel)

                                panel:sendMidiMessageNow(CtrlrMidiMessage(“f0”..CC..midiMsg..midiValue..channel..”f7″))

                                end

                                #70533
                                fundorin
                                Participant
                                  • Topics: 8
                                  • Replies: 66
                                  • Total: 74

                                  Possemo, you’re right!
                                  I can now send CC messages to channel 16!

                                  View post on imgur.com

                                  #70534
                                  proton
                                  Participant
                                    • Topics: 19
                                    • Replies: 94
                                    • Total: 113
                                    • ★★

                                    in CC = “B0”
                                    “B” is the code for CC and “0” is channel Nr.1
                                    same as 90 would be 9 – NoteOn massage and 0 channel 1
                                    or 82 would be 8 – Note Off massage and 2 – means channel 3

                                    clear?

                                    #70535
                                    fundorin
                                    Participant
                                      • Topics: 8
                                      • Replies: 66
                                      • Total: 74

                                      Proton, f0..f7 isn’t necessary when sending CC messages. Thank you both!

                                      #70536
                                      fundorin
                                      Participant
                                        • Topics: 8
                                        • Replies: 66
                                        • Total: 74

                                        This is the final code to form CC from given arguments:

                                        
                                        function setCC(msg, value, channel)
                                        		midiType = "B" -- ControlChange
                                        		midiChannel = string.format('%x', channel)	
                                        		midiMsg = string.format('%.2x', msg)	
                                        		midiValue = string.format('%.2x', value)	
                                        return midiType..midiChannel..midiMsg..midiValue
                                        end
                                        

                                        I’ll now add a wrapper function to shorten
                                        panel:sendMidiMessageNow(CtrlrMidiMessage(midiMessage))
                                        to something like sendMidi(midiMessage)

                                        • This reply was modified 7 years, 3 months ago by fundorin.
                                        #70538
                                        proton
                                        Participant
                                          • Topics: 19
                                          • Replies: 94
                                          • Total: 113
                                          • ★★

                                          Hahaha, yes, sure the page wasn’t refreshing so I couldn’t see Possemo already took initiative!
                                          Glad we could help.

                                          BTW – Thanks for the OSC tip, all is working now I just had OSC monitor using the same port as Ctrlr hence the trouble but once I switched it off all is good.
                                          Will check the string issue later today.
                                          Cheers!

                                          • This reply was modified 7 years, 3 months ago by proton.
                                          #70540
                                          fundorin
                                          Participant
                                            • Topics: 8
                                            • Replies: 66
                                            • Total: 74

                                            In case someone else would need this in future, sendMidi function looks like this:

                                            
                                            function sendMidi(midiMessage)
                                            	panel:sendMidiMessageNow(CtrlrMidiMessage(midiMessage))
                                            end
                                            

                                            setCC function:

                                            
                                            function setCC(msg, value, channel)
                                            	midiType = "B" -- ControlChange
                                            	midiChannel = string.format('%x', channel)	
                                            	midiMsg = string.format('%.2x', msg)	
                                            	midiValue = string.format('%.2x', value)	
                                            return midiType..midiChannel..midiMsg..midiValue
                                            end
                                            

                                            Code to send midi out, if incoming OSC message is matching the pattern:

                                            
                                            if oscMsg:match('/repeat') then	--oscMsg is path from OSC message
                                            	midiMessage = setCC(transport[5], oscValue, channel)
                                            	sendMidi(midiMessage)
                                            

                                            transport[5] contains midi CC number in decimal format (repeat button on my midi controller)
                                            oscValue changes between 1 and 0, depending of repeat button state in Reaper.
                                            global variable channel set to 15 (16 channel – 1)

                                          Viewing 20 posts - 1 through 20 (of 21 total)
                                          • The forum ‘General MIDI discussion’ is closed to new topics and replies.
                                          There is currently 0 users and 58 guests online
                                          No users are currently active
                                          Forum Statistics
                                          Threads: 2,495, Posts: 17,374, Members: 77,605
                                          Most users ever online was 12 on January 22, 2019 3:47 pm
                                          Ctrlr