Skip to content

Commit a9531c2

Browse files
authored
replacement of es5 functions to es6 classes feat p5.noise (#536)
1 parent 2896d6e commit a9531c2

File tree

2 files changed

+91
-84
lines changed

2 files changed

+91
-84
lines changed

src/app.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ p5.FFT = FFT;
3030
import './signal';
3131
import './oscillator';
3232
import './envelope';
33+
34+
35+
import Noise from './noise';
36+
p5.Noise = Noise;
37+
3338
import Pulse from './pulse';
3439
p5.Pulse = Pulse;
35-
import './noise';
40+
41+
3642

3743
import AudioIn from './audioin';
3844
p5.AudioIn = AudioIn;
@@ -72,15 +78,14 @@ import Compressor from './compressor';
7278
p5.Compressor = Compressor;
7379

7480

75-
7681
import peakDetect from './peakDetect';
7782
p5.peakDetect = peakDetect;
7883

79-
8084
import SoundRecorder from './soundRecorder';
8185
p5.SoundRecorder = SoundRecorder;
8286

8387

88+
8489
import Distortion from './distortion';
8590
p5.Distortion = Distortion;
8691

src/noise.js

Lines changed: 83 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import p5sound from './master';
2+
import Oscillator from './oscillator';
23

34
// generate noise buffers
45
const _whiteNoiseBuffer = (function () {
@@ -70,97 +71,98 @@ const _brownNoiseBuffer = (function () {
7071
* @param {String} type Type of noise can be 'white' (default),
7172
* 'brown' or 'pink'.
7273
*/
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;
7981

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;
8690
}
87-
this.buffer = assignType;
88-
};
8991

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+
}
91119

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;
112122
}
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);
114131
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;
127134
}
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-
};
136135

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+
}
142142
}
143-
};
144143

145-
p5.Noise.prototype.dispose = function () {
146-
var now = p5sound.audiocontext.currentTime;
144+
dispose() {
145+
var now = p5sound.audiocontext.currentTime;
147146

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);
151150

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;
155165
}
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

Comments
 (0)