Tone.js/test/tests/Core.js

119 lines
3.7 KiB
JavaScript
Raw Normal View History

2014-08-22 00:54:18 +00:00
/* global it, describe, after */
2014-07-30 19:10:45 +00:00
define(["chai", "Tone/core/Tone", "Tone/core/Master", "Tone/core/Bus"], function(chai, Tone, Master){
2014-06-19 02:33:08 +00:00
var expect = chai.expect;
describe("AudioContext", function(){
this.timeout(3000);
it ("was created", function(){
expect(Tone.context).to.be.instanceof(AudioContext);
});
it ("has OscillatorNode", function(){
expect(Tone.context.createOscillator).to.be.instanceof(Function);
});
it ("clock running", function(done){
var interval = setInterval(function(){
if (Tone.context.currentTime > 0){
clearInterval(interval);
done();
}
}, 20);
});
it ("has current API", function(){
expect(OscillatorNode.prototype.start).to.be.instanceof(Function);
expect(AudioBufferSourceNode.prototype.start).to.be.instanceof(Function);
expect(AudioContext.prototype.createGain).to.be.instanceof(Function);
});
});
2014-08-22 00:54:18 +00:00
describe("Tone", function(){
var tone = new Tone();
after(function(){
tone.dispose();
});
it("correctly calculates samples to seconds", function(){
var sampleRate = tone.context.sampleRate;
expect(tone.samplesToSeconds(100)).to.equal(100/sampleRate);
expect(tone.samplesToSeconds(800)).to.equal(800/sampleRate);
});
it("can convert gain to db", function(){
expect(tone.gainToDb(0)).to.equal(-Infinity);
expect(tone.gainToDb(1)).is.closeTo(0, 0.1);
expect(tone.gainToDb(0.5)).is.closeTo(-6, 0.1);
});
it("can convert db to gain", function(){
expect(tone.dbToGain(0)).is.closeTo(1, 0.1);
expect(tone.dbToGain(-12)).is.closeTo(0.25, 0.1);
expect(tone.dbToGain(-24)).is.closeTo(0.125, 0.1);
});
it("can convert back and forth between db and gain representations", function(){
expect(tone.dbToGain(tone.gainToDb(0))).is.closeTo(0, 0.01);
expect(tone.dbToGain(tone.gainToDb(0.5))).is.closeTo(0.5, 0.01);
expect(tone.gainToDb(tone.dbToGain(1))).is.closeTo(1, 0.01);
});
it("returns a default argument when the given is not defined", function(){
expect(tone.defaultArg(undefined, 0)).is.equal(0);
expect(tone.defaultArg(undefined, "also")).is.equal("also");
expect(tone.defaultArg("hihi", 100)).is.equal("hihi");
});
it("handles default arguments on an object", function(){
expect(tone.defaultArg({"b" : 10}, {"a" : 4, "b" : 10})).has.property("a", 4);
expect(tone.defaultArg({"b" : 10}, {"a" : 4, "b" : 10})).has.property("b", 10);
expect(tone.defaultArg({"b" : {"c" : 10}}, {"b" : {"c" : 20}})).has.deep.property("b.c", 10);
expect(tone.defaultArg({"a" : 10}, {"b" : {"c" : 20}})).has.deep.property("b.c", 20);
});
it("can convert notes into frequencies", function(){
expect(tone.noteToFrequency("A4")).to.be.closeTo(440, 0.0001);
expect(tone.noteToFrequency("Bb4")).to.be.closeTo(466.163761, 0.0001);
});
it("can convert frequencies into notes", function(){
expect(tone.frequencyToNote(440)).to.equal("A4");
expect(tone.frequencyToNote(4978.031739553295)).to.equal("D#8");
});
});
describe("Tone.Master", function(){
it ("exists", function(){
expect(Tone.Master).to.equal(Master);
});
});
describe("Tone.Bus", function(){
it ("provides a send and receive method", function(){
expect(Tone.prototype.send).is.a("function");
expect(Tone.prototype.receive).is.a("function");
});
});
2014-07-30 19:10:45 +00:00
describe("Tone.setContext", function(){
2014-07-30 19:10:45 +00:00
it ("can set a new context", function(){
var origCtx = Tone.context;
var ctx = new OfflineAudioContext(2, 44100, 44100);
Tone.setContext(ctx);
expect(Tone.context).to.equal(ctx);
expect(Tone.prototype.context).to.equal(ctx);
//then set it back
Tone.setContext(origCtx);
expect(Tone.context).to.equal(origCtx);
expect(Tone.prototype.context).to.equal(origCtx);
//and a saftey check
expect(ctx).to.not.equal(origCtx);
});
});
2014-07-30 19:10:45 +00:00
2014-06-19 02:33:08 +00:00
});