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.h Source File

SoundWaveGenerator.h

00001 #ifndef SOUND_WAVE_GENERATOR_H_
00002 #define SOUND_WAVE_GENERATOR_H_
00003 
00004 #include "snd_wave_generator/Wave.h"
00005 #include "mbed.h"
00006 
00007 namespace snd_wave_generator {
00008 
00009 /**
00010  * @brief A class to handle computed sound wave generation.
00011  * The generator is bound to an AnalogOut pin (p18 on LPC1768).
00012  * It has a fixed sample rate.
00013  */
00014 class SoundWaveGenerator {
00015 public:
00016     SoundWaveGenerator(std::size_t sampleRate, PinName pin = p18) : 
00017         sampleRate(sampleRate), wave(NULL), pos(0), output(pin), ticker()
00018     { }
00019     
00020     /**
00021      * @brief Starts playing a given wave.
00022      * The wave will be prepared for the generator's sample rate and then played back
00023      * on the bound analog output.
00024      */
00025     void play(Wave *wave);
00026     
00027     /**
00028      * @brief Stops playing.
00029      */
00030     void stop();
00031     
00032 private:
00033     void tickerHandler();
00034     
00035     std::size_t sampleRate;
00036     
00037     Wave *wave;    
00038     std::size_t pos;
00039     AnalogOut output;
00040     Ticker ticker;
00041 };
00042   
00043 } // snd_wave_generator
00044   
00045 #endif // SOUND_WAVE_GENERATOR_H_