[Solved] Help: jump to last instrument in Instrument box | Code for wr

I have created another crazy function related to insert a new metronome…

I need the code to jump to the last instrument. The API does not seem to have a specific code and seems to need to iterate?Do you have an example of the exact code?

Selecting a particular instrument number is simple: song.selected_instrument_index = 10 (jumps to instrument 10, range 1 to 255) Returning the total number of instruments available would be sufficient.How do I get this number?

function amic_CH03_4A() --CREATE METRONOME AND CONFIGURE
  local song, app = renoise.song(), renoise.app()
  local sid0 = song.selected_track_index -- start
  song:insert_track_at(song.sequencer_track_count + 1)
  song.selected_track_index = song.sequencer_track_count
  local sid = song.selected_track_index
  song.tracks[sid].name = "Metronome"
  song.tracks[sid].color = {00,00,00}
  song.tracks[sid].color_blend = 50
  song.tracks[sid].volume_column_visible = F
  song.tracks[sid].visible_effect_columns = 0
  song.selected_track_index = sid0 -- return start
  --- ---
  print("n-intr: ", renoise.Song.MAX_NUMBER_OF_INSTRUMENTS )
  song.selected_instrument_index = 10 -- Jump Instrument 10 to start -- replace by jump last instrument
  song:insert_instrument_at(10)
  app:load_instrument("Xrni/Metronome.xrni") --Insert Metronome
  app:show_status( "GT16-Colors: Metronome Inserted in last track." )
  --- ---
  song.transport.bpm = 176 -- 32-999 BPM
  song.transport.lpb = 8 -- 1-256 LPB
  song.patterns[song.selected_sequence_index].number_of_lines = 96 --LINES per pattern
  -- code for write specific notes in selected pattern with selected instrument
end

On the other hand, in the same function at the end, I would add the writing of some 4 notes in the selected pattern with the selected instrument. This is possible?If so, what is the code?

The whole function would be triggered with a simple button.

Explanation of function:

  • Insert a new track in last position, and change propierties (name, color…).
  • Insert a new instrument (metronome, a xrni) in instrument number 10 of instrument box ( I want it to be the last available )
  • Change some parameters, BPM, LPB and number of LINES in selected pattern.
  • Need to add the write some notes in new track.

They can help? Thank you very much!

Write notes is already solved! I have rearranged:

