Home › Forums › General › General MIDI discussion › Midi send from Lua
- This topic has 20 replies, 3 voices, and was last updated 7 years, 3 months ago by fundorin.
-
AuthorPosts
-
December 8, 2016 at 10:21 pm #70516
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))December 9, 2016 at 12:09 am #70517CtrlrMidiMessage 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″))December 9, 2016 at 12:15 am #70518Thanks 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.December 9, 2016 at 8:20 am #70519I 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
December 9, 2016 at 12:29 pm #70521I’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))
December 9, 2016 at 12:44 pm #70522I’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.
December 9, 2016 at 12:48 pm #70523There 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)
December 9, 2016 at 12:53 pm #70524Sysex 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″))
December 9, 2016 at 12:53 pm #70525Monitor doesn’t show anything (both input and output enabled in monitor).
Is it necessary to use sysex even for CC messages?
December 9, 2016 at 12:57 pm #70528Thanks 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. 🙂
December 9, 2016 at 1:02 pm #70529it works OK here:
December 9, 2016 at 1:04 pm #70530I 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)
December 9, 2016 at 1:04 pm #70531Thank you. Mine is also working, but I want regular CC message type, and not Sysex (blue messages in midi-ox).
December 9, 2016 at 1:05 pm #70532— 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= 10CC = “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
December 9, 2016 at 1:08 pm #70533Possemo, you’re right!
I can now send CC messages to channel 16!December 9, 2016 at 1:08 pm #70534in 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 3clear?
December 9, 2016 at 1:09 pm #70535Proton, f0..f7 isn’t necessary when sending CC messages. Thank you both!
December 9, 2016 at 1:12 pm #70536This 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.
December 9, 2016 at 1:16 pm #70538Hahaha, 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.
December 9, 2016 at 1:20 pm #70540In 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) -
AuthorPosts
- The forum ‘General MIDI discussion’ is closed to new topics and replies.