77import { FFT as ToneFFT } from "tone/build/esm/component/analysis/FFT.js" ;
88import { Waveform as ToneWaveform } from "tone/build/esm/component/analysis/Waveform.js" ;
99import { 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