Skip to content

Read 8 bit WAVE files with PCM datain the RIFF file format

License

Notifications You must be signed in to change notification settings

lewismoten/riff-wave-reader

Repository files navigation

Riff Wave Reader

Build Status npm version install size

This library reads the data within RIFF file with it's contents formatted as a WAVE file containing PCM data.

Live Demo on GitHub Example Waveform

Installation

This npm package is available as riff-wave-reader

From your terminal, install with the following command.

npm install riff-wave-reader --save

How to use

Node

import Reader from "riff-wave-reader/reader";
let reader;
const channel = 0;
const index = 0;

// from array
const data =
  "52 49 46 46 d5 10 00 00 57 41 56 45 66 6d 74 20 " +
  "10 00 00 00 01 00 01 00 40 1f 00 00 40 1f 00 00 " +
  "01 00 08 00 64 61 74 61 b1 10 00 00 7f";
const array = data.split(" ").map(v => parseInt(v, 16));
reader = new RiffWaveReader(array);

// To handle large files without loading them completely into memory
// from wrapped file, buffer, array, and array buffers
reader = new RiffWaveReader(new Reader("./samples/hello.wav"));
reader = new RiffWaveReader(new Reader(Buffer.from(array)));
reader = new RiffWaveReader(new Reader(array));
reader = new RiffWaveReader(new ArrayBuffer(array));

// Read header chunks
reader.readChunks().then(chunks => {
  console.log(chunks);
});
reader.readSample(channel, index).then(sample => console.log(sample));
// 127

The chunks would be written out as:

{
  "riff": {
    "tag": "RIFF",
    "size": 4309,
    "format": "WAVE"
  },
  "format": {
    "id": "fmt ",
    "size": 16,
    "type": 1,
    "channels": 1,
    "sampleRate": 8000,
    "byteRate": 8000,
    "blockAlignment": 1,
    "bitsPerSample": 8,
    "typeName": "PCM",
    "sampleSize": 1
  },
  "data": {
    "id": "data",
    "size": 4273,
    "start": 44,
    "sampleCount": 4265,
    "duration": 0.533125
  }
}

Web Browser

<script src="riff-wave-reader.js" type="text/javascript"></script>
<input type="file" change="changeFile(this.files)" />
<script type="text/javascript">
  function changeFile(files) {
    const fileReader = new FileReader();
    fileReader.onload = function(e) {
      var RiffWaveReader = window["riff-wave-reader"].RiffWaveReader;
      var riffReader = new RiffWaveReader(e.target.result);
      riffReader.readChunks().then(function(chunks) {
        console.log(chunks);
      });
    };
    fileReader.readAsArrayBuffer(files[0]);
  }
</script>

Modifications

Building

Building is done via webpack as a universal module definition (UMD) library.

From terminal

npm run build
  1. Creates a script in development mode under /docs/dist/riff-wave-reader.js
  2. Creates a script in production mode under /lib/dist/riff-wave-reader.js along with a sourcemap file

Local Testing

From terminal

npm run start

Your web browser will open http://localhost:3000/docs/index.html

About

Read 8 bit WAVE files with PCM datain the RIFF file format

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published