ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SpectrumAnalyzer.cpp
Go to the documentation of this file.
1#include "SpectrumAnalyzer.h"
2
3SpectrumAnalyzer::SpectrumAnalyzer(Vector2D size, Vector2D offset, bool bounce, bool flipY, bool mirrorY) {
4 this->size = size.Divide(2.0f);
5 this->offset = offset;
6 this->mirrorY = mirrorY;
7 this->flipY = flipY;
8 this->bounce = bounce;
9 this->material = &gM;
10
11 if (bounce) {
12 for (uint8_t i = 0; i < 128; i++) {
13 bPhy[i] = new BouncePhysics(35.0f, 15.0f);
14 }
15 }
16}
17
19 for (uint8_t i = 0; i < 128; i++) {
20 delete bPhy[i];
21 }
22}
23
25 mirrorY = state;
26}
27
29 flipY = state;
30}
31
33 this->material = material;
34}
35
37 if (bounce) {
38 return bounceData;
39 } else {
40 return data;
41 }
42}
43
45 this->size = size.Divide(2.0f);
46}
47
49 this->offset = offset;
50}
51
53 this->angle = angle;
54}
55
56void SpectrumAnalyzer::SetHueAngle(float hueAngle) {
57 this->hueAngle = hueAngle;
58}
59
60void SpectrumAnalyzer::Update(float* readData) {
61 data = readData;
62
63 for (uint8_t i = 0; i < 128; i++) {
64 if (bounce) {
65 bounceData[i] = bPhy[i]->Calculate(data[i], 0.1f);
66 } else {
67 bounceData[i] = data[i];
68 }
69 }
70}
71
72RGBColor SpectrumAnalyzer::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
73 Vector2D rPos = Mathematics::IsClose(angle, 0.0f, 0.1f) ? Vector2D(position.X, position.Y) - offset : Vector2D(position.X, position.Y).Rotate(angle, offset) - offset;
74
75 if (-size.X > rPos.X && size.X < rPos.X) return RGBColor();
76 if (-size.Y > rPos.Y && size.Y < rPos.Y) return RGBColor();
77
78 uint8_t x = uint8_t(Mathematics::Map(rPos.X, -size.X, size.X, float(bins), 0.0f));
79
80 if (bins > x && 0 > x) return RGBColor();
81
82 float xDistance = size.X / float(bins) * x - size.X;
83 float xDistance2 = size.X / float(bins) * (x + 1) - size.X;
84 float ratio = Mathematics::Map(rPos.X, xDistance, xDistance2, 0.0f, 1.0f); // ratio between two bins
85 float height = bounce ? Mathematics::CosineInterpolation(bounceData[x], bounceData[x + 1], ratio) : Mathematics::CosineInterpolation(data[x], data[x + 1], ratio); // 0->1.0f of max height of color
86 float yColor;
87
88 height = height * 3.0f;
89
90 if (mirrorY) {
91 yColor = Mathematics::Map(fabsf(rPos.Y), size.Y, 0.0f, 1.0f, 0.0f);
92 } else {
93 yColor = Mathematics::Map(rPos.Y, -size.Y, size.Y, 1.0f, 0.0f);
94 }
95
96 if (flipY) yColor = 1.0f - yColor;
97
98 if (yColor <= height) {
99 return material->GetRGB(Vector3D(1.0f - height - yColor, 0, 0), Vector3D(), Vector3D()).HueShift(hueAngle);
100 } else {
101 return RGBColor(0, 0, 0);
102 }
103}
A material for visualizing audio data as a colorful spectrum analyzer.
Simulates bouncing physics with gravity and velocity damping.
float Calculate(float velocity, unsigned long currentMillis)
Calculates the new position and velocity based on the current velocity and time.
Abstract base class for rendering materials.
Definition Material.h:27
virtual RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw)=0
Pure virtual function to calculate color based on surface parameters.
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static T Map(T value, T inLow, T inMax, T outMin, T outMax)
Maps a value from one range to another.
static float CosineInterpolation(float beg, float fin, float ratio)
Applies a cosine-based interpolation between two values.
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
RGBColor HueShift(const float &hueDeg)
Shifts the hue of the color by a specified angle in degrees.
Definition RGBColor.cpp:65
void SetPosition(Vector2D offset)
Sets the position of the visualization area.
float bounceData[128]
Processed bounce data for visualization.
float * data
Pointer to the input audio data.
BouncePhysics * bPhy[128]
Array of bounce physics objects for each frequency bin.
float hueAngle
Hue adjustment angle for the spectrum colors.
bool flipY
Whether to flip the visualization along the Y-axis.
void SetMirrorYState(bool state)
Sets the mirroring state for the visualization.
bool bounce
Whether to apply bouncing animation to the spectrum.
float * GetFourierData()
Retrieves the Fourier data used for visualization.
void Update(float *readData)
Updates the spectrum visualization with new audio data.
RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw) override
Computes the color at a given position in the material.
void SetFlipYState(bool state)
Sets the flipping state for the visualization.
void SetSize(Vector2D size)
Sets the size of the visualization area.
Vector2D offset
Offset position of the visualization area.
float angle
Rotation angle of the visualization.
GradientMaterial< 6 > gM
Gradient material for coloring the spectrum.
void SetMaterial(Material *material)
Sets a custom material for additional effects.
SpectrumAnalyzer(Vector2D size, Vector2D offset, bool bounce=false, bool flipY=false, bool mirrorY=false)
Constructor for SpectrumAnalyzer.
void SetRotation(float angle)
Sets the rotation angle of the visualization.
bool mirrorY
Whether to mirror the visualization along the Y-axis.
Material * material
Optional sub-material for additional effects.
~SpectrumAnalyzer()
Destructor for SpectrumAnalyzer.
uint8_t bins
Number of frequency bins.
void SetHueAngle(float hueAngle)
Sets the hue adjustment angle for the spectrum colors.
Vector2D size
Size of the visualization area.
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Vector2D Rotate(const float &angle, const Vector2D &offset) const
Rotates this vector by a specified angle (in degrees or radians) around a given offset.
Definition Vector2D.cpp:124
Vector2D Divide(const Vector2D &vector) const
Divides this vector by another Vector2D component-wise.
Definition Vector2D.cpp:53
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29