Notify when a new sample is manually loaded

Hello,

Does anyone have or know of a way to notify or make a tool aware of when a new sample has been manually loaded to the current sample buffer (from the Renoise browser or a file manager, etc)?

My tool makes a copy of the current buffer as soon as it is loaded and before it process it. I’m trying to deal with the possibility that a user may use the tool to process the current buffer, then decide to change the buffer with another sample. In this case, I looking to make a copy of the new current buffer once loaded, and to reset the tool to its default values.

Have you tried this?

-- Buffer.
renoise.song().instruments[].samples[].sample_buffer, _observable
  -> [read-only, renoise.SampleBuffer object]

I’m actually not quite sure how to try that out, I guess in particular, what to do with _observable which is probably what I would need, right?

This is one thing that confuses me about Lua docs and I guess Renoise docs in particular, this property, _observable -> ... style.

if you has the selected sample and your function “function_x” you can:

local function func_x()
  print("func_x")
  --your code
end

local function sample_buffer_notifier_2(boolean)
  print("sample_buffer_notifier_2")
  if (boolean) then
    local sam=renoise.song().selected_sample
    if (not sam.sample_buffer_observable:has_notifier(func_x)) then
      sam.sample_buffer_observable:add_notifier(func_x)
    end
  else
    local ins=renoise.song().selected_intrument
    for s=1,#ins.samples do
      local sam=ins:samples(s)
      if (sam.sample_buffer_observable:has_notifier(func_x)) then
        sam.sample_buffer_observable:remove_notifier(func_x)
      end
    end
  end
end

local function sample_buffer_notifier_1(boolean)
  print("sample_buffer_notifier_1")
  if (boolean) then
    if (not renoise.song().selected_sample_observable:has_notifier(sample_buffer_notifier_2)) then
      renoise.song().selected_sample_observable:add_notifier(sample_buffer_notifier_2)
    end
  else
    if (renoise.song().selected_sample_observable:has_notifier(sample_buffer_notifier_2)) then
      renoise.song().selected_sample_observable:remove_notifier(sample_buffer_notifier_2)
    end  
  end
end

function sample_buffer_notifier_1(true)  -- notifiy the selection of current sample
function sample_buffer_notifier_1(false) -- destroy this observable

--function sample_buffer_notifier_2(true) -- from the current sample, notify if the buffer changes
--function sample_buffer_notifier_2(false) -- from the current instrument, destroy these observables for all samples

Code directly written here, without testing it personally, but it should work if I’m not mistaken. Obviously, you should adapt it to your needs.

Every time you change the element (instrument, sample) you must add/remove the observables necessary for its correct functioning.

Oh wow, thank you for this. I’ll try it out. So sample_buffer, _observable translates to sample_buffer_observable, huh?

Yes. The Renoise API documentation is a bit confusing (a bit prepared for programmers with a bit of experience). But if you read all the documentation in detail you will find some examples that clarify it…

--------------------------------------------------------------------------------
-- renoise.Document.Observable
--------------------------------------------------------------------------------

-------- Functions

-- Checks if the given function, method was already registered as notifier.
observable:has_notifier(function or (object, function) or (function, object))
  -> [boolean]

-- Register a function or method as a notifier, which will be called as soon as
-- the observable's value changed.
observable:add_notifier(function or (object, function) or (function, object))

-- Unregister a previously registered notifier. When only passing an object to
-- remove_notifier, all notifier functions that match the given object will be
-- removed; a.k.a. all methods of the given object are removed. They will not
-- fire errors when none are attached.
observable:remove_notifier(function or (object, function) or
 (function, object) or (object))

Thanks. I did read that sections, but without knowing the "sample_buffer, _observable" translates to "sample_buffer_observable", I had no idea how to access an observable in the first place.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.