A synthesizer that is controlled by a USB MIDI Keyboard. Also displays corresponding frequencies via a simple spectrum analyzer on a LCD. Uses a watchdog timer to reset Mbed in case it freezes.
Dependencies: 4DGL-uLCD-SE USBHostMIDI mbed
Fork of USBHostMIDI_example by
MIDISpeaker.h@2:5366ce17fe55, 2017-12-13 (annotated)
- Committer:
- kjones99
- Date:
- Wed Dec 13 03:31:09 2017 +0000
- Revision:
- 2:5366ce17fe55
Final version as of right now.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kjones99 | 2:5366ce17fe55 | 1 | #include "mbed.h" |
kjones99 | 2:5366ce17fe55 | 2 | #include "uLCD_4DGL.h" |
kjones99 | 2:5366ce17fe55 | 3 | |
kjones99 | 2:5366ce17fe55 | 4 | #define BAUD_9600 312 |
kjones99 | 2:5366ce17fe55 | 5 | // new class to play a note on Speaker based on PwmOut class |
kjones99 | 2:5366ce17fe55 | 6 | uLCD_4DGL uLCD(p9,p10,p11); |
kjones99 | 2:5366ce17fe55 | 7 | int x1; |
kjones99 | 2:5366ce17fe55 | 8 | int y1; |
kjones99 | 2:5366ce17fe55 | 9 | class MIDISpeaker |
kjones99 | 2:5366ce17fe55 | 10 | { |
kjones99 | 2:5366ce17fe55 | 11 | public: |
kjones99 | 2:5366ce17fe55 | 12 | MIDISpeaker(PinName pin) : _pin(pin) { |
kjones99 | 2:5366ce17fe55 | 13 | // _pin(pin) means pass pin to the Speaker Constructor |
kjones99 | 2:5366ce17fe55 | 14 | } |
kjones99 | 2:5366ce17fe55 | 15 | // class method to play a note based on PwmOut class |
kjones99 | 2:5366ce17fe55 | 16 | void PlayNote(unsigned char note, unsigned char velocity) {//volatile int duration) { |
kjones99 | 2:5366ce17fe55 | 17 | int notef = int(note); //convert note to int |
kjones99 | 2:5366ce17fe55 | 18 | int velocityf = int(velocity); //convert velocity to int |
kjones99 | 2:5366ce17fe55 | 19 | double noteg = double(notef); //convert note to double |
kjones99 | 2:5366ce17fe55 | 20 | double velocityg = double(velocityf); //convert velocity to double |
kjones99 | 2:5366ce17fe55 | 21 | double f_n = double((noteg - 49)/12); //use piano frequency formula to find corresponding frequency |
kjones99 | 2:5366ce17fe55 | 22 | double freq = (pow(2, f_n)) * 440; |
kjones99 | 2:5366ce17fe55 | 23 | _pin.period(1.0/freq); //use frequency to set period of pwmout pin |
kjones99 | 2:5366ce17fe55 | 24 | velocityg = ((velocityg/127.0) * 0.5); //calculate volume of note and set to pwmout pin |
kjones99 | 2:5366ce17fe55 | 25 | _pin = velocityg; |
kjones99 | 2:5366ce17fe55 | 26 | x1 = ((freq/4000)*115) + 5; //calcuate starting position for frequency spectrum [0,4000 Hz] |
kjones99 | 2:5366ce17fe55 | 27 | y1 = 110 - ((_pin/0.5)*105); // calculate magnitude of spectrum based on velocity |
kjones99 | 2:5366ce17fe55 | 28 | uLCD.locate(0,13); |
kjones99 | 2:5366ce17fe55 | 29 | uLCD.printf("\nkey: %d, freq: %4.0f, volume: %.2f\n", note, freq, velocityg); |
kjones99 | 2:5366ce17fe55 | 30 | uLCD.textbackground_color(BLUE); |
kjones99 | 2:5366ce17fe55 | 31 | uLCD.filled_rectangle(x1, y1, x1+2, 109, BLACK); |
kjones99 | 2:5366ce17fe55 | 32 | } |
kjones99 | 2:5366ce17fe55 | 33 | void StopNote() { |
kjones99 | 2:5366ce17fe55 | 34 | _pin = 0.0; |
kjones99 | 2:5366ce17fe55 | 35 | //uLCD.filled_rectangle(x1, y1, x1+2, 109, BLUE); //code to clear rectangle from lcd |
kjones99 | 2:5366ce17fe55 | 36 | } |
kjones99 | 2:5366ce17fe55 | 37 | |
kjones99 | 2:5366ce17fe55 | 38 | private: |
kjones99 | 2:5366ce17fe55 | 39 | // sets up specified pin for PWM using PwmOut class |
kjones99 | 2:5366ce17fe55 | 40 | PwmOut _pin; |
kjones99 | 2:5366ce17fe55 | 41 | }; |
kjones99 | 2:5366ce17fe55 | 42 | |
kjones99 | 2:5366ce17fe55 | 43 | // Speaker test program - earlier euro police style siren now using new class method |
kjones99 | 2:5366ce17fe55 | 44 | // |