Skip to content

Commit ef20da3

Browse files
authored
Merge pull request #664 from KevinGrajeda/main
fix distortion and add example
2 parents f9604c9 + 9c1c68c commit ef20da3

File tree

2 files changed

+83
-20
lines changed

2 files changed

+83
-20
lines changed

src/distortion.js

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,72 @@ function makeDistortionCurve(amount) {
3030
* @class p5.Distortion
3131
* @extends p5.Effect
3232
* @constructor
33-
* @param {Number} [amount=0.25] Unbounded distortion amount.
34-
* Normal values range from 0-1.
35-
* @param {String} [oversample='none'] 'none', '2x', or '4x'.
33+
* @param {Number} [amount] Unbounded distortion amount.
34+
* Normal values range from 0-1 (defaults to 0.25)
35+
* @param {String} [oversample] 'none', '2x' (default), or '4x'.
36+
* @example
37+
* <div><code>
38+
* let osc, distortion, fft;
3639
*
40+
* function setup() {
41+
* let cnv = createCanvas(100, 100);
42+
* fft = new p5.FFT(0, 256);
43+
*
44+
* osc = new p5.TriOsc();
45+
* osc.amp(0.3);
46+
* osc.freq(375);
47+
*
48+
* distortion = new p5.Distortion();
49+
* distortion.process(osc);
50+
* cnv.mousePressed(oscStart);
51+
* }
52+
*
53+
* function draw() {
54+
* background(220);
55+
* // set the amount based on mouseX
56+
* let amount = constrain(map(mouseX, 0, width, 0, 1), 0, 1);
57+
*
58+
* // multiply the amount to smooth the value
59+
* distortion.set(amount * amount);
60+
*
61+
* noStroke();
62+
* fill(0);
63+
* text('tap to play', 10, 20);
64+
* text('amount: ' + amount, 10, 40);
65+
*
66+
* // draw the waveform
67+
* var samples = fft.waveform();
68+
* drawOscilloscope(samples);
69+
* }
70+
*
71+
* //function based on distortion example
72+
* function drawOscilloscope(samples) {
73+
* var yTranslateScope = 20;
74+
* var scopeWidth = width;
75+
* var scopeHeight = height;
76+
*
77+
* stroke(0);
78+
* strokeWeight(1);
79+
* noFill();
80+
*
81+
* beginShape();
82+
* for (var sampleIndex in samples) {
83+
* var x = map(sampleIndex, 0, samples.length, 0, scopeWidth);
84+
* var y = map(samples[sampleIndex], -1, 1, -scopeHeight / 4, scopeHeight / 4);
85+
* vertex(x, y + scopeHeight / 2 + yTranslateScope);
86+
* }
87+
* endShape();
88+
* }
89+
*
90+
* function oscStart() {
91+
* osc.start();
92+
* }
93+
*
94+
* function mouseReleased() {
95+
* osc.stop();
96+
* }
97+
*
98+
* </code></div>
3799
*/
38100
class Distortion extends Effect {
39101
constructor(amount, oversample) {
@@ -61,8 +123,7 @@ class Distortion extends Effect {
61123
* @property {AudioNode} WaveShaperNode
62124
*/
63125
this.waveShaperNode = this.ac.createWaveShaper();
64-
65-
this.amount = curveAmount;
126+
this.amount = amount;
66127
this.waveShaperNode.curve = makeDistortionCurve(curveAmount);
67128
this.waveShaperNode.oversample = oversample;
68129

@@ -76,9 +137,10 @@ class Distortion extends Effect {
76137
*
77138
* @method process
78139
* @for p5.Distortion
79-
* @param {Number} [amount=0.25] Unbounded distortion amount.
140+
* @param {Object} src An object that outputs audio
141+
* @param {Number} [amount] Unbounded distortion amount.
80142
* Normal values range from 0-1.
81-
* @param {String} [oversample='none'] 'none', '2x', or '4x'.
143+
* @param {String} [oversample] 'none', '2x', or '4x'.
82144
*/
83145
process(src, amount, oversample) {
84146
src.connect(this.input);
@@ -90,14 +152,15 @@ class Distortion extends Effect {
90152
*
91153
* @method set
92154
* @for p5.Distortion
93-
* @param {Number} [amount=0.25] Unbounded distortion amount.
155+
* @param {Number} [amount] Unbounded distortion amount.
94156
* Normal values range from 0-1.
95-
* @param {String} [oversample='none'] 'none', '2x', or '4x'.
157+
* @param {String} [oversample] 'none', '2x', or '4x'.
96158
*/
97159
set(amount, oversample) {
98-
if (amount) {
160+
if (typeof amount === 'number') {
99161
var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000);
100-
this.amount = curveAmount;
162+
//this.amount = curveAmount;
163+
this.amount = amount;
101164
this.waveShaperNode.curve = makeDistortionCurve(curveAmount);
102165
}
103166
if (oversample) {

test/tests/p5.Distortion.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('p5.Distortion', function () {
1010
expect(distortion.waveShaperNode).to.have.property('context').to.not.be
1111
.null;
1212
//default case
13-
expect(distortion.amount).to.equal(500);
13+
expect(distortion.amount).to.equal(0.25);
1414
expect(distortion.waveShaperNode.oversample).to.equal('2x');
1515

1616
distortion.dispose();
@@ -19,23 +19,23 @@ describe('p5.Distortion', function () {
1919

2020
it('can be created with only amount as a parameter', function () {
2121
let distortion = new p5.Distortion(0.3);
22-
expect(distortion.amount).to.equal(600);
22+
expect(distortion.amount).to.equal(0.3);
2323
expect(distortion.waveShaperNode.oversample).to.equal('2x');
2424

2525
//non-numerical value
2626
expect(() => new p5.Distortion('a')).to.throw();
2727
});
2828
it('can be created with only oversample as a parameter', function () {
2929
let distortion = new p5.Distortion(undefined, '4x');
30-
expect(distortion.amount).to.equal(500);
30+
expect(distortion.amount).to.equal(0.25);
3131
expect(distortion.waveShaperNode.oversample).to.equal('4x');
3232

3333
//non-numerical value
3434
expect(() => new p5.Distortion(undefined, 500)).to.throw();
3535
});
3636
it('can be created with both parameters', function () {
3737
let distortion = new p5.Distortion(0.8, '4x');
38-
expect(distortion.amount).to.equal(1600);
38+
expect(distortion.amount).to.equal(0.8);
3939
expect(distortion.waveShaperNode.oversample).to.equal('4x');
4040
});
4141

@@ -48,27 +48,27 @@ describe('p5.Distortion', function () {
4848

4949
//both params
5050
distortion.process(osc, 0.62, '4x');
51-
expect(distortion.amount).to.equal(1240);
51+
expect(distortion.amount).to.equal(0.62);
5252
expect(distortion.waveShaperNode.oversample).to.equal('4x');
5353
});
5454
it('can set amount and oversample', function () {
5555
let distortion = new p5.Distortion();
5656
//only one param
5757
distortion.set(0.98);
58-
expect(distortion.amount).to.equal(1960);
58+
expect(distortion.amount).to.equal(0.98);
5959
expect(distortion.waveShaperNode.oversample).to.equal('2x');
6060
distortion.set(undefined, '4x');
61-
expect(distortion.amount).to.equal(1960);
61+
expect(distortion.amount).to.equal(0.98);
6262
expect(distortion.waveShaperNode.oversample).to.equal('4x');
6363

6464
//both params
6565
distortion.set(0.14, '2x');
66-
expect(distortion.amount).to.equal(280);
66+
expect(distortion.amount).to.equal(0.14);
6767
expect(distortion.waveShaperNode.oversample).to.equal('2x');
6868
});
6969
it('can get amount', function () {
7070
let distortion = new p5.Distortion(0.43, '2x');
71-
expect(distortion.getAmount()).to.equal(860);
71+
expect(distortion.getAmount()).to.equal(0.43);
7272
});
7373
it('can get over sample', function () {
7474
let distortion = new p5.Distortion(0.43, '4x');

0 commit comments

Comments
 (0)