Vital
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2
9#include "common.h"
10
11#include <cmath>
12#include <complex>
13#include <cstdlib>
14#include <random>
15
16namespace vital {
17
22 namespace utils {
23
27 constexpr int kMaxOrderLength = 10;
29 constexpr mono_float kLogOf2 = 0.69314718056f;
31 constexpr mono_float kInvLogOf2 = 1.44269504089f;
32
38 constexpr int factorial(int value) {
39 int result = 1;
40 for (int i = 2; i <= value; ++i)
41 result *= i;
42 return result;
43 }
44
49 typedef union {
50 int i;
52 } int_float;
53
62 public:
64 static int next_seed_;
65
72 : engine_(next_seed_++), distribution_(min, max) { }
73
79 : engine_(next_seed_++),
80 distribution_(other.distribution_.min(), other.distribution_.max()) { }
81
86 return distribution_(engine_);
87 }
88
94 poly_float result;
95 for (int i = 0; i < poly_float::kSize; ++i)
96 result.set(i, next());
97 return result;
98 }
99
106 poly_float result;
107 for (int i = 0; i < poly_float::kSize; i += 2) {
108 mono_float value = next();
109 result.set(i, value);
110 result.set(i + 1, value);
111 }
112 return result;
113 }
114
121 poly_float result = 0.0f;
122 for (int i = 0; i < poly_float::kSize; ++i) {
123 if (mask[i])
124 result.set(i, next());
125 }
126 return result;
127 }
128
133 force_inline void seed(int new_seed) {
134 engine_.seed(new_seed);
135 }
136
137 private:
138 std::mt19937 engine_;
139 std::uniform_real_distribution<mono_float> distribution_;
140
141 JUCE_LEAK_DETECTOR(RandomGenerator)
142 };
143
150 int_float convert;
151 convert.i = i;
152 return convert.f;
153 }
154
161 int_float convert;
162 convert.f = f;
163 return convert.i;
164 }
165
170 return fmin(one, two);
171 }
172
177 return fmax(one, two);
178 }
179
188 return fmin(max_val, fmax(value, min_val));
189 }
190
197 template<class T>
198 force_inline T pass(T input) {
199 return input;
200 }
201
205 force_inline int imax(int one, int two) {
206 return (one > two) ? one : two;
207 }
208
212 force_inline int imin(int one, int two) {
213 return (one > two) ? two : one;
214 }
215
222 force_inline double interpolate(double from, double to, double t) {
223 return t * (to - from) + from;
224 }
225
230 return from + t * (to - from);
231 }
232
236 force_inline mono_float mod(double value, double* divisor) {
237 return modf(value, divisor);
238 }
239
243 force_inline mono_float mod(float value, float* divisor) {
244 return modff(value, divisor);
245 }
246
250 force_inline int iclamp(int value, int min_val, int max_val) {
251 return value > max_val ? max_val : (value < min_val ? min_val : value);
252 }
253
259 force_inline int ilog2(int value) {
260 #if defined(__GNUC__) || defined(__clang__)
261 constexpr int kMaxBitIndex = sizeof(int) * 8 - 1;
262 return kMaxBitIndex - __builtin_clz(std::max(value, 1));
263 #elif defined(_MSC_VER)
264 unsigned long result = 0;
265 _BitScanReverse(&result, value);
266 return result;
267 #else
268 int num = 0;
269 while (value >>= 1)
270 num++;
271 return num;
272 #endif
273 }
274
281 return value <= kEpsilon && value >= -kEpsilon;
282 }
283
290 return kDbGainConversionMult * log10f(magnitude);
291 }
292
299 return powf(10.0f, decibels / kDbGainConversionMult);
300 }
301
308 return powf(2.0f, cents / kCentsPerOctave);
309 }
310
317 return powf(2.0f, cents / kNotesPerOctave);
318 }
319
328
337
346
355
364
371 return roundf(powf(2.0f, ceilf(logf(value) * kInvLogOf2)));
372 }
373
380 force_inline bool isSilent(const mono_float* buffer, int length) {
381 for (int i = 0; i < length; ++i) {
382 if (!closeToZero(buffer[i]))
383 return false;
384 }
385 return true;
386 }
387
394 force_inline mono_float rms(const mono_float* buffer, int num) {
395 mono_float square_total = 0.0f;
396 for (int i = 0; i < num; ++i)
397 square_total += buffer[i] * buffer[i];
398
399 return sqrtf(square_total / num);
400 }
401
408 return 2.0f * logf((-t + 1.0f) / t);
409 }
410
417 return (t - 1.0f) / t;
418 }
419
426 mono_float encodeOrderToFloat(int* order, int size);
427
434 void decodeFloatToOrder(int* order, mono_float float_code, int size);
435
442 void floatToPcmData(int16_t* pcm_data, const float* float_data, int size);
443
450 void complexToPcmData(int16_t* pcm_data, const std::complex<float>* complex_data, int size);
451
458 void pcmToFloatData(float* float_data, const int16_t* pcm_data, int size);
459
466 void pcmToComplexData(std::complex<float>* complex_data, const int16_t* pcm_data, int size);
467
468 } // namespace utils
469} // namespace vital
A basic random number generator for producing uniform distributions of floats.
Definition utils.h:61
force_inline mono_float next()
Returns the next random float in [min, max].
Definition utils.h:85
static int next_seed_
Static seed counter used to automatically assign seeds if none specified.
Definition utils.h:64
force_inline poly_float polyVoiceNext()
Produces a poly_float with random values assigned in pairs (every 2 lanes share the same random value...
Definition utils.h:105
RandomGenerator(mono_float min, mono_float max)
Constructs a RandomGenerator with specified min and max values.
Definition utils.h:71
force_inline void seed(int new_seed)
Reseeds the internal random engine with new_seed.
Definition utils.h:133
force_inline poly_float polyNext()
Produces a poly_float with random values in each lane.
Definition utils.h:93
force_inline poly_float polyNext(poly_mask mask)
Produces a poly_float of random values, only generated for lanes set in mask.
Definition utils.h:120
RandomGenerator(const RandomGenerator &other)
Copy constructor, but it re-seeds the engine for uniqueness.
Definition utils.h:78
#define force_inline
Definition common.h:23
Contains a collection of utility functions and classes used throughout Vital.
force_inline int iclamp(int value, int min_val, int max_val)
Clamps an integer between [min_val, max_val].
Definition utils.h:250
force_inline poly_float mod(poly_float value)
Returns the fractional part of each lane by subtracting the floored value.
Definition poly_utils.h:814
constexpr mono_float kDbGainConversionMult
Multiplicative factor for converting dB <-> magnitude using dB = 20*log10(magnitude).
Definition utils.h:25
force_inline poly_float clamp(poly_float value, mono_float min, mono_float max)
Clamps each lane of a vector to [min, max].
Definition poly_utils.h:306
force_inline bool isSilent(const poly_float *buffer, int size)
Determines if the entire buffer is silent (very close to zero).
Definition poly_utils.h:669
constexpr int kMaxOrderLength
Maximum length for orders that can be encoded as a float via encodeOrderToFloat().
Definition utils.h:27
force_inline T pass(T input)
A pass-through function that simply returns the input. Often used in templated code.
Definition utils.h:198
force_inline mono_float inverseFltScale(mono_float t)
Another curve function, typically used for certain shape transformations.
Definition utils.h:416
force_inline int imax(int one, int two)
Returns the maximum of two integers.
Definition utils.h:205
force_inline mono_float inversePowerScale(mono_float t)
A curve function used for specific shaping or scaling of a parameter.
Definition utils.h:407
force_inline int ilog2(int value)
Computes the floor of the base-2 logarithm of an integer (effectively the index of the highest set bi...
Definition utils.h:259
force_inline poly_float min(poly_float left, poly_float right)
Returns the minimum of two poly_floats lane-by-lane.
Definition poly_utils.h:334
force_inline int nextPowerOfTwo(mono_float value)
Finds the next power of two greater than or equal to a float value.
Definition utils.h:370
constexpr mono_float kLogOf2
Natural log of 2.
Definition utils.h:29
void pcmToComplexData(std::complex< float > *complex_data, const int16_t *pcm_data, int size)
Converts 16-bit PCM data representing complex info (amp/phase) back to std::complex floats.
Definition utils.cpp:99
void floatToPcmData(int16_t *pcm_data, const float *float_data, int size)
Converts floating-point audio data to 16-bit PCM data.
Definition utils.cpp:77
force_inline poly_float magnitudeToDb(poly_float value)
Converts a magnitude value to decibels (vectorized).
Definition poly_utils.h:144
force_inline int floatToIntBits(mono_float f)
Reinterprets a float as an int (bitwise).
Definition utils.h:160
void decodeFloatToOrder(int *order, mono_float float_code, int size)
Decodes a float-encoded permutation back into order.
Definition utils.cpp:55
force_inline poly_float frequencyToMidiNote(poly_float value)
Converts a frequency to a MIDI note (vectorized).
Definition poly_utils.h:130
constexpr mono_float kInvLogOf2
Reciprocal of the natural log of 2 (1 / ln(2)).
Definition utils.h:31
constexpr int factorial(int value)
Computes the factorial of a given integer at compile time.
Definition utils.h:38
force_inline poly_float dbToMagnitude(poly_float value)
Converts a dB value to linear magnitude (vectorized).
Definition poly_utils.h:149
mono_float encodeOrderToFloat(int *order, int size)
Encodes a permutation (stored in order) into a single float.
Definition utils.cpp:32
force_inline mono_float rms(const mono_float *buffer, int num)
Computes the Root Mean Square (RMS) of a buffer of floats.
Definition utils.h:394
force_inline poly_float ratioToMidiTranspose(poly_float value)
Converts a ratio to MIDI transpose (vectorized).
Definition poly_utils.h:109
force_inline poly_float max(poly_float left, poly_float right)
Returns the maximum of two poly_floats lane-by-lane.
Definition poly_utils.h:327
void complexToPcmData(int16_t *pcm_data, const std::complex< float > *complex_data, int size)
Converts an array of complex floats (magnitude/phase) to PCM data.
Definition utils.cpp:82
force_inline poly_float midiNoteToFrequency(poly_float value)
Converts a MIDI note to a frequency (vectorized).
Definition poly_utils.h:123
void pcmToFloatData(float *float_data, const int16_t *pcm_data, int size)
Converts 16-bit PCM data to floating-point audio data.
Definition utils.cpp:94
force_inline poly_float noteOffsetToRatio(poly_float value)
Converts note offsets to frequency ratios. Similar to centsToRatio, but may differ in how offset is d...
Definition poly_utils.h:102
force_inline int imin(int one, int two)
Returns the minimum of two integers.
Definition utils.h:212
force_inline bool closeToZero(mono_float value)
Determines if a float is close to zero (within ±kEpsilon).
Definition utils.h:280
force_inline poly_float interpolate(poly_float from, poly_float to, mono_float t)
Performs a linear interpolation between two poly_floats using a scalar t in [0..1].
Definition poly_utils.h:182
force_inline poly_float frequencyToMidiCents(poly_float value)
Converts a frequency to MIDI cents (vectorized).
Definition poly_utils.h:137
force_inline poly_float midiCentsToFrequency(poly_float value)
Converts MIDI cents to frequency (vectorized).
Definition poly_utils.h:116
force_inline mono_float intToFloatBits(int i)
Reinterprets an int as a float (bitwise).
Definition utils.h:149
force_inline poly_float centsToRatio(poly_float value)
Converts semitone cents to a linear frequency ratio (vectorized).
Definition poly_utils.h:95
Contains classes and functions used within the Vital synthesizer framework.
constexpr mono_float kMidi0Frequency
Frequency of MIDI note 0 (C-1).
Definition common.h:47
constexpr int kNotesPerOctave
Number of semitones per octave.
Definition common.h:51
constexpr int kCentsPerOctave
Cents per octave (1200).
Definition common.h:53
constexpr int kCentsPerNote
Number of cents per semitone.
Definition common.h:52
constexpr mono_float kEpsilon
A small epsilon for floating comparisons.
Definition common.h:38
float mono_float
Definition common.h:33
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600
force_inline void vector_call set(size_t index, float new_value) noexcept
Sets a specific element in the SIMD register.
Definition poly_values.h:1182
Represents a vector of integer values using SIMD instructions.
Definition poly_values.h:56
A small union to reinterpret an int as a float or vice versa.
Definition utils.h:49
int i
Integer representation.
Definition utils.h:50
mono_float f
Float representation.
Definition utils.h:51