142 lines
2.9 KiB
C++
Executable File
142 lines
2.9 KiB
C++
Executable File
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <iterator>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <random>
|
|
#include <mpfr.h>
|
|
|
|
using std::shared_ptr;
|
|
using std::vector;
|
|
|
|
using std::cos;
|
|
using std::for_each;
|
|
using std::sin;
|
|
|
|
template <typename dataType>
|
|
class Generator
|
|
{
|
|
public:
|
|
virtual dataType getSample() = 0;
|
|
virtual void getSamples(size_t nSamples, dataType *buf);
|
|
|
|
virtual float getFrequency() const;
|
|
virtual void setFrequency(float frequency);
|
|
|
|
virtual float getAmplitude() const;
|
|
virtual void setAmplitude(float amplitude);
|
|
|
|
virtual bool isEnabled() const;
|
|
virtual void enable();
|
|
virtual void disable();
|
|
|
|
virtual ~Generator();
|
|
|
|
// disallow copy of virtual interface
|
|
Generator(Generator const &) = delete;
|
|
Generator &operator=(Generator const &) = delete;
|
|
|
|
protected:
|
|
Generator(uint32_t _sampleRate, float _frequency, float _amplitude = 1);
|
|
uint32_t sampleRate;
|
|
float frequency;
|
|
float amplitude;
|
|
bool enabled;
|
|
|
|
private:
|
|
};
|
|
|
|
template <typename dataType>
|
|
class SineGenerator : public Generator<dataType>
|
|
{
|
|
public:
|
|
SineGenerator(uint32_t _sampleRate, float _frequency, float _amplitude = 1);
|
|
|
|
dataType getSample() final;
|
|
|
|
void setFrequency(float _frequency) final;
|
|
void setFrequency(mpfr_t frequency);
|
|
|
|
virtual ~SineGenerator();
|
|
|
|
private:
|
|
mpfr_t instPhase;
|
|
mpfr_t phaseIncr;
|
|
|
|
// Constant 2*pi
|
|
mpfr_t PI_2;
|
|
|
|
mpfr_t tmpA;
|
|
};
|
|
|
|
template <typename dataType>
|
|
class NoiseGenerator : public Generator<dataType>
|
|
{
|
|
public:
|
|
enum NOISE_TYPE
|
|
{
|
|
WHITE,
|
|
};
|
|
|
|
NoiseGenerator(uint32_t _sampleRate, NOISE_TYPE type, float _amplitude = 1);
|
|
|
|
dataType getSample() final;
|
|
virtual ~NoiseGenerator();
|
|
|
|
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();
|
|
|
|
private:
|
|
std::vector<dataType> loop;
|
|
typename std::vector<dataType>::const_iterator pos;
|
|
};
|
|
|
|
template <typename dataType>
|
|
class SweepGenerator : public Generator<dataType>
|
|
{
|
|
public:
|
|
SweepGenerator(uint32_t sampleRate, float startFreq, float endFreq, float length, float amplitude = 1);
|
|
virtual ~SweepGenerator();
|
|
|
|
dataType getSample() final;
|
|
float getFrequency() const;
|
|
|
|
void setAmplitude(float amplitude);
|
|
|
|
private:
|
|
SineGenerator<dataType> sg;
|
|
mpfr_t startFreq;
|
|
mpfr_t curFreq;
|
|
mpfr_t freqStep;
|
|
mpfr_t endFreq;
|
|
};
|
|
|
|
template <typename dataType>
|
|
class SineSynth
|
|
{
|
|
public:
|
|
SineSynth(uint32_t _sampleRate);
|
|
|
|
void addSynth(shared_ptr<Generator<dataType>>);
|
|
vector<shared_ptr<Generator<dataType>>> getSynths() const;
|
|
void clearSynths();
|
|
|
|
void getSamples(size_t nSamples, dataType *buf);
|
|
|
|
private:
|
|
uint32_t sampleRate;
|
|
vector<shared_ptr<Generator<dataType>>> generators;
|
|
}; |