Skip to content

Commit

Permalink
Add distortion unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaStorm committed Aug 26, 2016
1 parent 236d943 commit a3fff33
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/p5.sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -9152,6 +9152,7 @@ distortion = function () {
* @type {Object} AudioNode
*/
this.waveShaperNode = this.ac.createWaveShaper();
this.amount = amount;
this.waveShaperNode.curve = makeDistortionCurve(amount);
this.waveShaperNode.oversample = oversample;
this.input.connect(this.waveShaperNode);
Expand All @@ -9173,12 +9174,19 @@ distortion = function () {
*/
p5.Distortion.prototype.set = function (amount, oversample) {
if (amount) {
this.amount = amount;
this.waveShaperNode.curve = makeDistortionCurve(amount);
}
if (oversample) {
this.waveShaperNode.oversample = oversample;
}
};
p5.Distortion.prototype.getAmount = function () {
return this.amount;
};
p5.Distortion.prototype.getOversample = function () {
return this.waveShaperNode.oversample;
};
/**
* Send output to a p5.sound or web audio object
*
Expand All @@ -9200,8 +9208,7 @@ distortion = function () {
p5.Distortion.prototype.dispose = function () {
var index = p5sound.soundArray.indexOf(this);
p5sound.soundArray.splice(index, 1);
this.waveShaperNode.curve = null;
this.waveShaperNode.oversample = null;
this.waveShaperNode = null;
if (typeof this.output !== 'undefined') {
this.output.disconnect();
this.output = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/p5.sound.min.js

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/distortion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ define(function (require) {

var p5sound = require('master');


/*
* Adapted from [Kevin Ennis on StackOverflow](http://stackoverflow.com/questions/22312841/waveshaper-node-in-webaudio-how-to-emulate-distortion)
*/
Expand Down Expand Up @@ -58,6 +57,7 @@ define(function (require) {
*/
this.waveShaperNode = this.ac.createWaveShaper();

this.amount = amount;
this.waveShaperNode.curve = makeDistortionCurve(amount);
this.waveShaperNode.oversample = oversample;

Expand All @@ -84,13 +84,22 @@ define(function (require) {
*/
p5.Distortion.prototype.set = function(amount, oversample) {
if (amount) {
this.amount = amount;
this.waveShaperNode.curve = makeDistortionCurve(amount);
}
if (oversample) {
this.waveShaperNode.oversample = oversample;
}
}

p5.Distortion.prototype.getAmount = function() {
return this.amount;
}

p5.Distortion.prototype.getOversample = function() {
return this.waveShaperNode.oversample;
}

/**
* Send output to a p5.sound or web audio object
*
Expand All @@ -115,8 +124,7 @@ define(function (require) {
var index = p5sound.soundArray.indexOf(this);
p5sound.soundArray.splice(index, 1);

this.waveShaperNode.curve = null;
this.waveShaperNode.oversample = null;
this.waveShaperNode = null;

if (typeof this.output !== 'undefined') {
this.output.disconnect();
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require.config({
}
});

var allTests = ['tests/p5.SoundFile', 'tests/p5.Amplitude', 'tests/p5.Oscillator', 'test/p5.Distortion'];
var allTests = ['tests/p5.SoundFile', 'tests/p5.Amplitude', 'tests/p5.Oscillator', 'tests/p5.Distortion'];

p5.prototype.masterVolume(0);

Expand Down
24 changes: 24 additions & 0 deletions test/tests/p5.Distortion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
define(['chai'],
function(chai) {

var expect = chai.expect;

describe('p5.Distortion', function() {
this.timeout(1000);

var dist = new p5.Distortion();

it('can be created and disposed', function() {
var d = new p5.Distortion();
d.dispose();
});

it('can set the amount and oversample', function() {
var initialAmt = dist.getAmount();
var initialOS = dist.getOversample();
dist.set(1000, '4x');
expect(dist.getAmount()).not.equal(initialAmt);
expect(dist.getOversample()).not.equal(initialOS);
});
});
});

0 comments on commit a3fff33

Please sign in to comment.