Lua: Renoise.song().tracks[].members[].track_Index


(joule) #1

There seems to be no way to get what track indexes are part of a group, the same way track index in .group_parent is not available.

These issues could perhaps both be resolved by adding:

renoise.song().tracks[]....track\_index  

(Unless I’m missing some fundamental lua trick)

EDIT: Perhaps another option would be to add a renoise.song().groups[] structure. That approach may be beneficial coding wise in many cases.


(eeter) #2

yes, and also the track[].group_parent should return nil when the track has ran away from it’s parents.


(Cas) #3

I’m running into the same thing updating my [Push Back](http://forum.renoise.com/index.php?/topic/35059-new-tool-28-push-back/page view findpost p 275113) tool. When the function is called from a group track I will have to

  • iterate over every track to the left of current track (type group)
  • go through it’s group_parent, possibly that one’s group_parent, etc. routing

to get the track indexes of subtracks (to be able to modify their delay_column data in this case).
With the current state I’m just gonna not complete the v1.0 of the tool, I might code it, but I’m not confident it’s gonna run properly on every sys with the turn of a dial you know? :) and it’s just double double inefficient code…
I would really like to know if there’s a better way.


(taktik) #4

You can create a small helper function to get the track index, by using “rawequal” to compare a track object with renoise.song().tracks. “rawequal” bypasses the object’s comparison operator (which in most cases is not defined anway), and tests object identity instead:

  
function get\_track\_index(\_track)  
 assert(\_track, "expected a valid track object")  
 for index, track in ipairs(renoise.song().tracks) do  
 if (rawequal(track, \_track)) then   
 return index  
 end  
 end  
end  
  
-- test  
assert(get\_track\_index(renoise.song().tracks[2]) == 2)  
assert(get\_track\_index(renoise.song().selected\_track) == renoise.song().selected\_track\_index)  
  

A sneaky way (just for fun): Classes in the renoise API are not read-only. So one can define new member functions for existing renoise.* classes, or replace existing ones.

One can also wrap a getter/setter function pair to define new properties this way. This is done with the global function “property()”:
property(getter, setter) takes a getter and setter (optional) function as parameter. Every time a property gets read, the getter function is called and it’s return value will be put on the stack. Every time you assign the property, the setter function is called with the given argument. The first argument of the passed functions will always be “self”.

In the example below, this is used to create a new “track_index” property to renoise.Track and GroupTrack. Because only a getter is present, assignment is disallowed too and will fire an error, which is exactly what we want.

  
-- Inject a new property "track\_index" into the renoise.[Group]Track class.  
-- Define this as early as possible in your script to make the property  
-- as early as possible available. Once it's defined, the property will be   
-- available for every existing and new object of type "renoise.[Group]Track",  
-- so you don't have to define this for every new track that gets created   
-- within Renoise.  
local track\_index\_property = property(function(self)  
 for index, track in ipairs(renoise.song().tracks) do  
 if (rawequal(self, track)) then   
 return index  
 end  
 end  
end)  
-- and apply it to all track classes in the API  
renoise.Track.track\_index = track\_index\_property   
renoise.GroupTrack.track\_index = track\_index\_property   
  
-- test  
assert(renoise.song().tracks[6].track\_index, 6)  
assert(renoise.song().selected\_track.track\_index, renoise.song().selected\_track\_index)  
  
-- assignment is not allowed (track\_index is read-only)  
assert(pcall(function() renoise.song().tracks[2].track\_index = 23 end) == false)   
  

Same thing could be done to create a “member_index” for tracks in groups of whatever else you need.


Oh and all this of course is not a lame excuse. track_index should be available by default in the API.


(Cas) #5

On the contrary, this is a crazy cool workaround and learning something too :D thx


(Frank Baumgartner) #6

Awesome !!


(re.dread) #7

I can’t help myself but wonder, is there a way to actually attach a new property with both read and write support?
Simply doing:

renoise.NoteColumn.quantize\_value = 0  

… works, but the value is read-only.

local quantize\_value\_property =   
 property(function(self) return self.quantize\_value end,  
 function(self, set) self.quantize\_value = set end)  
  
renoise.NoteColumn.quantize\_value = quantize\_value\_property  

… doesn’t work. it gives a ```
*** TestPad.lua:12: C stack overflow

  
what does work is:  

local value = 0
local quantize_value_property =
property(function(self) return value end,
function(self, set) value = set end)

renoise.NoteColumn.quantize_value = quantize_value_property

.. but it doesn't really make any sense, because the property is not unique for each NoteColumn then.  
  
also tried:  

local values = {}
local quantize_value_property =
property(function(self) return values[self] end,
function(self, set) values[self] = set end)

renoise.NoteColumn.quantize_value = quantize_value_property

but that gives me: ```  
\*\*\* TestPad.lua:18: unknown property or function 'quantize\_value'. can not assign new properties to an object of type 'NoteColumn'  

I know it’s quite a hack, but would be nice to get it working :)/>


(joule) #8

I know it’s quite a hack, but would be nice to get it working smile.gif/>

Late reply, but I wanted to mention that this should be possible if you ‘cache’ your objects, which might seem slightly impractical but totally doable. If you don’t cache them, calls like :line() or .lines[] are technically returning new objects everytime (afaik).

An example that seems to work:

renoise.NoteColumn.MY_DEFAULT_VALUE = 10
renoise.NoteColumn._my_value = renoise.NoteColumn.MY_DEFAULT_VALUE

local my_property = property(
  function(self) return self._my_value end,
  function(self, val) self._my_value = val end
)

renoise.NoteColumn.my_property = my_property

-- Testing

local ncol_1 = renoise.song():pattern(1):track(1):line(1):note_column(1)
local ncol_2 = renoise.song():pattern(1):track(1):line(1):note_column(2)

print(ncol_1.my_property)
print(ncol_2.my_property) -- should print 10

ncol_1.my_property = 50
ncol_2.my_property = 30

print(ncol_1.my_property)
print(ncol_2.my_property)

This is kind of a pandoras box :wink: You could, for example, make custom getters/setters and observables that will bang due to very specific changes to song data (when edited from a tool).