Skip to content

fix(voice): honor pcm16 audio sample rate#1909

Merged
cjol merged 3 commits into
mainfrom
fix/voice-pcm16-sample-rate
Jul 10, 2026
Merged

fix(voice): honor pcm16 audio sample rate#1909
cjol merged 3 commits into
mainfrom
fix/voice-pcm16-sample-rate

Conversation

@cjol

@cjol cjol commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a sampleRate option to VoiceAgentOptions for raw PCM output.
  • Include sampleRate in the server audio_config message so clients can play pcm16 at the provider-native rate.
  • Update VoiceClient to read sampleRate from audio_config and use it when constructing AudioBuffer instances for raw pcm16 payloads.
  • Preserve the existing 16 kHz fallback when the server omits sampleRate.
  • Add protocol and playback coverage for configured 24 kHz PCM and default 16 kHz behavior.

Validation

  • pnpm run check

Diff scope

This replacement PR is based directly on origin/main and contains only the voice sample-rate commit.


Open in Devin Review

Add a sampleRate option to VoiceAgentOptions and include it in the audio_config message sent when a call starts. This lets servers declare the native rate of raw pcm16 output while preserving the existing 16 kHz default.

Teach VoiceClient to read sampleRate from audio_config and use it when constructing AudioBuffer instances for pcm16 playback instead of always assuming 16 kHz. Encoded formats continue to rely on browser decoding.

Add protocol and playback tests for configured 24 kHz pcm16 output and the default 16 kHz fallback, including a dedicated test Durable Object binding.
@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6bacf49

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/voice Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 2 potential issues.

Open in Devin Review

Comment thread packages/voice/src/voice-client.ts Outdated
Comment on lines +680 to +682
if (typeof msg.sampleRate === "number" && msg.sampleRate > 0) {
this.#sampleRate = msg.sampleRate;
}

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.

🟡 Sample rate from a previous call leaks into the next call when the server omits it

The sample rate is only updated when the server sends a positive number (voice-client.ts:680-682) but is never reset to the default 16000 when the field is absent, so a stale value from a prior call persists.

Impact: Audio from a second call could play at the wrong speed (e.g. 24 kHz instead of 16 kHz) if the server omits the sample rate.

Trigger condition and mechanism

#sampleRate is initialized to 16000 at voice-client.ts:273 and conditionally set inside the audio_config handler at voice-client.ts:680-682. Unlike #audioFormat (which is unconditionally assigned at voice-client.ts:679), #sampleRate is only written when msg.sampleRate is a positive number. Neither endCall() (voice-client.ts:538-548), startCall() (voice-client.ts:499-536), nor #stopLocalCall() (voice-client.ts:560-571) resets it.

The VoiceServerMessage type at types.ts:47 declares sampleRate as optional (sampleRate?: number), so a server that sends { type: "audio_config", format: "pcm16" } without sampleRate is valid. If a prior call set #sampleRate to 24000, the second call's pcm16 audio will be decoded at 24 kHz instead of the default 16 kHz, producing distorted playback.

Suggested change
if (typeof msg.sampleRate === "number" && msg.sampleRate > 0) {
this.#sampleRate = msg.sampleRate;
}
if (typeof msg.sampleRate === "number" && msg.sampleRate > 0) {
this.#sampleRate = msg.sampleRate;
} else {
this.#sampleRate = 16000;
}
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +653 to +655
get sampleRate(): number {
return this.#sampleRate;
}

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.

🟡 Missing changeset for public API additions to a published package

New public API surface (a sampleRate getter on VoiceClient at voice-client.ts:653-655 and a sampleRate option on VoiceAgentOptions at voice.ts:131) is added to @cloudflare/voice without a corresponding changeset entry.

Impact: The feature will not appear in the changelog and the package version will not be bumped on release.

Rule and affected locations

AGENTS.md states: "Changes to packages/ that affect the public API or fix bugs need a changeset." The existing .changeset/voice-transcriber-readiness.md covers a different feature (transcriber readiness). This PR introduces a new public getter VoiceClient.sampleRate and a new configuration option VoiceAgentOptions.sampleRate, both of which are public API additions requiring their own changeset.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1909

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1909

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1909

create-think

npm i https://pkg.pr.new/create-think@1909

hono-agents

npm i https://pkg.pr.new/hono-agents@1909

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1909

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1909

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1909

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1909

commit: 6bacf49

cjol and others added 2 commits July 10, 2026 12:06
…s it; add changeset

Co-Authored-By: Beta-Devin AI <248786709+beta-devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Beta-Devin AI <248786709+beta-devin-ai-integration[bot]@users.noreply.github.com>
@cjol cjol merged commit 63491bd into main Jul 10, 2026
4 checks passed
@cjol cjol deleted the fix/voice-pcm16-sample-rate branch July 10, 2026 11:16
@github-actions github-actions Bot mentioned this pull request Jul 10, 2026
@cjol cjol linked an issue Jul 10, 2026 that may be closed by this pull request
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.

voice agent: support configuring the sample rate of the audio file

1 participant