preset number as index for array

Home Forums General Programming preset number as index for array

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #6652
    SWB
    Participant
      • Topics: 35
      • Replies: 157
      • Total: 192
      • ★★

      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)
      end

      Of course I’m probably overlooking the obvious, but all MY conversion attempts fail…

      #6653
      atom
      Keymaster
        • Topics: 159
        • Replies: 2945
        • Total: 3104
        • ★★★★★

        it 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

        #6654
        SWB
        Participant
          • Topics: 35
          • Replies: 157
          • Total: 192
          • ★★

          That 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.

          #6655
          atom
          Keymaster
            • Topics: 159
            • Replies: 2945
            • Total: 3104
            • ★★★★★

            what’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)
            end

            #6656
            SWB
            Participant
              • Topics: 35
              • Replies: 157
              • Total: 192
              • ★★

              soundtable(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

              #6657
              atom
              Keymaster
                • Topics: 159
                • Replies: 2945
                • Total: 3104
                • ★★★★★

                I 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)
                
                #6659
                SWB
                Participant
                  • Topics: 35
                  • Replies: 157
                  • Total: 192
                  • ★★

                  Thanks 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!

                  #6670
                  SWB
                  Participant
                    • Topics: 35
                    • Replies: 157
                    • Total: 192
                    • ★★

                    I 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…)

                    #6671
                    atom
                    Keymaster
                      • Topics: 159
                      • Replies: 2945
                      • Total: 3104
                      • ★★★★★

                      If 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.

                      #6672
                      atom
                      Keymaster
                        • Topics: 159
                        • Replies: 2945
                        • Total: 3104
                        • ★★★★★

                        Can you also give me the exact OSX version you are using ?

                        #6674
                        SWB
                        Participant
                          • Topics: 35
                          • Replies: 157
                          • Total: 192
                          • ★★

                          Thanks. 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.
                          #6676
                          atom
                          Keymaster
                            • Topics: 159
                            • Replies: 2945
                            • Total: 3104
                            • ★★★★★

                            What 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.

                            #6677
                            SWB
                            Participant
                              • Topics: 35
                              • Replies: 157
                              • Total: 192
                              • ★★

                              Thanks again. I will try this!

                            Viewing 13 posts - 1 through 13 (of 13 total)
                            • The forum ‘Programming’ is closed to new topics and replies.
                            There is currently 0 users and 92 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