Classes to produce a computed sound wave on the analog output.
Dependents: SoundWaveGeneratorTest SoundWaveGeneratorTest
WaveCombo.cpp@0:ed89ef772e92, 2011-01-24 (annotated)
- Committer:
- osmeest
- Date:
- Mon Jan 24 22:44:39 2011 +0000
- Revision:
- 0:ed89ef772e92
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 0:ed89ef772e92 | 1 | #include "snd_wave_generator/WaveCombo.h" |
osmeest | 0:ed89ef772e92 | 2 | |
osmeest | 0:ed89ef772e92 | 3 | #include <algorithm> |
osmeest | 0:ed89ef772e92 | 4 | #include <functional> |
osmeest | 0:ed89ef772e92 | 5 | #include <numeric> |
osmeest | 0:ed89ef772e92 | 6 | |
osmeest | 0:ed89ef772e92 | 7 | namespace snd_wave_generator { |
osmeest | 0:ed89ef772e92 | 8 | |
osmeest | 0:ed89ef772e92 | 9 | WaveCombo & WaveCombo::add(Wave *wave) { |
osmeest | 0:ed89ef772e92 | 10 | if (wave) { |
osmeest | 0:ed89ef772e92 | 11 | this->waves.push_back(wave); |
osmeest | 0:ed89ef772e92 | 12 | this->numWaves = this->waves.size(); |
osmeest | 0:ed89ef772e92 | 13 | } |
osmeest | 0:ed89ef772e92 | 14 | |
osmeest | 0:ed89ef772e92 | 15 | return *this; |
osmeest | 0:ed89ef772e92 | 16 | } |
osmeest | 0:ed89ef772e92 | 17 | |
osmeest | 0:ed89ef772e92 | 18 | void WaveCombo::prepare(std::size_t sampleRate) |
osmeest | 0:ed89ef772e92 | 19 | { |
osmeest | 0:ed89ef772e92 | 20 | std::for_each(this->waves.begin(), this->waves.end(), |
osmeest | 0:ed89ef772e92 | 21 | std::bind2nd(std::mem_fun(&Wave::prepare), sampleRate)); |
osmeest | 0:ed89ef772e92 | 22 | } |
osmeest | 0:ed89ef772e92 | 23 | |
osmeest | 0:ed89ef772e92 | 24 | struct AccumulateWaveValueAt { |
osmeest | 0:ed89ef772e92 | 25 | AccumulateWaveValueAt(std::size_t pos) : pos(pos) { } |
osmeest | 0:ed89ef772e92 | 26 | float operator()(float acc, const Wave *wave) const { |
osmeest | 0:ed89ef772e92 | 27 | return acc + wave->read(this->pos); |
osmeest | 0:ed89ef772e92 | 28 | } |
osmeest | 0:ed89ef772e92 | 29 | |
osmeest | 0:ed89ef772e92 | 30 | std::size_t pos; |
osmeest | 0:ed89ef772e92 | 31 | }; |
osmeest | 0:ed89ef772e92 | 32 | |
osmeest | 0:ed89ef772e92 | 33 | float WaveCombo::read(std::size_t pos) const { |
osmeest | 0:ed89ef772e92 | 34 | float v = std::accumulate(this->waves.begin(), this->waves.end(), 0.0f, |
osmeest | 0:ed89ef772e92 | 35 | AccumulateWaveValueAt(pos)); |
osmeest | 0:ed89ef772e92 | 36 | |
osmeest | 0:ed89ef772e92 | 37 | return v / this->numWaves; |
osmeest | 0:ed89ef772e92 | 38 | } |
osmeest | 0:ed89ef772e92 | 39 | |
osmeest | 0:ed89ef772e92 | 40 | } // snd_wave_generator |