Vital
Loading...
Searching...
No Matches
line_generator.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "common.h"
5#include "json/json.h"
6
7using json = nlohmann::json;
8
21public:
25 static constexpr int kMaxPoints = 100;
26
32 static constexpr int kDefaultResolution = 2048;
33
39 static constexpr int kExtraValues = 3;
40
49 static force_inline float smoothTransition(float t) {
50 return 0.5f * sinf((t - 0.5f) * vital::kPi) + 0.5f;
51 }
52
59
63 virtual ~LineGenerator() { }
64
72 void setLoop(bool loop) { loop_ = loop; render(); }
73
79 void setName(const std::string& name) { name_ = name; }
80
86 void setLastBrowsedFile(const std::string& path) { last_browsed_file_ = path; }
87
97
101 void initLinear();
102
106 void initTriangle();
107
111 void initSquare();
112
116 void initSin();
117
121 void initSawUp();
122
126 void initSawDown();
127
134 void render();
135
144
151 static bool isValidJson(json data);
152
158 void jsonToState(json data);
159
166 float valueAtPhase(float phase);
167
173 void checkLineIsLinear();
174
183 float getValueBetweenPoints(float x, int index_from, int index_to);
184
191 float getValueAtPhase(float phase);
192
198 std::string getName() const { return name_; }
199
205 std::string getLastBrowsedFile() const { return last_browsed_file_; }
206
215 void addPoint(int index, std::pair<float, float> position);
216
224 void addMiddlePoint(int index);
225
231 void removePoint(int index);
232
238 void flipHorizontal();
239
245 void flipVertical();
246
252 std::pair<float, float> lastPoint() const { return points_[num_points_ - 1]; }
253
259 float lastPower() const { return powers_[num_points_ - 1]; }
260
266 force_inline int resolution() const { return resolution_; }
267
273 force_inline bool linear() const { return linear_; }
274
280 force_inline bool smooth() const { return smooth_; }
281
289 force_inline vital::mono_float* getBuffer() const { return buffer_.get() + 1; }
290
299
306 force_inline std::pair<float, float> getPoint(int index) const {
307 VITAL_ASSERT(index < kMaxPoints && index >= 0);
308 return points_[index];
309 }
310
317 force_inline float getPower(int index) const {
318 VITAL_ASSERT(index < kMaxPoints && index >= 0);
319 return powers_[index];
320 }
321
328 return num_points_;
329 }
330
337 force_inline void setPoint(int index, std::pair<float, float> point) {
338 VITAL_ASSERT(index < kMaxPoints && index >= 0);
339 points_[index] = point;
341 }
342
349 force_inline void setPower(int index, float power) {
350 VITAL_ASSERT(index < kMaxPoints && index >= 0);
351 powers_[index] = power;
353 }
354
360 force_inline void setNumPoints(int num_points) {
361 VITAL_ASSERT(num_points <= kMaxPoints && num_points >= 0);
362 num_points_ = num_points;
364 }
365
371 int getRenderCount() const { return render_count_; }
372
373protected:
374 std::string name_;
375 std::string last_browsed_file_;
376 std::pair<float, float> points_[kMaxPoints];
380
381 std::unique_ptr<vital::mono_float[]> buffer_;
382 bool loop_;
383 bool smooth_;
384 bool linear_;
386
387 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LineGenerator)
388};
A class for generating and storing a line shape, defined by a series of points and associated powers.
Definition line_generator.h:20
std::string name_
The name of the line shape.
Definition line_generator.h:374
void setLastBrowsedFile(const std::string &path)
Stores the last browsed file path associated with this line.
Definition line_generator.h:86
int render_count_
Count of how many times render() was called.
Definition line_generator.h:385
std::pair< float, float > lastPoint() const
Returns the last point in the line.
Definition line_generator.h:252
void initSin()
Initializes the line as a sine-like shape.
Definition line_generator.cpp:54
void setName(const std::string &name)
Sets a name identifier for the line.
Definition line_generator.h:79
void setLoop(bool loop)
Sets whether the line should loop at the end.
Definition line_generator.h:72
std::string last_browsed_file_
The last browsed file path for saving/loading this line.
Definition line_generator.h:375
force_inline bool smooth() const
Indicates whether smoothing is enabled.
Definition line_generator.h:280
float valueAtPhase(float phase)
Gets the line value at a given normalized phase.
Definition line_generator.cpp:199
bool loop_
Whether the line loops at the end.
Definition line_generator.h:382
int getRenderCount() const
Gets the number of times the line has been rendered since initialization.
Definition line_generator.h:371
void render()
Renders the line into the internal buffer based on the current points and settings.
Definition line_generator.cpp:149
std::string getLastBrowsedFile() const
Gets the last browsed file path associated with this line.
Definition line_generator.h:205
std::string getName() const
Gets the current name of the line.
Definition line_generator.h:198
float lastPower() const
Returns the power (interpolation shape factor) of the last point.
Definition line_generator.h:259
virtual ~LineGenerator()
Virtual destructor.
Definition line_generator.h:63
void checkLineIsLinear()
Checks if the line is the default linear shape.
Definition line_generator.cpp:205
LineGenerator(int resolution=kDefaultResolution)
Constructs a LineGenerator with a given resolution.
Definition line_generator.cpp:5
force_inline int getNumPoints() const
Returns the current number of points defining the line.
Definition line_generator.h:327
float powers_[kMaxPoints]
Powers controlling interpolation shape between points.
Definition line_generator.h:377
force_inline void setPower(int index, float power)
Sets the power for a specific point.
Definition line_generator.h:349
void addMiddlePoint(int index)
Inserts a point exactly between two existing points.
Definition line_generator.cpp:251
float getValueBetweenPoints(float x, int index_from, int index_to)
Interpolates a value between two points at a given x position.
Definition line_generator.cpp:211
force_inline void setPoint(int index, std::pair< float, float > point)
Sets the position of a specific point.
Definition line_generator.h:337
int num_points_
Current number of points.
Definition line_generator.h:378
static bool isValidJson(json data)
Checks if a given JSON object contains valid line data.
Definition line_generator.cpp:116
void setSmooth(bool smooth)
Enables or disables smoothing behavior.
Definition line_generator.h:96
void removePoint(int index)
Removes the point at a specified index.
Definition line_generator.cpp:259
void flipHorizontal()
Flips the line horizontally around the x=0.5 vertical axis.
Definition line_generator.cpp:268
force_inline vital::mono_float * getBuffer() const
Gets a pointer to the internal buffer used for interpolation.
Definition line_generator.h:289
void initTriangle()
Initializes the line as a triangle shape.
Definition line_generator.cpp:23
force_inline int resolution() const
Gets the resolution of the line's internal buffer.
Definition line_generator.h:266
force_inline bool linear() const
Indicates whether the line is currently a simple linear shape.
Definition line_generator.h:273
std::pair< float, float > points_[kMaxPoints]
Array of points defining the line shape.
Definition line_generator.h:376
std::unique_ptr< vital::mono_float[]> buffer_
Internal buffer holding the rendered line values.
Definition line_generator.h:381
static force_inline float smoothTransition(float t)
Smooth transition function for smoothing between points.
Definition line_generator.h:49
float getValueAtPhase(float phase)
Returns the value of the line at a given phase by searching through the points.
Definition line_generator.cpp:230
void jsonToState(json data)
Restores the line state from a given JSON object.
Definition line_generator.cpp:125
force_inline void setNumPoints(int num_points)
Sets the number of points currently in use.
Definition line_generator.h:360
void initSawDown()
Initializes the line as a "saw down" shape.
Definition line_generator.cpp:82
static constexpr int kMaxPoints
Maximum number of points that can define the line.
Definition line_generator.h:25
void initSquare()
Initializes the line as a square shape.
Definition line_generator.cpp:37
json stateToJson()
Converts the current state of the line into a JSON object.
Definition line_generator.cpp:96
force_inline std::pair< float, float > getPoint(int index) const
Returns a point at the given index.
Definition line_generator.h:306
void initLinear()
Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1).
Definition line_generator.cpp:11
force_inline vital::mono_float * getCubicInterpolationBuffer() const
Gets a pointer to the buffer used for cubic interpolation.
Definition line_generator.h:298
bool linear_
Whether the line is the simple linear shape.
Definition line_generator.h:384
force_inline float getPower(int index) const
Returns the power at the given index.
Definition line_generator.h:317
bool smooth_
Whether to apply smoothing between points.
Definition line_generator.h:383
static constexpr int kExtraValues
Extra buffer values used for interpolation outside the main range.
Definition line_generator.h:39
void addPoint(int index, std::pair< float, float > position)
Inserts a new point into the line at a specified index.
Definition line_generator.cpp:239
void flipVertical()
Flips the line vertically around y=0.5.
Definition line_generator.cpp:286
void initSawUp()
Initializes the line as a "saw up" shape.
Definition line_generator.cpp:68
static constexpr int kDefaultResolution
Default resolution of the rendered line buffer.
Definition line_generator.h:32
int resolution_
Resolution of the internal buffer.
Definition line_generator.h:379
#define VITAL_ASSERT(x)
Definition common.h:11
#define force_inline
Definition common.h:23
nlohmann::json json
Definition line_generator.h:7
constexpr mono_float kPi
Pi constant.
Definition common.h:36
float mono_float
Definition common.h:33