Skip to content

Let stream/clear drop only audio from a timestamp on - #336

Merged
kahrendt merged 2 commits into
mainfrom
feat/stream-clear-from
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
feat/stream-clear-from

Conversation

@maximmaxim345

@maximmaxim345 maximmaxim345 commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Adds an optional from_timestamp to stream/clear, so a server can replace a player's buffered audio from a point on without waiting for the buffer to play out or leaving a silent gap.

Why

Servers can currently only append audio after everything a player has buffered, or clear the whole buffer and start over after a silent gap. With a large buffer, a changed next track or an announcement either waits for the buffer to play out or cuts to silence first.

What changes

  • With from_timestamp, players and visualizers drop only buffered data at or after it and keep playing everything before it. The server sends the replacement from that point.
  • The cut is per chunk: the server picks the timestamp of a chunk it sent, so players never split a chunk.
  • The cut has to be at least the larger of min_buffer_ms and required_lead_time_ms, plus output_delay_ms, ahead of the message. While a stream runs, required_lead_time_ms also has to cover the audio a player has buffered in its audio backend and can't discard. If a cut still lands inside that audio, it plays and the player drops the new audio up to where it ends.
  • Timestamps after the clear can be earlier than the chunks before it, and the clear doesn't change the stream's format.
  • Without from_timestamp, stream/clear works as before.

Breaking changes

  • Clients: yes, but it fails softly. Every player and visualizer has to support from_timestamp. A client that ignores it does a full clear instead, so it goes silent until the cut and then continues.
  • Servers: no. A server that never sends from_timestamp keeps working unchanged.

Announcements

This is also especially useful for announcements. The separate announcement role in #126 puts most of the work on the client: a second stream, mixing and ducking. With this change most of it moves to the server, which mixes the announcement into the music itself, so it's likely simpler for clients. It covers most of what #126 does, and grouped players stay in sync.

To play the announcement louder than the music, the server turns the music down further by the amount it raises the volume, and leaves a short gap of turned-down music before and after the clip. It raises the volume during the gap before the clip and restores it during the gap after. The gap is needed because volume commands aren't timed to the audio. This only works on players that accept volume commands. It also adds a short delay before the announcement, and other controllers see the volume go up and back down.

A separate announcement role would still be needed for:

  • the client knowing an announcement is playing, for example to stop only the announcement locally
  • announcing on devices without the player role, or on an idle speaker without it showing as playing
  • a second server announcing while another server plays music

How a server could play an announcement

  1. Pick a start time for the announcement. It has to be far enough ahead that each target player has a chunk start between its minimum lead and that time.
  2. Send each target player a stream/clear with from_timestamp set to that chunk start.
  3. Resend the music from the cut. Keep it unchanged up to the start time, then fade it down and mix in the clip, leaving a short gap of turned-down music before and after the clip. If the player's current format differs from the replacement's, send a stream/start first.
  4. To make the clip louder than the music, turn the music down further by the amount you raise the volume. Send the volume command during the gap before the clip and restore it during the gap after, so the music's level stays the same when the command lands.
  5. After the clip, raise the music back to its normal level and keep streaming on the same timeline. Players that weren't targeted keep playing unchanged.

Trade-offs

  • A cut that drops an in-place format change, like a buffered next track at another sample rate, doesn't roll the format back. The server has to send another stream/start before a replacement in the old format. I went with this since rolling back would need every client to keep a format history.
  • With Opus, the player's decoder has already decoded the dropped packets, so the first replacement packet can cause a brief artifact at the join. Clients shouldn't need a change for this. A server can avoid it by keeping its encoder state at the cut. FLAC and PCM are not affected.
  • I considered a separate required_clear_lead_time_ms field, so a player with a slow cold start doesn't delay every announcement by its startup time. The clients below report fixed values between 150 and 500 ms, so I don't think it's worth one more field.
  • A player with a deep audio backend buffer, like an ESPHome speaker, reports a higher required_lead_time_ms while playing. In a group, it pushes the cut later for every player. I made this a requirement on required_lead_time_ms instead of a new field, since a lead measured from a stream/clear on a running pipeline already includes waiting for that audio.

Client effort

I had Claude check three clients, so take this with a grain of salt. It looks moderate in all of them. Each already keeps a server timestamp on every buffered chunk, so dropping the tail itself is easy. The main work is dropping only the chunks at or after the cut that are still being decoded or queued when the clear arrives, since today a clear drops all of them.

Client Rough size Where buffered audio sits Main work required_lead_time_ms today
sendspin-js 100 to 150 lines Web Audio sources scheduled up to 20 s ahead, each can be stopped In-flight decodes are dropped by a counter every clear bumps, needs a check against the cut fixed 250 ms, may be close to Bluetooth output latency
sendspin-cpp 150 to 250 lines Encoded chunks in the inbound ring Drop the tail and release its buffer quota as soon as the message arrives 150 ms, covers the example sink, on ESPHome it also has to cover the speaker buffers it can't clear (Sendspin/sendspin-cpp#55)
sendspin-rs 200 to 400 lines Decoded audio in a sorted queue, tail drop is one retain The counter that fences off old audio needs a cut-aware version, touches public traits 500 ms, plenty

