Accessing selection inside the Pattern Matrix

The title says it all, is this possible with the current API? I couldn’t find it…

Yes!

-- Access to sequencer slot selection states. 
renoise.song().sequencer:track_sequence_slot_is_selected(track_index, sequence_index)
  -> [boolean]
renoise.song().sequencer:set_track_sequence_slot_is_selected(
  track_index, sequence_index, selected)

renoise.song().sequencer:set_track_sequence_slot_is_selected(1,1,true)
renoise.song().sequencer:set_track_sequence_slot_is_selected(1,1,false)

1 Like

Hmm… so I have to find the selection manually in the song using this?

I was hoping for something like selection_in_pattern which gives back the range of the current selection.

I meant calling track_sequence_slot_is_selected(t,s) with every slot until the selection rectangle is found.

Yes, use an iteration with a return to find it.

local function sel_matrix_start()
  local song=renoise.song()
  local ssq=song.sequencer
  local trk_seq={}
  --print(#ssq.pattern_sequence)
  --search first selected slot
  for seq=1,#ssq.pattern_sequence do
    for trk=1,#song.tracks do
      if (ssq:track_sequence_slot_is_selected(trk,seq)) then
        trk_seq[1]=trk
        trk_seq[2]=seq
        return trk_seq
      end
    end
  end
  return nil
end

local function sel_matrix_end()
  local song=renoise.song()
  local ssq=song.sequencer
  local trk_seq={0,0}
  --print(#ssq.pattern_sequence)
  --search first selected slot
  for seq=#ssq.pattern_sequence,1,-1 do
    for trk=#song.tracks,1,-1 do
      if (ssq:track_sequence_slot_is_selected(trk,seq)) then
        trk_seq[1]=trk
        trk_seq[2]=seq
        return trk_seq
      end
    end
  end
  return nil
end

--bang!
sel_matrix_start() --this function return a table with trk and seq index from start
sel_matrix_end() --this function return a table with trk and seq index from end
--return a "nil" if the selection not exist!

I just wrote this code here now. I haven’t tested it, but it should work.

Thanks @Raul, you didn’t have to! :sweat_smile:

Here is my version which returns an object like selection_in_pattern

function selection_in_matrix()
  local song = renoise.song()
  local seq = song.sequencer
  local selection_range = function(start_track, start_line, end_track, end_line)
    return {
      start_track = start_track,
      end_track = end_track,
      start_line = start_line,
      end_line = end_line
    }
  end
  local get_start = function()
    for s = 1, #seq.pattern_sequence do
      for t = 1, #song.tracks do
        if seq:track_sequence_slot_is_selected(t, s) then
          return selection_range(t,s,t,s)
        end
      end
    end
  end
  local get_end = function(selection)
    for s = #seq.pattern_sequence, 1, -1 do
      for t = #song.tracks, selection.start_track, -1 do
        if seq:track_sequence_slot_is_selected(t, s) then
          return selection_range(selection.start_track, selection.start_line, t, s)
        end
      end
    end
    return selection
  end
  
  return get_end(get_start())
end

The selection seems to be guaranteed to exist since the slot under the cursor always returns true for the check.

Also, I learned that it is possible to select islands as well, not only a rectangle, maybe this is why there is no such function built-in. However this doesn’t concern me right now as it is not possible to do this with keyboard.

1 Like

I am happy that is was useful for you. Sometimes it’s fun to write code without testing it and just by reading it you know it’s going to work.

2 Likes