Your code below (indented for clarity) seems strange and troublesome.
if renoise.app().window.active_middle_frame==0 then
renoise.song().selected_sample.sample_buffer_observable:add_notifier(function()
gotoeditor()
renoise.song().selected_sample.sample_buffer_observable:remove_notifier(function()
gotoeditor()
)
end
)
end
function gotoeditor()
renoise.app().window.active_middle_frame=4
end
If this really is a free-floating piece, here’s what happens, if I’m getting this right. I’ll try to break this down.
- tool loads
- the global function gotoeditor gets registered (I think this is done first)
- the if-then -structure checks whether active middle frame is 0 (whatever that 0 is. Maybe pattern editor. You really should use the constants provided with API to make stuff more reliable and readable)
- if the middle frame is not 0 when tool loads, nothing happens. The end, roll credits.
Provided the active middle frame happens to be 0 the story goes on.
5. you attach an anonymous function in the selected_sample_buffer_observable. What that means is that whenever the selected_sample_buffer changes, the anonymous function will run. The said function, separated from the rest of the code is:
function()
gotoeditor()
renoise.song().selected_sample.sample_buffer_observable:remove_notifier(function()
gotoeditor()
)
end
So you’ve got a notifier following the renoise.song().selected_sample.sample_buffer_observable now (provided that the active_middle_frame was 0 when tool loaded). The notifier will fire the above function when the selected_sample.sample_buffer changes. BTW this is where you get the renoise.song() -clash, as there is no song yet.
6. Nothing happens before the selected_sample.sample_buffer changes.
- You change the sample_buffer (by loading a sample for example?). Not sure if merely changing the selected sample will fire this.
- The above function goes off, and starts by calling the gotoeditor(). Changes the active_middle_frame to 4. (Which I’m guessing is sample editor. Again, you should use ‘renoise.ApplicationWindow.MIDDLE_FRAME_SAMPLE_EDITOR’ instead of ‘4’)
- After calling gotoeditor() my thoughtstream is broken. If your code is working, I cannot understand why. As you are trying to remove an anonymous function from the observable. The anonymous function broken off from the rest of the code is:
function()
gotoeditor()
Notice there is no ‘end’ in there. So that should bog out immediately. But I’m guessing you haven’t got that far if the song()-thing troubles you…? The another thing that should not work is that you’re trying to remove an anonymous function. As such, it never has been attached and should throw a ‘the notifier function was not added’ -error.
- Conclusion: this code is wretched in many ways.
Now. What you most likely WANT to do is this.
- tool loads
- declare a variable to hold your renoise.song() -reference. Don’t point it yet to song(), as it does not exist yet.
--renoise.song() placeholder
local s = nil
- attach a notifier function my_song_load() to app_new_document_observable. Wrapped in if-then to avoid trying to attach it when it is already attached. (fires error, I think)
if not renoise.tool().app_new_document_observable:has_notifier(my_song_load)
renoise.tool().app_new_document_observable:add_notifier(my_song_load)
end
- point the placeholder to the renoise.song() function inside your my_song_load() function
function my_song_load()
s = renoise.song()
end
What you’ve done here, is first outside the function you have declared the variable s (effectively making it a ‘global’ variable, and then inside the function reference to that same variable and set that to point into song(). You cannot for instance do:
function my_song_load()
local s = renoise.song()
end
(Notice the ‘local’), because this just creates a new s -variable that is local to the function my_song_load().
5. You should have the renoise.song() under control now.
Final code:
--renoise.song() placeholder
local s = nil
function my_song_load()
s = renoise.song()
end
if not renoise.tool().app_new_document_observable:has_notifier(my_song_load)
renoise.tool().app_new_document_observable:add_notifier(my_song_load)
end
if renoise.tool().app_release_document_observable:has_notifier(my_song_load)
renoise.tool().app_release_document_observable:remove_notifier(my_song_load)
end
Notice there’s an added ‘remove notifier when the song is released’ -bit. I’m told this is supposed to be there.
Disclaimer: All of the above is written without renoise. It is likely to contain errors. The principle should be solid, though.
EDIT: major brainfart on the app_release_document_observable. I’ll leave it there, but it will not do anything… Extra credit for figuring out why that won’t fly!