Player: harmonic wave form
This commit is contained in:
parent
5ccdc4f749
commit
a3a7fd2506
2 changed files with 20 additions and 8 deletions
|
@ -22,11 +22,8 @@
|
|||
#include "FreqPlayer.hpp"
|
||||
|
||||
template<typename sample_t> FreqPlayer<sample_t>::FreqPlayer(int _rate):
|
||||
freq(440),
|
||||
volume(0.5),
|
||||
rate(_rate),
|
||||
n_frame(0),
|
||||
waveform(W_TRIANGLE)
|
||||
n_frame(0)
|
||||
{
|
||||
k = K();
|
||||
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;
|
||||
}
|
||||
|
||||
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<> double FreqPlayer<double>::max() { return 1; }
|
||||
|
||||
|
@ -70,6 +73,7 @@ template<typename sample_t> sample_t FreqPlayer<sample_t>::AudioFrame()
|
|||
{
|
||||
double r = radius();
|
||||
double v;
|
||||
int i;
|
||||
|
||||
switch (waveform) {
|
||||
case W_SINUS:
|
||||
|
@ -86,6 +90,10 @@ template<typename sample_t> sample_t FreqPlayer<sample_t>::AudioFrame()
|
|||
v /= (M_PI / 2);
|
||||
break;
|
||||
|
||||
case W_HARMONIC:
|
||||
v = 0;
|
||||
for (i = 1; i <= nb_harmonics; i++) v += sin(r * i) / i;
|
||||
break;
|
||||
|
||||
default:
|
||||
v = 0;
|
||||
|
|
|
@ -27,23 +27,25 @@
|
|||
template<typename sample_t> class FreqPlayer {
|
||||
public:
|
||||
/// waveform style
|
||||
enum WAVEFORM { W_SINUS, W_TRIANGLE };
|
||||
enum WAVEFORM { W_SINUS, W_TRIANGLE, W_HARMONIC };
|
||||
|
||||
private:
|
||||
/// current frequency
|
||||
double freq;
|
||||
double freq = 440;
|
||||
/// current volume (linear)
|
||||
double volume;
|
||||
double volume = 0.5;
|
||||
/// audio rate
|
||||
int rate;
|
||||
/// current frame nb
|
||||
int n_frame;
|
||||
/// wave form
|
||||
WAVEFORM waveform;
|
||||
WAVEFORM waveform = W_HARMONIC;
|
||||
/// pre computed factor
|
||||
double k, k_update;
|
||||
// last frame written
|
||||
sample_t last_frame;
|
||||
/// number of hamonics for waveforms (W_HARMONIC only)
|
||||
int nb_harmonics = 5;
|
||||
|
||||
/// return k computed
|
||||
double K() const;
|
||||
|
@ -66,6 +68,8 @@ template<typename sample_t> class FreqPlayer {
|
|||
void SetFreq(double freq);
|
||||
/// set current volume
|
||||
void SetVolume(double volume);
|
||||
/// set waveform
|
||||
void SetWaveform(WAVEFORM form, int nb_harmonics);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue