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

Revision:
4:9f63e68b548e
Parent:
3:bc04c8daa46a
Child:
5:13a1e5e4494d
--- a/main.cpp	Thu Oct 29 16:28:51 2015 +0000
+++ b/main.cpp	Thu Oct 29 17:45:52 2015 +0000
@@ -95,8 +95,8 @@
 int synth_old[16];
 int synthtab[16]= {};
 int synthstat[16]= {};
-unsigned char synthoctave[16]={};
-unsigned char synthnote[16]={};
+unsigned char synthoctave[16]= {};
+unsigned char synthnote[16]= {};
 int noteidx=0;
 int i;
 int ii;
@@ -120,42 +120,38 @@
 0 - Button not pressed
 1 - Button pressed waiting for de-bounce count of 100 ticks (4.53 mS)
 2 - Button press stable, this staus can be read as the de-bounced status indicating the button press is clean
-3 - Button released waiting for de-bounce count of 100 ticks before setting status back to 0  
+3 - Button released waiting for de-bounce count of 100 ticks before setting status back to 0
 */
 void aout()
 {
-    diag=1;                                                                                             //Pulse DIAG Pin for Scoping purposes IE pin high 
+    diag=1;                                                                                             //Pulse DIAG Pin for Scoping purposes IE pin high
     redled=0;                                                                                           //Pulse Red LED to indicate Ticker working
     audio_out=0;                                                                                        //Clear Audio Accumulator
 
     SER1=keybuf[outidx+12];                                                                             //Send bit serially
 
     for (i =0; i<16; i++) {                                                                             //Do 16 channels
-//check if channel idle
-        if(synthstat[i]>0) {
-//Check is decay needs to start if close to 0 crossing
-            if (synthstat[i]==2) {
+        switch (synthstat[i]) {
+            case 1:     //Sustain phase
+                if (synth[i]>sample_loop_end[synthoctave[i]]) {                                         //Got to end of buffer?
+                    synth[i]=sample_loop_start[synthoctave[i]];                                         //Wrap around back to start
+                }
+                break;
+            case 2:     //Note off demand state            
                 if (synth[i]>=sample_attack[synthoctave[i]]) {                                          //Delay decay phase if not past end of attack
 //Check if the waveform is close to zero crossing this helps eliminate clicks looks for rising edge approaching 0x80
                     tempidx=synth[i];
-                    if (sample[synthoctave[i]][tempidx]<128) {
+                    if (sample[synthoctave[i]][tempidx]<128 & sample[synthoctave[i]][tempidx]>120) {    
                         if (sample[synthoctave[i]][tempidx]>synth_old[i]) {
-                            if (sample[synthoctave[i]][tempidx]>120) {
                                 synth[i]=sample_loop_off[synthoctave[i]];                               //Jump to start of decay
                                 synthstat[i]=3;                                                         //Say note in decay
-                            }
                         }
                     }
                 }
-            }
-//Check and process sustain phase
-            if (synthstat[i]==1) {                                                                      //Are we in sustain phase
-                if (synth[i]>sample_loop_end[synthoctave[i]]) {                                         //Got to end of buffer?
-                    synth[i]=sample_loop_start[synthoctave[i]];                                         //Wrap around back to start
-                }
-            }
+                break;
         }
-//get sample add to Audio accumulator
+
+//get sample and add to Audio Accumulator
         tempidx=synth[i];                                                                               //Convert to integer
         synth_old[i]=sample[synthoctave[i]][tempidx];                                                   //Get and save old sample
         audio_out=audio_out+synth_old[i];                                                               //add sample to audio out accumulator
@@ -170,19 +166,26 @@
         }
 
 //Generate clock pulses to send data serially
-        if(i==2) {
-            SRCK=1;                                                                                     //Generate serial output clock pulse
-        }
-        if(i==5) {
-            SRCK=0;
+        switch (i) {
+            case 2:
+                SRCK=1;                                                                                 //Generate serial output clock pulse
+                break;
+            case 5:
+                SRCK=0;
+                break;
+            case 10:
+                if (outidx==0) {
+                    RCK=1;                                                                              //Generate latch strobe after 96 bits sent
+                }
+                break;
+            case 15:
+                if (outidx==0) {
+                    RCK=0;                                                                              //Generate latch strobe after 96 bits sent
+                    outidx=96;
+                }
+                break;
         }
-        if (outidx==0 and i==10) {
-            RCK=1;                                                                                      //Generate latch strobe after 96 bits sent
-        }
-        if (outidx==0 and i==15) {
-            RCK=0;                                                                                      //Generate latch strobe after 96 bits sent
-            outidx=96;
-        }
+
     }                                                                                                   //Next Note
     outidx--;                                                                                           //Next serial data bit
 
@@ -255,7 +258,7 @@
                     if (synthtab[ii]!=(key-24) && synthstat[ii]==0) {                                   //Is synth already playing the note?
                         synthtab[ii]=key-24;                                                            //Store note value
                         synthoctave[ii]=synthtab[ii]/12;                                                //Store the octave
-                        synthnote[ii]= synthtab[ii]%12;                                                 //Note within the octave        
+                        synthnote[ii]= synthtab[ii]%12;                                                 //Note within the octave
                         synthstat[ii]=1;                                                                //Set status to playing
                         synthidx[ii]=freqtab[synthnote[ii]];                                            //Set the frequency
                         ii=16;                                                                          //exit loop
@@ -290,7 +293,7 @@
                 synthtab[ii]=255;                                                                       //Set to invalid
             }
             break;
-            
+
 //Any other MIDI Commands just ignore
         default:
             break;
@@ -437,7 +440,7 @@
                 lcd.putc((synthstat[noteidx]+48));
             }
         }
-        
+
     }                                                                                                   //End of main loop
 }                                                                                                       //End of program