Obtain the index of the last phrase played

Load an instrument that has many phrases. In total you can have up to 126 phrases.

In the phrases playback panel, you can select a specific phrase and play it.

With a tool, it is possible to reproduce any phrase without selecting it before, through the " Keymap" mode. Here each key can have a phrase linked.

So:

  1. How to get the index of the phrase associated with the X key?For example, the key B-0 , has associated the phrase 0C. How to get the index of that phrase?
  2. Is it possible to obtain the index of the last phrase that has been reproduced?The last phrase played does not have to be selected.

In case there are doubts, the phrases panel is here with Keymap:

7932 phrase-played.png

Any ideas?Any way to print that index?

Deleted… no interest

Raul, (looks like joule and danoise have left the country (don’t blame them in the least however…)) my quick thought on this first part (don’t know about the second part)…

How to get the index of the phrase associated with the X key? For example, the key B-0 , has associated the phrase 0C. How to get the index of that phrase?

Quickly looking at the API docs, you have the note_range span associated with each phrase (the upper and lower note_range would be equal in the above case) yes? So why not loop through (search) each phrase starting at 1 checking your B-0 (note number 11?) with the note_range and if that note number falls within the phrases note_range chances are you have found the phrase index associated with that note?

Also looking at that screenshot it seems like you have a pretty much one to one relationship with the note number B-0 (11) and the phrase number (12). It looks like you have assigned (in order) a phrase to each individual key.

Raul, (looks like joule and danoise have left the country (don’t blame them in the least however…)) my quick thought on this first part (don’t know about the second part)…

Quickly looking at the API docs, you have the note_range span associated with each phrase (the upper and lower note_range would be equal in the above case) yes? So why not loop through (search) each phrase starting at 1 checking your B-0 (note number 11?) with the note_range and if that note number falls within the phrases note_range chances are you have found the phrase index associated with that note?

Also looking at that screenshot it seems like you have a pretty much one to one relationship with the note number B-0 (11) and the phrase number (12). It looks like you have assigned (in order) a phrase to each individual key.

:DOk, great!

--phrase keymapped
function pht_phrase_keymapped( nte )
  local song = renoise.song()
  for i = 1, #song.selected_instrument.phrases do
    if ( song.selected_instrument:phrase( i ).mapping ~= nil ) then
      local note_1 = song.selected_instrument:phrase( i ).mapping.note_range[1]
      local note_2 = song.selected_instrument:phrase( i ).mapping.note_range[2]
      for r = note_1, note_2 do
        if ( r == nte ) then
          print( i )
          song.selected_phrase_index = i
          return i
        end
      end
    end
  end
end

It is a bit convoluted, but it returns exactly the index value desired. It also works if a range of notes is associated with the same phrase.If a note does not have an associated phrase, it does not return anything.

Thanks 4Tey!!!

On the other hand, I believe that the API documentation does not have any control over the play and stop buttons of the phrases. I have not found anything.

Ok Raul…but could I just mention quickly looking at your code snip why you have an inner for loop scan at lines 8-14? Off the top of my head could be replaced with just an if statement?:

local note1 = blah
local note2 = blah
if nte >= note_1 and nte <= note_2 then
  print(i)
  song.selected_phrase_index = i
  return i
end

I don’t know Raul I haven’t tested that (you’d have to try it see if it still works)…doesn’t really matter so long as it does what you want though :slight_smile:

Ok Raul…but could I just mention quickly looking at your code snip why you have an inner for loop scan at lines 8-14? Off the top of my head could be replaced with just an if statement?:

local note1 = blah
local note2 = blah
if nte >= note_1 and nte <= note_2 then
print(i)
song.selected_phrase_index = i
return i
end

I don’t know Raul I haven’t tested that (you’d have to try it see if it still works)…doesn’t really matter so long as it does what you want though :slight_smile:

It also works correctly:

function pht_phrase_keymapped( nte )
  local song = renoise.song()
  for i = 1, #song.selected_instrument.phrases do
    if ( song.selected_instrument:phrase( i ).mapping ~= nil ) then
      local note_1 = song.selected_instrument:phrase( i ).mapping.note_range[1]
      local note_2 = song.selected_instrument:phrase( i ).mapping.note_range[2]
      if ( nte >= note_1 ) and ( nte <= note_2 ) then
        print( i )
        song.selected_phrase_index = i
        return i
      end
    end
  end
end

What I would like is to improve these two lines:

local note_1 = song.selected_instrument:phrase( i ).mapping.note_range[1]
local note_2 = song.selected_instrument:phrase( i ).mapping.note_range[2]

mapping.note_range is a table of two values.Maybe there is some way to reduce it in this function…