Olivier Smeesters
/
DtmfKit
A DTMF sequence editor and player for HAM radio equipment command & control.
SoundWaveGenerator/SineWave.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/SineWave.h" |
osmeest | 0:1324e7d9d471 | 2 | #include <cmath> |
osmeest | 0:1324e7d9d471 | 3 | |
osmeest | 0:1324e7d9d471 | 4 | namespace snd_wave_generator { |
osmeest | 0:1324e7d9d471 | 5 | |
osmeest | 0:1324e7d9d471 | 6 | void SineWave::prepare(std::size_t sampleRate) |
osmeest | 0:1324e7d9d471 | 7 | { |
osmeest | 0:1324e7d9d471 | 8 | std::size_t cycleLength = sampleRate / this->frequency; |
osmeest | 0:1324e7d9d471 | 9 | this->buffer.resize(cycleLength); |
osmeest | 0:1324e7d9d471 | 10 | |
osmeest | 0:1324e7d9d471 | 11 | std::size_t middle = cycleLength / 2; |
osmeest | 0:1324e7d9d471 | 12 | std::size_t quarter = middle / 2; |
osmeest | 0:1324e7d9d471 | 13 | |
osmeest | 0:1324e7d9d471 | 14 | float delta = 6.28 / (float(sampleRate) / float(this->frequency)); |
osmeest | 0:1324e7d9d471 | 15 | for(std::size_t i = 0; i < quarter; ++i) { |
osmeest | 0:1324e7d9d471 | 16 | float dt = delta * i; |
osmeest | 0:1324e7d9d471 | 17 | float dv = std::sin(dt) / 2.0; |
osmeest | 0:1324e7d9d471 | 18 | float posValue = 0.5 + dv; |
osmeest | 0:1324e7d9d471 | 19 | float negValue = 0.5 - dv; |
osmeest | 0:1324e7d9d471 | 20 | this->buffer.write(i, posValue); |
osmeest | 0:1324e7d9d471 | 21 | this->buffer.write(middle - i, posValue); |
osmeest | 0:1324e7d9d471 | 22 | this->buffer.write(middle + i, negValue); |
osmeest | 0:1324e7d9d471 | 23 | this->buffer.write(cycleLength - i, negValue); |
osmeest | 0:1324e7d9d471 | 24 | } |
osmeest | 0:1324e7d9d471 | 25 | |
osmeest | 0:1324e7d9d471 | 26 | this->buffer.write(quarter, 1.0); |
osmeest | 0:1324e7d9d471 | 27 | if (middle - quarter != quarter) { |
osmeest | 0:1324e7d9d471 | 28 | this->buffer.write(middle - quarter, 1.0); |
osmeest | 0:1324e7d9d471 | 29 | } |
osmeest | 0:1324e7d9d471 | 30 | this->buffer.write(cycleLength - quarter, 0.0); |
osmeest | 0:1324e7d9d471 | 31 | if (middle + quarter != cycleLength - quarter) { |
osmeest | 0:1324e7d9d471 | 32 | this->buffer.write(middle + quarter, 0.0); |
osmeest | 0:1324e7d9d471 | 33 | } |
osmeest | 0:1324e7d9d471 | 34 | } |
osmeest | 0:1324e7d9d471 | 35 | |
osmeest | 0:1324e7d9d471 | 36 | } // snd_wave_generator |