FastLED 3.9.15
Loading...
Searching...
No Matches

◆ hexwave_engine_create()

HexWaveEngine * fl::third_party::hexwave::hexwave_engine_create ( int32_t width,
int32_t oversample,
float * user_buffer )

Create and initialize a new HexWaveEngine.

Parameters
widthSize of BLEP, from 4..64, larger is slower & more memory but less aliasing
oversample2+, number of subsample positions, larger uses more memory but less noise
user_bufferOptional, if provided the library will perform no allocations. 16*width*(oversample+1) bytes, must stay allocated as long as engine is used
Returns
Pointer to initialized engine, or nullptr on failure

Width can be larger than 64 if you define FL_STB_HEXWAVE_MAX_BLEP_LENGTH to a larger value

Definition at line 335 of file stb_hexwave.cpp.hpp.

336{
339
340 int32_t halfwidth = width/2;
341 int32_t half = halfwidth*oversample;
342 int32_t blep_buffer_count = width*(oversample+1);
343 int32_t n = 2*half+1;
344
345 #ifdef FL_STB_HEXWAVE_NO_ALLOCATION
346 (void)blep_buffer_count;
347 if (user_buffer == nullptr)
348 return nullptr; // Cannot allocate in no-allocation mode
349 float *buffers = user_buffer;
350 #else
351 float *buffers = user_buffer ? user_buffer : (float *) malloc(sizeof(float) * n * 2);
352 #endif
353
354 float *step = buffers+0*n;
355 float *ramp = buffers+1*n;
356 float *blep_buffer, *blamp_buffer;
357 double integrate_impulse=0, integrate_step=0;
358 int32_t i,j;
359
360 bool ownsBuffers = false;
361 if (user_buffer == 0) {
362 #ifndef FL_STB_HEXWAVE_NO_ALLOCATION
363 blep_buffer = (float *) malloc(sizeof(float)*blep_buffer_count);
364 blamp_buffer = (float *) malloc(sizeof(float)*blep_buffer_count);
365 ownsBuffers = true;
366 #else
367 blep_buffer = nullptr;
368 blamp_buffer = nullptr;
369 #endif
370 } else {
371 blep_buffer = ramp+n;
372 blamp_buffer = blep_buffer + blep_buffer_count;
373 }
374
375 // compute BLEP and BLAMP by integrating windowed sinc
376 for (i=0; i < n; ++i) {
377 for (j=0; j < 16; ++j) {
378 float sinc_t = 3.141592f* (i-half) / oversample;
379 float sinc = (i==half) ? 1.0f : (float) sin(static_cast<double>(sinc_t)) / (sinc_t);
380 float wt = 2.0f*3.1415926f * i / (n-1);
381 float window = (float) (0.355768 - 0.487396*cos(static_cast<double>(wt)) + 0.144232*cos(2*static_cast<double>(wt)) - 0.012604*cos(3*static_cast<double>(wt))); // Nuttall
382 double value = window * sinc;
383 integrate_impulse += value/16;
384 integrate_step += integrate_impulse/16;
385 }
386 step[i] = (float) integrate_impulse;
387 ramp[i] = (float) integrate_step;
388 }
389
390 // renormalize
391 for (i=0; i < n; ++i) {
392 step[i] = step[i] * (float) (1.0 / step[n-1]); // step needs to reach to 1.0
393 ramp[i] = ramp[i] * (float) (halfwidth / ramp[n-1]); // ramp needs to become a slope of 1.0 after oversampling
394 }
395
396 // deinterleave to allow efficient interpolation e.g. w/SIMD
397 for (j=0; j <= oversample; ++j) {
398 for (i=0; i < width; ++i) {
399 blep_buffer [j*width+i] = step[j+i*oversample];
400 blamp_buffer[j*width+i] = ramp[j+i*oversample];
401 }
402 }
403
404 // subtract out the naive waveform; note we can't do this to the raw data
405 // above, because we want the discontinuity to be in a different locations
406 // for j=0 and j=oversample (which exists to provide something to interpolate against)
407 for (j=0; j <= oversample; ++j) {
408 // subtract step
409 for (i=halfwidth; i < width; ++i)
410 blep_buffer [j*width+i] -= 1.0f;
411 // subtract ramp
412 for (i=halfwidth; i < width; ++i)
413 blamp_buffer[j*width+i] -= (j+i*oversample-half)*(1.0f/oversample);
414 }
415
416 // Allocate and initialize engine
417 #ifndef FL_STB_HEXWAVE_NO_ALLOCATION
418 HexWaveEngine *engine = (HexWaveEngine *) malloc(sizeof(HexWaveEngine));
419 engine->blep = blep_buffer;
420 engine->blamp = blamp_buffer;
421 engine->width = width;
422 engine->oversample = oversample;
423 engine->ownsBuffers = ownsBuffers;
424
425 if (user_buffer == 0)
426 free(buffers);
427
428 return engine;
429 #else
430 (void)ownsBuffers;
431 return nullptr;
432 #endif
433}
void * malloc(size_t size)
Definition malloc.cpp.hpp:9
enable_if< is_fixed_point< T >::value, T >::type cos(T angle) FL_NOEXCEPT
float * blep
Band-limited step table.
Engine state holding BLEP/BLAMP tables.
Definition stb_hexwave.h:98
fl::i32 int32_t
Definition coder.h:220
constexpr int type_rank< T >::value
u8 width
Definition blur.h:186
constexpr enable_if< is_fixed_point< T >::value, T >::type step(T edge, T x) FL_NOEXCEPT
enable_if< is_fixed_point< T >::value, T >::type sin(T angle) FL_NOEXCEPT
#define FL_STB_HEXWAVE_MAX_BLEP_LENGTH
Definition stb_hexwave.h:67

References fl::third_party::hexwave::HexWaveEngine::blamp, fl::third_party::hexwave::HexWaveEngine::blep, cos(), FL_NOEXCEPT, FL_STB_HEXWAVE_MAX_BLEP_LENGTH, free(), malloc(), fl::third_party::hexwave::HexWaveEngine::oversample, fl::third_party::hexwave::HexWaveEngine::ownsBuffers, sin(), fl::step(), fl::type_rank< T >::value, fl::third_party::hexwave::HexWaveEngine::width, and fl::width.

Referenced by hexwave_init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: