dotwav

Warning: This is a work in progress and the API isn't currently stable yet!

Usage no npm install needed!

<script type="module">
  import dotwav from 'https://cdn.skypack.dev/dotwav';
</script>

README

Warning: This is a work in progress and the API isn't currently stable yet!

Motivation

Needed a lib that provided both an encode and a decode function compatible with Typescript and with a simple API. Couldn't find exactly what I wanted so I adapted some code mainly from mohayonao/wav-encoder and mohayonao/wav-decoder.

Get started

Install

npm install --save unit-fns
# or
yarn add unit-fns

Use

import { encode, decode } from 'dotwav'
const fs = require('fs')

const whiteNoise1sec = {
  sampleRate: 44100,
  channelData: [
    new Float32Array(44100).map(() => Math.random() - 0.5),
    new Float32Array(44100).map(() => Math.random() - 0.5),
  ],
}

fs.writeFileSync('noise.wav', new Buffer(encode(whiteNoise1sec)))

const readFile = filepath => {
  return new Promise((resolve, reject) => {
    fs.readFile(filepath, (err, buffer) => {
      if (err) {
        return reject(err)
      }
      return resolve(buffer)
    })
  })
}

readFile('noise.wav').then(buffer => {
  const audioData = decode(buffer)
  console.log(audioData.sampleRate)
  console.log(audioData.channelData[0])
  console.log(audioData.channelData[1])
})

License

MIT