Snapcast.Clock (Snapcast v0.1.0)

Copy Markdown View Source

The server's monotonic audio clock, shared by all sessions and streams.

Snapcast timestamps are {sec, usec} on an arbitrary steady clock — only differences matter (the client derives its offset from Time sync), so we use a single process-wide epoch and express everything relative to it. Keeping values small also keeps sec within int32.

Summary

Functions

Convert an absolute System.monotonic_time(:microsecond) value to this clock.

Fix the epoch (call once at startup). Safe to call repeatedly.

Current time as a snapcast {sec, usec} tv.

Microseconds since the server epoch (monotonic, non-negative).

{sec, usec} → microseconds.

Microseconds → {sec, usec}. Uses truncating div/rem so sec*1e6 + usec == us exactly, including for negatives (Time-sync deltas can be negative).

Functions

from_monotonic_us(monotonic_us)

Convert an absolute System.monotonic_time(:microsecond) value to this clock.

init()

Fix the epoch (call once at startup). Safe to call repeatedly.

now_tv()

Current time as a snapcast {sec, usec} tv.

now_us()

Microseconds since the server epoch (monotonic, non-negative).

tv_to_us(arg)

{sec, usec} → microseconds.

us_to_tv(us)

Microseconds → {sec, usec}. Uses truncating div/rem so sec*1e6 + usec == us exactly, including for negatives (Time-sync deltas can be negative).