Player: harmonic wave form

This commit is contained in:
Louis-Joseph Fournier 2016-01-14 16:30:58 +01:00
parent 5ccdc4f749
commit a3a7fd2506
2 changed files with 20 additions and 8 deletions

View file

@ -22,11 +22,8 @@
#include "FreqPlayer.hpp" #include "FreqPlayer.hpp"
template<typename sample_t> FreqPlayer<sample_t>::FreqPlayer(int _rate): template<typename sample_t> FreqPlayer<sample_t>::FreqPlayer(int _rate):
freq(440),
volume(0.5),
rate(_rate), rate(_rate),
n_frame(0), n_frame(0)
waveform(W_TRIANGLE)
{ {
k = K(); k = K();
k_update = -1; // invalid: don't update k_update = -1; // invalid: don't update
@ -58,6 +55,12 @@ template<typename sample_t> void FreqPlayer<sample_t>::SetVolume(double volume)
this->volume = volume; this->volume = volume;
} }
template<typename sample_t> void FreqPlayer<sample_t>::SetWaveform(WAVEFORM form, int nb_harmonics)
{
if (form == W_HARMONIC) this->nb_harmonics = nb_harmonics;
waveform = form;
}
template<> int16_t FreqPlayer<int16_t>::max() { return INT16_MAX; } template<> int16_t FreqPlayer<int16_t>::max() { return INT16_MAX; }
template<> double FreqPlayer<double>::max() { return 1; } template<> double FreqPlayer<double>::max() { return 1; }
@ -70,6 +73,7 @@ template<typename sample_t> sample_t FreqPlayer<sample_t>::AudioFrame()
{ {
double r = radius(); double r = radius();
double v; double v;
int i;
switch (waveform) { switch (waveform) {
case W_SINUS: case W_SINUS:
@ -86,6 +90,10 @@ template<typename sample_t> sample_t FreqPlayer<sample_t>::AudioFrame()
v /= (M_PI / 2); v /= (M_PI / 2);
break; break;
case W_HARMONIC:
v = 0;
for (i = 1; i <= nb_harmonics; i++) v += sin(r * i) / i;
break;
default: default:
v = 0; v = 0;

View file

@ -27,23 +27,25 @@
template<typename sample_t> class FreqPlayer { template<typename sample_t> class FreqPlayer {
public: public:
/// waveform style /// waveform style
enum WAVEFORM { W_SINUS, W_TRIANGLE }; enum WAVEFORM { W_SINUS, W_TRIANGLE, W_HARMONIC };
private: private:
/// current frequency /// current frequency
double freq; double freq = 440;
/// current volume (linear) /// current volume (linear)
double volume; double volume = 0.5;
/// audio rate /// audio rate
int rate; int rate;
/// current frame nb /// current frame nb
int n_frame; int n_frame;
/// wave form /// wave form
WAVEFORM waveform; WAVEFORM waveform = W_HARMONIC;
/// pre computed factor /// pre computed factor
double k, k_update; double k, k_update;
// last frame written // last frame written
sample_t last_frame; sample_t last_frame;
/// number of hamonics for waveforms (W_HARMONIC only)
int nb_harmonics = 5;
/// return k computed /// return k computed
double K() const; double K() const;
@ -66,6 +68,8 @@ template<typename sample_t> class FreqPlayer {
void SetFreq(double freq); void SetFreq(double freq);
/// set current volume /// set current volume
void SetVolume(double volume); void SetVolume(double volume);
/// set waveform
void SetWaveform(WAVEFORM form, int nb_harmonics);
}; };
#endif #endif