Olivier Smeesters
/
DtmfKit
A DTMF sequence editor and player for HAM radio equipment command & control.
SoundWaveGenerator/TriangleWave.cpp@0:1324e7d9d471, 2011-03-07 (annotated)
- Committer:
- osmeest
- Date:
- Mon Mar 07 22:51:19 2011 +0000
- Revision:
- 0:1324e7d9d471
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 0:1324e7d9d471 | 1 | #include "snd_wave_generator/TriangleWave.h" |
osmeest | 0:1324e7d9d471 | 2 | |
osmeest | 0:1324e7d9d471 | 3 | namespace snd_wave_generator { |
osmeest | 0:1324e7d9d471 | 4 | |
osmeest | 0:1324e7d9d471 | 5 | void TriangleWave::prepare(std::size_t sampleRate) |
osmeest | 0:1324e7d9d471 | 6 | { |
osmeest | 0:1324e7d9d471 | 7 | std::size_t cycleLength = sampleRate / this->frequency; |
osmeest | 0:1324e7d9d471 | 8 | this->buffer.resize(cycleLength); |
osmeest | 0:1324e7d9d471 | 9 | |
osmeest | 0:1324e7d9d471 | 10 | std::size_t middle = cycleLength / 2; |
osmeest | 0:1324e7d9d471 | 11 | std::size_t quarter = middle / 2; |
osmeest | 0:1324e7d9d471 | 12 | |
osmeest | 0:1324e7d9d471 | 13 | float delta = 2.0 / (float(sampleRate) / float(this->frequency)); |
osmeest | 0:1324e7d9d471 | 14 | for(std::size_t i = 0; i < quarter; ++i) { |
osmeest | 0:1324e7d9d471 | 15 | float dt = delta * i; |
osmeest | 0:1324e7d9d471 | 16 | float posValue = 0.5 + dt; |
osmeest | 0:1324e7d9d471 | 17 | float negValue = 0.5 - dt; |
osmeest | 0:1324e7d9d471 | 18 | this->buffer.write(i, posValue); |
osmeest | 0:1324e7d9d471 | 19 | this->buffer.write(middle - i, posValue); |
osmeest | 0:1324e7d9d471 | 20 | this->buffer.write(middle + i, negValue); |
osmeest | 0:1324e7d9d471 | 21 | this->buffer.write(cycleLength - i, negValue); |
osmeest | 0:1324e7d9d471 | 22 | } |
osmeest | 0:1324e7d9d471 | 23 | |
osmeest | 0:1324e7d9d471 | 24 | this->buffer.write(quarter, 1.0); |
osmeest | 0:1324e7d9d471 | 25 | if (middle - quarter != quarter) { |
osmeest | 0:1324e7d9d471 | 26 | this->buffer.write(middle - quarter, 1.0); |
osmeest | 0:1324e7d9d471 | 27 | } |
osmeest | 0:1324e7d9d471 | 28 | this->buffer.write(cycleLength - quarter, 0.0); |
osmeest | 0:1324e7d9d471 | 29 | if (middle + quarter != cycleLength - quarter) { |
osmeest | 0:1324e7d9d471 | 30 | this->buffer.write(middle + quarter, 0.0); |
osmeest | 0:1324e7d9d471 | 31 | } |
osmeest | 0:1324e7d9d471 | 32 | } |
osmeest | 0:1324e7d9d471 | 33 | |
osmeest | 0:1324e7d9d471 | 34 | } // snd_wave_generator |