ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SpectrumAnalyzer.h
Go to the documentation of this file.
1/**
2 * @file SpectrumAnalyzer.h
3 * @brief A material for visualizing audio data as a colorful spectrum analyzer.
4 *
5 * This file defines the SpectrumAnalyzer class, which uses audio data to generate
6 * a visually dynamic spectrum effect, with options for mirroring, flipping, and bouncing.
7 *
8 * @date 22/12/2024
9 * @author Coela Can't
10 */
11
12#pragma once
13
14#include "../Material.h" // Base material class.
15#include "../Static/GradientMaterial.h" // For managing gradient colors.
16#include "../../../Physics/Utils/BouncePhysics.h" // For adding bounce physics to spectrum data.
17
18/**
19 * @class SpectrumAnalyzer
20 * @brief A material that visualizes audio data as a spectrum.
21 *
22 * The SpectrumAnalyzer class processes audio data to create a colorful and dynamic
23 * spectrum visualization with options for customization, including mirroring and flipping.
24 */
25class SpectrumAnalyzer : public Material {
26private:
27 BouncePhysics* bPhy[128]; ///< Array of bounce physics objects for each frequency bin.
28 Vector2D size; ///< Size of the visualization area.
29 Vector2D offset; ///< Offset position of the visualization area.
30 float angle = 0.0f; ///< Rotation angle of the visualization.
31 float hueAngle = 0.0f; ///< Hue adjustment angle for the spectrum colors.
32 uint8_t colors; ///< Number of colors in the gradient.
33 float* data; ///< Pointer to the input audio data.
34 float bounceData[128]; ///< Processed bounce data for visualization.
35 uint8_t bins = 128; ///< Number of frequency bins.
36 bool mirrorY = false; ///< Whether to mirror the visualization along the Y-axis.
37 bool flipY = false; ///< Whether to flip the visualization along the Y-axis.
38 bool bounce = false; ///< Whether to apply bouncing animation to the spectrum.
39
41 RGBColor(255, 0, 0),
42 RGBColor(255, 255, 0),
43 RGBColor(0, 255, 0),
44 RGBColor(0, 255, 255),
45 RGBColor(0, 0, 255),
46 RGBColor(255, 0, 255)
47 }; ///< Predefined rainbow color gradient.
48
49 GradientMaterial<6> gM = GradientMaterial<6>(rainbowSpectrum, 1.0f, false); ///< Gradient material for coloring the spectrum.
50 Material* material; ///< Optional sub-material for additional effects.
51
52public:
53 /**
54 * @brief Constructor for SpectrumAnalyzer.
55 *
56 * @param size The size of the visualization area.
57 * @param offset The offset position of the visualization.
58 * @param bounce Enables bouncing animation for the spectrum.
59 * @param flipY Enables flipping the visualization along the Y-axis.
60 * @param mirrorY Enables mirroring the visualization along the Y-axis.
61 */
62 SpectrumAnalyzer(Vector2D size, Vector2D offset, bool bounce = false, bool flipY = false, bool mirrorY = false);
63
64 /**
65 * @brief Destructor for SpectrumAnalyzer.
66 */
68
69 /**
70 * @brief Sets the mirroring state for the visualization.
71 *
72 * @param state True to enable mirroring along the Y-axis, false to disable.
73 */
74 void SetMirrorYState(bool state);
75
76 /**
77 * @brief Sets the flipping state for the visualization.
78 *
79 * @param state True to enable flipping along the Y-axis, false to disable.
80 */
81 void SetFlipYState(bool state);
82
83 /**
84 * @brief Sets a custom material for additional effects.
85 *
86 * @param material Pointer to the custom material.
87 */
89
90 /**
91 * @brief Retrieves the Fourier data used for visualization.
92 *
93 * @return Pointer to the array of Fourier data.
94 */
95 float* GetFourierData();
96
97 /**
98 * @brief Sets the size of the visualization area.
99 *
100 * @param size The new size as a Vector2D.
101 */
102 void SetSize(Vector2D size);
103
104 /**
105 * @brief Sets the position of the visualization area.
106 *
107 * @param offset The new position as a Vector2D.
108 */
110
111 /**
112 * @brief Sets the rotation angle of the visualization.
113 *
114 * @param angle The rotation angle in degrees.
115 */
116 void SetRotation(float angle);
117
118 /**
119 * @brief Sets the hue adjustment angle for the spectrum colors.
120 *
121 * @param hueAngle The hue adjustment angle in degrees.
122 */
123 void SetHueAngle(float hueAngle);
124
125 /**
126 * @brief Updates the spectrum visualization with new audio data.
127 *
128 * @param readData Pointer to the new audio data.
129 */
130 void Update(float* readData);
131
132 /**
133 * @brief Computes the color at a given position in the material.
134 *
135 * @param position The position in 3D space.
136 * @param normal The surface normal vector.
137 * @param uvw The texture coordinates.
138 * @return The computed color as an RGBColor.
139 */
140 RGBColor GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) override;
141};
Simulates bouncing physics with gravity and velocity damping.
Creates a customizable gradient material for rendering.
Abstract base class for rendering materials.
Definition Material.h:27
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
A material that visualizes audio data as a spectrum.
RGBColor rainbowSpectrum[6]
Predefined rainbow color gradient.
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.
uint8_t colors
Number of colors in the gradient.
void SetMaterial(Material *material)
Sets a custom material for additional effects.
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
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26