Best Way Of Iterating Through A Pattern

What’s the best way of iterating through a pattern?

If I do the following to insert an effect on every line it will be very erratic, and not on every line.

  
local function process_pattern(option)  
 local song = renoise.song()  
 local new_pos = song.transport.playback_pos  
 new_pos.line = 1  
 song.transport.playback_pos = new_pos  
 for pos, line in song.pattern_iterator:lines_in_pattern_track(1, 1) do  
 if new_pos.line < song.selected_pattern.number_of_lines then  
 new_pos.line = new_pos.line + 1  
 song.transport.playback_pos = new_pos  
 -- print(new_pos.line) -- prints properly  
 -- print(song.transport.playback_pos.line) -- prints erratically  
 insert_fx(option)  
 end  
 end  
end  
  

(printing new_pos.line results in a perfect iteration in the terminal, but printing song.transport.playback_pos.line gives very erratic results (instead of 1,2,3,4,5,6,7,…,64 it becomes 24,24,24,24,24,…,53,53,53,53,53,53,53))

From the API docs

  
renoise.song().transport.playback_pos  
 -> [renoise.SongPos object]  
  

It’s expecting renoise.SongPos() object, not an int.

Example usage:

  
local foo = renoise.SongPos()  
foo.sequence = 1  
foo.line = 1  
-- Insert missing code here  
song.transport.playback_pos = foo  
  

Hi Conner, isn’t that basically what I’m already doing with new_pos though?

i.e.

  
(...)  
 local new_pos = song.transport.playback_pos  
 new_pos.line = 1  
(...)  
 new_pos.line = new_pos.line + 1  
 song.transport.playback_pos = new_pos  
(...)  
  

No, I don’t think so. I don’t have Renoise in front of me but this in particular:

  
local new_pos = song.transport.playback_pos  
new_pos.line = 1  
  

Is probably not a copy, rather something like a pointer. Think “shortcut”.

You are directly manipulating “song.transport.playback_pos” whenever you mess
with “new_pos”

Later, you do:

  
song.transport.playback_pos = new_pos  
  

Which is basically pointing an object to itself. Which, is why it would behave erratically.

You need to create a separate object, and manipulate it on it’s own.

Thanks I was curious about that.
It seemed like you can’t actually change renoise.song().transport.playback_pos.line directly, but have to overwrite
the whole object, so I thought maybe it was a copy (this new_pos stuff is basically taken from the
custom pattern navigation tool).

Just changed the code to

  
local function process_pattern(option)  
 local song = renoise.song()  
 local new_pos = renoise.SongPos()  
 new_pos.sequence = 1  
 new_pos.line = 1  
 song.transport.playback_pos = new_pos  
 for pos, line in song.pattern_iterator:lines_in_pattern_track(1, 1) do  
 if new_pos.line < song.selected_pattern.number_of_lines then  
 new_pos.line = new_pos.line + 1  
 song.transport.playback_pos = new_pos  
 -- print(new_pos.line) -- prints properly  
 -- print(song.transport.playback_pos.line) -- prints erratically  
 insert_fx(option)  
 end  
 end  
end  
  

and it still has the same erratic behavior. :confused:
I guess there must be some other way of manipulating lines in the pattern where you’re not using playback_pos.
Or maybe I can slow the iteration speed down so the playback_pos gets properly updated every time.

playback_pos != edit_pos

edit_pos is what you want.

Thanks taktik! :)

Edit:

Spoke too soon… it seemed to work but now

  
local song = renoise.song()  
local new_pos = renoise.SongPos()  
new_pos.sequence = 1  
new_pos.line = 1  
song.transport.edit_pos = new_pos  
for pos, line in song.pattern_iterator:lines_in_pattern_track(1, 1) do  
 if new_pos.line < song.selected_pattern.number_of_lines then  
 new_pos.line = new_pos.line + 1  
 song.transport.edit_pos = new_pos  
 print(song.transport.edit_pos)  
 end  
end  
  

gives the same behavior again.

This is starting to turn into comedy? :)

  
renoise.song().transport.edit_pos  
 -> [renoise.SongPos object]  
  

Tips:

  • Do rprint() or oprint(), not print()
  • Print edit_pos.line, not edit_pos

What exactly do you want to do then? To iterate over lines,columns you don’t have to mess with edit/play_positions at all.

Maybe this helps you:

Conner seems like it. And you’re hilarious… ;D

Using print instead of oprint or rprint actually doesn’t matter (tried all three in all configurations… same results).
This doesn’t just happen when printing to the terminal, it happens when executing a function call (i.e. look at the original code),
I’m just using print for brevity.

Ok thanks, that explains it. I was wondering if I could do that because it didn’t seem intuitive to use the pos. :)

Now I finally get it: You thought that songpos returns the iterators current pos. Well, … that’s not the case.