Krotos Modules 3
Loading...
Searching...
No Matches
GrainVisualiser.h
Go to the documentation of this file.
1#pragma once
2
3namespace krotos
4{
5
11 {
12 public:
13 KrotosImage(Image initImage) : image{initImage} {}
14 Image image;
15 bool dirty{true}; // True indicates that the image is out of date
16 };
17
24 {
25 public:
34 void shallowCopy(SampleEngine* sampleEngine);
35
36#if JUCE_DEBUG
37 ProgressTracker& getProgressTracker() { return m_progressTracker; }
38#endif
39
40 std::vector<AudioDescriptor>& getGrainDescriptionByZ() { return m_grainDescriptionByZ; }
41
43
45
46 std::vector<Grain>& getGrainArray() { return m_grains; }
47
49
50 std::vector<Grain> m_grains;
51
52 private:
53 std::vector<AudioDescriptor> m_grainDescriptionByZ;
57#if JUCE_DEBUG
58 ProgressTracker m_progressTracker;
59#endif
60 };
61
66 {
67 public:
68 // Upper limit to how many Reformer instances we are prepared to display
69 // Instances beyond this number will be ignored
70 static constexpr int ReformerMaxDisplayedInstances{4};
71
73 ~GrainVisualiser() override;
74
85 void drawGrain(Graphics& graphicContext, Graphics& overlayContext, float x, float y, float diameter,
86 float sprite, float envelope = 0.f);
87
97 void drawGrainPerspective(Graphics& graphicContext, Graphics& overlayContext, float x, float y, float z,
98 float q, const AffineTransform& transform, float envelope = -1.f);
99
104 void drawBackdropGradient(juce::Graphics& g);
105
111 void drawBackdrop(juce::Graphics& g);
112
119 int calcBaseFrame(const float parallax, float& interFrame);
120
126 int calcBaseFrame(float parallax);
127
134 void drawInterpolatedBackground(juce::Graphics& g, float parallax);
135
140 void drawDisplayBuffer(juce::Graphics& g);
141
142#if JUCE_DEBUG
148 void drawProgressTracker(juce::Graphics& g, ProgressTracker& progessTracker);
149#endif
150
156 const AffineTransform makeParallaxRotationTransform(float parallax);
157
167 void paint(Graphics& g) override;
168
174 void updateBackgroundCacheFrame(SampleEngineShallow* sampleEngine, int index);
175
180
185 void paintPlayingGrains(SampleEngineShallow* sampleEngine);
186
193 void drawBackgroundFrame(Graphics& graphicContext, Graphics& overlayContext, SampleEngineShallow* sampleEngine,
194 float parallax);
195
200
205 {
206 // More stuff will go in here when we
207 m_shallowEngineArray.clear();
208 }
209
216 {
217 // If the number of active engines changes, we need to re-draw the background frames
219 {
222 }
223 }
224
230 {
231 auto engine = sound->getSampleEngine();
232 // For the pusposes of grain drawing, we treat an engine with no audio segments
233 // exactly like it is no engine at all. It is not pushed onto the array
234 // This allows detection of the active engine count to trigger a background
235 // redraw if an engine's segment count is reduced to zero
236 if (engine->getNumAudioSegments() > 0)
237 {
239 m_shallowEngineArray.back().shallowCopy(sound->getSampleEngine());
240 }
241 }
242
243 void setPuckPosition(Point<float> position)
244 {
245 m_puckPositionX = position.x;
246 m_puckPositionY = position.y;
247 }
248
253 void setPerformMode(bool val) { m_performMode = val; }
254
264
270
271 private:
272 // Overall control over the tansparency of the grain display
274 // The num frames taken to fade back up after global fade to transparent
275 const float NUM_GLOBAL_FADE_UP_FRAMES{10.f};
276
278
279 bool m_performMode{false};
280
282 volatile std::atomic<float> m_puckPositionX;
283 volatile std::atomic<float> m_puckPositionY;
284
285 CriticalSection objectLock;
286
287 float m_indicatorHue{0.f}; // For animated hue changes in the progress pie display
288
289 const float ROTATION_RANGE{0.7f}; // The range in radians through which the
290 // ML blob display can be rotated
291
292 const float PERSPECTIVE_SCALING_MIN{0.8f}; // A very cheap pseudo perspective - the scaling
293 const float PERSPECTIVE_SCALING_MAX{1.0f}; // factor for the back of the cube
294
295 const float ASPECT_RATIO{1.0f}; // Match the aspect ratio of the grain display
296 // to make grain blob ellipses circular
297 const float TWO_PI{6.28319f}; // A famous number multiplied by 2
298
299 const float Z_SEPARATION_SCALING{-0.4f}; // Fine tuning of the effect of the depth separation slider
300
301 const float LEGACY_MODE_ALPHA_VALUE{0.55f};
302
303 // The background is cached into an array of images (background frames) with different degrees of
304 // rotation for CPU economy. The more frames we use, the longer it will take to render the array
305 // We make the most of every frame by cross-dissolving between frames to represent angles
306 // inbetween the ones represented by the frames. Try to keep NUM_BACKGROUND_FRAMES as low as you can
307 // without causing unacceptable "ghosting" caused by the cross-dissolving
308 const int NUM_BACKGROUND_FRAMES{12}; // Number of cached frames we require
309 const float SMALLEST_FLOAT_VALUE{0.000001f};
312 std::vector<KrotosImage> m_backgroundCache; // Vector of cached background frames
313
314 const int DISPLAY_WIDTH{240};
315 const int DISPLAY_HEIGHT{240};
316
317 float m_grainDiameter{1.f};
318 float m_overlayHue{0.f};
319 float m_overlayAlpha{0.f};
320 float m_grainAlpha{0.f};
321 float m_grainZ{0.f};
323
324 Image m_backdropImage{Image(Image::ARGB, DISPLAY_WIDTH, DISPLAY_HEIGHT, true)}; // Pre-loaded backgdrop image
325
326 // We buffer the final rendered image of background plus playing grains, so that whenever the system calls
327 // paint() we can instantly paint an image of background and grains. This is required because we have several
328 // layered components, of which this is only one, which the system composits. (This plus the cursor and user
329 // messages for instance.
330 Image m_displayBuffer{Image(Image::ARGB, DISPLAY_WIDTH, DISPLAY_HEIGHT, true)}; // Display is painted into here
331
332 std::vector<KrotosImage> m_grainImages; // Vector of images used to portray grains in the grain visualiser
333
335
336 std::vector<SampleEngineShallow> m_shallowEngineArray;
338 };
339} // namespace krotos
A component to display the grains of the reformer engine.
Definition GrainVisualiser.h:66
std::vector< KrotosImage > m_backgroundCache
Definition GrainVisualiser.h:312
std::vector< KrotosImage > m_grainImages
Definition GrainVisualiser.h:332
Image m_displayBuffer
Definition GrainVisualiser.h:330
const float BACKGROUND_INTERPOLATION_SPAN
Definition GrainVisualiser.h:310
void setPerformMode(bool val)
Set the visualiser into display mode.
Definition GrainVisualiser.h:253
void drawInterpolatedBackground(juce::Graphics &g, float parallax)
Draw the background from the backgound frames store The final image will be interpolated from the two...
Definition GrainVisualiser.cpp:165
const int NUM_BACKGROUND_FRAMES
Definition GrainVisualiser.h:308
volatile std::atomic< float > m_puckPositionY
Definition GrainVisualiser.h:283
void setPuckPosition(Point< float > position)
Definition GrainVisualiser.h:243
void requestBackgroundCacheRegeneration()
Sets a flag to request regeneration of the background cache.
Definition GrainVisualiser.h:179
const float Z_SEPARATION_SCALING
Definition GrainVisualiser.h:299
float m_overlayAlpha
Definition GrainVisualiser.h:319
const int DISPLAY_HEIGHT
Definition GrainVisualiser.h:315
std::vector< SampleEngineShallow > m_shallowEngineArray
Definition GrainVisualiser.h:336
void drawBackdropGradient(juce::Graphics &g)
Draw a gradient for the display background.
Definition GrainVisualiser.cpp:180
const float SMALLEST_FLOAT_VALUE
Definition GrainVisualiser.h:309
const float ROTATION_RANGE
Definition GrainVisualiser.h:289
bool m_performMode
Definition GrainVisualiser.h:279
void drawGrainPerspective(Graphics &graphicContext, Graphics &overlayContext, float x, float y, float z, float q, const AffineTransform &transform, float envelope=-1.f)
Draw a grain blob into the grain display 3D.
Definition GrainVisualiser.cpp:130
float m_grainDiameter
Definition GrainVisualiser.h:317
bool m_advancedGraphics
Definition GrainVisualiser.h:322
const float NUM_GLOBAL_FADE_UP_FRAMES
Definition GrainVisualiser.h:275
const GrainRenderStyle DEFAULT_GRAIN_RENDER_STYLE
Definition GrainVisualiser.h:277
CriticalSection objectLock
Definition GrainVisualiser.h:285
float m_parallaxFromPuck
Definition GrainVisualiser.h:281
void markBackgroundFramesDirty()
Sets the dirty flag true for every background frame.
Definition GrainVisualiser.cpp:473
int m_numActiveEngines
Definition GrainVisualiser.h:337
const float TWO_PI
Definition GrainVisualiser.h:297
GrainRenderStyle
Enumerate the sets of PNGs used to render grains.
Definition GrainVisualiser.h:259
@ NUM_GRAIN_STYLES
Definition GrainVisualiser.h:262
@ Compound
Definition GrainVisualiser.h:260
@ DotsWithLines
Definition GrainVisualiser.h:261
float m_overlayHue
Definition GrainVisualiser.h:318
int m_redrawFrameIndex
Definition GrainVisualiser.h:334
float m_grainZ
Definition GrainVisualiser.h:321
float m_grainAlpha
Definition GrainVisualiser.h:320
void drawDisplayBuffer(juce::Graphics &g)
Draw the display buffer into a graphics context.
Definition GrainVisualiser.cpp:141
void finishedAddingSampleEngines()
Called to indicate that you have finished adding sample engines to the grain visualiser Checks to see...
Definition GrainVisualiser.h:215
void paintPlayingGrains(SampleEngineShallow *sampleEngine)
Paint the grain display into the display buffer.
Definition GrainVisualiser.cpp:377
float m_indicatorHue
Definition GrainVisualiser.h:287
volatile std::atomic< float > m_puckPositionX
Definition GrainVisualiser.h:282
Image m_backdropImage
Definition GrainVisualiser.h:324
const int DISPLAY_WIDTH
Definition GrainVisualiser.h:314
void drawBackgroundFrame(Graphics &graphicContext, Graphics &overlayContext, SampleEngineShallow *sampleEngine, float parallax)
Draws all the grain display components - background.
Definition GrainVisualiser.cpp:231
static constexpr int ReformerMaxDisplayedInstances
Definition GrainVisualiser.h:70
const AffineTransform makeParallaxRotationTransform(float parallax)
Make an affine transform to rotate X & Z around the Y axis.
Definition GrainVisualiser.cpp:372
const float PERSPECTIVE_SCALING_MAX
Definition GrainVisualiser.h:293
float m_globalDisplayOpacity
Definition GrainVisualiser.h:273
int calcBaseFrame(const float parallax, float &interFrame)
Calculates which frame index into the background cache for a given parallax value.
Definition GrainVisualiser.cpp:148
~GrainVisualiser() override
Definition GrainVisualiser.cpp:73
void shallowCopy(KrotosSampleOscillatorSound *sound)
Set which sound is to be displayed.
Definition GrainVisualiser.h:229
void drawGrain(Graphics &graphicContext, Graphics &overlayContext, float x, float y, float diameter, float sprite, float envelope=0.f)
Draw a grain blob into the grain display 2D.
Definition GrainVisualiser.cpp:75
const float LEGACY_MODE_ALPHA_VALUE
Definition GrainVisualiser.h:301
bool m_requestBackgroundCacheRegeneration
Definition GrainVisualiser.h:311
void updateBackgroundCacheFrame(SampleEngineShallow *sampleEngine, int index)
Regenerate a frame in the background cache.
Definition GrainVisualiser.cpp:451
const float PERSPECTIVE_SCALING_MIN
Definition GrainVisualiser.h:292
const float ASPECT_RATIO
Definition GrainVisualiser.h:295
void prepareToAddSampleEngines()
Called to indicate that you are about to start adding sample engines to the grain visualiser.
Definition GrainVisualiser.h:204
void loadBinaryAssetGrainGraphics(GrainRenderStyle style)
Loads the PNGs for display of grains from binary assets.
Definition GrainVisualiser.cpp:485
void drawBackdrop(juce::Graphics &g)
The backdrop is graphic which is drawn into the background of the display This method draws it into t...
Definition GrainVisualiser.cpp:146
GrainVisualiser()
Definition GrainVisualiser.cpp:6
void paint(Graphics &g) override
Paints in the grains display This method is called when the component is asked to re-paint itself Thi...
Definition GrainVisualiser.cpp:259
Definition KrotosSynthesiserSound.h:19
SampleEngine * getSampleEngine() override
Definition KrotosSynthesiserSound.h:44
ProgressTracker - a class to help with display of a progresas bar during audio analysis.
Definition KrotosAudioBufferDSP.h:64
Definition SampleEngine.h:84
A structure to hold a "shallow" copy of a SampleEngine structure It contains only the data from a Sam...
Definition GrainVisualiser.h:24
std::vector< Grain > m_grains
Definition GrainVisualiser.h:50
std::vector< Grain > & getGrainArray()
Definition GrainVisualiser.h:46
std::vector< AudioDescriptor > & getGrainDescriptionByZ()
Definition GrainVisualiser.h:40
void shallowCopy(SampleEngine *sampleEngine)
Make a shallow copy of a sample engine This might have been this class's constructor,...
Definition GrainVisualiser.cpp:351
AnalysisCoefficients & getAnalysisCoefficients()
Definition GrainVisualiser.h:48
bool m_shouldRegenerateDisplayCache
Definition GrainVisualiser.h:54
std::vector< AudioDescriptor > m_grainDescriptionByZ
Definition GrainVisualiser.h:53
AnalysisCoefficients m_analysisCoefficients
Definition GrainVisualiser.h:55
bool shouldRegenerateDisplayCache()
Definition GrainVisualiser.h:42
bool analysisResultsAreValid()
Definition GrainVisualiser.h:44
bool m_analysisResultsAreValid
Definition GrainVisualiser.h:56
Enables zooming of a painted component using special drawing methods.
Definition ZoomableComponent.h:7
Definition AirAbsorptionFilter.cpp:2
AnalysisCoefficients - A class containing attributes used during analysis of audio.
Definition KrotosAudioBufferDSP.h:42
A structure to associate a Juce image with any other required variables.
Definition GrainVisualiser.h:11
Image image
Definition GrainVisualiser.h:14
bool dirty
Definition GrainVisualiser.h:15
KrotosImage(Image initImage)
Definition GrainVisualiser.h:13