Skip to content

Commit e8574d9

Browse files
committed
analysis nodes extend P5SoundAnalyzerNode
1 parent a3ee390 commit e8574d9

File tree

2 files changed

+48
-71
lines changed

2 files changed

+48
-71
lines changed

src/Amplitude.js renamed to src/nodes/analysis/P5SoundAmplitude.js

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { Meter as ToneMeter } from "tone/build/esm/component/analysis/Meter.js";
8-
import { Context as ToneContext } from "tone/build/esm/core/context/Context.js";
8+
import {P5SoundAnalyzerNode} from "../core/P5SoundAnalyzerNode";
99

1010
/**
1111
* Get the current volume of a sound.
@@ -45,19 +45,15 @@ import { Context as ToneContext } from "tone/build/esm/core/context/Context.js";
4545
* </code>
4646
* </div>
4747
*/
48-
class Amplitude {
49-
constructor(smoothing = 0) {
50-
this.amplitude = new ToneMeter({normalRange:true, smoothing:smoothing});
51-
}
48+
export class P5SoundAmplitude extends P5SoundAnalyzerNode
49+
{
50+
constructor(smoothing = 0)
51+
{
52+
super();
5253

53-
/**
54-
* Connect an audio source to the amplitude object.
55-
* @method setInput
56-
* @for Amplitude
57-
* @param {Object} input - An object that has audio output.
58-
*/
59-
setInput(input) {
60-
input.getNode().connect(this.amplitude);
54+
this._toneMeterNode = new ToneMeter( { normalRange:true, smoothing:smoothing } );
55+
56+
this.configureInput(this._toneMeterNode);
6157
}
6258

6359
/**
@@ -66,35 +62,14 @@ class Amplitude {
6662
* @for Amplitude
6763
* @return {Number} Amplitude level (volume) of a sound.
6864
*/
69-
getLevel() {
70-
return this.amplitude.getValue();
71-
}
65+
get level() { return this._toneMeterNode.getValue(); }
7266

67+
get smooth() { return this._toneMeterNode.smoothing; }
7368
/**
74-
* Get the current amplitude value of a sound.
69+
* Set the current amplitude value of a sound.
7570
* @method smooth
7671
* @for Amplitude
77-
* @param {Number} Smooth Amplitude analysis by averaging with the last analysis frame. Off by default.
72+
* @param {Number} value Amplitude analysis by averaging with the last analysis frame. Off by default.
7873
*/
79-
smooth(s) {
80-
this.amplitude.smoothing = s;
81-
}
82-
83-
connect(destination) {
84-
if(typeof destination.getNode === 'function') {
85-
this.amplitude.connect(destination.getNode());
86-
} else {
87-
this.amplitude.connect(destination);
88-
}
89-
}
90-
91-
disconnect() {
92-
this.amplitude.disconnect(ToneContext.destination);
93-
}
94-
95-
getNode() {
96-
return this.amplitude;
97-
}
98-
}
99-
100-
export default Amplitude;
74+
set smooth(value) { this._toneMeterNode.smoothing = value; }
75+
}

src/FFT.js renamed to src/nodes/analysis/P5SoundFFT.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import { FFT as ToneFFT } from "tone/build/esm/component/analysis/FFT.js";
88
import { Waveform as ToneWaveform } from "tone/build/esm/component/analysis/Waveform.js";
99
import { Gain as ToneGain } from "tone/build/esm/core/context/Gain.js";
10+
import {P5SoundAnalyzerNode} from "../core/P5SoundAnalyzerNode";
1011

1112
/**
1213
* Analyze the frequency spectrum and waveform of sounds.
13-
* @class FFT
14+
* @class P5SoundFFT
1415
* @constructor
15-
* @param {Number} [fftSize] FFT analysis size. Must be a power of two between 16 and 1024. Defaults to 32.
16+
* @param {Number} [fftSize] P5SoundFFT analysis size. Must be a power of two between 16 and 1024. Defaults to 32.
1617
* @example
1718
* <div>
1819
* <code>
@@ -21,7 +22,7 @@ import { Gain as ToneGain } from "tone/build/esm/core/context/Gain.js";
2122
* function setup(){
2223
* let cnv = createCanvas(100,100);
2324
* cnv.mouseClicked(togglePlay);
24-
* fft = new p5.FFT(32);
25+
* fft = new p5.P5SoundFFT(32);
2526
* osc = new p5.TriOsc(440);
2627
* osc.connect(fft);
2728
* }
@@ -62,44 +63,45 @@ import { Gain as ToneGain } from "tone/build/esm/core/context/Gain.js";
6263
* </code>
6364
* </div>
6465
*/
65-
class FFT {
66-
constructor(fftSize = 32) {
67-
this.fftSize = fftSize;
68-
this.analyzer = new ToneFFT({
69-
size: this.fftSize,
70-
normalRange: true,
71-
});
72-
this.samples = new ToneWaveform();
73-
//creates a single gain node to connect to for the analyzer and waveform
74-
this.gain = new ToneGain(1);
75-
this.gain.connect(this.analyzer);
76-
this.gain.connect(this.samples);
77-
}
66+
export class P5SoundFFT extends P5SoundAnalyzerNode
67+
{
68+
constructor(fftSize = 32)
69+
{
70+
super();
71+
72+
this._fftSize = fftSize;
73+
74+
this._toneFFTNode = new ToneFFT
75+
(
76+
{
77+
size: this._fftSize,
78+
normalRange: true,
79+
}
80+
);
81+
82+
this._samples = new ToneWaveform();
83+
84+
this._thruNode = new ToneGain();
85+
this._thruNode.connect(this._toneFFTNode, this._samples);
7886

79-
//return the gain node which is the parent node to the analyzer and waveform
80-
getNode() {
81-
return this.gain;
87+
this.configureInput(this._thruNode);
8288
}
89+
90+
isP5SoundFFT = true;
8391

8492
/**
8593
* Returns the frequency spectrum of the input signal.
86-
* @method analyze
87-
* @for FFT
94+
* @method analysis
95+
* @for P5SoundFFT
8896
* @returns {Array} Array of amplitude values from 0 to 1.
8997
*/
90-
analyze() {
91-
return this.analyzer.getValue();
92-
}
98+
get analysis() { return this._toneFFTNode.getValue(); }
9399

94100
/**
95101
* Returns an array of sample values from the input audio.
96102
* @method waveform
97-
* @for FFT
103+
* @for P5SoundFFT
98104
* @return {Array} Array of sample values from -1 to -1.
99105
*/
100-
waveform() {
101-
return this.samples.getValue();
102-
}
103-
}
104-
105-
export default FFT;
106+
get waveform() { return this._samples.getValue(); }
107+
}

0 commit comments

Comments
 (0)