sinesynth/include/SineSynth.h

148 lines
3.5 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 <random>
2020-01-06 14:38:25 -08:00
#include <string>
#include <vector>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/mpfr.hpp>
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;
2020-01-06 14:38:25 -08:00
using std::for_each;
2020-01-06 14:04:34 -08:00
using namespace boost::multiprecision;
typedef cpp_bin_float_double MP_TYPE;
const MP_TYPE two_pi = boost::math::constants::two_pi<MP_TYPE>();
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 float getAmplitudeDb() const;
virtual void setAmplitudeDb(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
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:
};
template <typename dataType> class SineGenerator : public Generator<dataType> {
2020-01-06 14:04:34 -08:00
public:
SineGenerator(uint32_t _sampleRate, float _frequency, float _amplitude = 1,
bool caching = true);
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;
void setFrequency(MP_TYPE frequency);
void setAmplitude(float amplitude);
2020-01-06 14:38:25 -08:00
2020-01-06 14:04:34 -08:00
virtual ~SineGenerator();
private:
MP_TYPE instPhase;
MP_TYPE phaseIncr;
2020-01-06 20:53:38 -08:00
void dirtyCache();
dataType unCachedGetSample();
dataType cachedGetSample();
2020-01-06 20:53:38 -08:00
vector<dataType> sampleCache;
typename vector<dataType>::const_iterator cachePos;
const bool caching;
bool cacheInvalid;
2020-01-06 14:04:34 -08:00
};
template <typename dataType> class NoiseGenerator : public Generator<dataType> {
2020-01-06 14:38:25 -08:00
public:
enum NOISE_TYPE {
2020-01-06 20:53:38 -08:00
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> {
2020-01-06 20:53:38 -08:00
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
};
template <typename dataType> class SweepGenerator : public Generator<dataType> {
2020-01-06 14:38:25 -08:00
public:
SweepGenerator(uint32_t sampleRate, float startFreq, float endFreq,
float length, float amplitude = 1);
2020-01-06 20:53:38 -08:00
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);
void setFrequency(float frequency);
2020-01-06 14:38:25 -08:00
private:
2020-01-06 20:53:38 -08:00
SineGenerator<dataType> sg;
float length;
MP_TYPE startFreq;
MP_TYPE curFreq;
MP_TYPE freqStep;
2020-01-06 14:38:25 -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();
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
};