Home › Forums › General › Programming › preset number as index for array
- This topic has 12 replies, 2 voices, and was last updated 11 years, 2 months ago by SWB.
-
AuthorPosts
-
February 25, 2013 at 5:04 pm #6652
My Lua script gets a byte which contains a preset number. Then I call a function which uses this number as an index for an array containing the names of presets/sounds. In the process, I guess, I have to convert this “byte value” to an integer. How do I achieve this? My code so far looks like this:
— Called when a panel receives a midi message (does not need to match any modulator mask)
midiReceived = function(midi)
mt = midi:getType()
if mt == 7 then
index = midi:getData():getByte(1)
end
soundtable(index)
endOf course I’m probably overlooking the obvious, but all MY conversion attempts fail…
February 25, 2013 at 5:09 pm #6653it is a number, lua has no types, anything can be a key in a table (even functions can be keys) have a look at: http://lua-users.org/wiki/TablesTutorial
February 25, 2013 at 5:26 pm #6654That was also what i understood from your earlier answers to similar posts, BUT Ctrlr crashes on my Mac with this script. Only when I hard code a (integer) value to this variable ‘index’ then the code executes OK.
February 25, 2013 at 5:31 pm #6655what’s
soundtable(index)
it looks like you are calling a function with a parameter index
Also you’re code might pass “nil” to that function
if mt == 7 then index = midi:getData():getByte(1) end soundtable(index)
what if mt != 7 ?
you should at least initialize “index” to some default value so you don’t get bad data, or do:
if mt == 7 then
index = midi:getData():getByte(1)
soundtable(index)
endFebruary 25, 2013 at 5:47 pm #6656soundtable(index) calls the function soundtable() and passes the value of ‘index’ to this function. In this function I use the value for ‘index’ as an index to an array of preset names. In my script I actualy don’t use ‘index’ as a variable name, but I thought to make it more clear by using this name.Yes, it is sloppy code up to now, but this was just an example. When testing this code I’m 100% sure ‘mt’ and ‘index’ get values (using the input midi monitor to verify this).
This is the code for the soundtable() function, with the idea of displaying preset names in the lcd-component:
function soundtable(index)
sounds = {“Piano1″,”Piano2″,”ElGrand1″,”Rhodex1″,”Wurlitz1″,”DXPiano1″,”Clavin.1″,”Vibes”,”Harpsi1″,”Organ1″,”Pipe1″,”Strings”,”Choir”,”Mutesynt”,”StrBell”,”A.Bass”,”RockPian”,”HonkyTnk”,”ElGrand2″,”Rhodex2″,”Wurlitz2″,”DXPiano2″,”SynClavi”,”Marimb”,”Harpsi2″,”Organ2″,”Pipe2″,”Atkstrin”,”AtkChoir”,”SlowSynt”,”SlowBell”,”E.Bass”}
lcd_basic_text1 = “1.”
lcd_basic_text2 = “2.”
panel:getComponent(“LCD”):setComponentText(lcd_basic_text1..sounds[index]..” -Pg “..”\r”..lcd_basic_text2..sounds[index+1])
end
February 25, 2013 at 6:02 pm #6657I checked your code and it seems to work, i simplified the function:
function soundtable(index) sounds = {"Piano1","Piano2","ElGrand1","3123" } lcd_basic_text1 = "1. " lcd_basic_text2 = "2. " panel:getComponent("LCD"):setComponentText(lcd_basic_text1..sounds[index].." -Pg ".."\r"..lcd_basic_text2..sounds[index+1]) end
and when i cann it from the lua console, like so it wokrs (no output here cause the LCD label changed it’s content):
>>> soundtable(1) >>> mb = MemoryBlock({0x00, 0x01, 0x02, 0x03}) >>> index = mb:getByte(1) >>> soundtable(index) >>> soundtable(index-1) -- that's ok it shouldn't work there is no index==0 in the table ERROR: [string "function soundtable(index)..."]:6: attempt to concatenate field '?' (a nil value) >>> soundtable(index+1) >>> soundtable(index)
February 25, 2013 at 8:24 pm #6659Thanks for confirming this! Now that I know it works, I will try to find out why somewhere in the process an error comes up which crashes Cltrlr on my Mac. (I’m not able to check this code on Windows, because I cannot get my Midi to work on Windows :-(. Thanks again for your help, as always much appreciated!
February 26, 2013 at 5:17 pm #6670I found the cause of the problem. When the function soundtable() receives the value ‘0’ (zero) then Ctrlr (on Mac) crashes. Of course I could have known this, but thought the crash had other reasons and not this ‘simple’ cause of calling an array with a wrong value ‘0’… So what could be the reason why this version of Ctrlr (r1312 on Mac) crashes on errors instead of telling me which method is causing the error and then let me continue? (I asked this several times now…)
February 26, 2013 at 5:26 pm #6671If you are trying to access a piece of memory that’s not valid in Ctrlr, Lua will crash. There is no sandbox, VM whatever withing Lua. You are operating on the same memory segments as Ctrlr, and you need to obey the same rules as Ctrlr does. If Lua is causing a crash, Ctrlr will crash. It will be able to tell you about errors it can revive from, but if those are critical (like memory corruptions) there will be no warning/info, it will crash.
I’ll check and see on the Mac if some build settings are causing some additional conditions why Lua would crash more then on Windows. I’m not a Mac person myself so i assumed the default values i used for compiling Ctrlr on OSX are ok. But i might be wrong. Someone with more MAC experience would have to help with that.
February 26, 2013 at 5:27 pm #6672Can you also give me the exact OSX version you are using ?
February 26, 2013 at 5:49 pm #6674Thanks. See attached screen print. If needed I can send also the crash report, but I guess it is not of much use to you. I wished I could offer more help, but I can’t, this is a bit too much over my head! (I thought this guy ‘TK’ (Thorsten?) was quite experienced in compiling for Mac…).
Attachments:
You must be logged in to view attached files.February 26, 2013 at 5:49 pm #6676What i would do if i were you is a simple test like that, write a simple method:
function checkTable(index) -- Your method code here myTable = { "position 1", "position 2", "position 3" } str = myTable[index] debug (what (str)) console (str) end
and in the Lua console, play around with some vars, i tried this on a Linux build and i was never able to crash it, with calls like
checkTable(0) checkTable(33) checkTable(2)
it never crashed it just told me that str was [nil] and that it couldn’t execute console() with that parameter (since console takes a String as a parameter), but no reason to crash.
I imagine one of the other calls (maybe the setComponentText()) or some other is causing the crash since it’s calling the Lua API in Ctrlr and not just the native Lua stuff.
February 26, 2013 at 6:00 pm #6677Thanks again. I will try this!
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.