Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
jmpin
Date:
Thu Apr 28 22:16:40 2016 +0000
Revision:
8:f6699fd30737
Child:
21:0df25c61c475
Waveform generating functions added as well as output function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmpin 8:f6699fd30737 1 class Speaker
jmpin 8:f6699fd30737 2 {
jmpin 8:f6699fd30737 3 public:
jmpin 8:f6699fd30737 4 Speaker(PinName pin) : _pin(pin) {
jmpin 8:f6699fd30737 5 // _pin(pin) means pass pin to the Speaker Constructor
jmpin 8:f6699fd30737 6 // precompute 32 sample points on one sine wave cycle
jmpin 8:f6699fd30737 7 // used for continuous sine wave output later
jmpin 8:f6699fd30737 8 for(int k=0; k<32; k++) {
jmpin 8:f6699fd30737 9 Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
jmpin 8:f6699fd30737 10 // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
jmpin 8:f6699fd30737 11 }
jmpin 8:f6699fd30737 12
jmpin 8:f6699fd30737 13 }
jmpin 8:f6699fd30737 14 // class method to play a note based on AnalogOut class
jmpin 8:f6699fd30737 15 void PlayNote(float frequency, float duration, float volume) {
jmpin 8:f6699fd30737 16 // scale samples using current volume level arg
jmpin 8:f6699fd30737 17 for(int k=0; k<32; k++) {
jmpin 8:f6699fd30737 18 Analog_scaled_data[k] = Analog_out_data[k] * volume;
jmpin 8:f6699fd30737 19 }
jmpin 8:f6699fd30737 20 // reset to start of sample array
jmpin 8:f6699fd30737 21 i=0;
jmpin 8:f6699fd30737 22 // turn on timer interrupts to start sine wave output
jmpin 8:f6699fd30737 23 Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
jmpin 8:f6699fd30737 24 // play note for specified time
jmpin 8:f6699fd30737 25 wait(duration);
jmpin 8:f6699fd30737 26 // turns off timer interrupts
jmpin 8:f6699fd30737 27 Sample_Period.detach();
jmpin 8:f6699fd30737 28 // sets output to mid range - analog zero
jmpin 8:f6699fd30737 29 this->_pin.write_u16(32768);
jmpin 8:f6699fd30737 30
jmpin 8:f6699fd30737 31 }
jmpin 8:f6699fd30737 32 private:
jmpin 8:f6699fd30737 33 // sets up specified pin for analog using AnalogOut class
jmpin 8:f6699fd30737 34 AnalogOut _pin;
jmpin 8:f6699fd30737 35 // set up a timer to be used for sample rate interrupts
jmpin 8:f6699fd30737 36 Ticker Sample_Period;
jmpin 8:f6699fd30737 37
jmpin 8:f6699fd30737 38 //variables used by interrupt routine and PlayNote
jmpin 8:f6699fd30737 39 volatile int i;
jmpin 8:f6699fd30737 40 short unsigned Analog_out_data[32];
jmpin 8:f6699fd30737 41 short unsigned Analog_scaled_data[32];
jmpin 8:f6699fd30737 42
jmpin 8:f6699fd30737 43 // Interrupt routine
jmpin 8:f6699fd30737 44 // used to output next analog sample whenever a timer interrupt occurs
jmpin 8:f6699fd30737 45 void Sample_timer_interrupt(void) {
jmpin 8:f6699fd30737 46 // send next analog sample out to D to A
jmpin 8:f6699fd30737 47 this->_pin.write_u16(Analog_scaled_data[i]);
jmpin 8:f6699fd30737 48 // increment pointer and wrap around back to 0 at 32
jmpin 8:f6699fd30737 49 i = (i+1) & 0x01F;
jmpin 8:f6699fd30737 50 }
jmpin 8:f6699fd30737 51 };
jmpin 8:f6699fd30737 52