Josh Davy / Mbed OS MUSIC_K64F
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Music.cpp Source File

Music.cpp

00001 
00002 #include "Music.h"
00003 AnalogOut speaker(DAC0_OUT);
00004 /**
00005 * @brief Constructor (no paramateters)
00006 */
00007 Music::Music()
00008 {
00009 
00010 }
00011 /**
00012 * @brief Deconstructor
00013 */
00014 Music::~Music()
00015 {
00016     
00017     
00018 
00019 }
00020 
00021 /**
00022 * @brief Initialiser. Takes the sound data and initialises the buzzer
00023 * @param const int* data @details The audio data. A array of the amplitude of 
00024 * the waveform over time
00025 * @param length @details Length of above sound array 
00026 */
00027 void Music::init(const int* data,int length)
00028 {
00029     
00030     _data = data;
00031     _length = length;
00032  // Has to << then 1/sample rate
00033  // until music played stay silent
00034     _index = 0;
00035 
00036 }
00037 /**
00038 * @brief Plays the next sound sample. Must be called at the sample rate
00039 */
00040 void Music::play_next()
00041 {
00042     double duty_cycle;
00043     duty_cycle = _data[_index];
00044     //printf("%i = %f\n",_index,duty_cycle);
00045 
00046     if (_index < _length) {      
00047         speaker = duty_cycle/255;
00048         //printf("%f\n",duty_cycle/255); 
00049         
00050         _index++;
00051     } else {
00052         _index = 0;    
00053     }
00054         
00055 }
00056 
00057