concat-audio-buffers

raw JSON →
1.0.0 verified Mon Apr 27 auth: no javascript maintenance

A basic audio buffer concatenator that works with Rollup. Version 1.0.0 (last released 2020) is stable but not actively maintained. It combines multiple AudioBuffers into one, with optional stereo conversion. Unlike more complex audio processing libraries, this package is minimal and focused solely on concatenation, making it suitable for simple audio merging tasks in bundlers like Rollup.

error module not found: can't resolve 'concat-audio-buffers'
cause Missing installation or incorrect bundler configuration for ESM
fix
Run 'npm install concat-audio-buffers' and ensure your bundler supports ESM imports
error TypeError: concatAudioBuffers is not a function
cause Using CommonJS require() instead of ESM import
fix
Change to 'import concatAudioBuffers from 'concat-audio-buffers''
error Uncaught TypeError: Cannot read properties of undefined (reading 'length')
cause Passing undefined or non-array as first argument
fix
Ensure buffers is an array of AudioBuffer objects
breaking Callback-based API; no Promise or async/await support
fix Wrap the call in a Promise if async usage is needed: new Promise((resolve, reject) => concatAudioBuffers(buffers, channels, (err, result) => err ? reject(err) : resolve(result)));
deprecated No updates since 2020; may not work with newer bundlers or Node.js versions
fix Consider using modern alternatives like 'audio-buffer-utils' or implement your own with AudioBuffer.copyToChannel
gotcha The function does not validate input; passing non-AudioBuffer objects or incorrect channel count may cause silent failures
fix Validate buffers array and channel number before calling
gotcha The callback receives (error, combinedBuffer) but error is null on success; ensure error-first callback pattern
fix Check if error is truthy before using combinedBuffer
npm install concat-audio-buffers
yarn add concat-audio-buffers
pnpm add concat-audio-buffers

Shows how to create two AudioBuffers, concatenate them with stereo conversion, and play the result as a WAV blob.

import concatAudioBuffers from 'concat-audio-buffers';
import toWav from 'audiobuffer-to-wav';

const audioCtx = new AudioContext();
const createBuffer = (freq, duration) => {
  const sampleRate = audioCtx.sampleRate;
  const buffer = audioCtx.createBuffer(1, sampleRate * duration, sampleRate);
  const data = buffer.getChannelData(0);
  for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(2 * Math.PI * freq * i / sampleRate);
  }
  return buffer;
};

const buffers = [createBuffer(440, 1), createBuffer(880, 1)];

concatAudioBuffers(buffers, 2, (error, combinedBuffer) => {
  if (error) {
    console.error(error);
  } else {
    const blob = new Blob([toWav(combinedBuffer)], { type: 'audio/wav' });
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    audio.play();
  }
});