Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
name: CI

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
push:
branches: [main, master]
pull_request:
branches: [main, master]

jobs:
test:
name: Test on Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
test:
name: Test on Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x]
strategy:
matrix:
node-version: [22.x, 24.x]

steps:
- uses: actions/checkout@v4
steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libsdl2-dev
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libsdl2-dev

- name: Install Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: 'latest'
- name: Install Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: 'latest'

- name: Install dependencies
run: npm install
- name: Install dependencies
run: npm install

- name: Build WASM modules
run: npm run build:wasm
- name: Build WASM modules
run: npm run build:wasm

- name: Run linter
run: npm run lint
- name: Run linter
run: npm run lint

- name: Check formatting
run: npm run format:check
- name: Check formatting
run: npm run format:check

- name: Run tests with coverage
run: npm run test:coverage
- name: Run tests with coverage
run: npm run test:coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: ./coverage/lcov.info
fail_ci_if_error: false
if: matrix.node-version == '22.x'
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: ./coverage/lcov.info
fail_ci_if_error: false
if: matrix.node-version == '22.x'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ npm install webaudio-node

### Requirements

- Node.js 20+
- Node.js 22+
- No build step required - uses pre-compiled WASM

## 🎵 CLI Usage
Expand Down
1 change: 1 addition & 0 deletions scripts/build-unified-real.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ EXPORTED_FUNCTIONS='[
"_scheduleParameterValue",
"_scheduleParameterRamp",
"_processGraph",
"_deinterleaveAudio",
"_getGraphCurrentTime",
"_setGraphCurrentTime",
"_decodeMP3",
Expand Down
9 changes: 6 additions & 3 deletions src/javascript/nodes/AudioBufferSourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ export class AudioBufferSourceNode extends AudioNode {
this.context._registeredBuffers = new Set();
}

if (!this.context._registeredBuffers.has(this.buffer._id)) {
const registeredBufferKey = `${this.buffer._id}:${this.context.sampleRate}`;

if (!this.context._registeredBuffers.has(registeredBufferKey)) {
const bufferData = this.buffer._getInterleavedData();
this.context._engine.registerBuffer(
this.buffer._id,
bufferData,
this.buffer.length,
this.buffer.numberOfChannels
this.buffer.numberOfChannels,
this.buffer.sampleRate
);
this.context._registeredBuffers.add(this.buffer._id);
this.context._registeredBuffers.add(registeredBufferKey);
}

// Set buffer ID (not data) in native code
Expand Down
31 changes: 27 additions & 4 deletions src/wasm-integration/WasmAudioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// All heavy lifting (graph traversal, mixing, processing) is done in WASM

import { wasmModule as defaultWasmModule } from './WasmModule.js';
import { WasmAudioDecoders } from './WasmAudioDecoders.js';

// Parameter name to ID mapping - matches C++ ParamID enum
// Eliminates malloc/copy/free overhead on every parameter change
Expand Down Expand Up @@ -195,14 +196,36 @@ export class WasmAudioEngine {
this.wasmModule._stopNode(this.graphId, nodeId, when);
}

registerBuffer(bufferId, bufferData, length, channels) {
const totalSamples = length * channels;
registerBuffer(bufferId, bufferData, length, channels, sourceSampleRate = this.sampleRate) {
let registeredBuffer = {
audioData: bufferData,
length
};

if (sourceSampleRate !== this.sampleRate) {
registeredBuffer = WasmAudioDecoders.resampleAudio(
this.wasmModule,
bufferData,
length,
channels,
sourceSampleRate,
this.sampleRate
);
}

const totalSamples = registeredBuffer.length * channels;
const bufferPtr = this.wasmModule._malloc(totalSamples * 4);

// Use HEAPF32.set() with subarray (avoids alignment issues and is fast)
copyToWasmHeap(this.wasmModule, bufferData, bufferPtr);
copyToWasmHeap(this.wasmModule, registeredBuffer.audioData, bufferPtr);

this.wasmModule._registerBuffer(this.graphId, bufferId, bufferPtr, length, channels);
this.wasmModule._registerBuffer(
this.graphId,
bufferId,
bufferPtr,
registeredBuffer.length,
channels
);
// Don't free - WASM keeps a reference
}

Expand Down
32 changes: 32 additions & 0 deletions test/api-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,38 @@ console.log('\nTest 17: Stereo Rendering');
assert(left[100] === right[100], 'Stereo channels are identical');
}

// Test 18: AudioBufferSourceNode resamples mismatched buffer sample rates
console.log('\nTest 18: AudioBufferSourceNode Resampling');
{
const inputSampleRate = 1000;
const outputSampleRate = 2000;
const inputLength = 1000;
const outputLength = Math.ceil((inputLength * outputSampleRate) / inputSampleRate);
const ctx = new OfflineAudioContext({
numberOfChannels: 1,
length: outputLength,
sampleRate: outputSampleRate
});

const buffer = ctx.createBuffer(1, inputLength, inputSampleRate);
buffer.getChannelData(0).fill(1);

const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
source.start(0);

const rendered = await ctx.startRendering();
const data = rendered.getChannelData(0);
const earlyAverage = data.subarray(100, 500).reduce((sum, value) => sum + value, 0) / 400;
const lateAverage = data.subarray(1400, 1800).reduce((sum, value) => sum + value, 0) / 400;

assert(data.length === outputLength, 'Output length matches resampled duration');
assert(rendered.sampleRate === outputSampleRate, 'Output sample rate is context sample rate');
assert(earlyAverage > 0.9, 'Resampled buffer starts near full scale');
assert(lateAverage > 0.9, 'Resampled buffer remains active for full duration');
}

// Summary
console.log(`\n${'='.repeat(50)}`);
console.log(`Test Results: ${passed} passed, ${failed} failed`);
Expand Down
Loading