Sizes are rough estimates and exclude tests.

Related:

Servers could only append new audio after everything already buffered,
or clear the whole buffer and leave a silent gap, so a changed next
track or an announcement had to wait out up to the full send-ahead.
@kahrendt

kahrendt commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

The cut lead assumes a player can drop any audio more than max(min_buffer_ms, required_lead_time_ms) + output_delay_ms ahead, but nothing in the spec says how much audio a player has already handed off to its output and can't take back. required_lead_time_ms is measured at startup, when the output buffer is empty, so it doesn't capture that. min_buffer_ms doesn't describe it either; some buffers in a stack can sit empty waiting for the next chunk without anything audible.

On ESPHome, sendspin-cpp can add a way to clear the speaker's buffers (Sendspin/sendspin-cpp#55), but some of the audio stack can never be cleared. Should the player report that amount, either as its own field or as a stated requirement on required_lead_time_ms? And what should a player do when a cut lands inside audio it has already committed?

@maximmaxim345

Copy link
Copy Markdown
Member Author

Hmm, yeah. I was assuming that required_lead_time_ms already covered audio buffered in the audio backend, but it was never stated in the spec.
So I went ahead with adding a requirement to it, since I don't think a separate field would add much information. It is now only in effect while the stream runs.
Thanks for catching that!

@kahrendt kahrendt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks!

@kahrendt
kahrendt merged commit 75d7e1d into main Nov 4, 2026
1 check passed
@kahrendt
kahrendt deleted the feat/stream-clear-from branch November 4, 2026 14:37
@github-project-automation github-project-automation Bot moved this from Triage to Done in Sendspin Project Board Nov 4, 2026
maximmaxim345 added a commit to Sendspin/aiosendspin that referenced this pull request Nov 16, 2026
# What does this implement/fix?

<!-- Quick description and explanation of changes. -->

`PushStream.clear_from(timestamp_us)` replaces the audio committed from
a timestamp on, for example when the next track changes after it was
already sent ahead. Clients keep what they buffered before the cut and
the next commit continues at it, without a gap or a new `stream/start`.

Each player is cleared from the start of its last chunk at or before the
cut, since `from_timestamp` must be the timestamp of a sent chunk, and
the audio up to the cut is sent again. The visualizer is cleared at the
cut.

It waits for a running commit and returns False without changing
anything when the cut can't be made, for example when it is closer than
a player's lead time, so the caller can fall back to `clear()`. Clients
that don't know `from_timestamp` treat the message as a full clear, so
the caller decides when to use it.

**Related issue or spec PR (if applicable):**

- Sendspin/spec#336

## Types of changes

<!--
Tick exactly one box. CI (.github/workflows/pr-labels.yaml) applies the
matching label, and the release notes use it to sort this change.
-->

- [ ] Bugfix (non-breaking change which fixes an issue): `bugfix`
- [x] New feature (non-breaking change which adds functionality):
`new-feature`
- [ ] Enhancement to an existing feature: `enhancement`
- [ ] Breaking change (changes the public API or the protocol in a way
that is not backwards compatible): `breaking-change`
- [ ] Refactor (no behavior change): `refactor`
- [ ] Documentation only: `documentation`
- [ ] Maintenance / chore: `maintenance`
- [ ] CI / workflow change: `ci`
- [ ] Dependencies bump: `dependencies`
maximmaxim345 added a commit to Sendspin/sendspin-js that referenced this pull request Nov 25, 2026
# What does this implement/fix?

<!-- Quick description and explanation of changes. -->

A `stream/clear` with `from_timestamp` now drops only the audio from
that time on, and the player keeps playing what it buffered before it.
Until now the player cleared everything, so when a server replaced a
buffered next track, the rest of the current track went silent.

Apps get the timestamp as a new argument of `onStreamClear`, on
application roles and on `SendspinCore`. Chunks the clear keeps keep
their `generation`, so a `SendspinCore` app can receive them after
chunks of a newer one.

Tested by hand in the Music Assistant web player against a server that
replaces the next track. With 6.0.0, changing the next track near the
end of a track left the rest of the track silent. With this change, the
track played to its end and the new next track followed without a gap.

**Related issue or spec PR (if applicable):**

- Sendspin/spec#336

## Types of changes

<!--
Tick exactly one box. CI (.github/workflows/pr-labels.yaml) applies the
matching label, and the release notes use it to sort this change.
-->

- [ ] Bugfix (non-breaking change which fixes an issue): `bugfix`
- [x] New feature (non-breaking change which adds functionality):
`new-feature`
- [ ] Enhancement to an existing feature: `enhancement`
- [ ] Breaking change (changes the public API or the protocol in a way
that is not backwards compatible): `breaking-change`
- [ ] Refactor (no behavior change): `refactor`
- [ ] Documentation only: `documentation`
- [ ] Maintenance / chore: `maintenance`
- [ ] CI / workflow change: `ci`
- [ ] Dependencies bump: `dependencies`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants