45 lines
1.4 KiB
C++
45 lines
1.4 KiB
C++
#ifndef SYNTH_ENGINE_H
|
|
#define SYNTH_ENGINE_H
|
|
|
|
#include <stdint.h>
|
|
|
|
/**
|
|
* @class SynthEngine
|
|
* @brief A portable, platform-agnostic synthesizer engine.
|
|
*
|
|
* This class contains the core digital signal processing (DSP) logic.
|
|
* It has no dependencies on any specific hardware, OS, or audio API.
|
|
* It works by filling a provided buffer with 16-bit signed audio samples.
|
|
*
|
|
* The oscillator uses a 32-bit unsigned integer as a phase accumulator,
|
|
* which is highly efficient and avoids floating-point math in the audio loop,
|
|
* making it ideal for the RP2040.
|
|
*/
|
|
class SynthEngine {
|
|
public:
|
|
/**
|
|
* @brief Constructs the synthesizer engine.
|
|
* @param sampleRate The audio sample rate in Hz (e.g., 44100).
|
|
*/
|
|
SynthEngine(uint32_t sampleRate);
|
|
|
|
/**
|
|
* @brief Fills a buffer with audio samples. This is the main audio callback.
|
|
* @param buffer Pointer to the output buffer to be filled.
|
|
* @param numFrames The number of audio frames (samples) to generate.
|
|
*/
|
|
void process(int16_t* buffer, uint32_t numFrames);
|
|
|
|
/**
|
|
* @brief Sets the frequency of the oscillator.
|
|
* @param freq The frequency in Hz.
|
|
*/
|
|
void setFrequency(float freq);
|
|
|
|
private:
|
|
uint32_t _sampleRate;
|
|
uint32_t _phase; // Phase accumulator for the oscillator.
|
|
uint32_t _increment; // Phase increment per sample, determines frequency.
|
|
};
|
|
|
|
#endif // SYNTH_ENGINE_H
|