// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #pragma once // Port of WaveTables.kt: the wavetable synth's band-limited tables. #include #include #include #include namespace sizzle::wavetable { /** * A wavetable ready to play: [frames] single cycles, each kept as band-limited * copies, one per octave ("mip levels"). Level k holds at most `512 >> k` * harmonics in a cycle of size(k) samples; the top quarter of each level's * harmonics is faded out with a raised cosine. Immutable once built. */ class WaveTable { public: static constexpr int MAX_HARMONICS = 512; static constexpr int LEVELS = 10; /** max(64, 4 * (MAX_HARMONICS >> k)) */ static constexpr int SIZES[LEVELS] = {2048, 1024, 512, 256, 128, 64, 64, 64, 64, 64}; /** Harmonics level k carries. */ static constexpr int harmonics(int level) { return MAX_HARMONICS >> level; } WaveTable(const char *name, int frames); int size(int level) const { return SIZES[level]; } /** [frame * size(level) + i] */ const float *data(int level) const { return data_[level].data(); } float *data(int level) { return data_[level].data(); } const char *name() const { return name_; } const int frames; private: char name_[32]; std::vector data_[LEVELS]; }; /** * The tables: twelve factory tables made from formulas (no audio files ship), and * tables imported from WAV samples (Serum's 2048-sample frames, WaveEdit's 256, or * any power-of-two frame length). * * Everything is built off the audio thread. Factory tables: call prewarm() at * startup (builds on a background thread) or prewarmBlocking(). Lookups never * block or allocate; a table that isn't built yet is null (the oscillator is * silent). Built tables live for the rest of the process, as Kotlin's cache does. */ namespace WaveTables { constexpr int FRAMES = 16; constexpr int FACTORY_COUNT = 12; extern const char *const FACTORY_NAMES[FACTORY_COUNT]; constexpr const char *CUSTOM = "Custom"; /** Index of the factory table called [name], or -1. */ int factoryIndex(const char *name); /** The factory table [index] (FACTORY_NAMES order), or null while not built. Audio-safe. */ const WaveTable *factory(int index); const WaveTable *factory(const char *name); /** Build the factory set on a background thread (once). */ void prewarm(); /** Build every factory table not built yet, on the calling thread (not the audio thread). */ void prewarmBlocking(); // ---------------------------------------------------------------- custom --- // // Hook for the sample store (not ported yet). Kotlin's forSample queues a build // on its worker that reads SampleStore.get(id).data. Here the audio thread only // records the request; whoever owns the samples supplies them through a // SampleSource and calls buildRequested() (or buildForSample) off the audio thread. /** Fills [out] with the mono float samples of sample [id]; false if unknown. */ using SampleSource = bool (*)(const char *id, std::vector &out); void setSampleSource(SampleSource source); /** The table of custom sample [id] cut in [frameLen]-sample frames (0: detect), or * null while it isn't built (the request is then recorded once). Audio-safe: * no allocation, no locks. */ const WaveTable *forSample(const char *id, int frameLen); /** Build a custom table now from the SampleSource (caller off the audio thread) and cache it. */ const WaveTable *buildForSample(const char *id, int frameLen); /** Build every table forSample has asked for (caller off the audio thread). */ void buildRequested(); // ---------------------------------------------------------------- import --- /** The frame length of a wavetable WAV: Serum's `clm ` chunk ("2048 …") when * present, else WaveEdit's 64 x 256 layout, else 2048 or 256 if the length divides, * else the whole file as one cycle (0). [frames]: the WAV's sample frames. */ int detectFrameLength(const uint8_t *wavBytes, size_t size, int frames); int guessFrameLength(int frames); /** A table from a sample cut into [frameLen0]-sample cycles (0: detect; at most 256 * frames). Null for an empty sample (Kotlin would throw). Not audio-safe. */ std::unique_ptr fromSamples(const char *name, const float *x, int n, int frameLen0); } // namespace WaveTables } // namespace sizzle::wavetable