Is there some api shortcut for "getInstrumentOfTrack()"?

Assuming each track only is used by one instrument, is there a api shorthand / native code for getting the instrument of a track? Iterating over all the pattern lines until a first instrument number appears, that seems to be very costly.

Since there is the options “Auto capture instrument from pattern”, this seems to be already implemented…

The instrument autocapture option works for multiple instruments within the same track and appears to work “by proximity” from top to bottom.

You can do a pretty quick search of the first index of the instrument using the text string of the line, and jumping all the empty lines before with a iterator for-do, keeping in mind only the visible note columns.

Sure, and good point with visible note columns, thx.

But I would like to have that said functionality as API function, because I would guess that it’s way faster than any lua code.

EDIT: So tracks[].visible_note_columns will contain only the visible columns?

Yes, returns you the index of visible note columns. If you have 3 columns, it will return the number 3.

You can also speed up the search by skipping the empty track patterns.

Skipping all that is empty the search can be quite fast.

You can cut the length of the text string for each line based on the number of visible note columns (calculate the number of characters). You can isolate the values referring to the index of each instrument, from left to right, and return the value of the index with a “return” within the iteration.

hm but the patterniterator only returns a note_column object, not a visible_note_column object. Kinda annoying…[].visible_note_columns, _observable
  -> [number, 0 OR 1-12, depending on the track type]
>>> print(
C-400......0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | ---........0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000

I guess this way is faster than using tables from all note_column ()

The most extreme case is 512 lines x 1000 patterns, if there is only one instrument index at the end of the song.

Too lazy for that, used patterniterator. Is the patterniterator also iterating empty PatternTracks? If so, why? Maybe not…

I don’t usually use that.

local function search_instrument_index_per_track()
  local x=os.clock()
  local sti=song.selected_track_index --or the track index that you prefer
  local vnc=song.selected_track.visible_note_columns
  --search inside all patterns (from top to bottom)
  for pat=1,#song.patterns do
    --skip empty patterns
    if (not song:pattern(pat).is_empty) then
      local pat_=song:pattern(pat)
      --search inside all visible lines of pattern
      for lne=1,pat_.number_of_lines do
        --skip empty pattern-track
        local trk_=pat_:track(sti)
        if (not trk_.is_empty) then
          --skip empty lines
          local lne_obj=trk_:line(lne)
          if (not lne_obj.is_empty) then
            --convert object to string
            local lne_string=tostring(lne_obj)
            --each note column has 18 characters, interest characters 4 and 5 of each.
            for char=4,18*vnc,18 do
              if (string.sub(lne_string,char,char+1)~="..") then
                print(string.format("search_instrument_index_per_track: %.2f ms\n",(os.clock()- x)*1000)) 
                --return tonumber(string.sub(lne_string,char,char+1),16)
                return string.sub(lne_string,char,char+1)

I just wrote this function for fun right now. You can try it on TestPad.lua. You can use this function adapted to any other index.

About the speed

This function returns the two-character value (string or number, with tostring), corresponding to the index of the first instrument that it detects from top to bottom.

In a song of 16 patterns of 64 lines each with 12 columns of visible notes, if you write a note at the end, it takes 0.98 ms with my CPU.
In practically any real practical case that you use this function, the delay time will be even shorter.

It would be interesting to see an equivalent function using pattern_iterator to check which one is faster.

Note: with a little more code it is possible to put all the detected instrument index of a track into a table.

1 Like