hx3d  1
2D/3D Simple Game Framework
spectrum.hpp
1 /*
2  Spectrum display.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #ifndef HX3D_AUDIO_DISPLAY_SPECTRUM
22 #define HX3D_AUDIO_DISPLAY_SPECTRUM
23 
24 #include "hx3d/audio/display.hpp"
25 #include "hx3d/math/constants.hpp"
26 
27 namespace hx3d {
28 namespace audio {
29 
66 class Spectrum: public Display {
67 public:
77  Spectrum(const unsigned int minFreq, const unsigned int maxFreq, const unsigned int barCount);
78 
89  Spectrum(const unsigned int minFreq, const unsigned int maxFreq, const unsigned int barCount, const int refreshDelay);
90  ~Spectrum();
91 
92  virtual void update(const Sint16* stream, const int length, const float delta) override;
93  virtual void onInitialization() override;
94 
105  float getNormalizedFrequencyAmplitude(const unsigned int frequency, const unsigned int range);
106 
112  unsigned int getBarCount();
113 
114 private:
116  unsigned int _minFreq;
118  unsigned int _maxFreq;
120  unsigned int _barCount;
121 
123  Complex* _rawValues;
125  std::vector<float> _fftValues;
127  std::vector<float> _barValues;
129  std::vector<float> _normalizedBarValues;
131  std::vector<unsigned int> _barFrequencies;
132 
141  int nextCenterFreq(const int centerFreq, const float octaves);
142 
151  int prevCenterFreq(const int centerFreq, const float octaves);
152 
161  int lowerLimit(const int centerFreq, const float octaves);
162 
173  int lowerLimitSample(const int centerFreq, const float octaves, const int samplesLength);
174 
183  int upperLimit(const int centerFreq, const float octaves);
184 
195  int upperLimitSample(const int centerFreq, const float octaves, const int samplesLength);
196 
206  float averageFreq(const std::vector<float>& values, const int low, const int hi);
207 
218  float calculateOctave(const unsigned int lowFreq, const unsigned int hiFreq, const unsigned int bars);
219 };
220 
221 } /* audio */
222 } /* hx3d */
223 
224 #endif
Audio spectrum displaying.
Definition: spectrum.hpp:66
Audio effect display.
Definition: display.hpp:41
hx3d framework namespace
Definition: audio.hpp:26
virtual void update(const Sint16 *stream, const int length, const float delta) override
Update the display.
Definition: spectrum.cpp:77
Spectrum(const unsigned int minFreq, const unsigned int maxFreq, const unsigned int barCount)
Create an empty spectrum with a refresh delay of 50.
Definition: spectrum.cpp:35
float getNormalizedFrequencyAmplitude(const unsigned int frequency, const unsigned int range)
Get the average normalized amplitude for a center frequency and a range.
Definition: spectrum.cpp:149
virtual void onInitialization() override
Use this to execute code after initialization.
Definition: spectrum.cpp:48
unsigned int getBarCount()
Get the bar count.
Definition: spectrum.cpp:180