Old 04-06-2020, 10:53 AM   #1
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default Insert SYSEX message

Hi I am trying to write a script that will insert a sysex message where the play head is. All of my code works to get the user message and parse it to what the system uses, but I can't get it to insert.

I am using MIDI_InsertTextSysexEvt in Lua

Lua: boolean reaper.MIDI_InsertTextSysexEvt(MediaItem_Take take, boolean selected, boolean muted, number ppqpos, integer type, string bytestr)

My code for this part is

take = reaper.MIDIEditor_GetTake(reaper.MIDIEditor_GetAct ive())
time = reaper.time_precise()
ppqpos = reaper.MIDI_GetPPQPosFromProjTime(take,time)

reaper.MIDI_InsertTextSysexEvt(take,true,false,ppq pos,1,sysexMessage)

It doesn't throw any errors. But nothing is inserted (the sysexMessage is higher up in the code and works fine, the variable contains the message correctly)

Any ideas?
I'm not a great coder, but I'm not completely without understanding.
Thanks in advance
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 01:34 PM   #2
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

time_precise() gives you the time of day. You want the project timeline time, as given by GetCursorPosition() or similar.
schwa is offline   Reply With Quote
Old 04-06-2020, 01:36 PM   #3
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

well, now I feel dumb. Thank you.
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 01:40 PM   #4
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

As my grandpa used to say, the only dumb question is... that one you just asked.

(it wasn't a dumb question)
schwa is offline   Reply With Quote
Old 04-06-2020, 01:52 PM   #5
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

Ha ha ha. I like that. Thanks for your help
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 01:57 PM   #6
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

All of that works now, but nothing is placed in my midi track. Am I using the correct function to write a sysex message and put it in the take?
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 01:59 PM   #7
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

Yes, that should work. How are you constructing the message?
schwa is offline   Reply With Quote
Old 04-06-2020, 02:15 PM   #8
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

I take user input for what cue number they want which processes it into a string that reads
7F 01 02 01 01 31 35 2E 35 00 31

the 31, 35, 2E, 35 are the ones that change based on user input

This is the whole message without the F0 and F7 bounding boxes
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 02:30 PM   #9
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

I think you want bytestr to be "\x7F\x01\x02..."
schwa is offline   Reply With Quote
Old 04-06-2020, 02:31 PM   #10
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

That worked perfect.
Thank you so much. I really appreciate it.
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 02:36 PM   #11
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

Quote:
Originally Posted by schwa View Post
I think you want bytestr to be "\x7F\x01\x02..."
Exactly? with the slash and x?
what about spaces?
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 02:41 PM   #12
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

It's a byte string, not a string of printable characters. "\x7F\x01\x02" is a string of 3 bytes, starting with 7F. "7F 01 02" is a string of 8 bytes, starting with 0x37 which is the ascii code for the character '7'.
schwa is offline   Reply With Quote
Old 04-06-2020, 02:44 PM   #13
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

Ok, I think I understand.
I'll get on it
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 05:23 PM   #14
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

Ok, I suck. Do you have time to tell new how to concatenate a byte string.
There first part of my message is correct,
There user input part gets converted to the correct hex, but when i try to put then all together I get the correct information followed by random numbers.
Sorry. Thanks for your time
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 05:32 PM   #15
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

https://www.lua.org/manual/5.3/manual.html#3.4.6

schwa is offline   Reply With Quote
Old 04-06-2020, 05:40 PM   #16
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

The problem I'm getting is I have a static message of "\x7F\x01\x02..." and then the formatted message which is 31352E35 (15.5) and I can't get it into a "\x31\x35\x2E..." any idea?
LUX2152 is offline   Reply With Quote
Old 04-06-2020, 06:10 PM   #17
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

That's kind of advanced. You'll need to do something like a str:match to get the values out of the text string, then push them onto the bytestring.
schwa is offline   Reply With Quote
Old 04-06-2020, 08:39 PM   #18
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 2,812
Default

Code:
s_in = '31352E35'
s_out = ''
for char_pair in s_in:gmatch('..') do s_out = s_out..'\\x'..char_pair end
__________________
SoundCloud | MPL Scripts discussion | ReaPack | Donate
mpl is offline   Reply With Quote
Old 04-07-2020, 06:09 AM   #19
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,798
Default

Almost, but you can't construct a byte string by piecing together '\x' and values. You need to explicitly push the value on the the bytestring.

Code:
s_out = s_out..string.char(tonumber(char_pair,16))
char_pair is something like "33" or "1F"
tonumber(char_pair,16) interprets the two text characters as a hex value
string.char converts the number to a byte character

Last edited by schwa; 04-07-2020 at 10:31 AM.
schwa is offline   Reply With Quote
Old 04-07-2020, 09:49 AM   #20
LUX2152
Human being with feelings
 
Join Date: Apr 2020
Posts: 16
Default

Thank you so much for all your help. That worked and will make my programming of messages so much easier.
It comes out in the perfect format.
I still do not completely understand it, but I don't need to.
You guys are great!
LUX2152 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 05:50 PM.


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