@@ -30,10 +30,72 @@ function makeDistortionCurve(amount) {
30
30
* @class p5.Distortion
31
31
* @extends p5.Effect
32
32
* @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;
36
39
*
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>
37
99
*/
38
100
class Distortion extends Effect {
39
101
constructor ( amount , oversample ) {
@@ -62,7 +124,8 @@ class Distortion extends Effect {
62
124
*/
63
125
this . waveShaperNode = this . ac . createWaveShaper ( ) ;
64
126
65
- this . amount = curveAmount ;
127
+ //this.amount = curveAmount;
128
+ this . amount = amount ;
66
129
this . waveShaperNode . curve = makeDistortionCurve ( curveAmount ) ;
67
130
this . waveShaperNode . oversample = oversample ;
68
131
@@ -76,9 +139,10 @@ class Distortion extends Effect {
76
139
*
77
140
* @method process
78
141
* @for p5.Distortion
79
- * @param {Number } [amount=0.25] Unbounded distortion amount.
142
+ * @param {Object } Signal An object that outputs audio
143
+ * @param {Number } [amount] Unbounded distortion amount.
80
144
* Normal values range from 0-1.
81
- * @param {String } [oversample='none' ] 'none', '2x', or '4x'.
145
+ * @param {String } [oversample] 'none', '2x', or '4x'.
82
146
*/
83
147
process ( src , amount , oversample ) {
84
148
src . connect ( this . input ) ;
@@ -90,14 +154,15 @@ class Distortion extends Effect {
90
154
*
91
155
* @method set
92
156
* @for p5.Distortion
93
- * @param {Number } [amount=0.25 ] Unbounded distortion amount.
157
+ * @param {Number } [amount] Unbounded distortion amount.
94
158
* Normal values range from 0-1.
95
- * @param {String } [oversample='none' ] 'none', '2x', or '4x'.
159
+ * @param {String } [oversample] 'none', '2x', or '4x'.
96
160
*/
97
161
set ( amount , oversample ) {
98
- if ( amount ) {
162
+ if ( typeof amount === 'number' ) {
99
163
var curveAmount = p5 . prototype . map ( amount , 0.0 , 1.0 , 0 , 2000 ) ;
100
- this . amount = curveAmount ;
164
+ //this.amount = curveAmount;
165
+ this . amount = amount ;
101
166
this . waveShaperNode . curve = makeDistortionCurve ( curveAmount ) ;
102
167
}
103
168
if ( oversample ) {
0 commit comments