|
1 | 1 | import p5sound from './master';
|
| 2 | +import Oscillator from './oscillator'; |
2 | 3 |
|
3 | 4 | // generate noise buffers
|
4 | 5 | const _whiteNoiseBuffer = (function () {
|
@@ -70,97 +71,98 @@ const _brownNoiseBuffer = (function () {
|
70 | 71 | * @param {String} type Type of noise can be 'white' (default),
|
71 | 72 | * 'brown' or 'pink'.
|
72 | 73 | */
|
73 |
| -p5.Noise = function (type) { |
74 |
| - var assignType; |
75 |
| - p5.Oscillator.call(this); |
76 |
| - delete this.f; |
77 |
| - delete this.freq; |
78 |
| - delete this.oscillator; |
| 74 | +class Noise extends Oscillator { |
| 75 | + constructor(type) { |
| 76 | + super(); |
| 77 | + var assignType; |
| 78 | + delete this.f; |
| 79 | + delete this.freq; |
| 80 | + delete this.oscillator; |
79 | 81 |
|
80 |
| - if (type === 'brown') { |
81 |
| - assignType = _brownNoiseBuffer; |
82 |
| - } else if (type === 'pink') { |
83 |
| - assignType = _pinkNoiseBuffer; |
84 |
| - } else { |
85 |
| - assignType = _whiteNoiseBuffer; |
| 82 | + if (type === 'brown') { |
| 83 | + assignType = _brownNoiseBuffer; |
| 84 | + } else if (type === 'pink') { |
| 85 | + assignType = _pinkNoiseBuffer; |
| 86 | + } else { |
| 87 | + assignType = _whiteNoiseBuffer; |
| 88 | + } |
| 89 | + this.buffer = assignType; |
86 | 90 | }
|
87 |
| - this.buffer = assignType; |
88 |
| -}; |
89 | 91 |
|
90 |
| -p5.Noise.prototype = Object.create(p5.Oscillator.prototype); |
| 92 | + /** |
| 93 | + * Set type of noise to 'white', 'pink' or 'brown'. |
| 94 | + * White is the default. |
| 95 | + * |
| 96 | + * @method setType |
| 97 | + * @param {String} [type] 'white', 'pink' or 'brown' |
| 98 | + */ |
| 99 | + setType(type) { |
| 100 | + switch (type) { |
| 101 | + case 'white': |
| 102 | + this.buffer = _whiteNoiseBuffer; |
| 103 | + break; |
| 104 | + case 'pink': |
| 105 | + this.buffer = _pinkNoiseBuffer; |
| 106 | + break; |
| 107 | + case 'brown': |
| 108 | + this.buffer = _brownNoiseBuffer; |
| 109 | + break; |
| 110 | + default: |
| 111 | + this.buffer = _whiteNoiseBuffer; |
| 112 | + } |
| 113 | + if (this.started) { |
| 114 | + var now = p5sound.audiocontext.currentTime; |
| 115 | + this.stop(now); |
| 116 | + this.start(now + 0.01); |
| 117 | + } |
| 118 | + } |
91 | 119 |
|
92 |
| -/** |
93 |
| - * Set type of noise to 'white', 'pink' or 'brown'. |
94 |
| - * White is the default. |
95 |
| - * |
96 |
| - * @method setType |
97 |
| - * @param {String} [type] 'white', 'pink' or 'brown' |
98 |
| - */ |
99 |
| -p5.Noise.prototype.setType = function (type) { |
100 |
| - switch (type) { |
101 |
| - case 'white': |
102 |
| - this.buffer = _whiteNoiseBuffer; |
103 |
| - break; |
104 |
| - case 'pink': |
105 |
| - this.buffer = _pinkNoiseBuffer; |
106 |
| - break; |
107 |
| - case 'brown': |
108 |
| - this.buffer = _brownNoiseBuffer; |
109 |
| - break; |
110 |
| - default: |
111 |
| - this.buffer = _whiteNoiseBuffer; |
| 120 | + getType() { |
| 121 | + return this.buffer.type; |
112 | 122 | }
|
113 |
| - if (this.started) { |
| 123 | + start() { |
| 124 | + if (this.started) { |
| 125 | + this.stop(); |
| 126 | + } |
| 127 | + this.noise = p5sound.audiocontext.createBufferSource(); |
| 128 | + this.noise.buffer = this.buffer; |
| 129 | + this.noise.loop = true; |
| 130 | + this.noise.connect(this.output); |
114 | 131 | var now = p5sound.audiocontext.currentTime;
|
115 |
| - this.stop(now); |
116 |
| - this.start(now + 0.01); |
117 |
| - } |
118 |
| -}; |
119 |
| - |
120 |
| -p5.Noise.prototype.getType = function () { |
121 |
| - return this.buffer.type; |
122 |
| -}; |
123 |
| - |
124 |
| -p5.Noise.prototype.start = function () { |
125 |
| - if (this.started) { |
126 |
| - this.stop(); |
| 132 | + this.noise.start(now); |
| 133 | + this.started = true; |
127 | 134 | }
|
128 |
| - this.noise = p5sound.audiocontext.createBufferSource(); |
129 |
| - this.noise.buffer = this.buffer; |
130 |
| - this.noise.loop = true; |
131 |
| - this.noise.connect(this.output); |
132 |
| - var now = p5sound.audiocontext.currentTime; |
133 |
| - this.noise.start(now); |
134 |
| - this.started = true; |
135 |
| -}; |
136 | 135 |
|
137 |
| -p5.Noise.prototype.stop = function () { |
138 |
| - var now = p5sound.audiocontext.currentTime; |
139 |
| - if (this.noise) { |
140 |
| - this.noise.stop(now); |
141 |
| - this.started = false; |
| 136 | + stop() { |
| 137 | + var now = p5sound.audiocontext.currentTime; |
| 138 | + if (this.noise) { |
| 139 | + this.noise.stop(now); |
| 140 | + this.started = false; |
| 141 | + } |
142 | 142 | }
|
143 |
| -}; |
144 | 143 |
|
145 |
| -p5.Noise.prototype.dispose = function () { |
146 |
| - var now = p5sound.audiocontext.currentTime; |
| 144 | + dispose() { |
| 145 | + var now = p5sound.audiocontext.currentTime; |
147 | 146 |
|
148 |
| - // remove reference from soundArray |
149 |
| - var index = p5sound.soundArray.indexOf(this); |
150 |
| - p5sound.soundArray.splice(index, 1); |
| 147 | + // remove reference from soundArray |
| 148 | + var index = p5sound.soundArray.indexOf(this); |
| 149 | + p5sound.soundArray.splice(index, 1); |
151 | 150 |
|
152 |
| - if (this.noise) { |
153 |
| - this.noise.disconnect(); |
154 |
| - this.stop(now); |
| 151 | + if (this.noise) { |
| 152 | + this.noise.disconnect(); |
| 153 | + this.stop(now); |
| 154 | + } |
| 155 | + if (this.output) { |
| 156 | + this.output.disconnect(); |
| 157 | + } |
| 158 | + if (this.panner) { |
| 159 | + this.panner.disconnect(); |
| 160 | + } |
| 161 | + this.output = null; |
| 162 | + this.panner = null; |
| 163 | + this.buffer = null; |
| 164 | + this.noise = null; |
155 | 165 | }
|
156 |
| - if (this.output) { |
157 |
| - this.output.disconnect(); |
158 |
| - } |
159 |
| - if (this.panner) { |
160 |
| - this.panner.disconnect(); |
161 |
| - } |
162 |
| - this.output = null; |
163 |
| - this.panner = null; |
164 |
| - this.buffer = null; |
165 |
| - this.noise = null; |
166 |
| -}; |
| 166 | +} |
| 167 | + |
| 168 | +export default Noise; |
0 commit comments