USBMIDI sampled pipe organ uses real pipe organ samples to produce a realistic sound with 16 note polyphony. A serial output will drive external TPIC6A596 shift registers that could be used to drive pipe organ magnets (Solenoid valves)
Dependencies: TextLCD USBDevice mbed
main.cpp@2:8b22402c0b0f, 2015-10-29 (annotated)
- Committer:
- djbottrill
- Date:
- Thu Oct 29 10:43:58 2015 +0000
- Revision:
- 2:8b22402c0b0f
- Parent:
- 1:5a85b31f9142
- Child:
- 3:bc04c8daa46a
Working version with 4 different samples of which the Bourdon is probably the best
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
djbottrill | 0:4d06e0867376 | 1 | /* |
djbottrill | 0:4d06e0867376 | 2 | Program to drive organ magnets from a MIDI input |
djbottrill | 0:4d06e0867376 | 3 | From Midi note 36 the notes are send to a 96 bit shift register organ magnet driver board based on 12 x TPIC6B595 shift registers |
djbottrill | 0:4d06e0867376 | 4 | The program uses the DAC to generate 16 sampled audio channel outputs at 22.05Khz that mirror the solenoid outputs, but also run from |
djbottrill | 0:4d06e0867376 | 5 | MIDI note 24 so the bottom 16' octave can be generated electronically if 16' pipes are not feasible. |
djbottrill | 0:4d06e0867376 | 6 | In fact the sampled output could be tailored to fill in any missing note ranges, |
djbottrill | 0:4d06e0867376 | 7 | Repeated pressing sw3 cycles through a number of display modes. |
djbottrill | 0:4d06e0867376 | 8 | */ |
djbottrill | 0:4d06e0867376 | 9 | #include "mbed.h" |
djbottrill | 0:4d06e0867376 | 10 | |
djbottrill | 1:5a85b31f9142 | 11 | //#include "Rohrflute.h" //This is the best sample |
djbottrill | 2:8b22402c0b0f | 12 | //#include "Dubbelflojt.h" |
djbottrill | 2:8b22402c0b0f | 13 | //#include "Principal.h" |
djbottrill | 2:8b22402c0b0f | 14 | #include "Bourdon.h" |
djbottrill | 0:4d06e0867376 | 15 | |
djbottrill | 0:4d06e0867376 | 16 | #include "USBMIDI.h" |
djbottrill | 0:4d06e0867376 | 17 | |
djbottrill | 0:4d06e0867376 | 18 | #include "TextLCD.h" |
djbottrill | 0:4d06e0867376 | 19 | //For Linksprite LCD Shield |
djbottrill | 0:4d06e0867376 | 20 | //TextLCD lcd(PTC12, D9, D4, D5, D6, D7); // rs, e, d4-d7 |
djbottrill | 0:4d06e0867376 | 21 | //DigitalOut bl(D10); |
djbottrill | 0:4d06e0867376 | 22 | //D8 is wrong on K64F definition it is actually PTC12 not PTA0 |
djbottrill | 0:4d06e0867376 | 23 | |
djbottrill | 0:4d06e0867376 | 24 | //For Midas 2x16 OLED Display |
djbottrill | 0:4d06e0867376 | 25 | TextLCD lcd (D2, D3, D4, D5,D6, D7, TextLCD::LCD16x2, NC, NC, TextLCD::WS0010 ); // 4bit bus: RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=WS0010 |
djbottrill | 0:4d06e0867376 | 26 | |
djbottrill | 0:4d06e0867376 | 27 | //Serial pc(USBTX, USBRX); // tx, rx |
djbottrill | 0:4d06e0867376 | 28 | |
djbottrill | 0:4d06e0867376 | 29 | AnalogOut Aout(DAC0_OUT); |
djbottrill | 0:4d06e0867376 | 30 | |
djbottrill | 0:4d06e0867376 | 31 | DigitalOut redled(LED1); |
djbottrill | 0:4d06e0867376 | 32 | DigitalOut greenled(LED2); |
djbottrill | 0:4d06e0867376 | 33 | DigitalOut blueled(LED3); |
djbottrill | 0:4d06e0867376 | 34 | |
djbottrill | 0:4d06e0867376 | 35 | DigitalOut diag(D15); |
djbottrill | 0:4d06e0867376 | 36 | DigitalOut diag2(D14); |
djbottrill | 0:4d06e0867376 | 37 | |
djbottrill | 0:4d06e0867376 | 38 | DigitalIn sw1(PTC6); |
djbottrill | 0:4d06e0867376 | 39 | DigitalIn sw3(PTA4); |
djbottrill | 0:4d06e0867376 | 40 | |
djbottrill | 0:4d06e0867376 | 41 | //Wi-Fi connector J6 note pin 3 is incorrectly idenifed in the documentation as PTC12 |
djbottrill | 0:4d06e0867376 | 42 | DigitalOut SRCK(PTD7); //Shift Register clock |
djbottrill | 0:4d06e0867376 | 43 | DigitalOut RCK(PTD5); //Shift Register latch |
djbottrill | 0:4d06e0867376 | 44 | DigitalOut SER1(PTB20); //Shift Register serial data outout |
djbottrill | 0:4d06e0867376 | 45 | |
djbottrill | 0:4d06e0867376 | 46 | |
djbottrill | 0:4d06e0867376 | 47 | Ticker output_ticker; //22.05Khz Ticker for sound generation |
djbottrill | 0:4d06e0867376 | 48 | |
djbottrill | 0:4d06e0867376 | 49 | int keybuf[128]= {}; |
djbottrill | 0:4d06e0867376 | 50 | int oldkeybuf[128]= {}; |
djbottrill | 0:4d06e0867376 | 51 | int keystat[128]= {}; |
djbottrill | 0:4d06e0867376 | 52 | int keyidx=0; //Key index //Polyphony counter |
djbottrill | 0:4d06e0867376 | 53 | int outidx=96; //Serial output index |
djbottrill | 0:4d06e0867376 | 54 | |
djbottrill | 0:4d06e0867376 | 55 | int key=0; |
djbottrill | 0:4d06e0867376 | 56 | int velocity=0; |
djbottrill | 0:4d06e0867376 | 57 | int chan=0; |
djbottrill | 0:4d06e0867376 | 58 | int onoff=0; |
djbottrill | 0:4d06e0867376 | 59 | |
djbottrill | 0:4d06e0867376 | 60 | unsigned int audio_out; //Audio output value accumulator |
djbottrill | 0:4d06e0867376 | 61 | |
djbottrill | 0:4d06e0867376 | 62 | //OLELD display bar graph characters |
djbottrill | 0:4d06e0867376 | 63 | const char udc0[] = {0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 64 | const char udc1[] = {0x15, 0x10, 0x10, 0x10, 0x10, 0x10, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 65 | const char udc2[] = {0x15, 0x04, 0x04, 0x04, 0x04, 0x04, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 66 | const char udc3[] = {0x15, 0x14, 0x14, 0x14, 0x14, 0x14, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 67 | const char udc4[] = {0x15, 0x01, 0x01, 0x01, 0x01, 0x01, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 68 | const char udc5[] = {0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 69 | const char udc6[] = {0x15, 0x05, 0x05, 0x05, 0x05, 0x05, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 70 | const char udc7[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00}; |
djbottrill | 0:4d06e0867376 | 71 | |
djbottrill | 0:4d06e0867376 | 72 | //Variables for LCD/OLED display |
djbottrill | 0:4d06e0867376 | 73 | unsigned long int lcd_stat=0; |
djbottrill | 0:4d06e0867376 | 74 | unsigned long int lcd_stat_old=1; |
djbottrill | 0:4d06e0867376 | 75 | char display[2][16]; //Display Buffer |
djbottrill | 0:4d06e0867376 | 76 | char display_old[2][16]; //Old Display Buffer |
djbottrill | 0:4d06e0867376 | 77 | int pos=0; //Display Position counter |
djbottrill | 0:4d06e0867376 | 78 | int row=0; |
djbottrill | 0:4d06e0867376 | 79 | int tval=0; |
djbottrill | 0:4d06e0867376 | 80 | int keyidx_t=0; |
djbottrill | 0:4d06e0867376 | 81 | int lcdmode=0; //LCD Display Mode |
djbottrill | 0:4d06e0867376 | 82 | |
djbottrill | 0:4d06e0867376 | 83 | int sw1_count=0; //Switch de-bounce counter |
djbottrill | 0:4d06e0867376 | 84 | int sw1_old=0; |
djbottrill | 0:4d06e0867376 | 85 | int sw1_phase=0; |
djbottrill | 0:4d06e0867376 | 86 | int sw3_count=0; //Switch de-bounce counter |
djbottrill | 0:4d06e0867376 | 87 | int sw3_old=0; |
djbottrill | 0:4d06e0867376 | 88 | int sw3_phase=0; |
djbottrill | 0:4d06e0867376 | 89 | |
djbottrill | 0:4d06e0867376 | 90 | //Sound generator variables |
djbottrill | 0:4d06e0867376 | 91 | int tempidx=0; |
djbottrill | 0:4d06e0867376 | 92 | int freqidx=45; |
djbottrill | 0:4d06e0867376 | 93 | float synth[16]; |
djbottrill | 0:4d06e0867376 | 94 | float synthidx[16]; |
djbottrill | 0:4d06e0867376 | 95 | int synth_old[16]; |
djbottrill | 0:4d06e0867376 | 96 | int synthtab[16]= {}; |
djbottrill | 0:4d06e0867376 | 97 | int synthstat[16]= {}; |
djbottrill | 0:4d06e0867376 | 98 | unsigned char synthoctave[16]={}; |
djbottrill | 0:4d06e0867376 | 99 | unsigned char synthnote[16]={}; |
djbottrill | 0:4d06e0867376 | 100 | int noteidx=0; |
djbottrill | 0:4d06e0867376 | 101 | int i; |
djbottrill | 0:4d06e0867376 | 102 | int ii; |
djbottrill | 0:4d06e0867376 | 103 | |
djbottrill | 0:4d06e0867376 | 104 | |
djbottrill | 0:4d06e0867376 | 105 | /* |
djbottrill | 0:4d06e0867376 | 106 | Ticker routine to calculate and output the audio signal. |
djbottrill | 0:4d06e0867376 | 107 | As the DAC is 12 bit this allows for 16 concurrennt 8 bit samples running with no additional loss of precision. |
djbottrill | 0:4d06e0867376 | 108 | The samples should be normalised to maximise the 8 bit range as only one sample is used there is no need to generate sounds with different amplitude. |
djbottrill | 0:4d06e0867376 | 109 | Sound Generation is is in 4 states sored in synthstat: |
djbottrill | 0:4d06e0867376 | 110 | 0 = Synth Channel idle |
djbottrill | 0:4d06e0867376 | 111 | 1 = Attack / Sustain phase note running and looping if necessary |
djbottrill | 0:4d06e0867376 | 112 | 2 = Signal to start decay this may be delayed until the sound is paaased the initial attack |
djbottrill | 0:4d06e0867376 | 113 | 3 = Sample is in the decay phase. |
djbottrill | 0:4d06e0867376 | 114 | |
djbottrill | 0:4d06e0867376 | 115 | This routine also handles the Synchronous output to a 96 bit shift register Organ Pallet mangnet driver board, one bit is sent per tick. |
djbottrill | 0:4d06e0867376 | 116 | The data clock pulse is generated during the 16 channel synth scan and the final latch pulse is generated on the 96th Tick. |
djbottrill | 0:4d06e0867376 | 117 | All 96 bits are clocked out in around 4.4mS. |
djbottrill | 0:4d06e0867376 | 118 | |
djbottrill | 0:4d06e0867376 | 119 | Additional code to de-bounce the K64F onboard switches again using multiple ticks to de-bounce; has 4 states: |
djbottrill | 0:4d06e0867376 | 120 | 0 - Button not pressed |
djbottrill | 0:4d06e0867376 | 121 | 1 - Button pressed waiting for de-bounce count of 100 ticks (4.53 mS) |
djbottrill | 0:4d06e0867376 | 122 | 2 - Button press stable, this staus can be read as the de-bounced status indicating the button press is clean |
djbottrill | 0:4d06e0867376 | 123 | 3 - Button released waiting for de-bounce count of 100 ticks before setting status back to 0 |
djbottrill | 0:4d06e0867376 | 124 | */ |
djbottrill | 0:4d06e0867376 | 125 | void aout() |
djbottrill | 0:4d06e0867376 | 126 | { |
djbottrill | 0:4d06e0867376 | 127 | diag=1; //Pulse DIAG Pin for Scoping purposes IE pin high |
djbottrill | 0:4d06e0867376 | 128 | redled=0; //Pulse Red LED to indicate Ticker working |
djbottrill | 0:4d06e0867376 | 129 | audio_out=0; //Clear Audio Accumulator |
djbottrill | 0:4d06e0867376 | 130 | |
djbottrill | 0:4d06e0867376 | 131 | SER1=keybuf[outidx+12]; //Send bit serially |
djbottrill | 0:4d06e0867376 | 132 | |
djbottrill | 0:4d06e0867376 | 133 | for (i =0; i<16; i++) { //Do 16 channels |
djbottrill | 0:4d06e0867376 | 134 | //check if channel idle |
djbottrill | 0:4d06e0867376 | 135 | if(synthstat[i]>0) { |
djbottrill | 0:4d06e0867376 | 136 | //Check is decay needs to start if close to 0 crossing |
djbottrill | 0:4d06e0867376 | 137 | if (synthstat[i]==2) { |
djbottrill | 0:4d06e0867376 | 138 | if (synth[i]>=sample_attack[synthoctave[i]]) { //Delay decay phase if not past end of attack |
djbottrill | 0:4d06e0867376 | 139 | //Check if the waveform is close to zero crossing this helps eliminate clicks looks for rising edge approaching 0x80 |
djbottrill | 0:4d06e0867376 | 140 | tempidx=synth[i]; |
djbottrill | 0:4d06e0867376 | 141 | if (sample[synthoctave[i]][tempidx]<128) { |
djbottrill | 0:4d06e0867376 | 142 | if (sample[synthoctave[i]][tempidx]>synth_old[i]) { |
djbottrill | 0:4d06e0867376 | 143 | if (sample[synthoctave[i]][tempidx]>120) { |
djbottrill | 0:4d06e0867376 | 144 | synth[i]=sample_loop_off[synthoctave[i]]; //Jump to start of decay |
djbottrill | 0:4d06e0867376 | 145 | synthstat[i]=3; //Say note in decay |
djbottrill | 0:4d06e0867376 | 146 | } |
djbottrill | 0:4d06e0867376 | 147 | } |
djbottrill | 0:4d06e0867376 | 148 | } |
djbottrill | 0:4d06e0867376 | 149 | } |
djbottrill | 0:4d06e0867376 | 150 | } |
djbottrill | 0:4d06e0867376 | 151 | //Check and process sustain phase |
djbottrill | 0:4d06e0867376 | 152 | if (synthstat[i]==1) { //Are we in sustain phase |
djbottrill | 0:4d06e0867376 | 153 | if (synth[i]>sample_loop_end[synthoctave[i]]) { //Got to end of buffer? |
djbottrill | 0:4d06e0867376 | 154 | // synth[i]=synth[i]-sample_loop_start[synthoctave[i]]; //Wrap around back to start |
djbottrill | 0:4d06e0867376 | 155 | synth[i]=sample_loop_start[synthoctave[i]]; //Wrap around back to start |
djbottrill | 0:4d06e0867376 | 156 | } |
djbottrill | 0:4d06e0867376 | 157 | } |
djbottrill | 0:4d06e0867376 | 158 | } |
djbottrill | 0:4d06e0867376 | 159 | //get sample add to Audio accumulator |
djbottrill | 0:4d06e0867376 | 160 | tempidx=synth[i]; //Convert to integer |
djbottrill | 0:4d06e0867376 | 161 | synth_old[i]=sample[synthoctave[i]][tempidx]; //Get and save old sample |
djbottrill | 0:4d06e0867376 | 162 | audio_out=audio_out+synth_old[i]; //add sample to audio out accumulator |
djbottrill | 0:4d06e0867376 | 163 | synth[i]=synth[i]+synthidx[i]; //Get next sample pointer |
djbottrill | 0:4d06e0867376 | 164 | |
djbottrill | 0:4d06e0867376 | 165 | //Check if decay has completed |
djbottrill | 0:4d06e0867376 | 166 | if (synth[i]>=sample_len[synthoctave[i]]) { //End of decay? |
djbottrill | 0:4d06e0867376 | 167 | synthstat[i]=0; //say channel free |
djbottrill | 0:4d06e0867376 | 168 | synth[i]=0; //Set sample pointer to 0 |
djbottrill | 0:4d06e0867376 | 169 | synthidx[i]=0; //Set sample index to 0 |
djbottrill | 0:4d06e0867376 | 170 | synthtab[i]=255; //Set to invalid |
djbottrill | 0:4d06e0867376 | 171 | } |
djbottrill | 0:4d06e0867376 | 172 | |
djbottrill | 0:4d06e0867376 | 173 | //Generate clock pulses to send data serially |
djbottrill | 0:4d06e0867376 | 174 | if(i==2) { |
djbottrill | 0:4d06e0867376 | 175 | SRCK=1; //Generate serial output clock pulse |
djbottrill | 0:4d06e0867376 | 176 | } |
djbottrill | 0:4d06e0867376 | 177 | if(i==5) { |
djbottrill | 0:4d06e0867376 | 178 | SRCK=0; |
djbottrill | 0:4d06e0867376 | 179 | } |
djbottrill | 0:4d06e0867376 | 180 | if (outidx==0 and i==10) { |
djbottrill | 0:4d06e0867376 | 181 | RCK=1; //Generate latch strobe after 96 bits sent |
djbottrill | 0:4d06e0867376 | 182 | } |
djbottrill | 0:4d06e0867376 | 183 | if (outidx==0 and i==15) { |
djbottrill | 0:4d06e0867376 | 184 | RCK=0; //Generate latch strobe after 96 bits sent |
djbottrill | 0:4d06e0867376 | 185 | outidx=96; |
djbottrill | 0:4d06e0867376 | 186 | } |
djbottrill | 0:4d06e0867376 | 187 | } //Next Note |
djbottrill | 0:4d06e0867376 | 188 | outidx--; //Next serial data bit |
djbottrill | 0:4d06e0867376 | 189 | |
djbottrill | 0:4d06e0867376 | 190 | //Output to DAC |
djbottrill | 0:4d06e0867376 | 191 | Aout.write_u16(audio_out*16); |
djbottrill | 0:4d06e0867376 | 192 | |
djbottrill | 0:4d06e0867376 | 193 | // De-bounce SW1 |
djbottrill | 0:4d06e0867376 | 194 | if (sw1_phase==0 & sw1==0) { //Button just been pressed |
djbottrill | 0:4d06e0867376 | 195 | sw1_phase=1; |
djbottrill | 0:4d06e0867376 | 196 | sw1_count=100; |
djbottrill | 0:4d06e0867376 | 197 | } |
djbottrill | 0:4d06e0867376 | 198 | if (sw1_phase==1 & sw1_count==0 & sw1==0) { //Button still pressed after de-bounce period |
djbottrill | 0:4d06e0867376 | 199 | sw1_phase=2; |
djbottrill | 0:4d06e0867376 | 200 | } |
djbottrill | 0:4d06e0867376 | 201 | if (sw1_phase==1 & sw1_count==0 & sw1==1) { //Button no longer pressed after de-bounce period |
djbottrill | 0:4d06e0867376 | 202 | sw1_phase=0; |
djbottrill | 0:4d06e0867376 | 203 | } |
djbottrill | 0:4d06e0867376 | 204 | if (sw1_phase==2 & sw1==1) { //Button released |
djbottrill | 0:4d06e0867376 | 205 | sw1_phase=3; |
djbottrill | 0:4d06e0867376 | 206 | sw1_count=100; |
djbottrill | 0:4d06e0867376 | 207 | } |
djbottrill | 0:4d06e0867376 | 208 | if (sw1_phase==3 & sw1_count==0 & sw1==1) { //Button still released after timeout |
djbottrill | 0:4d06e0867376 | 209 | sw1_phase=0; |
djbottrill | 0:4d06e0867376 | 210 | } |
djbottrill | 0:4d06e0867376 | 211 | sw1_count--; |
djbottrill | 0:4d06e0867376 | 212 | |
djbottrill | 0:4d06e0867376 | 213 | // De-bounce SW3 |
djbottrill | 0:4d06e0867376 | 214 | if (sw3_phase==0 & sw3==0) { //Button just been pressed |
djbottrill | 0:4d06e0867376 | 215 | sw3_phase=1; |
djbottrill | 0:4d06e0867376 | 216 | sw3_count=100; |
djbottrill | 0:4d06e0867376 | 217 | } |
djbottrill | 0:4d06e0867376 | 218 | if (sw3_phase==1 & sw3_count==0 & sw3==0) { //Button still pressed after de-bounce period |
djbottrill | 0:4d06e0867376 | 219 | sw3_phase=2; |
djbottrill | 0:4d06e0867376 | 220 | } |
djbottrill | 0:4d06e0867376 | 221 | if (sw3_phase==1 & sw3_count==0 & sw3==1) { //Button no longer pressed after de-bounce period |
djbottrill | 0:4d06e0867376 | 222 | sw3_phase=0; |
djbottrill | 0:4d06e0867376 | 223 | } |
djbottrill | 0:4d06e0867376 | 224 | if (sw3_phase==2 & sw3==1) { //Button released |
djbottrill | 0:4d06e0867376 | 225 | sw3_phase=3; |
djbottrill | 0:4d06e0867376 | 226 | sw3_count=100; |
djbottrill | 0:4d06e0867376 | 227 | } |
djbottrill | 0:4d06e0867376 | 228 | if (sw3_phase==3 & sw3_count==0 & sw3==1) { //Button still released after timeout |
djbottrill | 0:4d06e0867376 | 229 | sw3_phase=0; |
djbottrill | 0:4d06e0867376 | 230 | } |
djbottrill | 0:4d06e0867376 | 231 | sw3_count--; |
djbottrill | 0:4d06e0867376 | 232 | |
djbottrill | 0:4d06e0867376 | 233 | redled=1; |
djbottrill | 0:4d06e0867376 | 234 | diag=0; |
djbottrill | 0:4d06e0867376 | 235 | } |
djbottrill | 0:4d06e0867376 | 236 | |
djbottrill | 0:4d06e0867376 | 237 | /* |
djbottrill | 0:4d06e0867376 | 238 | Interrupt routine to receive MIDI on/off message and set LED status accordingly |
djbottrill | 0:4d06e0867376 | 239 | MIDI note on/off events directly start/stop the synth channels and store the note status in keytab. |
djbottrill | 0:4d06e0867376 | 240 | */ |
djbottrill | 0:4d06e0867376 | 241 | void get_message(MIDIMessage msg) |
djbottrill | 0:4d06e0867376 | 242 | { |
djbottrill | 0:4d06e0867376 | 243 | greenled=0; //Pulse Green LED to indicate MIDI activity |
djbottrill | 0:4d06e0867376 | 244 | key=msg.key(); |
djbottrill | 0:4d06e0867376 | 245 | velocity=msg.velocity(); |
djbottrill | 0:4d06e0867376 | 246 | chan=msg.channel(); |
djbottrill | 0:4d06e0867376 | 247 | |
djbottrill | 0:4d06e0867376 | 248 | switch (msg.type()) { |
djbottrill | 0:4d06e0867376 | 249 | case MIDIMessage::NoteOnType: //MIDI note on received |
djbottrill | 0:4d06e0867376 | 250 | if (key>23) { |
djbottrill | 0:4d06e0867376 | 251 | onoff=1; |
djbottrill | 0:4d06e0867376 | 252 | keybuf[(key-24)]=1; //Store in keybuf, except for notes 24-35 |
djbottrill | 0:4d06e0867376 | 253 | //Process keys on |
djbottrill | 0:4d06e0867376 | 254 | //Find a free note generator and start the note playing, if no slots available just ignore the note |
djbottrill | 0:4d06e0867376 | 255 | for (ii=0; ii<16; ii++) { |
djbottrill | 0:4d06e0867376 | 256 | if (synthtab[ii]!=(key-24) && synthstat[ii]==0) { //Is synth already playing the note? |
djbottrill | 0:4d06e0867376 | 257 | synthtab[ii]=key-24; //Store note value |
djbottrill | 0:4d06e0867376 | 258 | synthoctave[ii]=synthtab[ii]/12; //Store the octave |
djbottrill | 0:4d06e0867376 | 259 | synthnote[ii]= synthtab[ii]%12; //Note within the octave |
djbottrill | 0:4d06e0867376 | 260 | synthstat[ii]=1; //Set status to playing |
djbottrill | 0:4d06e0867376 | 261 | synthidx[ii]=freqtab[synthnote[ii]]; //Set the frequency |
djbottrill | 0:4d06e0867376 | 262 | ii=16; //exit loop |
djbottrill | 0:4d06e0867376 | 263 | } //Next Synth slot |
djbottrill | 0:4d06e0867376 | 264 | } |
djbottrill | 0:4d06e0867376 | 265 | } |
djbottrill | 0:4d06e0867376 | 266 | break; |
djbottrill | 0:4d06e0867376 | 267 | |
djbottrill | 0:4d06e0867376 | 268 | //Process keys off |
djbottrill | 0:4d06e0867376 | 269 | case MIDIMessage::NoteOffType: //Midi note off received |
djbottrill | 0:4d06e0867376 | 270 | if (key>23) { |
djbottrill | 0:4d06e0867376 | 271 | onoff=0; |
djbottrill | 0:4d06e0867376 | 272 | keybuf[(key-24)]=0; //Update keybuf |
djbottrill | 0:4d06e0867376 | 273 | for (ii=0; ii<16; ii++) { |
djbottrill | 0:4d06e0867376 | 274 | if (synthstat[ii]==1 && synthtab[ii]==(key-24)) { //Note currently playing |
djbottrill | 0:4d06e0867376 | 275 | synthstat[ii]=2; //Set Start decay |
djbottrill | 0:4d06e0867376 | 276 | ii=16; |
djbottrill | 0:4d06e0867376 | 277 | } |
djbottrill | 0:4d06e0867376 | 278 | } |
djbottrill | 0:4d06e0867376 | 279 | } |
djbottrill | 0:4d06e0867376 | 280 | break; |
djbottrill | 0:4d06e0867376 | 281 | |
djbottrill | 0:4d06e0867376 | 282 | //Process all notes off command |
djbottrill | 0:4d06e0867376 | 283 | case MIDIMessage::AllNotesOffType: //Midi all notes off |
djbottrill | 0:4d06e0867376 | 284 | for (ii=0; ii<108; i++) { |
djbottrill | 0:4d06e0867376 | 285 | keybuf[ii]=0; //Clear Keybuf |
djbottrill | 0:4d06e0867376 | 286 | } |
djbottrill | 0:4d06e0867376 | 287 | for (ii=0; ii>16; ii++) { //Kill any sampled notes playing |
djbottrill | 0:4d06e0867376 | 288 | synthstat[ii]=0; //say channel free |
djbottrill | 0:4d06e0867376 | 289 | synth[ii]=0; //Set sample pointer to 0 |
djbottrill | 0:4d06e0867376 | 290 | synthidx[ii]=0; //Set sample index to 0 |
djbottrill | 0:4d06e0867376 | 291 | synthtab[ii]=255; //Set to invalid |
djbottrill | 0:4d06e0867376 | 292 | } |
djbottrill | 0:4d06e0867376 | 293 | break; |
djbottrill | 0:4d06e0867376 | 294 | |
djbottrill | 0:4d06e0867376 | 295 | //Any other MIDI Commands just ignore |
djbottrill | 0:4d06e0867376 | 296 | default: |
djbottrill | 0:4d06e0867376 | 297 | break; |
djbottrill | 0:4d06e0867376 | 298 | } |
djbottrill | 0:4d06e0867376 | 299 | greenled=1; |
djbottrill | 0:4d06e0867376 | 300 | } |
djbottrill | 0:4d06e0867376 | 301 | |
djbottrill | 0:4d06e0867376 | 302 | |
djbottrill | 0:4d06e0867376 | 303 | int main() |
djbottrill | 0:4d06e0867376 | 304 | { |
djbottrill | 0:4d06e0867376 | 305 | greenled=1; |
djbottrill | 0:4d06e0867376 | 306 | redled=1; |
djbottrill | 0:4d06e0867376 | 307 | blueled=1; |
djbottrill | 0:4d06e0867376 | 308 | SRCK=0; //Serial Clock low |
djbottrill | 0:4d06e0867376 | 309 | RCK=0; //Latch Clock low |
djbottrill | 0:4d06e0867376 | 310 | |
djbottrill | 0:4d06e0867376 | 311 | sw1.mode(PullUp); |
djbottrill | 0:4d06e0867376 | 312 | sw3.mode(PullUp); |
djbottrill | 0:4d06e0867376 | 313 | |
djbottrill | 0:4d06e0867376 | 314 | output_ticker.attach(&aout, sample_rate); //Set Sample frequency |
djbottrill | 0:4d06e0867376 | 315 | |
djbottrill | 0:4d06e0867376 | 316 | lcd.cls(); |
djbottrill | 0:4d06e0867376 | 317 | wait(0.1); |
djbottrill | 0:4d06e0867376 | 318 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
djbottrill | 0:4d06e0867376 | 319 | lcd.cls(); |
djbottrill | 0:4d06e0867376 | 320 | lcd.printf("MIDI Pipe Organ Waiting for USB"); |
djbottrill | 0:4d06e0867376 | 321 | |
djbottrill | 0:4d06e0867376 | 322 | NVIC_SetPriority(USB0_IRQn, 99); //Reduce Interrupt priority of USB |
djbottrill | 0:4d06e0867376 | 323 | |
djbottrill | 0:4d06e0867376 | 324 | USBMIDI midi; //Start up MIDI |
djbottrill | 0:4d06e0867376 | 325 | midi.attach(get_message); //callback for MIDI messages received |
djbottrill | 0:4d06e0867376 | 326 | |
djbottrill | 0:4d06e0867376 | 327 | lcd.cls(); |
djbottrill | 0:4d06e0867376 | 328 | lcd.printf("MIDI Pipe Organ"); |
djbottrill | 0:4d06e0867376 | 329 | wait(1); |
djbottrill | 0:4d06e0867376 | 330 | lcd.cls(); |
djbottrill | 0:4d06e0867376 | 331 | lcd.printf("On Chan Note Vel"); |
djbottrill | 0:4d06e0867376 | 332 | //Program user defined characters into OLED display for bar graph |
djbottrill | 0:4d06e0867376 | 333 | lcd.setUDC(0, (char *) udc0); |
djbottrill | 0:4d06e0867376 | 334 | lcd.setUDC(1, (char *) udc1); |
djbottrill | 0:4d06e0867376 | 335 | lcd.setUDC(2, (char *) udc2); |
djbottrill | 0:4d06e0867376 | 336 | lcd.setUDC(3, (char *) udc3); |
djbottrill | 0:4d06e0867376 | 337 | lcd.setUDC(4, (char *) udc4); |
djbottrill | 0:4d06e0867376 | 338 | lcd.setUDC(5, (char *) udc5); |
djbottrill | 0:4d06e0867376 | 339 | lcd.setUDC(6, (char *) udc6); |
djbottrill | 0:4d06e0867376 | 340 | lcd.setUDC(7, (char *) udc7); |
djbottrill | 0:4d06e0867376 | 341 | |
djbottrill | 0:4d06e0867376 | 342 | key=0; |
djbottrill | 0:4d06e0867376 | 343 | velocity=0; |
djbottrill | 0:4d06e0867376 | 344 | chan=0; |
djbottrill | 0:4d06e0867376 | 345 | onoff=0; |
djbottrill | 0:4d06e0867376 | 346 | |
djbottrill | 0:4d06e0867376 | 347 | //Main Loop |
djbottrill | 0:4d06e0867376 | 348 | while(1) { |
djbottrill | 0:4d06e0867376 | 349 | |
djbottrill | 0:4d06e0867376 | 350 | if (sw1_phase==2 | sw3_phase==2) { |
djbottrill | 0:4d06e0867376 | 351 | blueled=0; |
djbottrill | 0:4d06e0867376 | 352 | } else { |
djbottrill | 0:4d06e0867376 | 353 | blueled=1; |
djbottrill | 0:4d06e0867376 | 354 | } |
djbottrill | 0:4d06e0867376 | 355 | |
djbottrill | 0:4d06e0867376 | 356 | |
djbottrill | 0:4d06e0867376 | 357 | //Check for display mode button being presed |
djbottrill | 0:4d06e0867376 | 358 | if (sw3_phase != sw3_old) { //Mode Switch pressed |
djbottrill | 0:4d06e0867376 | 359 | if(sw3_phase==2) { |
djbottrill | 0:4d06e0867376 | 360 | lcdmode++; |
djbottrill | 0:4d06e0867376 | 361 | if (lcdmode>2) { |
djbottrill | 0:4d06e0867376 | 362 | lcdmode=0; |
djbottrill | 0:4d06e0867376 | 363 | } |
djbottrill | 0:4d06e0867376 | 364 | lcd.cls(); |
djbottrill | 0:4d06e0867376 | 365 | if (lcdmode==0) { |
djbottrill | 0:4d06e0867376 | 366 | lcd.printf("On Chan Note Vel"); |
djbottrill | 0:4d06e0867376 | 367 | } |
djbottrill | 0:4d06e0867376 | 368 | if (lcdmode==1) { |
djbottrill | 0:4d06e0867376 | 369 | //Initialise bar graph display |
djbottrill | 0:4d06e0867376 | 370 | |
djbottrill | 0:4d06e0867376 | 371 | for (row=0; row<2; row++) { |
djbottrill | 0:4d06e0867376 | 372 | for (pos=0; pos<16; pos++) { |
djbottrill | 0:4d06e0867376 | 373 | display[row][pos]=0; |
djbottrill | 0:4d06e0867376 | 374 | display_old[row][pos]=1; |
djbottrill | 0:4d06e0867376 | 375 | } |
djbottrill | 0:4d06e0867376 | 376 | } |
djbottrill | 0:4d06e0867376 | 377 | } |
djbottrill | 0:4d06e0867376 | 378 | if (lcdmode==2) { |
djbottrill | 0:4d06e0867376 | 379 | lcd.locate(0,1); |
djbottrill | 0:4d06e0867376 | 380 | lcd.printf("0123456789ABCDEF"); |
djbottrill | 0:4d06e0867376 | 381 | } |
djbottrill | 0:4d06e0867376 | 382 | } |
djbottrill | 0:4d06e0867376 | 383 | } |
djbottrill | 0:4d06e0867376 | 384 | sw3_old=sw3_phase; |
djbottrill | 0:4d06e0867376 | 385 | |
djbottrill | 0:4d06e0867376 | 386 | |
djbottrill | 0:4d06e0867376 | 387 | |
djbottrill | 0:4d06e0867376 | 388 | // OLED MIDI Status display |
djbottrill | 0:4d06e0867376 | 389 | if (lcdmode==0) { |
djbottrill | 0:4d06e0867376 | 390 | lcd_stat=key+(128*velocity)+(16384*chan)+(262144*onoff); |
djbottrill | 0:4d06e0867376 | 391 | if (lcd_stat!=lcd_stat_old) { |
djbottrill | 0:4d06e0867376 | 392 | |
djbottrill | 0:4d06e0867376 | 393 | lcd.locate(0,1); |
djbottrill | 0:4d06e0867376 | 394 | lcd.printf("%1d %2d %3d %3d", onoff, chan, key, velocity); |
djbottrill | 0:4d06e0867376 | 395 | lcd_stat_old=lcd_stat; |
djbottrill | 0:4d06e0867376 | 396 | } |
djbottrill | 0:4d06e0867376 | 397 | } |
djbottrill | 0:4d06e0867376 | 398 | |
djbottrill | 0:4d06e0867376 | 399 | // OLED MIDI Bar Display |
djbottrill | 0:4d06e0867376 | 400 | if (lcdmode==1) { |
djbottrill | 0:4d06e0867376 | 401 | keyidx=0; |
djbottrill | 0:4d06e0867376 | 402 | for (row=1; row>=0; row--) { |
djbottrill | 0:4d06e0867376 | 403 | for (pos=0; pos<16; pos++) { |
djbottrill | 0:4d06e0867376 | 404 | keyidx_t=keyidx*2; |
djbottrill | 0:4d06e0867376 | 405 | if(keyidx_t>94) { |
djbottrill | 0:4d06e0867376 | 406 | keyidx_t=keyidx_t-95; |
djbottrill | 0:4d06e0867376 | 407 | } |
djbottrill | 0:4d06e0867376 | 408 | tval=keybuf[keyidx_t]; |
djbottrill | 0:4d06e0867376 | 409 | keyidx++; |
djbottrill | 0:4d06e0867376 | 410 | keyidx_t=keyidx*2; |
djbottrill | 0:4d06e0867376 | 411 | if(keyidx_t>94) { |
djbottrill | 0:4d06e0867376 | 412 | keyidx_t=keyidx_t-95; |
djbottrill | 0:4d06e0867376 | 413 | } |
djbottrill | 0:4d06e0867376 | 414 | tval=tval+(2*keybuf[keyidx_t]); |
djbottrill | 0:4d06e0867376 | 415 | keyidx++; |
djbottrill | 0:4d06e0867376 | 416 | |
djbottrill | 0:4d06e0867376 | 417 | keyidx_t=keyidx*2; |
djbottrill | 0:4d06e0867376 | 418 | if(keyidx_t>94) { |
djbottrill | 0:4d06e0867376 | 419 | keyidx_t=keyidx_t-95; |
djbottrill | 0:4d06e0867376 | 420 | } |
djbottrill | 0:4d06e0867376 | 421 | tval=tval+(4*keybuf[keyidx_t]); |
djbottrill | 0:4d06e0867376 | 422 | keyidx++; |
djbottrill | 0:4d06e0867376 | 423 | display[row][pos]=tval; |
djbottrill | 0:4d06e0867376 | 424 | |
djbottrill | 0:4d06e0867376 | 425 | if(display[row][pos]!=display_old[row][pos]) { |
djbottrill | 0:4d06e0867376 | 426 | lcd.locate(pos,row); |
djbottrill | 0:4d06e0867376 | 427 | lcd.putc(display[row][pos]); |
djbottrill | 0:4d06e0867376 | 428 | display_old[row][pos]=display[row][pos]; |
djbottrill | 0:4d06e0867376 | 429 | } |
djbottrill | 0:4d06e0867376 | 430 | } |
djbottrill | 0:4d06e0867376 | 431 | } |
djbottrill | 0:4d06e0867376 | 432 | } |
djbottrill | 0:4d06e0867376 | 433 | |
djbottrill | 0:4d06e0867376 | 434 | //Display status of the Synth channels |
djbottrill | 0:4d06e0867376 | 435 | if (lcdmode==2) { |
djbottrill | 0:4d06e0867376 | 436 | lcd.locate(0,0); |
djbottrill | 0:4d06e0867376 | 437 | for (noteidx=0; noteidx<16; noteidx++) { |
djbottrill | 0:4d06e0867376 | 438 | lcd.putc((synthstat[noteidx]+48)); |
djbottrill | 0:4d06e0867376 | 439 | } |
djbottrill | 0:4d06e0867376 | 440 | } |
djbottrill | 0:4d06e0867376 | 441 | |
djbottrill | 0:4d06e0867376 | 442 | } //End of main loop |
djbottrill | 0:4d06e0867376 | 443 | } //End of program |
djbottrill | 0:4d06e0867376 | 444 | |
djbottrill | 0:4d06e0867376 | 445 | |
djbottrill | 0:4d06e0867376 | 446 | |
djbottrill | 0:4d06e0867376 | 447 |