FastLED 3.9.15
Loading...
Searching...
No Matches
stb_hexwave.h
Go to the documentation of this file.
1// stb_hexwave.h - Header for stb_hexwave audio oscillator
2//
3// This file contains the API declarations extracted from stb_hexwave.cpp.hpp.
4// It provides the public interface without including the implementation.
5//
6// For the implementation, include stb_hexwave.cpp.hpp (which is included
7// via the unity build system in _build.hpp).
8//
9// Original: https://github.com/nothings/stb/blob/master/stb_hexwave.h
10// Author: Sean Barrett - http://nothings.org/stb_vorbis/
11// License: MIT or Public Domain
12//
13// stb_hexwave - v0.5 - public domain, initial release 2021-04-01
14//
15// A flexible anti-aliased (bandlimited) digital audio oscillator.
16//
17// This library generates waveforms of a variety of shapes made of
18// line segments. It does not do envelopes, LFO effects, etc.; it
19// merely tries to solve the problem of generating an artifact-free
20// morphable digital waveform with a variety of spectra, and leaves
21// it to the user to rescale the waveform and mix multiple voices, etc.
22//
23// Usage:
24//
25// Initialization:
26//
27// hexwave_init(32, 16, nullptr); // read "header section" for alternatives
28//
29// Create oscillator:
30//
31// HexWave osc;
32// hexwave_create(&osc, reflect_flag, peak_time, half_height, zero_wait);
33// see "Waveform shapes" section in implementation for parameter meanings
34//
35// Generate audio:
36//
37// hexwave_generate_samples(output, number_of_samples, &osc, oscillator_freq)
38// where:
39// output is a buffer where the library will store floating point audio samples
40// number_of_samples is the number of audio samples to generate
41// osc is a pointer to a HexWave
42// oscillator_freq is the frequency of the oscillator divided by the sample rate
43//
44// Change oscillator waveform:
45//
46// hexwave_change(&osc, reflect_flag, peak_time, half_height, zero_wait);
47// can call in between calls to hexwave_generate_samples
48//
49// Classic waveforms:
50// peak half zero
51// reflect time height wait
52// Sawtooth 1 0 0 0
53// Square 1 0 1 0
54// Triangle 1 0.5 0 0
55//
56// LICENSE
57//
58// See end of file for license information.
59
60#ifndef FL_STB_HEXWAVE_INCLUDE_FL_STB_HEXWAVE_H
61#define FL_STB_HEXWAVE_INCLUDE_FL_STB_HEXWAVE_H
62
63#include "fl/stl/stdint.h"
64#include "fl/stl/noexcept.h"
65
66#ifndef FL_STB_HEXWAVE_MAX_BLEP_LENGTH
67#define FL_STB_HEXWAVE_MAX_BLEP_LENGTH 64 // good enough for anybody
68#endif
69
70namespace fl {
71namespace third_party {
72namespace hexwave {
73
75//
76// PUBLIC TYPES
77//
79
80// Internal waveform parameters structure
81typedef struct
82{
84 float peak_time;
85 float zero_wait;
88
105
106// Main oscillator state structure
115
117//
118// PUBLIC API - Instance-based (Recommended)
119//
121
130HexWaveEngine* hexwave_engine_create(int32_t width, int32_t oversample, float *user_buffer) FL_NOEXCEPT;
131
135
150void hexwave_create(HexWave *hex, HexWaveEngine *engine, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT;
151
158void hexwave_change(HexWave *hex, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT;
159
168void hexwave_generate_samples(float *output, int32_t num_samples, HexWave *hex, float freq) FL_NOEXCEPT;
169
171//
172// LEGACY API - Global state (Deprecated)
173//
174// These functions use a hidden global engine for backward compatibility.
175// New code should use the instance-based API above.
176//
178
183void hexwave_init(int32_t width, int32_t oversample, float *user_buffer) FL_NOEXCEPT;
184
187void hexwave_shutdown(float *user_buffer) FL_NOEXCEPT;
188
191void hexwave_create_legacy(HexWave *hex, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT;
192
193} // namespace hexwave
194} // namespace third_party
195} // namespace fl
196
197#endif // FL_STB_HEXWAVE_INCLUDE_FL_STB_HEXWAVE_H
198
199/*
200------------------------------------------------------------------------------
201This software is available under 2 licenses -- choose whichever you prefer.
202------------------------------------------------------------------------------
203ALTERNATIVE A - MIT License
204Copyright (c) 2017 Sean Barrett
205Permission is hereby granted, free of charge, to any person obtaining a copy of
206this software and associated documentation files (the "Software"), to deal in
207the Software without restriction, including without limitation the rights to
208use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
209of the Software, and to permit persons to whom the Software is furnished to do
210so, subject to the following conditions:
211The above copyright notice and this permission notice shall be included in all
212copies or substantial portions of the Software.
213THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
214IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
215FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
216AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
217LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
219SOFTWARE.
220------------------------------------------------------------------------------
221ALTERNATIVE B - Public Domain (www.unlicense.org)
222This is free and unencumbered software released into the public domain.
223Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
224software, either in source code form or as a compiled binary, for any purpose,
225commercial or non-commercial, and by any means.
226In jurisdictions that recognize copyright laws, the author or authors of this
227software dedicate any and all copyright interest in the software to the public
228domain. We make this dedication for the benefit of the public at large and to
229the detriment of our heirs and successors. We intend this dedication to be an
230overt act of relinquishment in perpetuity of all present and future rights to
231this software under copyright law.
232THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
233IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
234FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
235AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
236OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
237SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
238------------------------------------------------------------------------------
239*/
void hexwave_engine_destroy(HexWaveEngine *engine) FL_NOEXCEPT
Destroy a HexWaveEngine and free its resources.
void hexwave_create(HexWave *hex, HexWaveEngine *engine, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT
Create a new oscillator with the given waveform parameters.
void hexwave_change(HexWave *hex, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT
Change oscillator waveform parameters (takes effect at next cycle boundary)
void hexwave_create_legacy(HexWave *hex, int32_t reflect, float peak_time, float half_height, float zero_wait) FL_NOEXCEPT
Create oscillator using global engine (DEPRECATED)
void hexwave_init(int32_t width, int32_t oversample, float *user_buffer) FL_NOEXCEPT
Initialize the hexwave library (DEPRECATED - use hexwave_engine_create)
void hexwave_shutdown(float *user_buffer) FL_NOEXCEPT
Shutdown the hexwave library (DEPRECATED - use hexwave_engine_destroy)
HexWaveEngine * hexwave_engine_create(int32_t width, int32_t oversample, float *user_buffer) FL_NOEXCEPT
Create and initialize a new HexWaveEngine.
void hexwave_generate_samples(float *output, int32_t num_samples, HexWave *hex, float freq) FL_NOEXCEPT
Generate audio samples.
float * blep
Band-limited step table.
bool ownsBuffers
True if engine allocated blep/blamp (vs user-provided)
float * blamp
Band-limited ramp table.
int32_t oversample
Number of oversampled versions.
HexWaveEngine * engine
Pointer to engine with BLEP/BLAMP tables.
float buffer[FL_STB_HEXWAVE_MAX_BLEP_LENGTH]
int32_t width
Width of fixup in samples (4..64)
Definition stb_hexwave.h:99
Engine state holding BLEP/BLAMP tables.
Definition stb_hexwave.h:98
fl::i32 int32_t
Definition coder.h:220
const hex_t hex
Definition ios.cpp.hpp:6
u8 width
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FL_STB_HEXWAVE_MAX_BLEP_LENGTH
Definition stb_hexwave.h:67