hx3d  1
2D/3D Simple Game Framework
waveform.cpp
1 /*
2  Waveform 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 #include "hx3d/audio/display/waveform.hpp"
22 
23 #include "hx3d/math/interpolation.hpp"
24 #include "hx3d/utils/log.hpp"
25 
26 namespace hx3d {
27 namespace audio {
28 
30 Waveform::Waveform(const int refreshDelay): Display(refreshDelay) {}
31 Waveform::~Waveform() {}
32 
33 void Waveform::update(const Sint16* stream, const int length, const float delta) {
34 
35  if (!_initialized)
36  return;
37 
38  int w = _image.getWidth();
39  int h = _image.getHeight();
40 
41  if (_timer.hasEnded()) {
42 
43  _image.setRect(0, 0, w, h, Color::Black);
44  drawBorders();
45 
46  if (length > 0) {
47  int step = length / w;
48 
49  for (int i = 1; i < w - 1; ++i) {
50  int amp = stream[i * step];
51  if (amp == 0)
52  continue;
53 
54  float normalized_amp = (1.f/32767.f) * amp;
55  normalized_amp = 0.5f + normalized_amp / 2;
56 
57  Color color = math::interpolate(Color(255, 64, 0), Color::Red, normalized_amp, math::Interpolation::Linear);
58 
59  int y_pos = 0;
60  int bar_h = 0;
61 
62  y_pos = (h/2.f) - (normalized_amp * (h/2.f));
63  bar_h = (normalized_amp * (h/2.f)) * 2;
64 
65 
66  _image.setRect(i, y_pos, 1, bar_h, color);
67  }
68  }
69 
70  _image.updateTextureZone(0, 0, w, h);
71  _timer.reset();
72  }
73 
74  _timer.update(delta);
75 }
76 
77 } /* audio */
78 } /* hx3d */
void update(float delta)
Update the timer.
Definition: timer.cpp:76
void reset()
Reset the timer.
Definition: timer.cpp:41
void drawBorders()
Draw white borders.
Definition: display.cpp:51
void setRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, Color color)
Set a rectangle with a color.
Definition: image.cpp:52
Four [0..255] components defined color.
Definition: color.hpp:32
Audio effect display.
Definition: display.hpp:41
unsigned int getHeight()
Get the image height.
Definition: image.cpp:112
bool hasEnded()
Test if the timer has ended.
Definition: timer.cpp:58
Waveform()
Create an empty waveform with a refresh delay of 50.
Definition: waveform.cpp:29
Audio waveform displaying.
Definition: waveform.hpp:60
T interpolate(T a, T b, float t, Interpolation type)
Interpolate between two values.
hx3d framework namespace
Definition: audio.hpp:26
void updateTextureZone(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
Update an existing texture zone.
Definition: image.cpp:85
Timer _timer
Refresh timer.
Definition: display.hpp:99
virtual void update(const Sint16 *stream, const int length, const float delta) override
Update the display.
Definition: waveform.cpp:33
unsigned int getWidth()
Get the image width.
Definition: image.cpp:108
graphics::Image _image
Drawing image.
Definition: display.hpp:97
bool _initialized
Is the display initialized ?
Definition: display.hpp:103