Tone.js/test/tests/Instruments.js

419 lines
9.7 KiB
JavaScript
Raw Normal View History

/* global it, describe */
define(["tests/Core", "chai", "Tone/instrument/DuoSynth", "Tone/instrument/MonoSynth", "Tone/instrument/FMSynth",
2015-01-06 02:56:25 +00:00
"Tone/instrument/PolySynth", "Tone/instrument/Sampler",
2014-11-30 03:03:28 +00:00
"tests/Common", "Tone/instrument/Instrument", "Tone/instrument/PluckSynth", "Tone/instrument/AMSynth",
2015-04-20 14:39:57 +00:00
"Tone/instrument/NoiseSynth", "Tone/core/Buffer", "Tone/instrument/DrumSynth"],
2015-01-06 02:56:25 +00:00
function(Tone, chai, DuoSynth, MonoSynth, FMSynth, PolySynth, Sampler, Test, Instrument,
2015-04-20 14:39:57 +00:00
PluckSynth, AMSynth, NoiseSynth, Buffer, DrumSynth){
var expect = chai.expect;
Test.onlineContext();
2014-09-20 23:24:25 +00:00
function extendsInstrument(InstrumentFactory){
var inst = new InstrumentFactory();
expect(inst).to.be.instanceOf(Instrument);
inst.dispose();
}
describe("Tone.MonoSynth", function(){
it("can be created and disposed", function(){
var ms = new MonoSynth();
ms.dispose();
2014-09-20 23:24:25 +00:00
Test.wasDisposed(ms);
});
it("extends Instrument", function(){
extendsInstrument(MonoSynth);
});
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var ms = new MonoSynth();
Test.acceptsOutput(ms);
ms.dispose();
});
it("outputs a sound", function(done){
var ms;
Test.outputsAudio(function(dest){
ms = new MonoSynth();
ms.connect(dest);
ms.triggerAttack("C4");
}, function(){
ms.dispose();
done();
});
});
2015-02-10 21:35:19 +00:00
it("can be get/set", function(){
var ms = new MonoSynth();
var values = {
"oscillator" : {
"type" : "triangle"
},
"filter" : {
"Q" : 8,
},
};
ms.set(values);
expect(ms.get()).to.contain.keys(Object.keys(values));
expect(ms.oscillator.type).to.equal(values.oscillator.type);
expect(ms.filter.Q.value).to.equal(values.filter.Q);
ms.dispose();
});
});
describe("Tone.DuoSynth", function(){
it("can be created and disposed", function(){
var ds = new DuoSynth();
ds.dispose();
2014-09-20 23:24:25 +00:00
Test.wasDisposed(ds);
});
2014-10-13 21:15:58 +00:00
2014-09-20 23:24:25 +00:00
it("extends Instrument", function(){
extendsInstrument(DuoSynth);
});
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var ds = new DuoSynth();
Test.acceptsOutput(ds);
ds.dispose();
});
it("outputs a sound", function(done){
var ds;
Test.outputsAudio(function(dest){
ds = new DuoSynth();
ds.connect(dest);
ds.triggerAttack("C4");
}, function(){
ds.dispose();
done();
});
});
2015-02-10 21:35:19 +00:00
it("can be get/set", function(){
var ds = new DuoSynth();
var values = {
"voice0" : {
"oscillator" : {
"type" : "triangle"
},
},
"voice1" : {
"oscillator" : {
"type" : "sine"
},
}
};
ds.set(values);
expect(ds.get()).to.contain.keys(Object.keys(values));
expect(ds.voice0.oscillator.type).to.equal(values.voice0.oscillator.type);
expect(ds.voice1.oscillator.type).to.equal(values.voice1.oscillator.type);
ds.dispose();
});
});
describe("Tone.FMSynth", function(){
it("can be created and disposed", function(){
var fms = new FMSynth();
fms.dispose();
2014-09-20 23:24:25 +00:00
Test.wasDisposed(fms);
});
2014-10-13 21:15:58 +00:00
2014-09-20 23:24:25 +00:00
it("extends Instrument", function(){
extendsInstrument(FMSynth);
});
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var fms = new FMSynth();
Test.acceptsOutput(fms);
fms.dispose();
});
it("outputs a sound", function(done){
var fms;
Test.outputsAudio(function(dest){
fms = new FMSynth();
fms.connect(dest);
fms.triggerAttack("C4");
}, function(){
fms.dispose();
done();
});
2015-02-10 21:35:19 +00:00
});
it("can be get/set", function(){
var fms = new FMSynth();
var values = {
"carrier" : {
"oscillator" : {
"type" : "triangle"
},
},
"modulator" : {
"oscillator" : {
"type" : "sine"
},
}
};
fms.set(values);
expect(fms.get()).to.contain.keys(Object.keys(values));
expect(fms.carrier.oscillator.type).to.equal(values.carrier.oscillator.type);
expect(fms.modulator.oscillator.type).to.equal(values.modulator.oscillator.type);
fms.dispose();
});
});
describe("Tone.PolySynth", function(){
it("can be created and disposed", function(){
var ps = new PolySynth();
ps.dispose();
2014-09-20 23:24:25 +00:00
Test.wasDisposed(ps);
});
2014-10-13 21:15:58 +00:00
2014-09-20 23:24:25 +00:00
it("extends Instrument", function(){
extendsInstrument(PolySynth);
});
2014-09-20 23:24:25 +00:00
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var psynth = new PolySynth();
Test.acceptsOutput(psynth);
psynth.dispose();
});
it("outputs a sound", function(done){
var psynth;
Test.outputsAudio(function(dest){
psynth = new PolySynth();
psynth.connect(dest);
psynth.triggerAttack("C4");
}, function(){
psynth.dispose();
done();
});
});
it("accepts a chord", function(done){
var psynth;
Test.outputsAudio(function(dest){
psynth = new PolySynth(4, DuoSynth);
psynth.connect(dest);
psynth.triggerAttackRelease(["C4", "E4", "G4"], "8n");
}, function(){
psynth.dispose();
done();
});
});
});
describe("Tone.Sampler", function(){
it("can be created and disposed", function(){
var samp = new Sampler();
samp.dispose();
2014-09-20 23:24:25 +00:00
Test.wasDisposed(samp);
});
2014-10-13 21:15:58 +00:00
2014-09-20 23:24:25 +00:00
it("extends Instrument", function(){
extendsInstrument(Sampler);
});
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var samp = new Sampler();
Test.acceptsOutput(samp);
samp.dispose();
});
2015-01-06 03:00:48 +00:00
it("flattens a nested object of samples", function(done){
var samp = new Sampler({
"A" : {
"1" : "./testAudio/kick.mp3"
},
"B" : "./testAudio/hh.mp3"
});
Buffer.onload = function(){
2015-02-10 21:35:19 +00:00
samp.sample = "A.1";
2015-01-06 03:00:48 +00:00
samp.dispose();
done();
};
});
});
2014-09-20 23:24:25 +00:00
describe("Tone.PluckSynth", function(){
it("can be created and disposed", function(){
var pluck = new PluckSynth();
pluck.dispose();
Test.wasDisposed(pluck);
});
2014-10-13 21:15:58 +00:00
2014-09-20 23:24:25 +00:00
it("extends Instrument", function(){
extendsInstrument(PluckSynth);
});
2014-10-13 21:15:58 +00:00
it("handles output connections", function(){
var psynth = new PluckSynth();
Test.acceptsOutput(psynth);
psynth.dispose();
});
it("outputs a sound", function(done){
var psynth;
Test.outputsAudio(function(dest){
psynth = new PluckSynth();
psynth.connect(dest);
psynth.triggerAttack("C4");
}, function(){
psynth.dispose();
done();
});
2015-02-10 21:35:19 +00:00
});
it("handles getter/setter", function(){
var psynth = new PluckSynth();
var values = {
"attackNoise" : 3,
"dampening" : 5000,
"resonance" : 0.3
};
psynth.set(values);
expect(psynth.get()).to.contain.keys(Object.keys(values));
expect(psynth.attackNoise).to.equal(values.attackNoise);
expect(psynth.dampening.value).to.equal(values.dampening);
expect(psynth.resonance.value).to.be.closeTo(values.resonance, 0.05);
psynth.dispose();
});
});
2014-11-03 16:43:42 +00:00
describe("Tone.AMSynth", function(){
it("can be created and disposed", function(){
var ams = new AMSynth();
ams.dispose();
Test.wasDisposed(ams);
});
it("extends Instrument", function(){
extendsInstrument(AMSynth);
});
it("handles output connections", function(){
var ams = new AMSynth();
Test.acceptsOutput(ams);
ams.dispose();
});
it("outputs a sound", function(done){
var ams;
Test.outputsAudio(function(dest){
ams = new AMSynth();
ams.connect(dest);
ams.triggerAttack("C4");
}, function(){
ams.dispose();
done();
});
2015-02-10 21:35:19 +00:00
});
it("handles getters/setters", function(){
var ams = new AMSynth();
var values = {
"harmonicity" : 3,
"carrier" : {
"filterEnvelope" : {
"min" : 20,
}
},
"modulator" : {
"filterEnvelope" : {
"min" : 400,
}
}
};
ams.set(values);
expect(ams.get()).to.contain.keys(Object.keys(values));
expect(ams.harmonicity).to.be.closeTo(values.harmonicity, 0.05);
expect(ams.carrier.filterEnvelope.min).to.be.closeTo(values.carrier.filterEnvelope.min, 0.05);
expect(ams.modulator.filterEnvelope.min).to.be.closeTo(values.modulator.filterEnvelope.min, 0.05);
ams.dispose();
});
2014-11-03 16:43:42 +00:00
});
2014-11-30 03:03:28 +00:00
describe("Tone.NoiseSynth", function(){
it("can be created and disposed", function(){
var noiseSynth = new NoiseSynth();
noiseSynth.dispose();
Test.wasDisposed(noiseSynth);
});
it("extends Instrument", function(){
extendsInstrument(NoiseSynth);
});
it("handles output connections", function(){
var noiseSynth = new NoiseSynth();
Test.acceptsOutput(noiseSynth);
noiseSynth.dispose();
});
it("outputs a sound", function(done){
var noiseSynth;
Test.outputsAudio(function(dest){
noiseSynth = new NoiseSynth();
noiseSynth.connect(dest);
noiseSynth.triggerAttack();
}, function(){
noiseSynth.dispose();
done();
});
});
});
2015-04-20 14:39:57 +00:00
describe("Tone.DrumSynth", function(){
it("can be created and disposed", function(){
var drumSynth = new DrumSynth();
drumSynth.dispose();
Test.wasDisposed(drumSynth);
});
it("extends Instrument", function(){
extendsInstrument(DrumSynth);
});
it("handles output connections", function(){
var drumSynth = new DrumSynth();
Test.acceptsOutput(drumSynth);
drumSynth.dispose();
});
it("outputs a sound", function(done){
var drumSynth;
Test.outputsAudio(function(dest){
drumSynth = new DrumSynth();
drumSynth.connect(dest);
drumSynth.triggerAttack("C2");
}, function(){
drumSynth.dispose();
done();
});
});
it("handles getters/setters", function(){
var drumSynth = new DrumSynth();
var values = {
"pitchDecay" : 0.06,
"octaves" : 9,
"oscillator" : {
"type" : "triangle",
},
"envelope" : {
"attack" : 0.01,
}
};
drumSynth.set(values);
expect(drumSynth.get()).to.contain.keys(Object.keys(values));
expect(drumSynth.pitchDecay).to.be.closeTo(values.pitchDecay, 0.001);
expect(drumSynth.octaves).to.be.closeTo(values.octaves, 0.001);
expect(drumSynth.envelope.attack).to.be.closeTo(drumSynth.envelope.attack, 0.001);
drumSynth.dispose();
});
});
});