Skip to content

Commit

Permalink
Merge f8de8b0 into 0bad66d
Browse files Browse the repository at this point in the history
  • Loading branch information
360disrupt committed Sep 19, 2019
2 parents 0bad66d + f8de8b0 commit 06f0d42
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/wavefile-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import interleave from './interleave';
import dwChannelMask from './dw-channel-mask';
import validateNumChannels from './validate-num-channels';
import validateSampleRate from './validate-sample-rate';
import {packArrayTo, packTo, unpack} from 'byte-data';
import {packArrayTo, packTo, unpack, unpackArray} from 'byte-data';

/**
* A class to read, write and create wav files.
Expand Down Expand Up @@ -176,6 +176,25 @@ export default class WaveFileCreator extends WaveFileParser {
this.dataType);
}

/**
* Return the sample at a given index.
* @param {number} startIndex The sample start index.
* @param {number} stopIndex The sample stop index.
* @return {number} The sample.
* @throws {Error} If the sample index is off range.
*/
getSamples(startIndex, stopIndex) {
startIndex = startIndex * (this.dataType.bits / 8);
stopIndex = stopIndex * (this.dataType.bits / 8);
if (stopIndex + this.dataType.bits / 8 > this.data.samples.length) {
throw new Error('Range error');
}
return unpackArray(
this.data.samples.slice(startIndex, stopIndex),
this.dataType
);
}

/**
* Set the sample at a given index.
* @param {number} index The sample index.
Expand Down
67 changes: 67 additions & 0 deletions test/src/getSamples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* WaveFile: https://github.com/rochars/wavefile
* Copyright (c) 2017-2018 Rafael da Silva Rocha, Andreas Geissinger. MIT License.
*
* Test writing 8-bit files with the fromScratch() method.
*
*/

var assert = assert || require('assert');
var WaveFile = WaveFile || require('../loader.js');

describe('getSamples(): 8-bit wave file from scratch', function() {

var wav = new WaveFile();
wav.fromScratch(1, 48000, '8', [0, 255, 2, 3]);

it('samples from 0 - 1 should be 0, 255', function() {
assert.equal(wav.getSamples(0,1), [0,255]);
});

it('samples from 0 - 4 should throw error', function() {
assert.throws(function() {wav.getSamples(0,4);}, Error);
});
});

describe('getSamples(): 16-bit wave file from scratch', function() {

var wav = new WaveFile();
wav.fromScratch(1, 48000, '16', [0, 1, -32768, 32767]);

it('samples from 0 - 1 should be 0, 255', function() {
assert.equal(wav.getSamples(0, 1), [0, 255]);
});

it('sample from 0 - 4 should throw error', function() {
assert.throws(function() {wav.getSamples(0, 4);}, Error);
});
});

describe('getSamples(): 24-bit wave file from scratch', function() {

var wav = new WaveFile();
wav.fromScratch(1, 48000, '24', [0, 1, -8388608, 8388607]);

it('samples from 0 - 1 should be 0, 1', function() {
assert.equal(wav.getSamples(0, 1), [0, 1]);
});

it('samples from 0 - 4 should throw error', function() {
assert.throws(function() {wav.getSamples(0, 4);}, Error);
});
});

describe('getSamples(): 32-bit wave file from scratch', function() {

var wav = new WaveFile();
wav.fromScratch(1, 44100, '32', [0, 1, -2147483648, 2147483647]);

it('samples from 0 - 1 should be 0, 1', function() {
assert.equal(wav.getSamples(0, 1), [0, 1]);
});

it('sample from 0 - 4 should throw error', function() {
assert.throws(function() {wav.getSamples(0, 4);}, Error);
});

});

0 comments on commit 06f0d42

Please sign in to comment.