diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c031a8..550b54d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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' diff --git a/README.md b/README.md index b62959d..f98b8e0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/scripts/build-unified-real.sh b/scripts/build-unified-real.sh index 6c56a14..6e926e6 100755 --- a/scripts/build-unified-real.sh +++ b/scripts/build-unified-real.sh @@ -60,6 +60,7 @@ EXPORTED_FUNCTIONS='[ "_scheduleParameterValue", "_scheduleParameterRamp", "_processGraph", + "_deinterleaveAudio", "_getGraphCurrentTime", "_setGraphCurrentTime", "_decodeMP3", diff --git a/src/javascript/nodes/AudioBufferSourceNode.js b/src/javascript/nodes/AudioBufferSourceNode.js index c94d642..5d2f72f 100644 --- a/src/javascript/nodes/AudioBufferSourceNode.js +++ b/src/javascript/nodes/AudioBufferSourceNode.js @@ -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 diff --git a/src/wasm-integration/WasmAudioEngine.js b/src/wasm-integration/WasmAudioEngine.js index c8e6499..6e21a3a 100644 --- a/src/wasm-integration/WasmAudioEngine.js +++ b/src/wasm-integration/WasmAudioEngine.js @@ -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 @@ -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 } diff --git a/test/api-suite.js b/test/api-suite.js index 62a9203..df91e1d 100644 --- a/test/api-suite.js +++ b/test/api-suite.js @@ -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`);