[Solved] Help: order of newly inserted pattern

Hi.I have a problem with the following code:

function amic_CH02_4A()
  local song = renoise.song()
  local ssid = song.selected_sequence_index
  print ("ssid: ", ssid)
  song.sequencer:insert_new_pattern_at(ssid + 1)
  --song.sequencer:insert_sequence_at(ssid, ssid + 1) -- clone?
end

I simply intend to add a new pattern below the selected pattern, and follow the order of the sequence in Matrix Editor. But I find it impossible.

The code inserts a new pattern, but its number takes as reference the last number of the index. If the song has 90 patterns, and you are in pattern 00, insert a new pattern number 91:

00 00
01 91 pattern inserted
02 01
03 02
04 03


90 90

What would be the code to include a new pattern (empty) respecting the order?
What would be the code to include a 4 pattern (empty) respecting the order?
What would it be like to do the same with cloned patterns?

I found it easier to control the tracks and groups.Any help with this?

Start from zero, apparently, the numbering goes backwards?
Play|Order
00 04
01 03
02 02
03 01
04 00

After you’ve inserted your pattern(s), you can call the following function to sort them:

renoise.song().sequencer:sort()

Note: This may be quite intensive on larger songs, so there’s a chance that your script may time out.

After you’ve inserted your pattern(s), you can call the following function to sort them:

renoise.song().sequencer:sort()

Note: This may be quite intensive on larger songs, so there’s a chance that your script may time out.

Yes,I already tried the code:

function amic_CH02_4A(i)--PENDING
  local song = renoise.song()
  local ssid = song.selected_sequence_index
  print ("ssid: ", ssid)
  song.sequencer:insert_new_pattern_at(ssid)
  song.sequencer:sort()
end

This does what I want.But somehow, the line “song.sequencer:sort()” is very heavy, even at the beginning, when there is only one pattern.Is there any way to do the same with another “optimized” code?

I have experienced that after running this function, the “+ button” in Matrix Editor of insert a new pattern (left click) also suffers.Something does not seem right here, it does not work fast.

My intention is to have 5 functions:

  1. Insert new patternorganized (equivalent to the + button in Matrix Editor)
  2. Insert 4 new patternsorganized
  3. Insert 4 new patterns organized and start a new section for rename (this is possible?)
  4. Insert clone patternorganized(equivalent to the clone button in Matrix Editor)
  5. Insert 4 clone patterns, with tracks previously selected in Matrix Editor (this seems more complicated)

These functions will be triggered by MIDI buttons…The interests me most is the 3rd.

It is not likely that you can do it any faster than the native :sort() (unless devs made a very suboptimal sorting routine where a LUA improvement would overweigh the general speed of native code). It’s probaby heavy since an object is moved (in the sence “copied / deleted” in memory) for its index to change. Having 100 patterns and inserting a pattern at sequence 2 would require 99 objects to be moved, is my guess.

I had a similar thing with an instrument list sorting tool. It’s time consuming to swap heavy instruments, and the only way to optimize it is to find a more clever sorting routine (e g, not pure bubble sort). I’m fairly confident that sequence :sort() has already been optimized in that regard and won’t move (much) more data in memory than necessary.

It is not likely that you can do it any faster than the native :sort() (unless devs made a very suboptimal sorting routine where a LUA improvement would overweigh the general speed of native code). It’s probaby heavy since an object is moved (in the sence “copied / deleted” in memory) for its index to change. Having 100 patterns and inserting a pattern at sequence 2 would require 99 objects to be moved, is my guess.

It’s the feeling that something is misspelled in my code.If I do not use the tool, only the native buttons of the Matrix Editor, it seems to run thinner, faster.I do not expect it to work faster, but just like the “-” “+” “clone” native buttons in Matrix Editor.

Slowness seems to be caused by this line:song.sequencer: sort** ()**. I use R3.1 x64 Win10.

I had a similar thing with an instrument list sorting tool. It’s time consuming to swap heavy instruments, and the only way to optimize it is to find a more clever sorting routine (e g, not pure bubble sort). I’m fairly confident that sequence :sort() has already been optimized in that regard and won’t move (much) more data in memory than necessary.

