Skip to content

Clock features#498

Merged
tehn merged 3 commits intomonome:mainfrom
evnoj:clock-features
Dec 17, 2025
Merged

Clock features#498
tehn merged 3 commits intomonome:mainfrom
evnoj:clock-features

Conversation

@evnoj
Copy link
Copy Markdown
Contributor

@evnoj evnoj commented Dec 9, 2025

Consider this as a draft.

I've added 3 features to the clock system. I'm happy to make changes, amend this PR to drop some of the features, or split this into multiple PRs. If you don't want these features, that's fine too.

For any features you do want to merge, I can also add the corresponding documentation to the docs repo. I can also go through the steps to make a release if desired.


clock.sync offset capability

e2c23ff adds an optional offset parameter to clock.sync(), which is intended to work exactly as it does on norns. The implementation closely follows the norns implementation.

I figure you're familiar with how this works, but let me know if you'd like me to elaborate on this feature.


clock.handlers.tempo_change user-defined handler

ce0028b adds a handler that is called when the clock tempo changes, like clock.tempo_change_handler on norns. Since crow modifies the clock metatable to prevent setting new indices, I took the approach of using the existing clock.transport table, but added a new key clock.handlers that points to this same table. We could simply have clock.transport.tempo_change to do away with the new handlers key. We could also set clock.tempo_change_handler to an empty function that would then always be called when the tempo changes, but this would allow the user to set clock.tempo_change_handler = function(tempo) ... end like on norns.


clock.time_since_last_input() to check when an external clock trigger was last received

b661e39 adds a function clock.time_since_last_input() which returns the time in seconds since a trigger was received at an input set to drive the clock by input[n].mode('clock', div). If no input has been set to drive the clock, it returns -1.

I wanted to make a script that could be externally clocked, but only if an input was actually receiving a clock. Because there is no "jack detection", the only way I could think to do this is with a timeout that switches back to being unclocked if no external clock is received for some amount of time. I could not figure out a way to access the time that the last external trigger was received, although it is tracked internally in the C layer. I simply added a getter for this value. This enables a "timeout" like this:

function await_clock()
    input[2].mode( 'change', 3, 0.1, 'rising' )
    input[2].change = function()
        input[2].mode( 'clock', 1/4)
        print('clocked')
        clock_timeout:start()
    end
end

clock_timeout = metro.init{
    event = function()
        if clock.time_since_last_input() > 4 then -- 4 second timeout
            clock_timeout:stop()
            await_clock()
            unclocked()
        end
    end,
    time  = 1.0,
    count = -1
}

function unclocked()
    print('unclocked')
end

await_clock()

Let me know if you have questions, concerns, or want changes!

evnoj added 3 commits December 9, 2025 11:58
I needed this function to automatically switch between internal and external clock with a timeout system
@tehn
Copy link
Copy Markdown
Member

tehn commented Dec 9, 2025 via email

@evnoj
Copy link
Copy Markdown
Contributor Author

evnoj commented Dec 10, 2025

I just realized I left an unneeded #include "caw.h" in lib/clock.c because I was using Caw.printf while testing. I'll remove it in that commit with a force push during cleanup before merging, after we've decided on any other changes that need to be made.

Copy link
Copy Markdown
Member

@tehn tehn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good to me. haven't tested on hardware.

maybe @trentgill will have a minute to glance at this, otherwise we can merge end of the week.

@trentgill
Copy link
Copy Markdown
Collaborator

all looks good to me at a glance - i had no idea the clock lib had been extended on norns :/

won't have a chance to test this any time soon, but if @evnoj has done a good amount, then i'm happy to merge!

thanks for the work here!

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