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:
Thu Nov 12 22:48:31 2015 +0000
Revision:
17:7f27f4921305
Parent:
16:d2694fb530c3
Child:
18:472954b6b711
Corrupted samples

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