You mean for the next version of Renoise?

No. I’m explaining the probable reason to why song.sequencer: sort** () is slow, and that it is unlikely that it’s possible to do a custom sorting function that is faster in LUA.**

No. I’m explaining the probable reason to why song.sequencer: sort** () is slow, and that it is unlikely that it’s possible to do a custom sorting function that is faster in LUA.**

Ok!When I have time I will look at other tools, see if there is any better code.And I will copy it here…Surely, I will not be the first to face this.

PS. Isn’t the thing that users that really care about ordered pattern indexes will already have ticked “Keep sequence ordered” option? This will sort the list as soon as something is inserted/deleted (even from lua).

Users that don’t have it ticked don’t want you to mess with the pattern indexes from a tool - at least not thru-out the song. I would consider it bad practice and almost as taboo as changing pattern data without asking/notifying the user :slight_smile:

Maybe you already have a clear statement that your tool will change pattern indexes thru-out the song, and that it must for some reason. Otherwise I wouldn’t do it, but let the user decide via the native toggle.

PS. Isn’t the thing that users that really care about ordered pattern indexes will already have ticked “Keep sequence ordered” option? This will sort the list as soon as something is inserted/deleted (even from lua).

Users that don’t have it ticked don’t want you to mess with the pattern indexes from a tool - at least not thru-out the song. I would consider it bad practice and almost as taboo as changing pattern data without asking/notifying the user :slight_smile:

Maybe you already have a clear statement that your tool will change pattern indexes thru-out the song, and that it must for some reason. Otherwise I wouldn’t do it, but let the user decide via the native toggle.

I am not looking for strange behavior but the same as the native “+” or “clone” buttons of Matrix Editor. The same!

I have some tools installed.Perhaps some tool impinges on slow?I’ll have to try Renoise v3.1 without any tools. Using only Renoise, my computer looks very resentful now, when she should not.I had not realized this until now.

I had never noticed Renoise so slow in include or clone patterns with the native buttons of the Matrix Editor.

:blink:Wow, I just turned off other tools and it seems to work fine now!

Let’s see if I can find the problem. It seems that some tool of another user slows down Renoise…

This is the first time this happens to me.

I have already encountered the problem of slowness: joule.no0b-testpad.xrnx

https://forum.renoise.com/t/nb-worked-around-slight-overhaul-of-alias-pattern-index-observable/46723

For some reason, I had this tool turned on. She is the cause.I guess you’d be testing her.

Continuing with the functions, now that I already work fine…

I want the code to " start a new section in current pattern" in Pattern Matrix.I have done this function:

function amic_CH02_3A() --INSERT 4 NEW PATTERNS & START A NEW SECTION -- PENDING!
  local song = renoise.song()
  local sid = song.selected_sequence_index
  song.sequencer:insert_new_pattern_at( sid + 1 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 2 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 3 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 4 ) --insert next pattern
  song.selected_sequence_index = sid + 4 --select next pattern
  song.sequencer:sort() --sort sequence
  --Start a new section at the current pattern -- pending
    --renoise.song().sequencer:sequence_section_name(sequence_index) 
    --renoise.song().sequencer:set_sequence_section_name(sequence_index, string) 
    --renoise.song().sequencer:sequence_section_name_observable(sequence_index)
  renoise.app():show_status( "GT16-Colors AMIC: new 4 patterns inserted & new section." )
end

Obviously, it has to do with order when composing. The function works well, it is necessary to add the " start a new section in current pattern". I found these lines in “Renoise.Song.API.lua”, but I do not know how they work:

renoise.song().sequencer:sequence_section_name(sequence_index) 
renoise.song().sequencer:set_sequence_section_name(sequence_index, string) 
renoise.song().sequencer:sequence_section_name_observable(sequence_index)

Any ideas?This particular function interests me a lot…

Slightly off topic (disregard if irrelevant):

I haven’t looked into section names, but it should be really simple finding out how it works.

Have you experimented with the API methods in the LUA scripting terminal or testpad.lua? The methods (and objects) can be accessed and called directly from the terminal to quickly see their effects (and verify what their arguments/syntax should be). This is a great way to trial&error your way into learning the Renoise API.

