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 SoundWaveGenerator.cpp Source File

SoundWaveGenerator.cpp

00001 #include "snd_wave_generator/SoundWaveGenerator.h"
00002 
00003 namespace snd_wave_generator {
00004 
00005 void SoundWaveGenerator::play(Wave *wave)
00006 {
00007     wave->prepare(this->sampleRate);
00008 
00009     this->wave = wave;
00010     
00011     this->pos = 0;
00012     this->ticker.attach(this, &SoundWaveGenerator::tickerHandler, 1.0 / this->sampleRate);
00013 }
00014 
00015 void SoundWaveGenerator::stop()
00016 {
00017     this->ticker.detach();
00018     this->output.write(0.0);
00019     this->wave = NULL;
00020 }
00021     
00022 void SoundWaveGenerator::tickerHandler()
00023 {
00024     float v = this->wave ? this->wave->read(this->pos++) : 0.0;
00025     this->output.write(v);
00026 }
00027 
00028 } // snd_wave_generator