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.
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.
local function search_instrument_index_per_track()
local sti=song.selected_track_index --or the track index that you prefer
--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
--search inside all visible lines of pattern
for lne=1,pat_.number_of_lines do
--skip empty pattern-track
if (not trk_.is_empty) then
--skip empty lines
if (not lne_obj.is_empty) then
--convert object to string
--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))
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.