[Solved] Help: name_observable pattern & disordered sequence

Hi.I have a small problem with a observable, related with the name of the selected pattern and the sequence disordered.I am using the following function for observables:

function section_name( song )
  song = renoise.song()
  local function name()
    if song.sequencer:sequence_section_name( song.selected_sequence_index ) ~= 'Untitled Section' then -- avoid default rename 'Untitled Section'
      vb.views['PMSS_TF_SEC'].text = ( "%s" ):format( song.sequencer:sequence_section_name( song.selected_sequence_index ) ) -- vb.views name section (renoise --> tool)
    end
    vb.views['PMSS_TF_PATT'].text = ( "%s" ):format( song.patterns[song.selected_sequence_index].name ) -- vb.views name pattern (renoise --> tool)
    --print('ssi:', song.selected_sequence_index )
    vb.views['PMSS_TX_NUM'].text = ( "%s" ):format( song.selected_sequence_index ) --vb.views number pattern sequence (renoise --> tool)
  end
  song.selected_sequence_index_observable:add_notifier( name ) -- _observable notifier selected pattern
  song.sequencer:sequence_section_name_observable(song.selected_sequence_index):add_notifier( name ) -- _observable name section (renoise --> tool)
  song.patterns[song.selected_sequence_index].name_observable:add_notifier( name ) -- _observable name pattern (renoise --> tool)
end
renoise.tool().app_new_document_observable:add_notifier( section_name ) -- document_observable function

If the sequence is ordered, the function does not return any errors.But if the sequence is disordered, selecting the last pattern returns an error. If the sequence is very disordered, it can return some error when selecting the penultimate pattern or earlier. The typic error:

***.\tools/tool_02.lua:17: attempt to index field '?' (a nil value)
*** stack traceback:
***.\tools/tool_02.lua:17: in function <.\tools/tool_02.lua:13>

This error is caused by the line:

vb.views['PMSS_TF_PATT'].text = ( "%s" ):format( song.patterns[song.selected_sequence_index].name )

Related with this:

song.sequencer:sequence_section_name_observable(song.selected_sequence_index):add_notifier( name )

Having the sequence disordered, returns unexpected errors. I can not find a way to solve it. Please, help!


Theme aside. Can you provide me with a function that orders the entire sequence of patterns forcefully? (the same of Organize/Flatten Sequence (Remove Repeats), in Pattern Matrix). I used ç

  • song.sequencer:sort() or
  • song.sequencer.keep_sequence_sorted = true

But they do not serve to sort the whole sequence.

Thanks!!!

Just a short pointer without penetrating the issue indepth :slight_smile:

“song.patterns[song.selected_sequence_index]” probably doens’t make any sense since a pattern index is not the same as a sequence index (looks like an error to happen, unless you’re lucky and a pattern_index always matches the same sequence_index).

You most likely should fetch the proper pattern index by looking into “renoise.song().sequencer.pattern_sequence[song.selected_sequence_index]”.

So, this might fix the problem?

vb.views['PMSS_TF_PATT'].text = ( "%s" ):format( song.patterns[song.sequencer.pattern_sequence[song.selected_sequence_index] ].name )

Just a short pointer without penetrating the issue indepth :slight_smile:

“song.patterns[song.selected_sequence_index]” probably doens’t make any sense since a pattern index is not the same as a sequence index (looks like an error to happen, unless you’re lucky and a pattern_index always matches the same sequence_index).

You most likely should fetch the proper pattern index by looking into “renoise.song().sequencer.pattern_sequence[song.selected_sequence_index]”.

So, this might fix the problem?

vb.views['PMSS_TF_PATT'].text = ( "%s" ):format( song.patterns[song.sequencer.pattern_sequence[song.selected_sequence_index] ].name )

