ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
InfraredAnimation.h
Go to the documentation of this file.
1#pragma once
2
3#include "../Animation.h"
4#include "../KeyFrameTrack.h"
5#include "../EasyEaseAnimator.h"
6#include "../../Objects/Background.h"
7#include "../../Objects/LEDStripBackground.h"
8#include "../../Morph/Commissions/InfraredFace.h"
9#include "../../Render/Scene.h"
10#include "../../Signals/FunctionGenerator.h"
11#include "../../Sensors/APDS9960.h"
12
13#include "../../Menu/SingleButtonMenu.h"
14#include "../../Materials/Animated/SpectrumAnalyzer.h"
15#include "../../Materials/Animated/AudioReactiveGradient.h"
16#include "../../Materials/Animated/Oscilloscope.h"
17#include "../../Materials/Animated/RainbowNoise.h"
18#include "../../Materials/Animated/RainbowSpiral.h"
19
20#include "../../Materials/MaterialAnimator.h"
21
22#include "../AnimationTracks/BlinkTrack.h"
23
24#include "../../Signals/FFTVoiceDetection.h"
25
26#include "../../Sensors/MicrophoneFourier_MAX9814.h"
27
28class InfraredAnimation : public Animation<3> {
29private:
30 InfraredFace pM;
31 Background background;
32 LEDStripBackground ledStripBackground;
33 EasyEaseAnimator<21> eEA = EasyEaseAnimator<21>(EasyEaseInterpolation::Cosine, 1.0f, 0.35f);
34
35 //Materials
40
41 RGBColor gradientSpectrum[2] = {RGBColor(255, 0, 0), RGBColor(255, 255, 0)};
43
46
47 SpectrumAnalyzer sA = SpectrumAnalyzer(Vector2D(200, 100), Vector2D(100, 50), true, true);
50
51 //Animation controllers
53
60
62
64
65 float offsetFaceSA = 0.0f;
66 float offsetFaceARG = 0.0f;
67 float offsetFaceOSC = 0.0f;
68 uint8_t offsetFaceIndSA = 50;
69 uint8_t offsetFaceIndARG = 51;
70 uint8_t offsetFaceIndOSC = 52;
71
73 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Angry), InfraredFace::Angry, 15, 0.0f, 1.0f);
74 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Sad), InfraredFace::Sad, 50, 0.0f, 1.0f);
75 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Calm), InfraredFace::Calm, 60, 0.0f, 1.0f);
76 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Determination), InfraredFace::Determination, 20, 0.0f, 1.0f);
77 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Happy), InfraredFace::Happy, 30, 0.0f, 1.0f);
78 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Mad), InfraredFace::Mad, 15, 0.0f, 1.0f);
79 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Flatten), InfraredFace::Flatten, 30, 0.0f, 1.0f);
80
81 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::TalkA), InfraredFace::TalkA, 3, 0.0f, 1.0f);
82 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::TalkB), InfraredFace::TalkB, 3, 0.0f, 1.0f);
83 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::TalkC), InfraredFace::TalkC, 3, 0.0f, 1.0f);
84
85 eEA.AddParameter(pM.GetMorphWeightReference(InfraredFace::Blush), InfraredFace::Blush, 30, 0.0f, 1.0f);
86
90 }
91
93 blink.AddParameter(pM.GetMorphWeightReference(InfraredFace::Blink));
94 }
95
97 eEA.SetInterpolationMethod(InfraredFace::TalkA, EasyEaseInterpolation::Linear);
98 eEA.SetInterpolationMethod(InfraredFace::TalkB, EasyEaseInterpolation::Linear);
99 eEA.SetInterpolationMethod(InfraredFace::TalkC, EasyEaseInterpolation::Linear);
100 }
101
113
114public:
116 scene.AddObject(pM.GetObject());
117 scene.AddObject(background.GetObject());
118 scene.AddObject(ledStripBackground.GetObject());
119
120 LinkEasyEase();
122
124
126
127 pM.GetObject()->SetMaterial(&materialAnimator);
128 background.GetObject()->SetMaterial(&backgroundMaterial);
129 ledStripBackground.GetObject()->SetMaterial(&materialAnimator);
130
131 boop.Initialize(10);
132
133 Menu::Initialize(10, 20, 500);//10 is number of faces
134 MicrophoneFourierIT::Initialize(15, 8000, 50.0f, 120.0f);//8KHz sample rate, 50dB min, 120dB max
135 }
136
138 blink.Update();
139 }
140
141 void Default(){
142 blink.Play();
143
145
146 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
147 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
148 }
149
150 void Angry(){
151 blink.Play();
152 eEA.AddParameterFrame(InfraredFace::Angry, 1.0f);
154
155 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
156 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
157
158 }
159
160 void Sad(){
161 blink.Pause();
162 eEA.AddParameterFrame(InfraredFace::Sad, 1.0f);
164
165 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
166 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
167 }
168
169 void Surprised(){
170 blink.Pause();
171 eEA.AddParameterFrame(InfraredFace::Blush, 1.0f);
174
175 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
176 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
177 }
178
179 void Happy(){
180 blink.Pause();
181 eEA.AddParameterFrame(InfraredFace::Happy, 1.0f);
183
184 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
185 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
186 }
187
189 blink.Play();
190 eEA.AddParameterFrame(InfraredFace::Determination, 1.0f);
192
193 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
194 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
195 }
196
197 void Calm(){
198 blink.Play();
199 eEA.AddParameterFrame(InfraredFace::Calm, 1.0f);
201
202 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
203 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
204 }
205
206 void Mad(){
207 blink.Play();
208 eEA.AddParameterFrame(InfraredFace::Mad, 1.0f);
210
211 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 1.0f);
212 pM.SetMorphWeight(InfraredFace::EyeBigger, 1.0f);
213 }
214
216 blink.Pause();
218
220
221 eEA.AddParameterFrame(InfraredFace::Flatten, 1.0f);
222
223 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 0.0f);
224 pM.SetMorphWeight(InfraredFace::EyeBigger, 0.0f);
225 }
226
228 blink.Pause();
230
232
233 eEA.AddParameterFrame(InfraredFace::Flatten, 1.0f);
234
235 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 0.0f);
236 pM.SetMorphWeight(InfraredFace::EyeBigger, 0.0f);
237 }
238
240 blink.Pause();
242
244
245 eEA.AddParameterFrame(InfraredFace::Flatten, 1.0f);
246
247 pM.SetMorphWeight(InfraredFace::EyeBrowThicker, 0.0f);
248 pM.SetMorphWeight(InfraredFace::EyeBigger, 0.0f);
249 }
250
253 };
254
255 uint8_t GetBrightness(){
256 return Menu::GetBrightness();
257 };
258
259 void FadeIn(float stepRatio) override {}
260 void FadeOut(float stepRatio) override {}
261
263 return pM.GetObject();
264 }
265
268 eEA.AddParameterFrame(InfraredFace::TalkA, MicrophoneFourierIT::GetCurrentMagnitude() / 2.0f);
269
270 if(MicrophoneFourierIT::GetCurrentMagnitude() > 0.05f){
271 voiceDetection.Update(MicrophoneFourierIT::GetFourierFiltered(), MicrophoneFourierIT::GetSampleRate());
272
274 //eEA.AddParameterFrame(InfraredFace::TalkA, voiceDetection.GetViseme(voiceDetection.UH));
275 //eEA.AddParameterFrame(InfraredFace::TalkB, voiceDetection.GetViseme(voiceDetection.AR));
276 //eEA.AddParameterFrame(InfraredFace::TalkC, voiceDetection.GetViseme(voiceDetection.ER));
277 //eEA.AddParameterFrame(InfraredFace::TalkA, voiceDetection.GetViseme(voiceDetection.AH));
279 }
280 }
281 }
282
283 void Update(float ratio) override {
284 pM.Reset();
285
286 float xOffset = fGenMatXMove.Update();
287 float yOffset = fGenMatYMove.Update();
288
289 Menu::Update(ratio);
290
291 bool isBooped = Menu::UseBoopSensor() ? boop.isBooped() : 0;
292 uint8_t mode = Menu::GetFaceState();//change by button press
293
294 MicrophoneFourierIT::Update();
295
296 sA.Update(MicrophoneFourierIT::GetFourierFiltered());
297 sA.SetHueAngle(ratio * 360.0f * 4.0f);
300
301 aRG.SetRadius((xOffset + 2.0f) * 2.0f + 25.0f);
302 aRG.SetSize(Vector2D((xOffset + 2.0f) * 10.0f + 50.0f, (xOffset + 2.0f) * 10.0f + 50.0f));
303 aRG.SetHueAngle(ratio * 360.0f * 8.0f);
304 aRG.SetRotation(ratio * 360.0f * 2.0f);
305 aRG.SetPosition(Vector2D(80.0f + xOffset * 4.0f, 48.0f + yOffset * 4.0f));
306
307 oSC.SetSize(Vector2D(200.0f, 100.0f));
308 oSC.SetHueAngle(ratio * 360.0f * 8.0f);
309 oSC.SetPosition(Vector2D(100.0f, 50.0f));
310
311 voiceDetection.SetThreshold(map(Menu::GetMicLevel(), 0, 10, 1000, 50));
312
314
315 if (isBooped && mode != 6){
316 Surprised();
317 }
318 else{
319 if (mode == 0) Default();
320 else if (mode == 1) Angry();
321 else if (mode == 2) Mad();
322 else if (mode == 3) Determination();
323 else if (mode == 4) Happy();
324 else if (mode == 5) Calm();
325 else if (mode == 6) Sad();
326 else if (mode == 7) {
327 aRG.Update(MicrophoneFourierIT::GetFourierFiltered());
329 }
330 else if (mode == 8){
331 oSC.Update(MicrophoneFourierIT::GetSamples());
333 }
334 else {
335 sA.Update(MicrophoneFourierIT::GetFourierFiltered());
337 }
338 }
339
341
342 eEA.Update();
343 pM.Update();
344
345 rainbowNoise.Update(ratio);
346 rainbowSpiral.Update(ratio);
349
351
352 background.GetObject()->GetTransform()->SetPosition(Vector3D(0.0f, 0.0f, 1200.0f));
353
354 background.GetObject()->UpdateTransform();
355
356 int8_t faceSize = Menu::GetFaceSize();
357 float menuRatio = Menu::ShowMenu();
358 float scale = menuRatio * 0.6f + 0.4f;
359 float xShift = (1.0f - menuRatio) * 80.0f;
360 float yShift = (1.0f - menuRatio) * 30.0f + offsetFaceSA * -200.0f + offsetFaceARG * -200.0f + offsetFaceOSC * -200.0f;
361 float adjustFacePos = float(faceSize) * 6.0f;
362 float adjustFaceX = float(faceSize) * 0.05f;
363
364 pM.GetObject()->GetTransform()->SetRotation(Vector3D(0.0f, 0.0f, -7.5f));
365 pM.GetObject()->GetTransform()->SetPosition(Vector3D(135.0f - xShift + xOffset - adjustFacePos, 67.5f - yShift + yOffset, 600.0f));
366 pM.GetObject()->GetTransform()->SetScale(Vector3D(-0.625f + adjustFaceX, 0.825f, 1.0f).Multiply(scale));
367
368 pM.GetObject()->UpdateTransform();
369 }
370};
A class for managing the Adafruit APDS9960 sensor.
Definition APDS9960.h:27
static bool Initialize(uint8_t threshold)
Initializes the APDS9960 sensor.
Definition APDS9960.cpp:14
static bool isBooped()
Checks if the sensor is "booped" (close proximity detected).
Definition APDS9960.cpp:43
void AddParameter(float *parameter)
Adds a parameter to the animation track.
void Play()
Starts or resumes playback of the animation track.
void Pause()
Pauses playback of the animation track.
float Update()
Updates the animation track and returns the current parameter value.
A material class for creating an audio-reactive gradient effect.
void SetRadius(float radius)
Sets the radius for circular gradient patterns.
void SetPosition(Vector2D offset)
Sets the position of the gradient.
void Update(float *readData)
Updates the gradient based on new audio data.
void SetSize(Vector2D size)
Sets the size of the gradient.
void SetRotation(float angle)
Sets the rotation angle of the gradient.
void SetHueAngle(float hueAngle)
Sets the hue angle for color adjustments.
A template class for animating eye blinking using keyframes.
Definition BlinkTrack.h:27
A template class for implementing advanced animation easing.
void AddParameterFrame(uint16_t dictionaryValue, float value) override
Adds a single frame value for a parameter.
void Update() override
Updates the animator, advancing all animations.
void SetInterpolationMethod(uint16_t dictionaryValue, InterpolationMethod interpMethod) override
Sets the interpolation method for a specific parameter.
void AddParameter(float *parameter, uint16_t dictionaryValue, uint16_t frames, float basis, float goal) override
Adds a parameter to the animator.
Detects visemes based on FFT voice analysis.
float GetViseme(MouthShape viseme)
Retrieves the probability of a specific viseme.
void SetThreshold(float threshold)
Sets the threshold for formant calculations.
void Update(float *peaks, float maxFrequency)
Updates the viseme probabilities based on new FFT data.
A class to generate various waveform functions with customizable parameters.
@ Sine
Sine waveform.
@ Triangle
Triangle waveform.
float Update()
Updates and calculates the next value of the waveform.
Creates a customizable gradient material for rendering.
void HueShift(float hueDeg)
Shifts the hue of the gradient by a specified degree.
FunctionGenerator fGenMatHue
FunctionGenerator fGenMatYMove
SpectrumAnalyzer sA
SimpleMaterial redMaterial
RainbowSpiral rainbowSpiral
SimpleMaterial blueMaterial
BlinkTrack< 1 > blink
LEDStripBackground ledStripBackground
GradientMaterial< 2 > gradientMat
FunctionGenerator fGenMatPos
MaterialAnimator< 5 > materialAnimator
FunctionGenerator fGenScale
FunctionGenerator fGenRotation
void FadeIn(float stepRatio) override
void FadeOut(float stepRatio) override
RainbowNoise rainbowNoise
FunctionGenerator fGenMatXMove
void Update(float ratio) override
FFTVoiceDetection< 128 > voiceDetection
RGBColor gradientSpectrum[2]
MaterialAnimator< 4 > backgroundMaterial
EasyEaseAnimator< 21 > eEA
AudioReactiveGradient aRG
Animates transitions and blends between multiple materials.
void AddMaterial(Material::Method method, Material *material, uint16_t frames, float minOpacity, float maxOpacity)
Adds a material to the animation with specified properties.
void SetBaseMaterial(Material::Method method, Material *material)
Sets the base material for the animation.
void AddMaterialFrame(Material &material, float opacity)
Adds a specific frame for a material in the animation.
void Update()
Updates the animator, advancing the transitions.
@ Lighten
Chooses the lighter color.
Definition Material.h:40
@ Add
Adds colors together.
Definition Material.h:35
@ Replace
Replaces the base color.
Definition Material.h:44
static uint8_t GetFaceState()
Retrieves the current face state. In hardware mode, it is read from the MenuHandler.
Definition Menu.cpp:381
static uint8_t GetMicLevel()
Gets the current microphone level.
Definition Menu.cpp:417
static float ShowMenu()
Returns how much of the menu is shown, typically normalized (0.0 to 1.0).
Definition Menu.cpp:494
static Material * GetMaterial()
Provides a pointer to the material used for rendering the menu text.
Definition Menu.cpp:155
static uint8_t MirrorSpectrumAnalyzer()
Checks if the spectrum analyzer mirroring is toggled on or off.
Definition Menu.cpp:435
static uint8_t GetFaceSize()
Gets the current face size.
Definition Menu.cpp:444
static void Initialize(uint8_t faceCount, uint8_t pin, uint16_t holdingTime, Vector2D size=Vector2D(240, 50))
Initializes the Menu using a face count, input pin, holding time, and size.
Definition Menu.cpp:102
static uint8_t UseMicrophone()
Checks if the microphone usage is toggled on or off.
Definition Menu.cpp:408
static uint8_t UseBoopSensor()
Checks if the boop sensor usage is toggled on or off.
Definition Menu.cpp:426
static void Update(float ratio)
Updates the menu each frame, handling transitions, wiggle effects, and text generation.
Definition Menu.cpp:208
static uint8_t GetBrightness()
Gets the current brightness level. In hardware mode, it is read from the MenuHandler.
Definition Menu.cpp:390
static uint8_t GetAccentBrightness()
Gets the current accent brightness level.
Definition Menu.cpp:399
Represents a 3D object with geometry, material, and transformation data.
Definition Object3D.h:28
A dynamic oscilloscope material for visualizing audio signals.
void SetPosition(Vector2D offset)
Sets the position of the oscilloscope visualization.
void SetSize(Vector2D size)
Sets the size of the oscilloscope visualization.
void Update(float *data)
Updates the oscilloscope visualization based on new audio data.
void SetHueAngle(float hueAngle)
Sets the hue angle for color adjustments.
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
A dynamic material that creates a rainbow effect using simplex noise.
void Update(float ratio)
Updates the material animation based on the time ratio.
A dynamic material creating a colorful rainbow spiral animation.
void Update(float ratio)
Updates the material animation based on the time ratio.
A material that applies a single, solid RGB color to surfaces.
A material that visualizes audio data as a spectrum.
void SetMirrorYState(bool state)
Sets the mirroring state for the visualization.
void Update(float *readData)
Updates the spectrum visualization with new audio data.
void SetFlipYState(bool state)
Sets the flipping state for the visualization.
void SetHueAngle(float hueAngle)
Sets the hue adjustment angle for the spectrum colors.
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
@ OO
Mouth shape corresponding to the "OO" sound.
@ EE
Mouth shape corresponding to the "EE" sound.