Classes to produce a computed sound wave on the analog output.

Dependents:   SoundWaveGeneratorTest SoundWaveGeneratorTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WaveCombo.cpp Source File

WaveCombo.cpp

00001 #include "snd_wave_generator/WaveCombo.h"
00002 
00003 #include <algorithm>
00004 #include <functional>
00005 #include <numeric>
00006 
00007 namespace snd_wave_generator {
00008 
00009 WaveCombo & WaveCombo::add(Wave *wave) { 
00010     if (wave) { 
00011         this->waves.push_back(wave);
00012         this->numWaves = this->waves.size(); 
00013     }
00014     
00015     return *this;
00016 }
00017 
00018 void WaveCombo::prepare(std::size_t sampleRate)
00019 {
00020     std::for_each(this->waves.begin(), this->waves.end(),
00021         std::bind2nd(std::mem_fun(&Wave::prepare), sampleRate));
00022 }
00023 
00024 struct AccumulateWaveValueAt {
00025     AccumulateWaveValueAt(std::size_t pos) : pos(pos) { }
00026     float operator()(float acc, const Wave *wave) const {
00027         return acc + wave->read(this->pos);
00028     }
00029     
00030     std::size_t pos;
00031 };
00032 
00033 float WaveCombo::read(std::size_t pos) const {
00034     float v = std::accumulate(this->waves.begin(), this->waves.end(), 0.0f,
00035         AccumulateWaveValueAt(pos));
00036                 
00037     return v / this->numWaves;
00038 }
00039 
00040 } // snd_wave_generator