John Pinion / Mbed 2 deprecated BlueSynth

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed

Fork of 4180_Final_Design_Project by John Pinion

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Speaker.h Source File

Speaker.h

00001 class Speaker
00002 {
00003 public:
00004     Speaker(PinName pin) : _pin(pin) {
00005 // _pin(pin) means pass pin to the Speaker Constructor
00006 // precompute 32 sample points on one sine wave cycle
00007 // used for continuous sine wave output later
00008 
00009     }
00010 // class method to play a note based on AnalogOut class
00011     void PlayNote(float frequency, float duration, float coefficientMatrix[32], short unsigned Analog_out_data[32]) {
00012         // scale samples using current volume level arg
00013         for(int k=0; k<32; k++) {
00014             Analog_scaled_data[k] = Analog_out_data[k] * coefficientMatrix[k];
00015         }
00016         // reset to start of sample array
00017         i=0;
00018         // turn on timer interrupts to start sine wave output
00019         Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
00020         // play note for specified time
00021         wait(duration);
00022         // turns off timer interrupts
00023         Sample_Period.detach();
00024         // sets output to mid range - analog zero
00025         this->_pin.write_u16(32768);
00026 
00027     }
00028 private:
00029 // sets up specified pin for analog using AnalogOut class
00030     AnalogOut _pin;
00031     // set up a timer to be used for sample rate interrupts
00032     Ticker Sample_Period;
00033 
00034     //variables used by interrupt routine and PlayNote
00035     volatile int i;
00036     short unsigned Analog_scaled_data[32];
00037 
00038 // Interrupt routine
00039 // used to output next analog sample whenever a timer interrupt occurs
00040     void Sample_timer_interrupt(void) {
00041         // send next analog sample out to D to A
00042         this->_pin.write_u16(Analog_scaled_data[i]);
00043         // increment pointer and wrap around back to 0 at 32
00044         i = (i+1) & 0x01F;
00045     }
00046 };
00047