Skip to content

feat: add clock.link.get_number_of_peers() #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 14, 2025

Conversation

robbielyman
Copy link
Contributor

@robbielyman robbielyman commented Apr 8, 2025

this PR adds _norns.clock_link_get_number_of_peers() a new Lua function which will return the number of peers connected to this Norns over Ableton Link.

related discussion on Lines:

this PR does not make a user-facing change to display the number of peers anywhere on norns, but it enables that change to be made solely in Lua. i'd happily help brainstorm or contribute that Lua change as well, if there's interest.

this commit adds `number_of_peers` to `clock_link_shared_data_t` and
keeps this data in sync with the Link session. in the header, a new
function to return this number of peers is added.

this commit does not yet expose this functionality to Lua.
this commit adds (provided `HAVE_ABLETON_LINK` is true) a new Lua
function which will return the number of peers connected via Ableton
Link. When Link functionality is disabled, this function returns 0.

this commit does not add any UI changes.
@artfwo
Copy link
Member

artfwo commented Apr 8, 2025

thanks for the contribution!

i recall there's no need to pass returned number of peers through protected link_shared_data struct, as numPeers()/abl_link_num_peers() is thread-safe, so the lua function could just call abl_link_num_peers() directly every time (saving the need to update the field in the scheduler callback, which gets called very frequently).

could you update the PR accordingly? thx!

@robbielyman
Copy link
Contributor Author

yes, i’d be happy to make that change, but unfortunately i need more guidance: since the link pointer is only present in the clock thread, the ableton function can currently only feasibly be called from that thread. changing that would require placing the link pointer somewhere accessible from the main thread, which seems to me like a can of worms better left unopened.

in case this is useful information, given that the function is marked as realtime safe in the header by Ableton, it sounds like it’s not a problem to call very frequently.

@artfwo
Copy link
Member

artfwo commented Apr 8, 2025

yes, i’d be happy to make that change, but unfortunately i need more guidance: since the link pointer is only present in the clock thread, the ableton function can currently only feasibly be called from that thread. changing that would require placing the link pointer somewhere accessible from the main thread, which seems to me like a can of worms better left unopened.

in case this is useful information, given that the function is marked as realtime safe in the header by Ableton, it sounds like it’s not a problem to call very frequently.

yeah, link object itself could be moved out of the thread, for instance declared as static in clock_link.c, and initialized in clock_link_init().

the wrapper in clock.c is also superfluous, clock_link.h is included in weaver, so the function can be called from there directly too. hope this helps! lmk if you have any more questions!

rather than managing the number of link peers automatically, the Lua
instance will now query the Link library directly.
@robbielyman
Copy link
Contributor Author

i made the change! it might now not be the case that the number of peers is zero when norns has Link disabled—i'm not likely to test what the behavior is for several days, but it would be a pretty easy check for anyone able to both build norns and connect to it via Link.

@artfwo
Copy link
Member

artfwo commented Apr 8, 2025

the number of peers should be displayed only when clock source is set to link. i'd leave that logic to the scripts.

@robbielyman
Copy link
Contributor Author

good point. well ok, i think this is ready for review :)

@artfwo
Copy link
Member

artfwo commented Apr 9, 2025

good point. well ok, i think this is ready for review :)

number_of_peers field in the shared struct is unnecessary.

@robbielyman robbielyman marked this pull request as draft April 9, 2025 19:33
@robbielyman
Copy link
Contributor Author

just got a chance to sync up with my norns; everything seems a-ok! here's a little test script to demonstrate:

function init()
  clock.run(function()
      while true do
        clock.sleep(1)
        redraw()
      end
  end)
end

function redraw()
  screen.font_size(15)
  screen.move(1,50)
  screen.clear()
  if params:get("clock_source") ~= 3 then
    screen.text("source != link")
  else
    screen.text("peers: " .. _norns.clock_link_get_number_of_peers())
  end
  screen.update()
end

to verify, make sure your norns is on the same network as another link-enabled device, set the clock_source param to "link" and join the link session with another device. the script's screen should say peers: 1 (or some other positive number matching your expectations).

@robbielyman robbielyman marked this pull request as ready for review April 10, 2025 21:49
@artfwo
Copy link
Member

artfwo commented Apr 11, 2025

Generally the _norns API is sort of reserved for libraries. I'd suggest providing a wrapper in core/clock.lua.

clock.get_num_link_peers or similar.

@robbielyman
Copy link
Contributor Author

i’m well aware of the distinction between the _norns interface and user-facing Lua changes like clock.lua. (i mean, i also know that you know that i’m aware.) as i mentioned in the PR description, making those changes is beyond the scope i set for myself.

now, i’m happy to increase the scope, since reading between the lines of your comment it sounds to me like you want me to. i think it would be helpful for me if you could communicate similar asks more directly in the future.

@artfwo
Copy link
Member

artfwo commented Apr 11, 2025

now, i’m happy to increase the scope, since reading between the lines of your comment it sounds to me like you want me to. i think it would be helpful for me if you could communicate similar asks more directly in the future.

yeah, it's a tiny change that improves the usability of the feature, so why not :) thanks for adding it!

@robbielyman robbielyman changed the title feat: add C hooks to enable querying the number of Link peers feat: add clock.link.get_number_of_peers() Apr 11, 2025
@tehn tehn merged commit 48dbb73 into monome:main Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants