/wav-encoder

isomorphic wav data encoder

Primary LanguageJavaScript

wav-encoder

Build Status NPM Version License

isomorphic wav data encoder

Installation

$ npm install wav-encoder

API

WavEncoder

  • constructor([format: object])
    • format
      • bitDepth: number = 16
      • floatingPoint: boolean = false

Class methods

  • encode(audioData: AudioData, [format: object]): Promise<ArrayBuffer>

Instance methods

  • encode(audioData: AudioData, [format: object]): Promise<ArrayBuffer>
Attributes
interface AudioData {
  sampleRate: number;
  channelData: Float32Array[];
}

Usage

node.js

var fs = require("fs");
var WavEncoder = require("wav-encoder");

var audioData = {
  sampleRate: 44100,
  channelData: [
    new Float32Array(100),
    new Float32Array(100),
  ]
};

WavDecoder.encode(audioData).then(function(buffer) {
  // buffer is an instanceof Buffer
  fs.writeFileSync("foobar.wav", buffer);
});

browser

<script src="/path/to/wav-encoder.js"></script>
var audioData = {
  sampleRate: 44100,
  channelData: [
    new Float32Array(100),
    new Float32Array(100),
  ]
};

function arrayBufferToBase64(buffer) {
  return btoa([].slice.call(new Uint8Array(buffer)).map(String.fromCharCode).join(""));
}

WavEncoder.encode(audioData).then(function(buffer) {
  // buffer is an instance of ArrayBuffer
  new Audio("data:audio/wav;base64," + arrayBufferToBase64(buffer)).play();
});

License

MIT