Skip to content

Add resampleAudioBuffer utility - #90

Merged
KristinnRoach merged 1 commit into
mainfrom
resample-audio-buffer
Oct 2, 2026
Merged

KristinnRoach merged 1 commit into
mainfrom
resample-audio-buffer

Conversation

@KristinnRoach

@KristinnRoach KristinnRoach commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

Exports resampleAudioBuffer(buffer, sampleRate), which renders an AudioBuffer at another rate via OfflineAudioContext. Returns the input unchanged when rates match; rejects for unsupported rates.

SamplePlayer requires buffers at the context's sample rate, so consumers (e.g. hljod-smali's sample-rate switch) can use this instead of their own conversion code. Loading behavior is unchanged.

Summary by CodeRabbit

  • New Features
    • Added audio resampling so audio buffers can be converted to a requested sample rate, including to match a playback context. Buffers already at the requested rate remain unchanged. Unsupported sample rates are rejected.

SamplePlayer requires buffers at the context's sample rate; consumers need a shared way to convert imported, recorded or previously loaded buffers.
@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Essentials

Run ID: f97e611b-302b-48af-ac04-dec888de60c0

📥 Commits

Reviewing files that changed from the base of the PR and between 6afd27b and 894c006.

📒 Files selected for processing (4)
  • .changeset/resample-audio-buffer.md
  • src/index.ts
  • src/utils/audiodata/convert/resampleAudioBuffer.browser.test.ts
  • src/utils/audiodata/convert/resampleAudioBuffer.ts

Included review availability: This review used your included allowance. 3 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 4 reviews per hour. Your free on-demand review promotion remains active until October 9, 2026 at 6:00 PM UTC.


Walkthrough

The package adds and exports resampleAudioBuffer. The function returns the original buffer when its sample rate matches the requested rate. Otherwise, it renders the buffer at the requested rate through an OfflineAudioContext. Browser tests and a minor changeset are included.

Changes

Audio buffer resampling

Layer / File(s) Summary
Resampling function and package API
src/utils/audiodata/convert/resampleAudioBuffer.ts, src/index.ts, src/utils/audiodata/convert/resampleAudioBuffer.browser.test.ts, .changeset/resample-audio-buffer.md
Adds and exports resampleAudioBuffer. Browser tests check sample rate, length, channel count, a sample value, and rejection for target rate 0. The changeset describes the function and its use case.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Merge Risk: ⚪ Minimal · up to 894c0

The new resampling utility appears ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: adding the resampleAudioBuffer utility.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (1 skipped: 1 …
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Add a public AudioBuffer resampling utility

✨ Enhancement 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Export a reusable way to match an AudioBuffer to a target sample rate before loading it into
 SamplePlayer.
• Preserve duration and channels during conversion; return matching-rate buffers unchanged and
 reject unsupported rates.
• Add browser tests and a minor-release changeset without changing SamplePlayer loading behavior.
Diagram

graph TD
  C["Consumer"] --> U["Resampling utility"] --> R{"Rates match?"} -->|Yes| I["Original buffer"] --> P["SamplePlayer"]
  R -->|No| O["Offline rendering"] --> B["Resampled buffer"] --> P
Loading
High-Level Assessment

Keep resampling explicit and reusable rather than adding it to SamplePlayer.loadLayers: this serves other AudioBuffer consumers while preserving the loader's current sample-rate checks.

Files changed (4) +49 / -0

Enhancement (2) +23 / -0
index.tsExport resampleAudioBuffer from the package +1/-0

Export resampleAudioBuffer from the package

• Makes the new utility available through the package's main entry point.

src/index.ts

resampleAudioBuffer.tsRender AudioBuffers at a requested sample rate +22/-0

Render AudioBuffers at a requested sample rate

• Adds an async utility that returns matching-rate buffers unchanged or renders them through OfflineAudioContext. It preserves channel count and rounds the target duration to whole frames.

src/utils/audiodata/convert/resampleAudioBuffer.ts

Tests (1) +21 / -0
resampleAudioBuffer.browser.test.tsTest browser-based buffer resampling +21/-0

Test browser-based buffer resampling

• Checks the output rate, length, channel count, and sample value after conversion. Also checks rejection of an unsupported target rate.

src/utils/audiodata/convert/resampleAudioBuffer.browser.test.ts

Other (1) +5 / -0
resample-audio-buffer.mdDeclare a minor release for the new utility +5/-0

Declare a minor release for the new utility

• Adds a minor changeset for @kidlib/web-audio and describes resampling buffers before SamplePlayer.loadLayers.

.changeset/resample-audio-buffer.md

@KristinnRoach
KristinnRoach merged commit a6c7d64 into main Oct 2, 2026
3 checks passed
@KristinnRoach
KristinnRoach deleted the resample-audio-buffer branch October 2, 2026 16:49
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.

1 participant