“This” is what you should find from only a couple of minutes of experimenting:

local seq_index = 3
renoise.song().sequencer:set_sequence_is_start_of_section(seq_index, true) -- enables/disables a sequence as a section start. "sections" are pretty much just labels (almost).
renoise.song().sequencer:set_sequence_section_name(seq_index, "This is the new section") -- sets a section name. (even if section doesn't "exist" (e.g is unvisible), appearantly this information is still stored in the sequence).

Slightly off topic (disregard if irrelevant):

I haven’t looked into section names, but it should be really simple finding out how it works.

Have you experimented with the API methods in the LUA scripting terminal or testpad.lua? The methods (and objects) can be accessed and called directly from the terminal to quickly see their effects (and verify what their arguments/syntax should be). This is a great way to trial&error your way into learning the Renoise API.

“This” is what you should find from only a couple of minutes of experimenting:

local seq_index = 3
renoise.song().sequencer:set_sequence_is_start_of_section(seq_index, true) -- enables/disables a sequence as a section start. "sections" are pretty much just labels (almost).
renoise.song().sequencer:set_sequence_section_name(seq_index, "This is the new section") -- sets a section name. (even if section doesn't "exist" (e.g is unvisible), appearantly this information is still stored in the sequence).

I had problems with my "sequence_index"It is already resolved. Thank you very much for the help! :

function amic_CH02_3A() --INSERT 4 NEW PATTERNS & START A NEW SECTION -- OK!
  local song = renoise.song()
  local sid = song.selected_sequence_index
  song.sequencer:insert_new_pattern_at( sid + 1 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 2 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 3 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 4 ) --insert next pattern
  song.selected_sequence_index = sid + 4 --select next pattern
  song.sequencer:sort() --sort sequence
  song.sequencer:set_sequence_is_start_of_section(song.selected_sequence_index, true) --Start a new section at the current pattern
    --renoise.song().sequencer:sequence_section_name(sequence_index) 
    --renoise.song().sequencer:sequence_section_name_observable(sequence_index)
  renoise.app():show_status( "GT16-Colors AMIC: new 4 patterns inserted & new section." )
end
song.sequencer:set_sequence_is_start_of_section(song.selected_sequence_index, true)

Yes, sometimes I use the terminal to include the lines and check, but there are things I do not understand, until I test them thoroughly.

With the name new section changed (I put it separately to have references in the forums):

function amic_CH02_3A() --INSERT 4 NEW PATTERNS & START A NEW SECTION RENOWNED -- OK!
  local song = renoise.song()
  local sid = song.selected_sequence_index
  song.sequencer:insert_new_pattern_at( sid + 1 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 2 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 3 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 4 ) --insert next pattern
  song.selected_sequence_index = sid + 4 --select next pattern
  song.sequencer:sort() --sort sequence
  song.sequencer:set_sequence_is_start_of_section(song.selected_sequence_index, true) --start a new section at the current pattern
  song.sequencer:set_sequence_section_name(song.selected_sequence_index, "New Section x4") --sets a section name
  renoise.app():show_status( "GT16-Colors AMIC: new 4 patterns inserted & new section renowned." )
end
song.sequencer:set_sequence_section_name(song.selected_sequence_index, "New Section x4") --sets a section name

This is wonderful!It will serve me for other functions…

Maybe the order of this new function is more coherent:

function amic_CH02_3A() --START A NEW SECTION RENOWNED & INSERT 4 NEW PATTERNS -- OK!
  local song = renoise.song()
  local sid = song.selected_sequence_index
  song.sequencer:set_sequence_is_start_of_section(sid, true) --start a new section at the current pattern
  song.sequencer:set_sequence_section_name(sid, "New Section x4") --sets a section name
  song.sequencer:insert_new_pattern_at( sid + 1 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 2 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 3 ) --insert next pattern
  song.sequencer:insert_new_pattern_at( sid + 4 ) --insert next pattern
  song.selected_sequence_index = sid + 4 --select next pattern
  song.sequencer:sort() --sort sequence
  renoise.app():show_status( "GT16-Colors AMIC: new section renowned & new 4 patterns inserted." )
end