function amic_CH03_4A() --METRONOME falta corregir el indice, que salte al final*1
  local song, app = renoise.song(), renoise.app()
  local sid0 = song.selected_track_index -- start
  song:insert_track_at(song.sequencer_track_count + 1)
  song.selected_track_index = song.sequencer_track_count
  local sid = song.selected_track_index
  song.tracks[sid].name = "Metronome"
  song.tracks[sid].color = {00,00,00}
  song.tracks[sid].color_blend = 50
  song.tracks[sid].volume_column_visible = F
  song.tracks[sid].visible_effect_columns = 0
  --- ---
  --print("n-intr: ", renoise.Song.MAX_NUMBER_OF_INSTRUMENTS )
  song.selected_instrument_index = 10 -- Jump Instrument 10 to start (1 to 256)
  song:insert_instrument_at(10)
  app:load_instrument("Xrni/Metronome.xrni") --Insert Metronome
  song.patterns[song.selected_sequence_index].number_of_lines = 96 --LINES per pattern
  song.patterns[song.selected_sequence_index].tracks[sid].lines[1].note_columns[1].note_value = 48 -- (0-119, 120=Off, 121=Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[1].note_columns[1].instrument_value = 9 --(0-254, 255==Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[25].note_columns[1].note_value = 48 -- (0-119, 120=Off, 121=Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[25].note_columns[1].instrument_value = 9 --(0-254, 255==Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[49].note_columns[1].note_value = 48 -- (0-119, 120=Off, 121=Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[49].note_columns[1].instrument_value = 9 --(0-254, 255==Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[73].note_columns[1].note_value = 50 -- (0-119, 120=Off, 121=Empty)
  song.patterns[song.selected_sequence_index].tracks[sid].lines[73].note_columns[1].instrument_value = 9 --(0-254, 255==Empty)
  song.selected_track_index = sid0 -- return start
  song.transport.bpm = 176 -- 32-999 BPM
  song.transport.lpb = 8 -- 1-256 LPB
  app:show_status( "GT16-Colors: Metronome Inserted in last track." )
end

Thanks to this you can include many metronomes, different speeds, sounds or volume…

To end, I just need to get the maximum number of slots of instruments available in the instrument box. This is not therenoise.Song.MAX_NUMBER_OF_INSTRUMENTS, this is 255.

For example, when starting a new song (new XRNS) the number is 10.

How to get this? Thanks! :slight_smile:

EDIT :Wow, I need to deactivate Renoise’s metronome, in case it is touched. But this I know how to do.

renoise.song().selected_instrument_index = #renoise.song().instruments

-- which is also the same as:
-- renoise.song().selected_instrument_index = table.getn(renoise.song().instruments)

Note that this only work on tables with indices (1, 2, 3, 4…) as keys. In other cases, you’d have to iterate using pairs (or more likely: select a better data structure for your table).

PS. Shortcut for writing better code in general: try to stay within 80 columns in your text editor :badteeth:

Bad:

song.patterns[song.selected_sequence_index].tracks[sid].lines[1].note_columns[1].note_value = 48 -- (0-119, 120=Off, 121=Empty)
song.patterns[song.selected_sequence_index].tracks[sid].lines[1].note_columns[1].instrument_value = 9 --(0-254, 255==Empty)

Better:

local pattern = song:pattern(song.selected_sequence_index)
local note_column = pattern:track(sid):line(1):note_column(1)
note_column.note_value = 48 -- (0-119, 120=Off, 121=Empty)
note_column.instrument_value = 9 --(0-254, 255==Empty)

@joule. Wow, that’s right, I forgot the pad (#). Thanks!!!I had used it many times with functions with tracks.

I am very grateful for your advice.At menoud, I’m struggling with the issue of shortening and sorting the code, not to fill it with redundant information.

In the end I have had to add some new lines to solve possible errors.This is how I have stayed the code:

function amic_CH03_4A() --INSERT METRONOME AND MORE
  local song, app = renoise.song(), renoise.app()
  local sid0, sin0 = song.selected_track_index, song.selected_instrument_index --initial track and instrument slot
  if #song.instruments < 255 then
    song:insert_track_at(song.sequencer_track_count + 1) --insert last new track 
    song.selected_track_index = song.sequencer_track_count --select last track
    local sid = song.selected_track_index
    song.tracks[sid].name = "Metronome" --rename name metronome
    song.tracks[sid].color = {00,00,00} --change color track
    song.tracks[sid].color_blend = 50 --change color blend track
    song.tracks[sid].volume_column_visible = F --hide volume column
    song.tracks[sid].visible_effect_columns = 0 --hide visible effect column
    --print( "n-instr-max: ", renoise.Song.MAX_NUMBER_OF_INSTRUMENTS ) -- =255
    --print( "n-inst-aval: ", #renoise.song().instruments )
    song.selected_instrument_index = #song.instruments --jump last slot available in intrument box (1-255)
    song:insert_instrument_at(#song.instruments) --insert instrument in last slot available
    app:load_instrument("Xrni/Metronome.xrni") --insert Metronome XRNI
    local pattern = song:pattern(song.selected_sequence_index)
    pattern.number_of_lines = 96 --change LINES per pattern
    local note_column = pattern:track(sid):line(1):note_column(1)
    note_column.note_value = 48 --(0-119, 120=Off, 121=Empty) --C-4
    print( "n-inst-aval: ", #renoise.song().instruments )
    note_column.instrument_value = #song.instruments - 2 --(0-254, 255==Empty)
    local note_column = pattern:track(sid):line(25):note_column(1)
    note_column.note_value = 48 --C-4
    note_column.instrument_value = #song.instruments - 2
    local note_column = pattern:track(sid):line(49):note_column(1)
    note_column.note_value = 48 --C-4
    note_column.instrument_value = #song.instruments - 2
    local note_column = pattern:track(sid):line(73):note_column(1)
    note_column.note_value = 50 --D-4
    note_column.instrument_value = #song.instruments - 2
    song.selected_track_index = sid0 --return initial track
    song.selected_instrument_index = sin0 --return initial instrument slot
    song.transport.bpm = 176 --BPM (32 to 999)
    song.transport.lpb = 8 --LPB (1 to 256)
    song.transport.metronome_enabled = false --metronome off
    app:show_status( "GT16-Colors, AMIC: Metronome Inserted in last instrument slot." ) --status
  else
    app:show_status( "⚠ GT16-Colors, AMIC: you are 255 slots used. Please, delete a instrument slot to insert Metronome!" ) --status
  end
end

This code already works perfect! It is only necessary that the archive “Metronome.xrni” is configured according to the code (or vice versa).In this way, it is possible to create similar functions with a multitude of different metronomes…

Joule, thank you very much for the help!!! :slight_smile:

:wacko:I have realized that I can reduce the code of the function just by adding a single note (9 lines less). Would use a phrase with a only note related.It’s simpler, and I can create multitude of metronomes just by using a XRNI only.

Maybe I built an independent tool with just this idea: multitude of controllable metronomes within reach of a single button.