diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 171721f4..d5c8882f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,6 @@ set (SFIZZ_SOURCES sfizz/SfzFilter.cpp sfizz/Curve.cpp sfizz/Wavetables.cpp - sfizz/Resources.cpp sfizz/Effects.cpp sfizz/effects/Nothing.cpp sfizz/effects/Filter.cpp diff --git a/src/sfizz/Config.h b/src/sfizz/Config.h index b2a6f8a2..e1bab309 100644 --- a/src/sfizz/Config.h +++ b/src/sfizz/Config.h @@ -73,6 +73,12 @@ namespace config { Limit of how many "fxN" buses are accepted (in SFZv2, maximum is 4) */ constexpr int maxEffectBuses { 256 }; + // Wavetable constants; amplitude values are matched to reference + static constexpr unsigned tableSize = 1024; + static constexpr double amplitudeSine = 0.625; + static constexpr double amplitudeTriangle = 0.625; + static constexpr double amplitudeSaw = 0.515; + static constexpr double amplitudeSquare = 0.515; } // namespace config // Enable or disable SIMD accelerators by default diff --git a/src/sfizz/Resources.cpp b/src/sfizz/Resources.cpp deleted file mode 100644 index af250853..00000000 --- a/src/sfizz/Resources.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// SPDX-License-Identifier: BSD-2-Clause - -// This code is part of the sfizz library and is licensed under a BSD 2-clause -// license. You should have receive a LICENSE.md file along with the code. -// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz - -#include "Resources.h" -#include "Wavetables.h" - -namespace sfz -{ - -static constexpr unsigned kTableSize = 1024; - -// amplitude values are matched to reference -static constexpr double kAmplitudeSine = 0.625; -static constexpr double kAmplitudeTriangle = 0.625; -static constexpr double kAmplitudeSaw = 0.515; -static constexpr double kAmplitudeSquare = 0.515; - -static const WavetableMulti* getWaveSin() -{ - static auto wave = WavetableMulti::createForHarmonicProfile( - HarmonicProfile::getSine(), kAmplitudeSine, kTableSize); - return &wave; -} - -static const WavetableMulti* getWaveTriangle() -{ - static auto wave = WavetableMulti::createForHarmonicProfile( - HarmonicProfile::getTriangle(), kAmplitudeTriangle, kTableSize); - return &wave; -} - -static const WavetableMulti* getWaveSaw() -{ - static auto wave = WavetableMulti::createForHarmonicProfile( - HarmonicProfile::getSaw(), kAmplitudeSaw, kTableSize); - return &wave; -} - -static const WavetableMulti* getWaveSquare() -{ - static auto wave = WavetableMulti::createForHarmonicProfile( - HarmonicProfile::getSquare(), kAmplitudeSquare, kTableSize); - return &wave; -} - -Resources::Resources() - : waveSin(getWaveSin()), - waveTriangle(getWaveTriangle()), - waveSaw(getWaveSaw()), - waveSquare(getWaveSquare()) -{ -} - - -} diff --git a/src/sfizz/Resources.h b/src/sfizz/Resources.h index f4ec3f3d..99bc9dac 100644 --- a/src/sfizz/Resources.h +++ b/src/sfizz/Resources.h @@ -9,6 +9,7 @@ #include "FilterPool.h" #include "EQPool.h" #include "Logger.h" +#include "Wavetables.h" namespace sfz { @@ -21,12 +22,6 @@ struct Resources FilePool filePool { logger }; FilterPool filterPool { midiState }; EQPool eqPool { midiState }; - - const WavetableMulti* waveSin { nullptr }; - const WavetableMulti* waveTriangle { nullptr }; - const WavetableMulti* waveSaw { nullptr }; - const WavetableMulti* waveSquare { nullptr }; - - Resources(); + WavetablePool wavePool; }; } diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index 9ecd48b8..659f3a80 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -44,17 +44,17 @@ void sfz::Voice::startVoice(Region* region, int delay, int number, uint8_t value case hash("*silence"): break; case hash("*sine"): - wave = resources.waveSin; + wave = resources.wavePool.getWaveSin(); break; case hash("*triangle"): // fallthrough case hash("*tri"): - wave = resources.waveTriangle; + wave = resources.wavePool.getWaveTriangle(); break; case hash("*square"): - wave = resources.waveSquare; + wave = resources.wavePool.getWaveSquare(); break; case hash("*saw"): - wave = resources.waveSaw; + wave = resources.wavePool.getWaveSaw(); break; } waveOscillator.setWavetable(wave); diff --git a/src/sfizz/Wavetables.cpp b/src/sfizz/Wavetables.cpp index 4c8017fa..fe19a4b6 100644 --- a/src/sfizz/Wavetables.cpp +++ b/src/sfizz/Wavetables.cpp @@ -273,4 +273,13 @@ void WavetableMulti::fillExtra() } } + +WavetablePool::WavetablePool() +: waveSin(WavetableMulti::createForHarmonicProfile(HarmonicProfile::getSine(), config::amplitudeSine)) +, waveTriangle(WavetableMulti::createForHarmonicProfile(HarmonicProfile::getTriangle(), config::amplitudeTriangle)) +, waveSaw(WavetableMulti::createForHarmonicProfile(HarmonicProfile::getSaw(), config::amplitudeSaw)) +, waveSquare(WavetableMulti::createForHarmonicProfile(HarmonicProfile::getSquare(), config::amplitudeSquare)) +{ } + + } // namespace sfz diff --git a/src/sfizz/Wavetables.h b/src/sfizz/Wavetables.h index 8a314fdc..9f7a9bce 100644 --- a/src/sfizz/Wavetables.h +++ b/src/sfizz/Wavetables.h @@ -5,6 +5,7 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #pragma once +#include "Config.h" #include #include #include @@ -146,7 +147,7 @@ public: // the reference sample rate is the minimum value accepted by the DSP // system (most defavorable wrt. aliasing) static WavetableMulti createForHarmonicProfile( - const HarmonicProfile& hp, double amplitude, unsigned tableSize, double refSampleRate = 44100.0); + const HarmonicProfile& hp, double amplitude, unsigned tableSize = config::tableSize, double refSampleRate = 44100.0); // create the tiniest wavetable with null content for use with oscillators static WavetableMulti createSilence(); @@ -174,4 +175,22 @@ private: std::unique_ptr _multiData; }; +/** + * @brief Holds predefined wavetables. + * + */ +class WavetablePool { +public: + WavetablePool(); + const WavetableMulti* getWaveSin() const { return &waveSin; } + const WavetableMulti* getWaveTriangle() const { return &waveTriangle; } + const WavetableMulti* getWaveSaw() const { return &waveSaw; } + const WavetableMulti* getWaveSquare() const { return &waveSquare; }; +private: + const WavetableMulti waveSin; + const WavetableMulti waveTriangle; + const WavetableMulti waveSaw; + const WavetableMulti waveSquare; +}; + } // namespace sfz