2019-01-27 18:05:20 +00:00
|
|
|
import OutputAudio from "helper/OutputAudio";
|
|
|
|
import Instrument from "Tone/instrument/Instrument";
|
|
|
|
import OutputAudioStereo from "helper/OutputAudioStereo";
|
|
|
|
import Test from "helper/Test";
|
|
|
|
import Offline from "helper/Offline";
|
|
|
|
import Tone from "Tone/core/Tone";
|
|
|
|
import Meter from "helper/Meter";
|
|
|
|
|
|
|
|
export default function(Constr, note, constrArg, optionsIndex){
|
|
|
|
|
|
|
|
context("Instrument Tests", function(){
|
|
|
|
|
|
|
|
it("extends Tone.Instrument", function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
expect(instance).to.be.an.instanceof(Instrument);
|
|
|
|
instance.dispose();
|
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can connect the output", function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.connect(Test);
|
|
|
|
instance.dispose();
|
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can set the volume", function(){
|
|
|
|
if (!optionsIndex){
|
|
|
|
var instance = new Constr({
|
|
|
|
"volume" : -10
|
|
|
|
});
|
|
|
|
} else if (optionsIndex === 1){
|
|
|
|
var instance = new Constr(constrArg, {
|
|
|
|
"volume" : -10
|
|
|
|
});
|
|
|
|
}
|
|
|
|
expect(instance.volume.value).to.be.closeTo(-10, 0.1);
|
|
|
|
instance.dispose();
|
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("makes a sound", function(){
|
|
|
|
return OutputAudio(function(){
|
2016-07-07 02:33:07 +00:00
|
|
|
var instance = new Constr(constrArg);
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.toMaster();
|
|
|
|
instance.triggerAttack(note);
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("produces sound in both channels", function(){
|
|
|
|
return OutputAudioStereo(function(){
|
2016-07-07 02:33:07 +00:00
|
|
|
var instance = new Constr(constrArg);
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.toMaster();
|
|
|
|
instance.triggerAttack(note);
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("is silent before being triggered", function(){
|
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
}).then(function(buffer){
|
|
|
|
expect(buffer.isSilent()).to.be.true;
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
if (Constr.prototype.triggerRelease){
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can trigger release after attack", function(){
|
|
|
|
return Offline(function(){
|
2017-02-20 21:40:56 +00:00
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
2019-01-27 18:05:20 +00:00
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.05);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.05);
|
|
|
|
}
|
|
|
|
instance.triggerRelease(0.1);
|
|
|
|
}, 1).then(function(buffer){
|
|
|
|
expect(buffer.getFirstSoundTime()).to.be.within(0.05, 0.1);
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
2018-01-02 21:49:41 +00:00
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can trigger another attack before the release has ended", function(){
|
|
|
|
//compute the end time
|
2017-02-20 21:40:56 +00:00
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
2019-01-27 18:05:20 +00:00
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.05);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.05);
|
|
|
|
}
|
|
|
|
instance.triggerRelease(0.1);
|
|
|
|
}, 1).then(function(buffer){
|
|
|
|
return buffer.getLastSoundTime();
|
|
|
|
}).then(function(bufferDuration){
|
|
|
|
var secondTrigger = 0.15;
|
2017-04-30 18:18:50 +00:00
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.05);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.05);
|
|
|
|
}
|
|
|
|
instance.triggerRelease(0.1);
|
2019-01-27 18:05:20 +00:00
|
|
|
//star the note again before the last one has finished
|
2018-02-08 16:12:17 +00:00
|
|
|
if (note){
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.triggerAttack(note, secondTrigger);
|
2018-02-08 16:12:17 +00:00
|
|
|
} else {
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.triggerAttack(secondTrigger);
|
2018-02-08 16:12:17 +00:00
|
|
|
}
|
2019-01-27 18:05:20 +00:00
|
|
|
}, bufferDuration + secondTrigger * 2).then(function(buffer){
|
|
|
|
expect(buffer.getLastSoundTime()).to.be.gt(bufferDuration);
|
2018-02-08 16:12:17 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2018-02-08 16:12:17 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can combine triggerAttack and triggerRelease", function(){
|
2017-02-20 21:40:56 +00:00
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
2015-08-31 15:37:10 +00:00
|
|
|
if (note){
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.triggerAttackRelease(note, 0.1, 0.05);
|
2015-08-31 15:37:10 +00:00
|
|
|
} else {
|
2019-01-27 18:05:20 +00:00
|
|
|
instance.triggerAttackRelease(0.1, 0.05);
|
2015-08-31 15:37:10 +00:00
|
|
|
}
|
2017-02-20 21:40:56 +00:00
|
|
|
}, 0.2).then(function(buffer){
|
2019-01-27 18:05:20 +00:00
|
|
|
expect(buffer.getFirstSoundTime()).to.be.within(0.05, 0.1);
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
}
|
2015-08-31 15:37:10 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("be scheduled to start in the future", function(){
|
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.1);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.1);
|
|
|
|
}
|
|
|
|
}, 0.2).then(function(buffer){
|
|
|
|
expect(buffer.getFirstSoundTime()).to.be.within(0.1, 0.15);
|
2018-01-02 21:49:41 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2018-01-02 21:49:41 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can sync triggerAttack to the Transport", function(){
|
|
|
|
return Offline(function(Transport){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
instance.sync();
|
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.1);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.1);
|
|
|
|
}
|
|
|
|
Transport.start(0.1);
|
|
|
|
}, 0.3).then(function(buffer){
|
|
|
|
expect(buffer.getFirstSoundTime()).to.be.within(0.19, 0.25);
|
2018-01-02 21:49:41 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2018-01-02 21:49:41 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can unsync triggerAttack to the Transport", function(){
|
|
|
|
return Offline(function(Transport){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
instance.sync();
|
|
|
|
if (note){
|
|
|
|
instance.triggerAttack(note, 0.1);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttack(0.1);
|
|
|
|
}
|
|
|
|
instance.unsync();
|
|
|
|
Transport.start(0.1);
|
|
|
|
}, 0.3).then(function(buffer){
|
|
|
|
expect(buffer.isSilent()).to.be.true;
|
2018-01-02 21:49:41 +00:00
|
|
|
});
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
2018-01-02 21:49:41 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
it("can sync triggerAttackRelease to the Transport", function(){
|
|
|
|
return Offline(function(Transport){
|
|
|
|
var instance = new Constr(constrArg);
|
|
|
|
instance.toMaster();
|
|
|
|
instance.sync();
|
|
|
|
if (note){
|
|
|
|
instance.triggerAttackRelease(note, 0.25, 0.1);
|
|
|
|
} else {
|
|
|
|
instance.triggerAttackRelease(0.25, 0.1);
|
|
|
|
}
|
|
|
|
Transport.start(0.1);
|
|
|
|
}, 1).then(function(buffer){
|
|
|
|
expect(buffer.getFirstSoundTime()).to.be.within(0.19, 0.25);
|
|
|
|
//test a sample enough in the future for the decay to die down
|
|
|
|
var endSample = Math.floor(0.9 * Tone.context.sampleRate);
|
|
|
|
expect(buffer.getRMS()[endSample]).to.be.closeTo(0, 0.1);
|
|
|
|
});
|
2015-08-31 15:37:10 +00:00
|
|
|
});
|
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|