Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
add symmetric option (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohayonao committed Aug 11, 2017
1 parent 5a30197 commit e3f63d1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
39 changes: 35 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function encodeSync(audioData, opts) {

writeHeader(writer, format, dataView.buffer.byteLength - 8);

var err = writeData(writer, format, length, audioData);
var err = writeData(writer, format, length, audioData, opts);

if (err instanceof Error) {
throw err;
Expand Down Expand Up @@ -79,10 +79,10 @@ function writeHeader(writer, format, length) {
writer.uint16(format.bitDepth);
}

function writeData(writer, format, length, audioData) {
function writeData(writer, format, length, audioData, opts) {
var bitDepth = format.bitDepth;
var floatingPoint = format.floatingPoint ? "f" : "";
var methodName = "pcm" + bitDepth + floatingPoint;
var encoderOption = format.floatingPoint ? "f" : opts.symmetric ? "s" : "";
var methodName = "pcm" + bitDepth + encoderOption;

if (!writer[methodName]) {
return new TypeError("Not supported bit depth: " + bitDepth);
Expand Down Expand Up @@ -130,13 +130,25 @@ function createWriter(dataView) {
dataView.setUint8(pos, value, true);
pos += 1;
},
pcm8s: function(value) {
value = Math.round(value * 128) + 128;
value = Math.max(0, Math.min(value, 255));
dataView.setUint8(pos, value, true);
pos += 1;
},
pcm16: function(value) {
value = Math.max(-1, Math.min(value, +1));
value = value < 0 ? value * 32768 : value * 32767;
value = Math.round(value)|0;
dataView.setInt16(pos, value, true);
pos += 2;
},
pcm16s: function(value) {
value = Math.round(value * 32768);
value = Math.max(-32768, Math.min(value, 32767));
dataView.setInt16(pos, value, true);
pos += 2;
},
pcm24: function(value) {
value = Math.max(-1, Math.min(value, +1));
value = value < 0 ? 0x1000000 + value * 8388608 : value * 8388607;
Expand All @@ -151,13 +163,32 @@ function createWriter(dataView) {
dataView.setUint8(pos + 2, x2);
pos += 3;
},
pcm24s: function(value) {
value = Math.round(value * 8388608);
value = Math.max(-8388608, Math.min(value, 8388607));

var x0 = (value >> 0) & 0xFF;
var x1 = (value >> 8) & 0xFF;
var x2 = (value >> 16) & 0xFF;

dataView.setUint8(pos + 0, x0);
dataView.setUint8(pos + 1, x1);
dataView.setUint8(pos + 2, x2);
pos += 3;
},
pcm32: function(value) {
value = Math.max(-1, Math.min(value, +1));
value = value < 0 ? value * 2147483648 : value * 2147483647;
value = Math.round(value)|0;
dataView.setInt32(pos, value, true);
pos += 4;
},
pcm32s: function(value) {
value = Math.round(value * 2147483648);
value = Math.max(-2147483648, Math.min(value, +2147483647));
dataView.setInt32(pos, value, true);
pos += 4;
},
pcm32f: function(value) {
dataView.setFloat32(pos, value, true);
pos += 4;
Expand Down
50 changes: 42 additions & 8 deletions test/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,48 @@ const assert = require("assert");
const encoder = require("..");

const testSpec = [
{ opts: { bitDepth: 8 }, TypedArray: Uint8Array,
data: [ -1, -0.5, 0, 0.5, 1 ], expected: [ 0, 64, 128, 191, 255 ] },
{ opts: { bitDepth: 16 }, TypedArray: Int16Array,
data: [ -1, -0.5, 0, 0.5, 1 ], expected: [ -32768, -16384, 0, 16384, 32767 ] },
{ opts: { bitDepth: 32 }, TypedArray: Int32Array,
data: [ -1, -0.5, 0, 0.5, 1 ], expected: [ -2147483648, -1073741824, 0, 1073741824, 2147483647 ] },
{ opts: { bitDepth: 32, float: true }, TypedArray: Float32Array,
data: [ -1, -0.5, 0, 0.5, 1 ], expected: [ -1, -0.5, 0, 0.5, 1 ] },
{
opts: { bitDepth: 8 },
TypedArray: Uint8Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ 0, 32, 64, 128, 191, 223, 255 ],
},
{
opts: { bitDepth: 8, symmetric: true },
TypedArray: Uint8Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ 0, 32, 64, 128, 192, 224, 255 ],
},
{
opts: { bitDepth: 16 },
TypedArray: Int16Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ -32768, -24576 , -16384, 0, 16384, 24575, 32767 ],
},
{
opts: { bitDepth: 16, symmetric: true },
TypedArray: Int16Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ -32768, -24576, -16384, 0, 16384, 24576, 32767 ],
},
{
opts: { bitDepth: 32 },
TypedArray: Int32Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ -2147483648, -1610612736, -1073741824, 0, 1073741824, 1610612735, 2147483647 ],
},
{
opts: { bitDepth: 32, symmetric: true },
TypedArray: Int32Array,
data: [ -1, -0.75, -0.5, 0, 0.5, 0.75, 1 ],
expected: [ -2147483648, -1610612736, -1073741824, 0, 1073741824, 1610612736, 2147483647 ],
},
{
opts: { bitDepth: 32, float: true },
TypedArray: Float32Array,
data: [ -1, -0.5, 0, 0.5, 1 ],
expected: [ -1, -0.5, 0, 0.5, 1 ],
},
];

describe("encoding", () => {
Expand Down

0 comments on commit e3f63d1

Please # to comment.