Yes, solved! :o

  1. vb.views[‘PMSS_TF_PATT’].text = ( “%s” ):format( song.patterns[song.selected_sequence_index].name ) --incorrect! (sequence index)
  2. vb.views[‘PMSS_TF_PATT’].text = ( “%s” ):format( song.patterns[song.sequencer.pattern_sequence[song.selected_sequence_index] ].name ) --correct! (pattern index)

Thaaaanks joule!

So is.If the sequence is ordered the pattern index and sequence index is the same (same value), so in this case did not return an error, although it is not correct to point to the sequence index.I understand that it is correct in both cases to point towards the pattern index.I had not realized this detail.


Theme aside. Can you provide me with a function that orders the entire sequence of patterns forcefully? (the same of Organize/Flatten Sequence (Remove Repeats), in Pattern Matrix). I used ç

  • song.sequencer:sort() or
  • song.sequencer.keep_sequence_sorted = true

But they do not serve to sort the whole sequence.

Thanks!!!

I have solved this doubt with a simple function. I add it in case someone wants to use it:

--Sort Sequence of Patterns (pattern_index = sequence_index)
function pmss_fn_sort( song )
  song = renoise.song()
  for i = 1, #song.sequencer.pattern_sequence do --#song.patterns do
    --print ("i: ", i)
    song.selected_sequence_index = i
    song.selected_pattern_index = song.selected_sequence_index
  end
end

This will make the sequence_index identical to the corresponding pattern_index, but it will mess up the structure of your song since the pattern data is not shuffled along with the index (like sequencer:sort does).

Making a perfect clone of sequencer:sort() is not a trivial task.

PS. Btw, sidenote… I was a bit baffled to find that sequencer.pattern_sequence is read_only, and that you have to change the cursor position to change a pattern index. A lack in the API I think (possibly a compromise made by the devs not to confuse some of the ad-hoc sorting matters…)

This will make the sequence_index identical to the corresponding pattern_index, but it will mess up the structure of your song since the pattern data is not shuffled along with the index (like sequencer:sort does).

Making a perfect clone of sequencer:sort() is not a trivial task.

PS. Btw, sidenote… I was a bit baffled to find that sequencer.pattern_sequence is read_only, and that you have to change the cursor position to change a pattern index. A lack in the API I think (possibly a compromise made by the devs not to confuse some of the ad-hoc sorting matters…)

Yes, this function only sort the patterns = the sequence, without saving the initial order, that is, the order of the notes written in the initial sequence.Actually does what I want, sort the number of patterns identical to the sequence number.It is valid to start over, or to sort the patterns manually afterwards. The native sort() works differently,Preserves the initial structure of the song, arranging the numbers that are possible upwards.


Actually, what I’m looking for is this function: “Organize/Flatten Sequence (Remove Repeats)”.It is not the same as “Organize/Sort Sequence”.I would like 2 separate functions to do both, which I already have and this:Organize/Flatten Sequence (Remove Repeats)

I’m learning new things.Which force me to review functions written long ago by me, because I think I can improve them.Everything surrounding the sequence and patterns is a bit lousy. This is where more problems than using the API.

With the theme of the tracks it is easier, wxcept clone several child groups with tracks.And in both cases, patterns and clues, I have come across bugs in Renoise, which prevent me from finishing things.Some have already been resolved for the next version, supposedly, 3.1.1.However, I still do not know things that I do not know what they are for. For example:

– When true, the sequence will be auto sorted.

renoise.song().sequencer.keep_sequence_sorted, _observable

→ [boolean]

How exactly would it be used?Things like that.

“keep_sequence_sorted” (also found in the sequencer right-click context menu) is making sure that everytime you insert/remove a new sequence, the pattern indexes are reorganized so that they are sequential in the sequence.

“…(remove repeats)” is a bit easier to script. Find out what patterns occur at more than one place, and make all duplicates unique by cloning the pattern (pattern:copy_from)

Hmm… the design flaud i mentioned earlier is bugging me. renoise.song().selected_pattern_index should really be read-only, and pattern_sequence should be read/write. Everything is so elegant otherwise :slight_smile: