Skip to content

Commit

Permalink
index_test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dlepaux committed Mar 15, 2018
1 parent d6561d6 commit baffdad
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';

/**
* Get expect function for testing
*/
import chai from "chai";
let expect = chai.expect;
import utils from "./../src/utils";
import analyzer from "./../src/analyzer";
import "web-audio-test-api";


/**
* Unit test for the RealTime BPM Analyzer
*/

describe('RealTime BPM Analyzer', () => {

/**
* Test Utility functions
*/

describe('Utils', () => {

it('Test onLoop function', (done) => {
utils.loopOnThresolds((object, thresold, stop) => {
// We add an entry to object
object.foo = thresold;
// Stop the loop at first iteration
stop(true);
}, (object) => {
// Check if object have only ONE entry
expect(JSON.stringify(object)).to.be.equal('{"foo":0.8999999999999999}');
done();
});
});

it('Test callback (second param) without onLoop', (done) => {
utils.loopOnThresolds(null, (object) => {
expect(JSON.stringify(object)).to.be.equal('{}');
done();
});
});

it('Test Object Model generation', (done) => {
utils.generateObjectModel(false, (object) => {
expect(JSON.stringify(object)).to.be.not.equal('{}');
done();
});
});
});


/**
* Test Analyzer functions
*/

describe('Analyzer', () => {
it('Test low pass filter applying', (done) => {

var audioContext = new (global.WebAudioTestAPI && global.WebAudioTestAPI.AudioContext)();
// Create an empty three-second stereo buffer at the sample rate of the AudioContext
var buffer = audioContext.createBuffer(2, audioContext.sampleRate * 3, audioContext.sampleRate);
// Fill the buffer with white noise;
// just random values between -1.0 and 1.0
for (var channel = 0; channel < buffer.numberOfChannels; channel++) {
// This gives us the actual array that contains the data
var nowBuffering = buffer.getChannelData(channel);
for (var i = 0; i < buffer.length; i++) {
// Math.random() is in [0; 1.0]
// audio needs to be in [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}

const source = analyzer.getLowPassSource(buffer, global.WebAudioTestAPI.OfflineAudioContext);
expect(typeof source.buffer).to.be.not.equal('AudioBuffer');
done();
});


it('Test onLoop function', (done) => {
// findPeaksAtThresold(data, thresold, *offset, *callback)
// -- EASY

//
// getTopCandidates(candidates)
// -- EASY
// identifyIntervals(peaks)
// -- MEDIUM
// groupByTempo(sampleRate)
// -- MEDIUM
// computeBPM(data, callback)
// -- HARD
done();
});
});
});

0 comments on commit baffdad

Please sign in to comment.