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

Committer:
djbottrill
Date:
Sun Nov 15 12:55:31 2015 +0000
Revision:
23:31cb90fa661f
Parent:
22:7d4e07edf503
Child:
26:7b9267a5b3c3
Changed stop inputs to toggles, defaults to Bourdon 8 being on

Who changed what in which revision?

UserRevisionLine numberNew 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 16:d2694fb530c3 4 The program uses the DAC to generate 16 sampled audio channel outputs at up to 44.1Khz 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 21:04febed27b55 11 #include "Bourdon.h"
djbottrill 20:b4c61f856346 12 #include "Montre.h"
djbottrill 18:472954b6b711 13 const float sample1_rate = 0.00002268; //44.1KHz
djbottrill 21:04febed27b55 14 //For Organ Sample played at sample speed
djbottrill 21:04febed27b55 15 const float freqtab[] = {1, 1.0596, 1.1227, 1.1893, 1.2599, 1.3348, 1.4144, 1.4985, 1.5872, 1.6819, 1.782, 1.888, 2};
djbottrill 17:7f27f4921305 16
djbottrill 0:4d06e0867376 17
djbottrill 0:4d06e0867376 18 #include "USBMIDI.h"
djbottrill 0:4d06e0867376 19
djbottrill 0:4d06e0867376 20 #include "TextLCD.h"
djbottrill 0:4d06e0867376 21 //For Linksprite LCD Shield
djbottrill 0:4d06e0867376 22 //TextLCD lcd(PTC12, D9, D4, D5, D6, D7); // rs, e, d4-d7
djbottrill 0:4d06e0867376 23 //DigitalOut bl(D10);
djbottrill 0:4d06e0867376 24 //D8 is wrong on K64F definition it is actually PTC12 not PTA0
djbottrill 0:4d06e0867376 25
djbottrill 0:4d06e0867376 26 //For Midas 2x16 OLED Display
djbottrill 0:4d06e0867376 27 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 28
djbottrill 0:4d06e0867376 29 //Serial pc(USBTX, USBRX); // tx, rx
djbottrill 0:4d06e0867376 30
djbottrill 0:4d06e0867376 31 AnalogOut Aout(DAC0_OUT);
djbottrill 0:4d06e0867376 32
djbottrill 0:4d06e0867376 33 DigitalOut redled(LED1);
djbottrill 0:4d06e0867376 34 DigitalOut greenled(LED2);
djbottrill 0:4d06e0867376 35 DigitalOut blueled(LED3);
djbottrill 0:4d06e0867376 36
djbottrill 0:4d06e0867376 37 DigitalOut diag(D15);
djbottrill 0:4d06e0867376 38 DigitalOut diag2(D14);
djbottrill 0:4d06e0867376 39
djbottrill 0:4d06e0867376 40 DigitalIn sw1(PTC6);
djbottrill 0:4d06e0867376 41 DigitalIn sw3(PTA4);
djbottrill 0:4d06e0867376 42
djbottrill 12:74b52900cddb 43 //Stop switches on = 0
djbottrill 15:c1854904f0d7 44 DigitalIn p1 (PTB2); //16'
djbottrill 15:c1854904f0d7 45 DigitalIn p2 (PTB3); //8'
djbottrill 15:c1854904f0d7 46 DigitalIn p3 (PTB10); //4'
djbottrill 15:c1854904f0d7 47 DigitalIn p4 (PTB11); //2 2/3'
djbottrill 15:c1854904f0d7 48 DigitalIn p5 (PTC11); //2'
djbottrill 12:74b52900cddb 49
djbottrill 0:4d06e0867376 50 //Wi-Fi connector J6 note pin 3 is incorrectly idenifed in the documentation as PTC12
djbottrill 9:b0f110c02b1b 51 //Connects the shift register solenoid driver board
djbottrill 0:4d06e0867376 52 DigitalOut SRCK(PTD7); //Shift Register clock
djbottrill 0:4d06e0867376 53 DigitalOut RCK(PTD5); //Shift Register latch
djbottrill 0:4d06e0867376 54 DigitalOut SER1(PTB20); //Shift Register serial data outout
djbottrill 0:4d06e0867376 55
djbottrill 0:4d06e0867376 56
djbottrill 9:b0f110c02b1b 57 Ticker output_ticker; //Ticker for sound generation
djbottrill 0:4d06e0867376 58
djbottrill 11:a27a71f7c149 59 unsigned char keybuf[128]= {};
djbottrill 11:a27a71f7c149 60 unsigned char oldkeybuf[128]= {};
djbottrill 0:4d06e0867376 61 int keystat[128]= {};
djbottrill 16:d2694fb530c3 62 int keyidx=0; //Key index
djbottrill 0:4d06e0867376 63
djbottrill 0:4d06e0867376 64 int key=0;
djbottrill 0:4d06e0867376 65 int velocity=0;
djbottrill 0:4d06e0867376 66 int chan=0;
djbottrill 0:4d06e0867376 67 int onoff=0;
djbottrill 0:4d06e0867376 68
djbottrill 0:4d06e0867376 69 unsigned int audio_out; //Audio output value accumulator
djbottrill 0:4d06e0867376 70
djbottrill 0:4d06e0867376 71 //OLELD display bar graph characters
djbottrill 0:4d06e0867376 72 const char udc0[] = {0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00};
djbottrill 0:4d06e0867376 73 const char udc1[] = {0x15, 0x10, 0x10, 0x10, 0x10, 0x10, 0x15, 0x00};
djbottrill 0:4d06e0867376 74 const char udc2[] = {0x15, 0x04, 0x04, 0x04, 0x04, 0x04, 0x15, 0x00};
djbottrill 0:4d06e0867376 75 const char udc3[] = {0x15, 0x14, 0x14, 0x14, 0x14, 0x14, 0x15, 0x00};
djbottrill 0:4d06e0867376 76 const char udc4[] = {0x15, 0x01, 0x01, 0x01, 0x01, 0x01, 0x15, 0x00};
djbottrill 0:4d06e0867376 77 const char udc5[] = {0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x15, 0x00};
djbottrill 0:4d06e0867376 78 const char udc6[] = {0x15, 0x05, 0x05, 0x05, 0x05, 0x05, 0x15, 0x00};
djbottrill 0:4d06e0867376 79 const char udc7[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00};
djbottrill 0:4d06e0867376 80
djbottrill 0:4d06e0867376 81 //Variables for LCD/OLED display
djbottrill 0:4d06e0867376 82 unsigned long int lcd_stat=0;
djbottrill 0:4d06e0867376 83 unsigned long int lcd_stat_old=1;
djbottrill 0:4d06e0867376 84 char display[2][16]; //Display Buffer
djbottrill 0:4d06e0867376 85 char display_old[2][16]; //Old Display Buffer
djbottrill 0:4d06e0867376 86 int pos=0; //Display Position counter
djbottrill 0:4d06e0867376 87 int row=0;
djbottrill 0:4d06e0867376 88 int tval=0;
djbottrill 12:74b52900cddb 89 int tval1=0;
djbottrill 0:4d06e0867376 90 int keyidx_t=0;
djbottrill 0:4d06e0867376 91 int lcdmode=0; //LCD Display Mode
djbottrill 8:df5f305aab22 92 int x =0;
djbottrill 8:df5f305aab22 93 int x1=1;
djbottrill 9:b0f110c02b1b 94
djbottrill 23:31cb90fa661f 95 unsigned int swi;
djbottrill 23:31cb90fa661f 96 unsigned int pressed;
djbottrill 23:31cb90fa661f 97 unsigned int sw[8];
djbottrill 23:31cb90fa661f 98 unsigned int sw_count[8]; //Switch de-bounce counter
djbottrill 23:31cb90fa661f 99 unsigned int sw_phase[8];
djbottrill 23:31cb90fa661f 100 unsigned int sw_status[8]= {0,1,0,0,0,0,0,0};
djbottrill 23:31cb90fa661f 101 unsigned int sw_old[8];
djbottrill 16:d2694fb530c3 102 int oldstops=1;
djbottrill 0:4d06e0867376 103
djbottrill 0:4d06e0867376 104 //Sound generator variables
djbottrill 17:7f27f4921305 105 int sampleset=0;
djbottrill 0:4d06e0867376 106 int tempidx=0;
djbottrill 0:4d06e0867376 107 int freqidx=45;
djbottrill 0:4d06e0867376 108 float synth[16];
djbottrill 0:4d06e0867376 109 float synthidx[16];
djbottrill 0:4d06e0867376 110 int synth_old[16];
djbottrill 0:4d06e0867376 111 int synthtab[16]= {};
djbottrill 0:4d06e0867376 112 int synthstat[16]= {};
djbottrill 4:9f63e68b548e 113 unsigned char synthoctave[16]= {};
djbottrill 4:9f63e68b548e 114 unsigned char synthnote[16]= {};
djbottrill 12:74b52900cddb 115 int sucess=0;
djbottrill 0:4d06e0867376 116 int noteidx=0;
djbottrill 0:4d06e0867376 117 int i;
djbottrill 0:4d06e0867376 118 int ii;
djbottrill 9:b0f110c02b1b 119 int cl;
djbottrill 0:4d06e0867376 120
djbottrill 0:4d06e0867376 121 /*
djbottrill 0:4d06e0867376 122 Ticker routine to calculate and output the audio signal.
djbottrill 0:4d06e0867376 123 As the DAC is 12 bit this allows for 16 concurrennt 8 bit samples running with no additional loss of precision.
djbottrill 0:4d06e0867376 124 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 5:13a1e5e4494d 125 Sound Generation is in 4 states stored in synthstat:
djbottrill 0:4d06e0867376 126 0 = Synth Channel idle
djbottrill 0:4d06e0867376 127 1 = Attack / Sustain phase note running and looping if necessary
djbottrill 5:13a1e5e4494d 128 2 = Signal to start decay this may be delayed until the sound is paat the initial attack point typically 100-200 mS
djbottrill 0:4d06e0867376 129 3 = Sample is in the decay phase.
djbottrill 0:4d06e0867376 130
djbottrill 0:4d06e0867376 131 */
djbottrill 0:4d06e0867376 132 void aout()
djbottrill 0:4d06e0867376 133 {
djbottrill 15:c1854904f0d7 134 // diag=1; //Pulse DIAG Pin for Scoping purposes IE pin high
djbottrill 15:c1854904f0d7 135 // redled=0; //Pulse Red LED to indicate Ticker working
djbottrill 0:4d06e0867376 136 audio_out=0; //Clear Audio Accumulator
djbottrill 17:7f27f4921305 137 if (sampleset==0) {
djbottrill 19:4cec60669626 138 for (i =0; i<16; i++) { //Do 16 channels
djbottrill 17:7f27f4921305 139 switch (synthstat[i]) {
djbottrill 17:7f27f4921305 140 case 1: //Sustain phase
djbottrill 19:4cec60669626 141 if (synth[i]>sample1_loop_end[synthoctave[i]]) { //Got to end of buffer?
djbottrill 19:4cec60669626 142 synth[i]=sample1_loop_start[synthoctave[i]]; //Wrap around back to start
djbottrill 17:7f27f4921305 143 }
djbottrill 17:7f27f4921305 144 break;
djbottrill 17:7f27f4921305 145 case 2: //Note off demand state
djbottrill 19:4cec60669626 146 if (synth[i]>=sample1_attack[synthoctave[i]]) { //Delay decay phase if not past end of attack
djbottrill 0:4d06e0867376 147 //Check if the waveform is close to zero crossing this helps eliminate clicks looks for rising edge approaching 0x80
djbottrill 17:7f27f4921305 148 tempidx=synth[i];
djbottrill 17:7f27f4921305 149 if (sample1[synthoctave[i]][tempidx]<128 & sample1[synthoctave[i]][tempidx]>120) {
djbottrill 17:7f27f4921305 150 if (sample1[synthoctave[i]][tempidx]>synth_old[i]) {
djbottrill 19:4cec60669626 151 synth[i]=sample1_loop_off[synthoctave[i]]; //Jump to start of decay
djbottrill 19:4cec60669626 152 synthstat[i]=3; //Say note in decay
djbottrill 17:7f27f4921305 153 }
djbottrill 0:4d06e0867376 154 }
djbottrill 0:4d06e0867376 155 }
djbottrill 17:7f27f4921305 156 break;
djbottrill 17:7f27f4921305 157 case 3: //Check if decay has completed
djbottrill 19:4cec60669626 158 if (synth[i]>=sample1_len[synthoctave[i]]) { //End of decay?
djbottrill 19:4cec60669626 159 synthstat[i]=0; //say channel free
djbottrill 19:4cec60669626 160 synth[i]=0; //Set sample pointer to 0
djbottrill 19:4cec60669626 161 synthidx[i]=0; //Set sample index to 0
djbottrill 19:4cec60669626 162 synthtab[i]=255; //Set to invalid
djbottrill 17:7f27f4921305 163 }
djbottrill 17:7f27f4921305 164 break ;
djbottrill 17:7f27f4921305 165 }
djbottrill 4:9f63e68b548e 166
djbottrill 4:9f63e68b548e 167 //get sample and add to Audio Accumulator
djbottrill 19:4cec60669626 168 synth_old[i]=sample1[synthoctave[i]][(int)(synth[i])]; //Get and save old sample
djbottrill 20:b4c61f856346 169 audio_out=audio_out+synth_old[i]; //add sample to audio out accumulator
djbottrill 19:4cec60669626 170 synth[i]=synth[i]+synthidx[i]; //Get next sample pointer
djbottrill 17:7f27f4921305 171
djbottrill 19:4cec60669626 172 } //Next Note
djbottrill 17:7f27f4921305 173 } else {
djbottrill 17:7f27f4921305 174
djbottrill 17:7f27f4921305 175 //Sample Set 2
djbottrill 16:d2694fb530c3 176
djbottrill 19:4cec60669626 177 for (i =0; i<16; i++) { //Do 16 channels
djbottrill 17:7f27f4921305 178 switch (synthstat[i]) {
djbottrill 17:7f27f4921305 179 case 1: //Sustain phase
djbottrill 19:4cec60669626 180 if (synth[i]>sample2_loop_end[synthoctave[i]]) { //Got to end of buffer?
djbottrill 19:4cec60669626 181 synth[i]=sample2_loop_start[synthoctave[i]]; //Wrap around back to start
djbottrill 17:7f27f4921305 182 }
djbottrill 17:7f27f4921305 183 break;
djbottrill 17:7f27f4921305 184 case 2: //Note off demand state
djbottrill 19:4cec60669626 185 if (synth[i]>=sample2_attack[synthoctave[i]]) { //Delay decay phase if not past end of attack
djbottrill 17:7f27f4921305 186 //Check if the waveform is close to zero crossing this helps eliminate clicks looks for rising edge approaching 0x80
djbottrill 17:7f27f4921305 187 tempidx=synth[i];
djbottrill 17:7f27f4921305 188 if (sample2[synthoctave[i]][tempidx]<128 & sample2[synthoctave[i]][tempidx]>120) {
djbottrill 17:7f27f4921305 189 if (sample2[synthoctave[i]][tempidx]>synth_old[i]) {
djbottrill 19:4cec60669626 190 synth[i]=sample2_loop_off[synthoctave[i]]; //Jump to start of decay
djbottrill 19:4cec60669626 191 synthstat[i]=3; //Say note in decay
djbottrill 17:7f27f4921305 192 }
djbottrill 17:7f27f4921305 193 }
djbottrill 17:7f27f4921305 194 }
djbottrill 17:7f27f4921305 195 break;
djbottrill 17:7f27f4921305 196 case 3: //Check if decay has completed
djbottrill 19:4cec60669626 197 if (synth[i]>=sample2_len[synthoctave[i]]) { //End of decay?
djbottrill 19:4cec60669626 198 synthstat[i]=0; //say channel free
djbottrill 19:4cec60669626 199 synth[i]=0; //Set sample pointer to 0
djbottrill 19:4cec60669626 200 synthidx[i]=0; //Set sample index to 0
djbottrill 19:4cec60669626 201 synthtab[i]=255; //Set to invalid
djbottrill 17:7f27f4921305 202 }
djbottrill 17:7f27f4921305 203 break ;
djbottrill 17:7f27f4921305 204 }
djbottrill 17:7f27f4921305 205
djbottrill 17:7f27f4921305 206 //get sample and add to Audio Accumulator
djbottrill 19:4cec60669626 207 synth_old[i]=sample2[synthoctave[i]][(int)(synth[i])]; //Get and save old sample
djbottrill 19:4cec60669626 208 audio_out=audio_out+synth_old[i]; //add sample to audio out accumulator
djbottrill 19:4cec60669626 209 synth[i]=synth[i]+synthidx[i]; //Get next sample pointer
djbottrill 17:7f27f4921305 210
djbottrill 19:4cec60669626 211 } //Next Note
djbottrill 17:7f27f4921305 212 }
djbottrill 17:7f27f4921305 213
djbottrill 8:df5f305aab22 214
djbottrill 0:4d06e0867376 215 //Output to DAC
djbottrill 0:4d06e0867376 216 Aout.write_u16(audio_out*16);
djbottrill 7:e1ea1623c6c2 217
djbottrill 15:c1854904f0d7 218 // redled=1;
djbottrill 15:c1854904f0d7 219 // diag=0;
djbottrill 0:4d06e0867376 220 }
djbottrill 0:4d06e0867376 221
djbottrill 0:4d06e0867376 222 /*
djbottrill 0:4d06e0867376 223 Interrupt routine to receive MIDI on/off message and set LED status accordingly
djbottrill 0:4d06e0867376 224 MIDI note on/off events directly start/stop the synth channels and store the note status in keytab.
djbottrill 0:4d06e0867376 225 */
djbottrill 0:4d06e0867376 226 void get_message(MIDIMessage msg)
djbottrill 0:4d06e0867376 227 {
djbottrill 0:4d06e0867376 228 greenled=0; //Pulse Green LED to indicate MIDI activity
djbottrill 0:4d06e0867376 229 key=msg.key();
djbottrill 0:4d06e0867376 230 velocity=msg.velocity();
djbottrill 0:4d06e0867376 231 chan=msg.channel();
djbottrill 0:4d06e0867376 232 switch (msg.type()) {
djbottrill 0:4d06e0867376 233 case MIDIMessage::NoteOnType: //MIDI note on received
djbottrill 21:04febed27b55 234 if (key >23 & key<120) {
djbottrill 0:4d06e0867376 235 onoff=1;
djbottrill 23:31cb90fa661f 236 if (sw_status[0]==1 & key >35) {
djbottrill 13:decb94698003 237 keybuf[(key-36)]|= 1 << 0; //Store @ 16' pitch
djbottrill 12:74b52900cddb 238 }
djbottrill 23:31cb90fa661f 239 if (sw_status[1]==1) {
djbottrill 13:decb94698003 240 keybuf[(key-24)]|= 1 << 1; //Store @ 8' pitch
djbottrill 15:c1854904f0d7 241 }
djbottrill 23:31cb90fa661f 242 if (sw_status[2]==1& key<108) {
djbottrill 13:decb94698003 243 keybuf[(key)-12]|= 1 << 2; //Store @ 4' pitch
djbottrill 15:c1854904f0d7 244 }
djbottrill 23:31cb90fa661f 245 if (sw_status[3]==1 & key<101) {
djbottrill 13:decb94698003 246 keybuf[(key)-5]|= 1 << 3; //Store @ 2 2/3' pitch
djbottrill 15:c1854904f0d7 247 }
djbottrill 23:31cb90fa661f 248 if (sw_status[4]==1 & key <96) {
djbottrill 13:decb94698003 249 keybuf[(key)]|= 1 << 4; //Store @ 2' pitch
djbottrill 15:c1854904f0d7 250 }
djbottrill 0:4d06e0867376 251 }
djbottrill 6:073e10217962 252
djbottrill 0:4d06e0867376 253 break;
djbottrill 0:4d06e0867376 254
djbottrill 0:4d06e0867376 255 //Process keys off
djbottrill 0:4d06e0867376 256 case MIDIMessage::NoteOffType: //Midi note off received
djbottrill 21:04febed27b55 257 if (key >23 & key<120) {
djbottrill 0:4d06e0867376 258 onoff=0;
djbottrill 21:04febed27b55 259 if (key>35) {
djbottrill 21:04febed27b55 260 keybuf[(key-36)]&= ~(1 << 0); //Kill note @ 16' pitch
djbottrill 21:04febed27b55 261 }
djbottrill 15:c1854904f0d7 262 keybuf[(key-24)]&= ~(1 << 1); //Kill note @ 8' pitch
djbottrill 22:7d4e07edf503 263 if (key<108) {
djbottrill 21:04febed27b55 264 keybuf[(key-12)]&= ~(1 << 2); //Kill note @ 4' pitch
djbottrill 21:04febed27b55 265 }
djbottrill 21:04febed27b55 266 if (key<101) {
djbottrill 21:04febed27b55 267 keybuf[(key-5)]&= ~(1 << 3); //Kill note @ 2 2/3' pitch
djbottrill 21:04febed27b55 268 }
djbottrill 21:04febed27b55 269 if (key<96) {
djbottrill 21:04febed27b55 270 keybuf[(key)]&= ~(1 << 4); //Kill note @ 2' pitch
djbottrill 21:04febed27b55 271 }
djbottrill 0:4d06e0867376 272 }
djbottrill 0:4d06e0867376 273 break;
djbottrill 0:4d06e0867376 274
djbottrill 0:4d06e0867376 275 //Process all notes off command
djbottrill 0:4d06e0867376 276 case MIDIMessage::AllNotesOffType: //Midi all notes off
djbottrill 11:a27a71f7c149 277 for (int it=0; it<108; it++) {
djbottrill 11:a27a71f7c149 278 keybuf[it]=0; //Clear Keybuf
djbottrill 0:4d06e0867376 279 }
djbottrill 0:4d06e0867376 280 break;
djbottrill 4:9f63e68b548e 281
djbottrill 0:4d06e0867376 282 //Any other MIDI Commands just ignore
djbottrill 0:4d06e0867376 283 default:
djbottrill 0:4d06e0867376 284 break;
djbottrill 0:4d06e0867376 285 }
djbottrill 0:4d06e0867376 286 greenled=1;
djbottrill 0:4d06e0867376 287 }
djbottrill 0:4d06e0867376 288
djbottrill 0:4d06e0867376 289
djbottrill 0:4d06e0867376 290 int main()
djbottrill 0:4d06e0867376 291 {
djbottrill 0:4d06e0867376 292 greenled=1;
djbottrill 0:4d06e0867376 293 redled=1;
djbottrill 0:4d06e0867376 294 blueled=1;
djbottrill 0:4d06e0867376 295 SRCK=0; //Serial Clock low
djbottrill 0:4d06e0867376 296 RCK=0; //Latch Clock low
djbottrill 0:4d06e0867376 297
djbottrill 0:4d06e0867376 298 sw1.mode(PullUp);
djbottrill 0:4d06e0867376 299 sw3.mode(PullUp);
djbottrill 15:c1854904f0d7 300 p1.mode(PullUp);
djbottrill 15:c1854904f0d7 301 p2.mode(PullUp);
djbottrill 15:c1854904f0d7 302 p3.mode(PullUp);
djbottrill 15:c1854904f0d7 303 p4.mode(PullUp);
djbottrill 15:c1854904f0d7 304 p5.mode(PullUp);
djbottrill 15:c1854904f0d7 305
djbottrill 17:7f27f4921305 306 output_ticker.attach(&aout, sample1_rate); //Set Sample frequency
djbottrill 0:4d06e0867376 307
djbottrill 0:4d06e0867376 308 lcd.cls();
djbottrill 0:4d06e0867376 309 wait(0.1);
djbottrill 0:4d06e0867376 310 lcd.setCursor(TextLCD::CurOff_BlkOff);
djbottrill 0:4d06e0867376 311 lcd.cls();
djbottrill 0:4d06e0867376 312 lcd.printf("MIDI Pipe Organ Waiting for USB");
djbottrill 0:4d06e0867376 313
djbottrill 0:4d06e0867376 314 NVIC_SetPriority(USB0_IRQn, 99); //Reduce Interrupt priority of USB
djbottrill 0:4d06e0867376 315
djbottrill 0:4d06e0867376 316 USBMIDI midi; //Start up MIDI
djbottrill 0:4d06e0867376 317 midi.attach(get_message); //callback for MIDI messages received
djbottrill 0:4d06e0867376 318
djbottrill 0:4d06e0867376 319 lcd.cls();
djbottrill 0:4d06e0867376 320 lcd.printf("MIDI Pipe Organ");
djbottrill 0:4d06e0867376 321 wait(1);
djbottrill 0:4d06e0867376 322 lcd.cls();
djbottrill 0:4d06e0867376 323 lcd.printf("On Chan Note Vel");
djbottrill 0:4d06e0867376 324
djbottrill 0:4d06e0867376 325 key=0;
djbottrill 0:4d06e0867376 326 velocity=0;
djbottrill 0:4d06e0867376 327 chan=0;
djbottrill 0:4d06e0867376 328 onoff=0;
djbottrill 0:4d06e0867376 329
djbottrill 0:4d06e0867376 330 //Main Loop
djbottrill 0:4d06e0867376 331 while(1) {
djbottrill 0:4d06e0867376 332
djbottrill 11:a27a71f7c149 333 diag2=1;
djbottrill 11:a27a71f7c149 334 //Process notes off
djbottrill 11:a27a71f7c149 335 for (ii=0; ii<16; ii++) {
djbottrill 11:a27a71f7c149 336 if (synthstat[ii]==1 & keybuf[synthtab[ii]]==0) { //Note currently playing
djbottrill 11:a27a71f7c149 337 synthstat[ii]=2; //Set Start decay
djbottrill 11:a27a71f7c149 338 }
djbottrill 11:a27a71f7c149 339 }
djbottrill 11:a27a71f7c149 340 //Process keys on
djbottrill 11:a27a71f7c149 341 //Find a free note generator and start the note playing, if no slots available just ignore the note
djbottrill 12:74b52900cddb 342 for (keyidx=95; keyidx>=0; keyidx--) {
djbottrill 15:c1854904f0d7 343 if (keybuf[keyidx]>0 & oldkeybuf[keyidx]==0) { //Key is pressed
djbottrill 12:74b52900cddb 344 //Find and use any channel that is free
djbottrill 12:74b52900cddb 345 sucess=0;
djbottrill 11:a27a71f7c149 346 ii=0;
djbottrill 11:a27a71f7c149 347 while (ii<16) { //Find available synth channel
djbottrill 11:a27a71f7c149 348 if (synthstat[ii]==0) { //Is synth channel free?
djbottrill 11:a27a71f7c149 349 synthtab[ii]=keyidx; //Store note value
djbottrill 11:a27a71f7c149 350 synthoctave[ii]=synthtab[ii]/12; //Store the octave
djbottrill 16:d2694fb530c3 351 //Uncomment the following lines if you want to wrap around the top octave
djbottrill 16:d2694fb530c3 352 // if (synthoctave[ii]==7) { //Wrap around the top octave
djbottrill 15:c1854904f0d7 353 // synthoctave[ii]--;
djbottrill 15:c1854904f0d7 354 // }
djbottrill 11:a27a71f7c149 355 synthnote[ii]= synthtab[ii]%12; //Note within the octave
djbottrill 11:a27a71f7c149 356 synthstat[ii]=1; //Set status to playing
djbottrill 11:a27a71f7c149 357 synthidx[ii]=freqtab[synthnote[ii]]; //Set the frequency
djbottrill 12:74b52900cddb 358 sucess=1;
djbottrill 11:a27a71f7c149 359 ii=16; //exit loop
djbottrill 11:a27a71f7c149 360 } //Next Synth slot
djbottrill 11:a27a71f7c149 361 ii++;
djbottrill 11:a27a71f7c149 362 }
djbottrill 12:74b52900cddb 363 //As a last resort find any channel that is in decay
djbottrill 12:74b52900cddb 364 if (sucess==0) {
djbottrill 12:74b52900cddb 365 ii=0;
djbottrill 15:c1854904f0d7 366 while (ii<16) { //Find available synth channel
djbottrill 15:c1854904f0d7 367 if (synthstat[ii]>1) { //Is synth channel free?
djbottrill 15:c1854904f0d7 368 synthtab[ii]=keyidx; //Store note value
djbottrill 15:c1854904f0d7 369 synthoctave[ii]=synthtab[ii]/12; //Store the octave
djbottrill 15:c1854904f0d7 370 synthnote[ii]= synthtab[ii]%12; //Note within the octave
djbottrill 15:c1854904f0d7 371 synthstat[ii]=1; //Set status to playing
djbottrill 15:c1854904f0d7 372 synthidx[ii]=freqtab[synthnote[ii]]; //Set the frequency
djbottrill 12:74b52900cddb 373 sucess=1;
djbottrill 15:c1854904f0d7 374 ii=16; //exit loop
djbottrill 15:c1854904f0d7 375 } //Next Synth slot
djbottrill 12:74b52900cddb 376 ii++;
djbottrill 12:74b52900cddb 377 }
djbottrill 12:74b52900cddb 378 }
djbottrill 11:a27a71f7c149 379 }
djbottrill 12:74b52900cddb 380 if (sucess==1) {
djbottrill 16:d2694fb530c3 381 oldkeybuf[keyidx]=keybuf[keyidx]; //Store old value if note sucessfull
djbottrill 12:74b52900cddb 382 }
djbottrill 11:a27a71f7c149 383 }
djbottrill 11:a27a71f7c149 384 diag2=0;
djbottrill 11:a27a71f7c149 385 wait_ms(1);
djbottrill 15:c1854904f0d7 386
djbottrill 15:c1854904f0d7 387
djbottrill 15:c1854904f0d7 388 //Output to Shift register
djbottrill 15:c1854904f0d7 389 for (keyidx=107; keyidx>11; keyidx--) {
djbottrill 15:c1854904f0d7 390 if (keybuf[keyidx]>0) {
djbottrill 15:c1854904f0d7 391 SER1=1;
djbottrill 15:c1854904f0d7 392 } else {
djbottrill 15:c1854904f0d7 393 SER1=0;
djbottrill 15:c1854904f0d7 394 }
djbottrill 15:c1854904f0d7 395 SRCK=1; //Pulse Serial clock
djbottrill 15:c1854904f0d7 396 wait_us(4);
djbottrill 15:c1854904f0d7 397 SRCK=0;
djbottrill 15:c1854904f0d7 398 wait_us(4);
djbottrill 15:c1854904f0d7 399 } //Next bit
djbottrill 15:c1854904f0d7 400
djbottrill 15:c1854904f0d7 401 RCK=1; //Transfer data to outputs
djbottrill 15:c1854904f0d7 402 wait_us(4);
djbottrill 15:c1854904f0d7 403 RCK=0;
djbottrill 15:c1854904f0d7 404 wait_us(4);
djbottrill 15:c1854904f0d7 405
djbottrill 16:d2694fb530c3 406 //Read Stop inputs
djbottrill 23:31cb90fa661f 407 sw[0]=p1;
djbottrill 23:31cb90fa661f 408 sw[1]=p2;
djbottrill 23:31cb90fa661f 409 sw[2]=p3;
djbottrill 23:31cb90fa661f 410 sw[3]=p4;
djbottrill 23:31cb90fa661f 411 sw[4]=p5;
djbottrill 23:31cb90fa661f 412 sw[6]=sw1;
djbottrill 23:31cb90fa661f 413 sw[7]=sw3;
djbottrill 15:c1854904f0d7 414
djbottrill 16:d2694fb530c3 415 //Additional code to de-bounce the K64F onboard switches again using multiple loops to de-bounce; has 3 states:
djbottrill 16:d2694fb530c3 416 //0 - Button not pressed
djbottrill 16:d2694fb530c3 417 //1 - Button pressed waiting for de-bounce count of 10 loops
djbottrill 16:d2694fb530c3 418 //2 - Button press stable, this status can be read as the de-bounced status indicating the button press is clean
djbottrill 16:d2694fb530c3 419
djbottrill 23:31cb90fa661f 420 // De-bounce Switches
djbottrill 23:31cb90fa661f 421 pressed=0;
djbottrill 23:31cb90fa661f 422 for (swi=0; swi<8; swi++) { //Process 8 witches
djbottrill 23:31cb90fa661f 423 if (sw_phase[swi]==0 & sw[swi]==0) { //Button just been pressed
djbottrill 23:31cb90fa661f 424 sw_phase[swi]=1;
djbottrill 23:31cb90fa661f 425 sw_count[swi]=10;
djbottrill 23:31cb90fa661f 426 }
djbottrill 23:31cb90fa661f 427 if (sw_phase[swi]==1 & sw_count[swi]==0 & sw[swi]==0) { //Button still pressed after de-bounce period
djbottrill 23:31cb90fa661f 428 sw_phase[swi]=2;
djbottrill 23:31cb90fa661f 429 pressed++; //Inc button pressed count
djbottrill 23:31cb90fa661f 430 sw_status[swi]=!sw_status[swi]; //Toggle switch status
djbottrill 23:31cb90fa661f 431 }
djbottrill 23:31cb90fa661f 432 if (sw_phase[swi]==1 & sw_count[swi]==0 & sw[swi]==1) { //Button no longer pressed after de-bounce period
djbottrill 23:31cb90fa661f 433 sw_phase[swi]=0;
djbottrill 23:31cb90fa661f 434 }
djbottrill 23:31cb90fa661f 435 if (sw_phase[swi]==2 & sw[swi]==1) { //Button released
djbottrill 23:31cb90fa661f 436 sw_phase[swi]=0;
djbottrill 23:31cb90fa661f 437 }
djbottrill 23:31cb90fa661f 438 sw_count[swi]--;
djbottrill 6:073e10217962 439 }
djbottrill 23:31cb90fa661f 440 //light blue LED if any button presed
djbottrill 23:31cb90fa661f 441 if (pressed>0) {
djbottrill 0:4d06e0867376 442 blueled=0;
djbottrill 0:4d06e0867376 443 } else {
djbottrill 0:4d06e0867376 444 blueled=1;
djbottrill 0:4d06e0867376 445 }
djbottrill 0:4d06e0867376 446
djbottrill 0:4d06e0867376 447
djbottrill 17:7f27f4921305 448 //Button 1 will clear all playing notes & switch sampleset
djbottrill 23:31cb90fa661f 449 if (sw_status[6] != sw_old[6]) {
djbottrill 9:b0f110c02b1b 450 for (cl=0; cl<108; cl++) {
djbottrill 17:7f27f4921305 451 keybuf[cl]=0; //Clear Keybuf
djbottrill 17:7f27f4921305 452 }
djbottrill 23:31cb90fa661f 453 sampleset=sw_status[6];
djbottrill 17:7f27f4921305 454 oldstops=255; //Force a display refresh
djbottrill 23:31cb90fa661f 455 sw_old[6]=sw_status[6];
djbottrill 9:b0f110c02b1b 456 }
djbottrill 22:7d4e07edf503 457
djbottrill 21:04febed27b55 458
djbottrill 0:4d06e0867376 459 //Check for display mode button being presed
djbottrill 23:31cb90fa661f 460 if (sw_status[7] != sw_old[7]) { //Mode Switch pressed
djbottrill 0:4d06e0867376 461 lcdmode++;
djbottrill 16:d2694fb530c3 462 if (lcdmode>4) {
djbottrill 0:4d06e0867376 463 lcdmode=0;
djbottrill 0:4d06e0867376 464 }
djbottrill 0:4d06e0867376 465 lcd.cls();
djbottrill 0:4d06e0867376 466 if (lcdmode==0) {
djbottrill 0:4d06e0867376 467 lcd.printf("On Chan Note Vel");
djbottrill 0:4d06e0867376 468 }
djbottrill 0:4d06e0867376 469 if (lcdmode==1) {
djbottrill 0:4d06e0867376 470 //Initialise bar graph display
djbottrill 10:fa2dee836ceb 471 //Program user defined characters into OLED display for bar graph
djbottrill 10:fa2dee836ceb 472 lcd.setUDC(0, (char *) udc0);
djbottrill 10:fa2dee836ceb 473 lcd.setUDC(1, (char *) udc1);
djbottrill 10:fa2dee836ceb 474 lcd.setUDC(2, (char *) udc2);
djbottrill 10:fa2dee836ceb 475 lcd.setUDC(3, (char *) udc3);
djbottrill 10:fa2dee836ceb 476 lcd.setUDC(4, (char *) udc4);
djbottrill 10:fa2dee836ceb 477 lcd.setUDC(5, (char *) udc5);
djbottrill 10:fa2dee836ceb 478 lcd.setUDC(6, (char *) udc6);
djbottrill 10:fa2dee836ceb 479 lcd.setUDC(7, (char *) udc7);
djbottrill 0:4d06e0867376 480 for (row=0; row<2; row++) {
djbottrill 0:4d06e0867376 481 for (pos=0; pos<16; pos++) {
djbottrill 0:4d06e0867376 482 display[row][pos]=0;
djbottrill 0:4d06e0867376 483 display_old[row][pos]=1;
djbottrill 0:4d06e0867376 484 }
djbottrill 0:4d06e0867376 485 }
djbottrill 0:4d06e0867376 486 }
djbottrill 0:4d06e0867376 487 if (lcdmode==2) {
djbottrill 0:4d06e0867376 488 lcd.locate(0,1);
djbottrill 0:4d06e0867376 489 lcd.printf("0123456789ABCDEF");
djbottrill 0:4d06e0867376 490 }
djbottrill 8:df5f305aab22 491 if (lcdmode==3) {
djbottrill 8:df5f305aab22 492 lcd.cls();
djbottrill 8:df5f305aab22 493 lcd.locate(0,0);
djbottrill 8:df5f305aab22 494 lcd.printf("Utilisation:");
djbottrill 8:df5f305aab22 495 }
djbottrill 16:d2694fb530c3 496 if (lcdmode==4) {
djbottrill 16:d2694fb530c3 497 oldstops=255; //Force a refresh
djbottrill 16:d2694fb530c3 498 }
djbottrill 23:31cb90fa661f 499 sw_old[7]=sw_status[7];
djbottrill 0:4d06e0867376 500 }
djbottrill 22:7d4e07edf503 501
djbottrill 0:4d06e0867376 502
djbottrill 0:4d06e0867376 503
djbottrill 0:4d06e0867376 504
djbottrill 0:4d06e0867376 505 // OLED MIDI Status display
djbottrill 0:4d06e0867376 506 if (lcdmode==0) {
djbottrill 0:4d06e0867376 507 lcd_stat=key+(128*velocity)+(16384*chan)+(262144*onoff);
djbottrill 0:4d06e0867376 508 if (lcd_stat!=lcd_stat_old) {
djbottrill 0:4d06e0867376 509
djbottrill 0:4d06e0867376 510 lcd.locate(0,1);
djbottrill 0:4d06e0867376 511 lcd.printf("%1d %2d %3d %3d", onoff, chan, key, velocity);
djbottrill 0:4d06e0867376 512 lcd_stat_old=lcd_stat;
djbottrill 0:4d06e0867376 513 }
djbottrill 0:4d06e0867376 514 }
djbottrill 0:4d06e0867376 515
djbottrill 0:4d06e0867376 516 // OLED MIDI Bar Display
djbottrill 0:4d06e0867376 517 if (lcdmode==1) {
djbottrill 0:4d06e0867376 518 keyidx=0;
djbottrill 0:4d06e0867376 519 for (row=1; row>=0; row--) {
djbottrill 0:4d06e0867376 520 for (pos=0; pos<16; pos++) {
djbottrill 0:4d06e0867376 521 keyidx_t=keyidx*2;
djbottrill 0:4d06e0867376 522 if(keyidx_t>94) {
djbottrill 0:4d06e0867376 523 keyidx_t=keyidx_t-95;
djbottrill 0:4d06e0867376 524 }
djbottrill 15:c1854904f0d7 525 tval=keybuf[keyidx_t];
djbottrill 12:74b52900cddb 526 if(tval>0) {
djbottrill 12:74b52900cddb 527 tval=1;
djbottrill 12:74b52900cddb 528 }
djbottrill 0:4d06e0867376 529 keyidx++;
djbottrill 0:4d06e0867376 530 keyidx_t=keyidx*2;
djbottrill 0:4d06e0867376 531 if(keyidx_t>94) {
djbottrill 0:4d06e0867376 532 keyidx_t=keyidx_t-95;
djbottrill 0:4d06e0867376 533 }
djbottrill 15:c1854904f0d7 534 tval1=keybuf[keyidx_t];
djbottrill 12:74b52900cddb 535 if (tval1>0) {
djbottrill 12:74b52900cddb 536 tval1=1;
djbottrill 12:74b52900cddb 537 }
djbottrill 12:74b52900cddb 538 tval=tval+(2*tval1);
djbottrill 0:4d06e0867376 539 keyidx++;
djbottrill 0:4d06e0867376 540
djbottrill 0:4d06e0867376 541 keyidx_t=keyidx*2;
djbottrill 0:4d06e0867376 542 if(keyidx_t>94) {
djbottrill 0:4d06e0867376 543 keyidx_t=keyidx_t-95;
djbottrill 0:4d06e0867376 544 }
djbottrill 15:c1854904f0d7 545 tval1=keybuf[keyidx_t];
djbottrill 12:74b52900cddb 546 if (tval1>0) {
djbottrill 12:74b52900cddb 547 tval1=1;
djbottrill 12:74b52900cddb 548 }
djbottrill 12:74b52900cddb 549 tval=tval+(4*tval1);
djbottrill 0:4d06e0867376 550 keyidx++;
djbottrill 0:4d06e0867376 551 display[row][pos]=tval;
djbottrill 0:4d06e0867376 552
djbottrill 0:4d06e0867376 553 if(display[row][pos]!=display_old[row][pos]) {
djbottrill 0:4d06e0867376 554 lcd.locate(pos,row);
djbottrill 0:4d06e0867376 555 lcd.putc(display[row][pos]);
djbottrill 0:4d06e0867376 556 display_old[row][pos]=display[row][pos];
djbottrill 0:4d06e0867376 557 }
djbottrill 0:4d06e0867376 558 }
djbottrill 0:4d06e0867376 559 }
djbottrill 0:4d06e0867376 560 }
djbottrill 0:4d06e0867376 561
djbottrill 0:4d06e0867376 562 //Display status of the Synth channels
djbottrill 0:4d06e0867376 563 if (lcdmode==2) {
djbottrill 0:4d06e0867376 564 lcd.locate(0,0);
djbottrill 0:4d06e0867376 565 for (noteidx=0; noteidx<16; noteidx++) {
djbottrill 0:4d06e0867376 566 lcd.putc((synthstat[noteidx]+48));
djbottrill 0:4d06e0867376 567 }
djbottrill 0:4d06e0867376 568 }
djbottrill 4:9f63e68b548e 569
djbottrill 8:df5f305aab22 570 //Display utilisation bar graph
djbottrill 8:df5f305aab22 571 if (lcdmode==3) {
djbottrill 8:df5f305aab22 572 x=0;
djbottrill 8:df5f305aab22 573 for (noteidx=0; noteidx<16; noteidx++) {
djbottrill 8:df5f305aab22 574 if (synthstat[noteidx] >0) {
djbottrill 8:df5f305aab22 575 x++;
djbottrill 8:df5f305aab22 576 }
djbottrill 8:df5f305aab22 577 }
djbottrill 8:df5f305aab22 578 for (pos=0; pos<x; pos++) {
djbottrill 8:df5f305aab22 579 display[1][pos]=0xff;
djbottrill 8:df5f305aab22 580 }
djbottrill 8:df5f305aab22 581 while(pos<16) {
djbottrill 8:df5f305aab22 582 display[1][pos]=0x20;
djbottrill 8:df5f305aab22 583 pos++;
djbottrill 8:df5f305aab22 584 }
djbottrill 8:df5f305aab22 585 row=1;
djbottrill 8:df5f305aab22 586 for (pos=0; pos<16; pos++) {
djbottrill 8:df5f305aab22 587 if (x!=x1) {
djbottrill 8:df5f305aab22 588 lcd.locate(13,0);
djbottrill 8:df5f305aab22 589 lcd.printf("%2d", x);
djbottrill 8:df5f305aab22 590 x1=x;
djbottrill 8:df5f305aab22 591 }
djbottrill 8:df5f305aab22 592 if(display[row][pos]!=display_old[row][pos]) {
djbottrill 8:df5f305aab22 593 lcd.locate(pos,row);
djbottrill 8:df5f305aab22 594 lcd.putc(display[row][pos]);
djbottrill 8:df5f305aab22 595 display_old[row][pos]=display[row][pos];
djbottrill 8:df5f305aab22 596 }
djbottrill 8:df5f305aab22 597 }
djbottrill 8:df5f305aab22 598 }
djbottrill 16:d2694fb530c3 599 //Stop display
djbottrill 16:d2694fb530c3 600 if (lcdmode==4) {
djbottrill 8:df5f305aab22 601
djbottrill 23:31cb90fa661f 602 pos=sw_status[0] + sw_status[1]+sw_status[2]+sw_status[3]+sw_status[4];
djbottrill 16:d2694fb530c3 603 if (oldstops != pos) {
djbottrill 17:7f27f4921305 604 oldstops=pos;
djbottrill 16:d2694fb530c3 605 lcd.cls();
djbottrill 16:d2694fb530c3 606 for (pos=0; pos<16; pos++) {
djbottrill 16:d2694fb530c3 607 lcd.locate(pos,0);
djbottrill 17:7f27f4921305 608 if (sampleset==0) {
djbottrill 17:7f27f4921305 609 lcd.putc(stopname1[pos]);
djbottrill 17:7f27f4921305 610 } else {
djbottrill 17:7f27f4921305 611 lcd.putc(stopname2[pos]);
djbottrill 17:7f27f4921305 612 }
djbottrill 16:d2694fb530c3 613 }
djbottrill 16:d2694fb530c3 614 lcd.locate(0,1);
djbottrill 23:31cb90fa661f 615 if (sw_status[0]==1) {
djbottrill 16:d2694fb530c3 616 lcd.printf("16 ");
djbottrill 16:d2694fb530c3 617 }
djbottrill 16:d2694fb530c3 618
djbottrill 23:31cb90fa661f 619 if (sw_status[1]==1) {
djbottrill 16:d2694fb530c3 620 lcd.printf("8 ");
djbottrill 16:d2694fb530c3 621 }
djbottrill 16:d2694fb530c3 622
djbottrill 23:31cb90fa661f 623 if (sw_status[2]==1) {
djbottrill 16:d2694fb530c3 624 lcd.printf("4 ");
djbottrill 16:d2694fb530c3 625 }
djbottrill 16:d2694fb530c3 626
djbottrill 23:31cb90fa661f 627 if (sw_status[3]==1) {
djbottrill 16:d2694fb530c3 628 lcd.printf("2-2/3 ");
djbottrill 16:d2694fb530c3 629 }
djbottrill 16:d2694fb530c3 630
djbottrill 23:31cb90fa661f 631 if (sw_status[4]==1) {
djbottrill 16:d2694fb530c3 632 lcd.printf("2");
djbottrill 16:d2694fb530c3 633 }
djbottrill 16:d2694fb530c3 634 }
djbottrill 16:d2694fb530c3 635 }
djbottrill 8:df5f305aab22 636
djbottrill 0:4d06e0867376 637 } //End of main loop
djbottrill 0:4d06e0867376 638 } //End of program
djbottrill 0:4d06e0867376 639
djbottrill 0:4d06e0867376 640
djbottrill 0:4d06e0867376 641
djbottrill 0:4d06e0867376 642