ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Oscilloscope.cpp
Go to the documentation of this file.
1#include "Oscilloscope.h"
2
4 this->size = size.Divide(2.0f);
5 this->offset = offset;
6 this->material = &gM;
7}
8
10 for (uint8_t i = 0; i < 128; i++) {
11 delete bPhy[i];
12 }
13}
14
16 this->material = material;
17}
18
20 return data;
21}
22
24 this->size = size.Divide(2.0f);
25}
26
28 this->offset = offset;
29}
30
31void Oscilloscope::SetRotation(float angle) {
32 this->angle = angle;
33}
34
35void Oscilloscope::SetHueAngle(float hueAngle) {
36 this->hueAngle = hueAngle;
37}
38
39void Oscilloscope::Update(float* data) {
40 this->data = data;
41
42 minValue = minF.Filter(data[bins / 2 + 1]);
43 maxValue = maxF.Filter(data[bins / 2 + 1]);
44
45 midPoint = (maxValue - minValue) / 2.0f + minValue;
46}
47
48RGBColor Oscilloscope::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
49 Vector2D rPos = Mathematics::IsClose(angle, 0.0f, 0.1f) ? Vector2D(position.X, position.Y) - offset : Vector2D(position.X, position.Y).Rotate(angle, offset) - offset;
50
51 // Outside of size bounds
52 if (rPos.X < -size.X || rPos.X > size.X) return RGBColor();
53 if (rPos.Y < -size.Y || rPos.Y > size.Y) return RGBColor();
54
55 uint8_t x = uint8_t(Mathematics::Map(rPos.X, -size.X, size.X, float(bins - 1), float(bins - 1) / 2));
56
57 if (bins > x && 0 > x) return RGBColor();
58
59 float xDistance = size.X / float(bins) * x - size.X;
60 float xDistance2 = size.X / float(bins) * (x + 2) - size.X;
61 float ratio = Mathematics::Map(rPos.X, xDistance, xDistance2, 0.0f, 1.0f); // ratio between two bins
62
63 float firstPoint = Mathematics::Map(data[x], minValue, maxValue, 0.0f, 0.75f);
64 float secondPoint = Mathematics::Map(data[x + 2], minValue, maxValue, 0.0f, 0.75f);
65
66 float height = Mathematics::CosineInterpolation(firstPoint, secondPoint, ratio); // 0->1.0f of max height of color
67 float yColor = Mathematics::Map(rPos.Y, 0.0f, size.Y, 1.0f, 0.0f);
68
69 if (rPos.Y < height * size.Y / 2.0f && rPos.Y > height * size.Y / 2.0f - size.Y * 0.1f) {
70 return material->GetRGB(Vector3D(1.0f + height - yColor, 0, 0), Vector3D(), Vector3D()).HueShift(hueAngle);
71 } else {
72 return RGBColor(0, 0, 0);
73 }
74}
A dynamic oscilloscope material for visualizing audio signals.
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.
float Filter(float value)
Filters the given value, updating the maximum value within the memory window.
float Filter(float value)
Filters the given value, updating the minimum value within the memory window.
MaxFilter< 40 > maxF
Filter for tracking maximum values.
Oscilloscope(Vector2D size, Vector2D offset)
Constructs an Oscilloscope with the specified size and offset.
void SetPosition(Vector2D offset)
Sets the position of the oscilloscope visualization.
float * data
Pointer to the audio sample data.
BouncePhysics * bPhy[128]
Array of BouncePhysics instances for dynamic bouncing behavior.
float hueAngle
Hue angle for color adjustments.
float midPoint
Midpoint for audio data normalization.
RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw) override
Computes the color at a given position in the visualization.
float * GetSampleData()
Retrieves the current sample data.
void SetSize(Vector2D size)
Sets the size of the oscilloscope visualization.
MinFilter< 40 > minF
Filter for tracking minimum values.
~Oscilloscope()
Destroys the Oscilloscope instance.
Vector2D offset
The position offset of the visualization.
float angle
Rotation angle of the visualization.
void Update(float *data)
Updates the oscilloscope visualization based on new audio data.
GradientMaterial< 6 > gM
Gradient material for the rainbow spectrum.
float maxValue
Minimum and maximum values for data scaling.
void SetMaterial(Material *material)
Sets an additional material for visual effects.
void SetRotation(float angle)
Sets the rotation angle of the visualization.
Material * material
Pointer to an additional material for effects.
uint8_t bins
Number of bins for audio data processing.
void SetHueAngle(float hueAngle)
Sets the hue angle for color adjustments.
Vector2D size
The size of the oscilloscope visualization.
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
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