1 |
|
---|
2 | var WFFreqSpectrum = function(frameBufferLength,channels,samplerate,totalNumFrames) {
|
---|
3 |
|
---|
4 | this.frameBufferLength = frameBufferLength;
|
---|
5 | this.channels = channels;
|
---|
6 | this.samplerate = samplerate;
|
---|
7 |
|
---|
8 | this.totalNumFrames = totalNumFrames; // **** Not currently used
|
---|
9 |
|
---|
10 | this.defaults();
|
---|
11 |
|
---|
12 | var fftWindowSize = Math.floor(this.frameBufferLength/channels); // check this is a power of 2??
|
---|
13 |
|
---|
14 | this.fft = new FFTCustom(fftWindowSize, this.samplerate);
|
---|
15 |
|
---|
16 | this.freqSpectrumSequence = new Array();
|
---|
17 | }
|
---|
18 |
|
---|
19 | WFFreqSpectrum.inherits(Component);
|
---|
20 |
|
---|
21 | //----------------------------- OVERVIEW -----------------------------------------------------
|
---|
22 |
|
---|
23 | WFFreqSpectrum.staticfield('Component', {
|
---|
24 |
|
---|
25 | creator: "David Bainbridge",
|
---|
26 | description: "Overview: Computes the frequency spectrum of the (mono-valued) input frame",
|
---|
27 | name: "freqSpectrum",
|
---|
28 | tags: "signal process, frequency-spectrum",
|
---|
29 | dependency: [ "dsp.js" ]
|
---|
30 | });
|
---|
31 |
|
---|
32 | //------------------------------ INPUTS ------------------------------------------------------
|
---|
33 |
|
---|
34 | WFFreqSpectrum.staticfield('ComponentInputArray', [
|
---|
35 | {
|
---|
36 | name: "monoAudioInputFrames",
|
---|
37 | description: "Frames of (mono, or stereo down-mixed to mono) audio."
|
---|
38 | }
|
---|
39 |
|
---|
40 | ]);
|
---|
41 |
|
---|
42 |
|
---|
43 | //------------------------------ PROPERTIES --------------------------------------------------
|
---|
44 |
|
---|
45 | WFFreqSpectrum.staticfield('ComponentPropertyArray', [
|
---|
46 | ]);
|
---|
47 |
|
---|
48 |
|
---|
49 | //------------------------------ OUTPUTS -----------------------------------------------------
|
---|
50 |
|
---|
51 | WFFreqSpectrum.staticfield('ComponentOutputArray', [
|
---|
52 | {
|
---|
53 | name: "freqSpectrumFrames",
|
---|
54 | description: "Per frame frequency spectrum (potentially the power spectrum, based on the Component's parameters"
|
---|
55 | }
|
---|
56 | ]);
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | WFFreqSpectrum.prototype.reset = function()
|
---|
61 | {
|
---|
62 | this.maxval = 0.0;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | WFFreqSpectrum.prototype.setMaxVal = function(v)
|
---|
67 | {
|
---|
68 | this.maxval = v;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | WFFreqSpectrum.prototype.getMaxVal = function()
|
---|
73 | {
|
---|
74 | var maxVal = this.maxval;
|
---|
75 |
|
---|
76 | return maxVal;
|
---|
77 | }
|
---|
78 |
|
---|
79 | WFFreqSpectrum.prototype.getFreqSpectrumSequence = function() {
|
---|
80 | return this.freqSpectrumSequence;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | // ******
|
---|
85 | // ******
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | WFFreqSpectrum.prototype.defaults = function() {
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | WFFreqSpectrum.prototype.initializeCallback = function(ccp) {
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | WFFreqSpectrum.prototype.executeCallBack = function(cc) {
|
---|
98 |
|
---|
99 | // typeof(cc) => ComponentContext
|
---|
100 |
|
---|
101 | // get the "[D" input
|
---|
102 |
|
---|
103 | //console.info("*** WFFreqSpectrum::executeCallBack(): away to request 'monoAudioInputFrames'");
|
---|
104 |
|
---|
105 | var frame = cc.getDataComponentFromInput("monoAudioInputFrames");
|
---|
106 |
|
---|
107 | this.fft.forward(frame);
|
---|
108 |
|
---|
109 | var freqSpectrumCopy = new Float32Array(this.fft.powerSpectrum); // need a copy of the array (not just a reference)
|
---|
110 | this.freqSpectrumSequence.push(freqSpectrumCopy);
|
---|
111 |
|
---|
112 | cc.pushDataComponentToOutput("freqSpectrumFrames",this.fft.powerSpectrum);
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|