sinesynth/include/SineSynth.h

142 lines
2.9 KiB
C
Raw Normal View History

2020-01-06 14:04:34 -08:00
#pragma once
2020-01-06 14:38:25 -08:00
#include <algorithm>
2020-01-06 14:04:34 -08:00
#include <cstdint>
#include <iterator>
2020-01-06 14:38:25 -08:00
#include <memory>
#include <string>
#include <vector>
2020-01-06 20:53:38 -08:00
#include <random>
#include <mpfr.h>
2020-01-06 14:04:34 -08:00
2020-01-06 14:38:25 -08:00
using std::shared_ptr;
2020-01-06 14:04:34 -08:00
using std::vector;
using std::cos;
2020-01-06 14:38:25 -08:00
using std::for_each;
using std::sin;
2020-01-06 14:04:34 -08:00
2020-01-06 20:53:38 -08:00
template <typename dataType>
class Generator
{
2020-01-06 14:04:34 -08:00
public:
2020-01-06 20:53:38 -08:00
virtual dataType getSample() = 0;
virtual void getSamples(size_t nSamples, dataType *buf);
2020-01-06 14:38:25 -08:00
virtual float getFrequency() const;
2020-01-06 20:53:38 -08:00
virtual void setFrequency(float frequency);
virtual float getAmplitude() const;
virtual void setAmplitude(float amplitude);
2020-01-06 14:38:25 -08:00
virtual bool isEnabled() const;
virtual void enable();
virtual void disable();
2020-01-06 14:04:34 -08:00
virtual ~Generator();
// disallow copy of virtual interface
2020-01-06 20:53:38 -08:00
Generator(Generator const &) = delete;
Generator &operator=(Generator const &) = delete;
2020-01-06 14:04:34 -08:00
protected:
2020-01-06 20:53:38 -08:00
Generator(uint32_t _sampleRate, float _frequency, float _amplitude = 1);
2020-01-06 14:04:34 -08:00
uint32_t sampleRate;
float frequency;
float amplitude;
2020-01-06 14:38:25 -08:00
bool enabled;
2020-01-06 14:04:34 -08:00
private:
};
2020-01-06 20:53:38 -08:00
template <typename dataType>
class SineGenerator : public Generator<dataType>
{
2020-01-06 14:04:34 -08:00
public:
2020-01-06 20:53:38 -08:00
SineGenerator(uint32_t _sampleRate, float _frequency, float _amplitude = 1);
2020-01-06 14:38:25 -08:00
2020-01-06 20:53:38 -08:00
dataType getSample() final;
2020-01-06 14:38:25 -08:00
void setFrequency(float _frequency) final;
2020-01-06 20:53:38 -08:00
void setFrequency(mpfr_t frequency);
2020-01-06 14:38:25 -08:00
2020-01-06 14:04:34 -08:00
virtual ~SineGenerator();
private:
2020-01-06 20:53:38 -08:00
mpfr_t instPhase;
mpfr_t phaseIncr;
// Constant 2*pi
mpfr_t PI_2;
mpfr_t tmpA;
2020-01-06 14:04:34 -08:00
};
2020-01-06 20:53:38 -08:00
template <typename dataType>
class NoiseGenerator : public Generator<dataType>
{
2020-01-06 14:38:25 -08:00
public:
2020-01-06 20:53:38 -08:00
enum NOISE_TYPE
{
WHITE,
};
2020-01-06 14:38:25 -08:00
2020-01-06 20:53:38 -08:00
NoiseGenerator(uint32_t _sampleRate, NOISE_TYPE type, float _amplitude = 1);
dataType getSample() final;
2020-01-06 14:38:25 -08:00
virtual ~NoiseGenerator();
2020-01-06 20:53:38 -08:00
private:
std::mt19937_64 rand_gen;
};
template <typename dataType>
class JTestGenerator : public Generator<dataType>
{
public:
JTestGenerator(uint32_t sampleRate, uint8_t nbits);
dataType getSample() final;
virtual ~JTestGenerator();
2020-01-06 14:38:25 -08:00
2020-01-06 14:04:34 -08:00
private:
2020-01-06 20:53:38 -08:00
std::vector<dataType> loop;
typename std::vector<dataType>::const_iterator pos;
2020-01-06 14:38:25 -08:00
};
2020-01-06 20:53:38 -08:00
template <typename dataType>
class SweepGenerator : public Generator<dataType>
{
2020-01-06 14:38:25 -08:00
public:
2020-01-06 20:53:38 -08:00
SweepGenerator(uint32_t sampleRate, float startFreq, float endFreq, float length, float amplitude = 1);
virtual ~SweepGenerator();
2020-01-06 14:38:25 -08:00
2020-01-06 20:53:38 -08:00
dataType getSample() final;
float getFrequency() const;
2020-01-06 14:38:25 -08:00
2020-01-06 20:53:38 -08:00
void setAmplitude(float amplitude);
2020-01-06 14:38:25 -08:00
private:
2020-01-06 20:53:38 -08:00
SineGenerator<dataType> sg;
mpfr_t startFreq;
mpfr_t curFreq;
mpfr_t freqStep;
mpfr_t endFreq;
2020-01-06 14:38:25 -08:00
};
2020-01-06 20:53:38 -08:00
template <typename dataType>
class SineSynth
{
2020-01-06 14:38:25 -08:00
public:
SineSynth(uint32_t _sampleRate);
2020-01-06 20:53:38 -08:00
void addSynth(shared_ptr<Generator<dataType>>);
vector<shared_ptr<Generator<dataType>>> getSynths() const;
2020-01-06 14:38:25 -08:00
void clearSynths();
2020-01-06 20:53:38 -08:00
void getSamples(size_t nSamples, dataType *buf);
2020-01-06 14:38:25 -08:00
private:
uint32_t sampleRate;
2020-01-06 20:53:38 -08:00
vector<shared_ptr<Generator<dataType>>> generators;
2020-01-06 14:04:34 -08:00
};