Old 03-05-2017, 03:46 PM   #1
airon
Human being with feelings
 
airon's Avatar
 
Join Date: Aug 2006
Location: Berlin
Posts: 11,817
Default Midi messages command queue

Just a quick one.

I wrote a bunch of little scripts to do the following, simple things :

  • Send a midi command to one device to change its bank(Midi Fighter Twister in this case)
  • Light up one in a group of four buttons and clear the others


The basic command to do this is reaper.StuffMIDIMessage.

I hope this is useful to someone. It certainly helps me mix more quickly.

One version without comments, the second has lots of comments in it, which is what I prefer..

Code:
local m_com = {
          {   device = "Twister1", -- will be replaced with  target device number+16 for actual command
              midicmd = '0x9'..string.format("%x", 3), -- NoteOn on midi channel 0-15
              midinotecc = 0 , -- bank no.1
              vel = 127 }, -- velocity or CC value

          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0),
              midinotecc = 8 ,
              vel = 1 },
          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0),
              midinotecc = 9 ,
              vel = 0 },
          {  device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0),
              midinotecc = 10 ,
              vel = 0 },
          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0),
              midinotecc = 11 ,
              vel = 0 }
}
local target_devices = {} ; local i,k = 0 , 0 ; local mdev_outno = reaper.GetNumMIDIOutputs()

for k=1, #m_com, 1 do ; target_devices[k] = m_com[k].device ; end

for k=1, #target_devices, 1 do
  for i=1, mdev_outno, 1 do
    retval, mdev_name = reaper.GetMIDIOutputName(i, "")
    if retval then
      if string.find(mdev_name,target_devices[k]) then -- check for target device name(s)
        m_com[k].target_devnum = i -- add device number to command list
      end
    end
  end
end

for i=1, #m_com, 1 do
  reaper.StuffMIDIMessage( tonumber(m_com[i].target_devnum)+16 , m_com[i].midicmd, m_com[i].midinotecc, m_com[i].vel )
end



Code:
local m_com = {
  -- Access to commands via m_com[1] and so on
  -- m_com[k].target_devnum will hold the devices number for each command
  -- Both devices in this example are renamed from their default names to something small and specific
  -- "Midi Fighter Twister" became "Twister1" because I have two of them. The X-Touch Mini was renamed to have shorter name

  -- Twister1, NoteOn channel 4, Note 3(bank no.4), velocity 127
          {   device = "Twister1", -- will be replaced with  target device number+16 for actual command
              midicmd = '0x9'..string.format("%x", 3), -- NoteOn on midi channel 0-15
              midinotecc = 0 , -- bank no.1
              vel = 127 }, -- velocity or CC value
  -- X-TOUCH MINI, NoteOn channel 11, Note , velocity 1 (button ON)
  -- Turn OFF other buttons
  -- NOTE for X-Touch Mini: Buttons have to be in TOGGLE mode, set via the editor
          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0), -- NoteOn on midi channel 1
              midinotecc = 8 , -- Bottom row, button 1 from the left
              vel = 1 }, -- button ON
          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0), -- NoteOn on midi channel 1
              midinotecc = 9 , -- Bottom row, button 2 from the left
              vel = 0 }, -- button OFF
          {  device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0), -- NoteOn on midi channel 1
              midinotecc = 10 , -- Bottom row, button 3 from the left
              vel = 0 }, -- button OFF
          {   device = "Xmini1",
              midicmd = '0x9'..string.format("%x", 0), -- NoteOn on midi channel 1
              midinotecc = 11 , -- Bottom row, button 4 from the left
              vel = 0 }, -- button OFF
}
local target_devices = {}
local i,k = 0 , 0
local mdev_outno = reaper.GetNumMIDIOutputs() -- grab number of midi outputs in Reaper

-- collect device ids for each command
for k=1, #m_com, 1 do
  target_devices[k] = m_com[k].device
end

for k=1, #target_devices, 1 do
  for i=1, mdev_outno, 1 do
    retval, mdev_name = reaper.GetMIDIOutputName(i, "")
    if retval then
      if string.find(mdev_name,target_devices[k]) then -- check for target device name(s)
        m_com[k].target_devnum = i -- add device number to command list
      end
    end
  end
end
-- Now we have the current device ID for each device we'll be targeting, so away we go.
for i=1, #m_com, 1 do
  reaper.StuffMIDIMessage( tonumber(m_com[i].target_devnum)+16 , m_com[i].midicmd, m_com[i].midinotecc, m_com[i].vel )
end
__________________
Using Latch Preview (Video) - Faderport 16 setup for CSI 1.1 , CSI 3.10
Website
"My ego comes pre-shrunk" - Randy Thom

Last edited by airon; 03-06-2017 at 04:47 AM.
airon is offline   Reply With Quote
Old 03-07-2017, 03:07 AM   #2
airon
Human being with feelings
 
airon's Avatar
 
Join Date: Aug 2006
Location: Berlin
Posts: 11,817
Default

For any script writers, I added an almost comment-less version.

It makes sending stacks of midi commands to a target by device name, which you can change in the Preferences/Midi Devices section for each input and output device, a little easier.

If you have any questions, ideas or requests, write them down here.
__________________
Using Latch Preview (Video) - Faderport 16 setup for CSI 1.1 , CSI 3.10
Website
"My ego comes pre-shrunk" - Randy Thom
airon is offline   Reply With Quote
Old 03-07-2017, 06:00 AM   #3
mccrabney
Human being with feelings
 
mccrabney's Avatar
 
Join Date: Aug 2015
Posts: 3,671
Default

thanks for this, this kind of thing is going to be important moving forward and applies well to stuff like the launchpad.

is there any library that would allow for midi to be scripted in such a way that doesn't require entering the values in hex?

off topic, but is there any way to test whether or not a function is enabled (global input quantize for example) and blast a midi value if so?
__________________
mccrabney scripts: MIDI edits from the Arrange screen ala jjos/MPC sequencer
|sis - - - anacru| isn't what we performed: pls no extra noteons in loop recording
| - - - - - anacru|sis <==this is what we actually performed.
mccrabney is offline   Reply With Quote
Old 03-08-2017, 02:52 AM   #4
airon
Human being with feelings
 
airon's Avatar
 
Join Date: Aug 2006
Location: Berlin
Posts: 11,817
Default

That's a good question. You may want to put that question in to its own thread. Some midi folks here can probably answer that question if they see it.
__________________
Using Latch Preview (Video) - Faderport 16 setup for CSI 1.1 , CSI 3.10
Website
"My ego comes pre-shrunk" - Randy Thom
airon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.