32template <std::
floating_po
int FloatType,
size_t NumSamples,
class Function>
33constexpr std::array<FloatType, NumSamples>
makeWaveTable (Function func)
35 std::array<FloatType, NumSamples> ar;
36 std::iota (ar.begin(), ar.end(), 0);
37 std::for_each (ar.begin(), ar.end(), [] (
auto& x)
38 { x = x / (NumSamples - 1); });
39 std::for_each (ar.begin(), ar.end(), func);
48template <std::
floating_po
int FloatType,
size_t N>
53 x =
ame::sin (x * ame::twoPi<float>);
55 return ame::makeWaveTable<FloatType, N> (f);
62template <std::
floating_po
int FloatType,
size_t N>
65 using FloatTypeBase =
typename std::remove_cv<FloatType>::type;
68 explicit WavetableOscillator (std::span<FloatType, N> wavetable,
const FloatTypeBase sampleRate)
69 : wavetable (wavetable)
71 tableIndex.changeLength (wavetable.size());
81 samplingPeriod = 1.0f / sampleRate;
89 tableIndexIncrement = freq * tableIndex.getLength() * samplingPeriod;
97 assert (! newWavetable.empty());
98 wavetable = newWavetable;
99 tableIndex.changeLength (wavetable.size());
107 const uint32_t aIndex = std::floor (tableIndex.get());
108 const uint32_t bIndex = std::floor (tableIndex.get (1));
109 const auto t = tableIndex.get() - aIndex;
110 const auto a = wavetable[aIndex];
111 const auto b = wavetable[bIndex];
113 tableIndex += tableIndexIncrement;
114 return std::lerp (a, b, t);
118 std::span<FloatType, N> wavetable;
120 FloatTypeBase samplingPeriod {};
121 std::atomic<FloatTypeBase> tableIndexIncrement {};
127template <std::
floating_po
int FloatType>
130 static_assert (! std::is_const<FloatType>::value,
"FloatType is must NOT be const.");
148 samplingPeriod = FloatType (1.0) / sampleRate;
156 phaseIncrement = freq * twoPi<FloatType> * samplingPeriod;
164 const auto p = phase.load();
174 assert (0 <= newPhase && newPhase <= twoPi<FloatType>);
175 if (FloatType (0.0) <= newPhase && newPhase <= twoPi<FloatType>)
182 FloatType samplingPeriod;
183 std::atomic<FloatType> phaseIncrement {};
184 std::atomic<FloatType> phase {};
constexpr float sin(float x)
sin for float
Definition: ame_Math.hpp:61
constexpr std::array< FloatType, N > makeSineTable()
Sine wave wavetable generator.
Definition: ame_Oscillator.hpp:49
constexpr std::array< FloatType, NumSamples > makeWaveTable(Function func)
Wavetable generator.
Definition: ame_Oscillator.hpp:33
Some utilities functions.
constexpr FloatType addModulo2Pi(FloatType phase, FloatType increment) noexcept
Increment the phase and returns in the range of 0~2pi.
Definition: ame_Util.hpp:43
Sine wave oscillator.
Definition: ame_Oscillator.hpp:129
void setFrequency(FloatType freq) noexcept
Set the sine wave frequency.
Definition: ame_Oscillator.hpp:154
void setSampleRate(FloatType sampleRate)
Set sampling rate.
Definition: ame_Oscillator.hpp:146
float nextSample() noexcept
Generate single sample.
Definition: ame_Oscillator.hpp:162
SineOscillator(FloatType sampleRate) noexcept
Create sine wave oscillator instance.
Definition: ame_Oscillator.hpp:137
void resetPhase(FloatType newPhase=0.0f)
Reset the phase to any value.
Definition: ame_Oscillator.hpp:172
Wavetable oscillator.
Definition: ame_Oscillator.hpp:64
void setSampleRate(const FloatTypeBase sampleRate)
Set sampling rate.
Definition: ame_Oscillator.hpp:79
FloatTypeBase nextSample() noexcept
Generate single sample.
Definition: ame_Oscillator.hpp:105
void setFrequency(const FloatTypeBase freq) noexcept
Set the frequency.
Definition: ame_Oscillator.hpp:87
void setWavetable(std::span< FloatType, N > newWavetable)
Set the wavetable.
Definition: ame_Oscillator.hpp:95
A number to wrap between 0~length.
Definition: ame_Util.hpp:254