2017-10-26 04:49:42 +00:00
|
|
|
define(["helper/Basic", "Tone/source/Oscillator", "helper/Offline", "helper/SourceTests",
|
2018-02-05 18:55:37 +00:00
|
|
|
"helper/OscillatorTests", "helper/OutputAudio", "Tone/core/Transport", "helper/CompareToFile"],
|
2018-05-28 22:01:03 +00:00
|
|
|
function(BasicTests, Oscillator, Offline, SourceTests, OscillatorTests, OutputAudio, Transport, CompareToFile){
|
2015-08-21 21:03:48 +00:00
|
|
|
|
|
|
|
describe("Oscillator", function(){
|
|
|
|
|
|
|
|
//run the common tests
|
|
|
|
BasicTests(Oscillator);
|
|
|
|
SourceTests(Oscillator);
|
|
|
|
OscillatorTests(Oscillator);
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("matches a file", function(){
|
|
|
|
return CompareToFile(function(){
|
2018-06-02 02:02:05 +00:00
|
|
|
var osc = new Oscillator().toMaster();
|
2018-02-05 18:55:37 +00:00
|
|
|
osc.type = "square";
|
|
|
|
osc.start(0).stop(0.2);
|
2018-05-28 22:01:03 +00:00
|
|
|
}, "oscillator.wav", 0.005);
|
2018-02-05 18:55:37 +00:00
|
|
|
});
|
|
|
|
|
2015-08-21 21:03:48 +00:00
|
|
|
context("Get/Set", function(){
|
2017-10-26 04:49:42 +00:00
|
|
|
|
2015-08-21 21:03:48 +00:00
|
|
|
it("can be set with an options object", function(){
|
|
|
|
var osc = new Oscillator();
|
|
|
|
osc.set({
|
|
|
|
"frequency" : 231,
|
|
|
|
"detune" : -21,
|
|
|
|
"type" : "square"
|
|
|
|
});
|
|
|
|
expect(osc.frequency.value).to.equal(231);
|
|
|
|
expect(osc.detune.value).to.equal(-21);
|
|
|
|
expect(osc.type).to.equal("square");
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can be get the values as an object", function(){
|
|
|
|
var osc = new Oscillator(450, "square");
|
|
|
|
expect(osc.get().frequency).to.equal(450);
|
|
|
|
expect(osc.get().type).to.equal("square");
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("Phase Rotation", function(){
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can change the phase to 90", function(){
|
2017-02-20 19:06:44 +00:00
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Oscillator({
|
2015-08-21 21:03:48 +00:00
|
|
|
"phase" : 90,
|
|
|
|
"frequency" : 1
|
|
|
|
});
|
2017-02-20 19:06:44 +00:00
|
|
|
instance.toMaster();
|
2015-08-21 21:03:48 +00:00
|
|
|
instance.start(0);
|
2017-02-20 19:06:44 +00:00
|
|
|
}, 1).then(function(buffer){
|
|
|
|
buffer.forEach(function(sample, time){
|
|
|
|
if (time < 0.25){
|
|
|
|
expect(sample).to.be.within(-1, 0);
|
2017-10-26 04:49:42 +00:00
|
|
|
} else if (time > 0.25 && time < 0.5){
|
2017-02-20 19:06:44 +00:00
|
|
|
expect(sample).to.be.within(0, 1);
|
|
|
|
}
|
|
|
|
});
|
2015-08-21 21:03:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can change the phase to -90", function(){
|
2017-02-20 19:06:44 +00:00
|
|
|
return Offline(function(){
|
|
|
|
var instance = new Oscillator({
|
2015-08-21 21:03:48 +00:00
|
|
|
"phase" : 270,
|
|
|
|
"frequency" : 1
|
|
|
|
});
|
2017-02-20 19:06:44 +00:00
|
|
|
instance.toMaster();
|
2015-08-21 21:03:48 +00:00
|
|
|
instance.start(0);
|
2017-02-20 19:06:44 +00:00
|
|
|
}, 1).then(function(buffer){
|
|
|
|
buffer.forEach(function(sample, time){
|
|
|
|
if (time < 0.25){
|
|
|
|
expect(sample).to.be.within(0, 1);
|
2017-10-26 04:49:42 +00:00
|
|
|
} else if (time > 0.25 && time < 0.5){
|
2017-02-20 19:06:44 +00:00
|
|
|
expect(sample).to.be.within(-1, 0);
|
|
|
|
}
|
|
|
|
});
|
2015-08-21 21:03:48 +00:00
|
|
|
});
|
|
|
|
});
|
2017-10-26 04:49:42 +00:00
|
|
|
|
2015-08-21 21:03:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
context("Type", function(){
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can get and set the type", function(){
|
2015-08-21 21:03:48 +00:00
|
|
|
var osc = new Oscillator({
|
|
|
|
"type" : "sawtooth",
|
|
|
|
});
|
|
|
|
expect(osc.type).to.equal("sawtooth");
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("handles 4 basic types", function(){
|
2015-08-21 21:03:48 +00:00
|
|
|
var osc = new Oscillator();
|
|
|
|
var types = ["triangle", "sawtooth", "sine", "square"];
|
|
|
|
for (var i = 0; i < types.length; i++){
|
|
|
|
osc.type = types[i];
|
|
|
|
expect(osc.type).to.equal(types[i]);
|
|
|
|
}
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("throws an error if invalid type is set", function(){
|
2015-08-21 21:03:48 +00:00
|
|
|
var osc = new Oscillator();
|
|
|
|
expect(function(){
|
|
|
|
osc.type = "invalid";
|
|
|
|
}).to.throw(Error);
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can set extended types", function(){
|
2015-08-21 21:03:48 +00:00
|
|
|
var osc = new Oscillator();
|
|
|
|
osc.type = "sine5";
|
|
|
|
expect(osc.type).to.equal("sine5");
|
|
|
|
osc.type = "triangle2";
|
|
|
|
expect(osc.type).to.equal("triangle2");
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-31 20:42:35 +00:00
|
|
|
context("Partials", function(){
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can pass partials in the constructor", function(){
|
2015-08-31 20:42:35 +00:00
|
|
|
var osc = new Oscillator({
|
|
|
|
"partials" : [1, 0.3, 0.3],
|
|
|
|
"type" : "custom"
|
|
|
|
});
|
|
|
|
expect(osc.type).to.equal("custom");
|
|
|
|
expect(osc.partials[1]).to.equal(0.3);
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("can set partials", function(){
|
2015-08-31 20:42:35 +00:00
|
|
|
var osc = new Oscillator();
|
|
|
|
osc.partials = [1, 0.2, 0.2, 0.2];
|
|
|
|
expect(osc.type).to.equal("custom");
|
|
|
|
expect(osc.partials[1]).to.equal(0.2);
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
2018-02-05 18:55:37 +00:00
|
|
|
it("makes a sound with custom partials", function(){
|
2017-02-20 19:06:44 +00:00
|
|
|
return OutputAudio(function(){
|
|
|
|
var osc = new Oscillator().toMaster().start();
|
2015-08-31 20:42:35 +00:00
|
|
|
osc.partials = [1, 0.2, 0.2, 0.2];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-09 23:45:01 +00:00
|
|
|
it("outputs the partials of the given waveform", function(){
|
|
|
|
var osc = new Oscillator();
|
2015-08-31 20:42:35 +00:00
|
|
|
osc.type = "sine2";
|
|
|
|
expect(osc.type).to.equal("sine2");
|
2018-12-09 23:45:01 +00:00
|
|
|
expect(osc.partials.length).to.equal(2);
|
|
|
|
expect(osc.partials).to.deep.equal([1, 1]);
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("partialCount is 0 when set to max", function(){
|
|
|
|
var osc = new Oscillator();
|
|
|
|
expect(osc.partialCount).to.equal(0);
|
|
|
|
osc.type = "square32";
|
|
|
|
expect(osc.partialCount).to.equal(32);
|
|
|
|
osc.type = "square";
|
|
|
|
expect(osc.partialCount).to.equal(0);
|
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can pass in number of partials into constructor", function(){
|
|
|
|
var osc = new Oscillator({
|
|
|
|
"type" : "sine",
|
|
|
|
"partialCount" : 3
|
|
|
|
});
|
|
|
|
expect(osc.type).to.equal("sine3");
|
|
|
|
expect(osc.partialCount).to.equal(3);
|
2015-08-31 20:42:35 +00:00
|
|
|
osc.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-08-21 21:03:48 +00:00
|
|
|
context("Synchronization", function(){
|
2017-02-20 19:06:44 +00:00
|
|
|
it("can sync the frequency to the Transport", function(){
|
|
|
|
return Offline(function(Transport){
|
2015-08-21 21:03:48 +00:00
|
|
|
Transport.bpm.value = 120;
|
2017-02-20 19:06:44 +00:00
|
|
|
var osc = new Oscillator(2);
|
|
|
|
osc.frequency.toMaster();
|
2015-08-21 21:03:48 +00:00
|
|
|
osc.syncFrequency();
|
|
|
|
Transport.bpm.value = 240;
|
2017-02-20 19:06:44 +00:00
|
|
|
}).then(function(buffer){
|
|
|
|
expect(buffer.value()).to.be.closeTo(4, 0.001);
|
2015-08-31 20:42:35 +00:00
|
|
|
});
|
|
|
|
});
|
2015-08-21 21:03:48 +00:00
|
|
|
|
2017-02-20 19:06:44 +00:00
|
|
|
it("can unsync the frequency from the Transport", function(){
|
|
|
|
return Offline(function(Transport){
|
2015-08-21 21:03:48 +00:00
|
|
|
Transport.bpm.value = 120;
|
2017-02-20 19:06:44 +00:00
|
|
|
var osc = new Oscillator(2);
|
|
|
|
osc.frequency.toMaster();
|
2015-08-21 21:03:48 +00:00
|
|
|
osc.syncFrequency();
|
|
|
|
Transport.bpm.value = 240;
|
|
|
|
osc.unsyncFrequency();
|
2017-02-20 19:06:44 +00:00
|
|
|
}).then(function(buffer){
|
|
|
|
expect(buffer.value()).to.be.closeTo(2, 0.001);
|
2015-08-31 20:42:35 +00:00
|
|
|
});
|
|
|
|
});
|
2015-08-21 21:03:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-10-26 04:49:42 +00:00
|
|
|
});
|