possibly simultaneously

Dependencies:   TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG

Revision:
3:3a2c11f459c4
Parent:
2:58378cb7e76f
--- a/main.cpp	Sat May 04 08:46:30 2019 +0000
+++ b/main.cpp	Sat May 04 09:19:10 2019 +0000
@@ -34,7 +34,7 @@
 {  
     lcd.Clear(LCD_COLOR_BLUE);
     ee.attach(&screen, 0.03);
-    re.attach(&play, 0.5);//เล่นตามtempo
+    re.attach(&play, speed);//เล่นตามtempo
     er.attach(&test, 0.25);//กดช้ากดเร็วแล้วแต่
     //rr.attach(&knob,0.5);//readknob ทุก0.5
    
@@ -90,7 +90,6 @@
 }
 void kick44_sample ()
 {
- 
     speaker = kick44_data[l]/16777215.0;//scale to 0.0 to 1.0 for PWM
     l++;
     if (l>= num_kick44) {
@@ -99,53 +98,69 @@
         //led = 0;
     }
 }
-
+/////////////////////////////////////////////////////////////////////////////  PLAY   PART //////////////////////////////////////////////
 void playsnare()
 {
-    speaker.period(1.0/330750.0); //PWM freq >10X audio sample rate
+    speaker = sn44_data[h]/16777215.0;//scale to 0.0 to 1.0 for PWM
+    h++;
+    speaker.period(1.0/330750.0); //PWM freq >10X audio sample rate        
     
-
-        sn44tick.attach(&sn44_sample, 1.0 / 33075);
-
-        wait(0.5);
+    if (h>= num_sn44) {
+        h = 0;
+        sn44tick.detach();
+        }
     
 }
+
 void playkick()
 {
+    speaker = kick44_data[l]/16777215.0;//scale to 0.0 to 1.0 for PWM
+    l++;
     speaker.period(1.0/441000.0); //PWM freq >10X audio sample rate
-
-
-        kick44tick.attach(&kick44_sample, 1.0 / 33075);
-
-        wait(0.2);
+    
+     if (l>= num_kick44) {
+        l = 0;
+        kick44tick.detach();
+        }
     
 }
 void playclap()
 {
+    speaker = cl44_data[i]/16777215.0;//scale to 0.0 to 1.0 for PWM
+    i++;
     speaker.period(1.0/220500.0); //PWM freq >10X audio sample rate
     
-
-        cl44tick.attach(&cl44_sample, 1.0 /22050);
+    if (i>= num_cl44) {
+        i = 0;
+        cl44tick.detach();
+        }
 
-        wait(0.5);
-    
 }
+
 void playclhh()
 {
+    speaker = clhh44_data[j]/16777215.0;//scale to 0.0 to 1.0 for PWM
+    j++;
     speaker.period(1.0/330750.0); //PWM freq >10X audio sample rate
     
-        clhh44tick.attach(&clhh44_sample, 1.0 / 33075);
+     if (j>= num_clhh44) {
+        j = 0;
+        clhh44tick.detach();
+        }
+        
+}
 
-        wait(0.5);
-    
-}
 void playophh()
 {
+    speaker = ophh44_data[k]/16777215.0;//scale to 0.0 to 1.0 for PWM
+    k++;
     speaker.period(1.0/330750.0); //PWM freq >10X audio sample rate
-        ophh44tick.attach(&ophh44_sample, 1.0 / 33075);
-
-        wait(0.2);
     
+    if (k>= num_ophh44) {
+        k = 0;
+        ophh44tick.detach();
+        }
+        
 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 void play() 
@@ -161,41 +176,280 @@
             
             if(tracking==1)
             {
-                               
-                
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][0]==1)
+                    {
+                        pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][0]==1)
+                    {
+                        pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][0]==1)
+                    {
+                        pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][0]==1)
+                    {
+                        pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][0]==1)
+                    {
+                        pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
+                         
             }
             else if(tracking==2)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][1]==1)
+                    {
+                        //pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][1]==1)
+                    {
+                        //pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][1]==1)
+                    {
+                        //pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][1]==1)
+                    {
+                        //pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][1]==1)
+                    {
+                        //pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==3)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][2]==1)
+                    {
+                        //pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][2]==1)
+                    {
+                        //pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][2]==1)
+                    {
+                        //pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][2]==1)
+                    {
+                        //pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][2]==1)
+                    {
+                        //pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==4)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][3]==1)
+                    {
+                        //pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][3]==1)
+                    {
+                        //pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][3]==1)
+                    {
+                        //pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][3]==1)
+                    {
+                        //pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][3]==1)
+                    {
+                        //pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==5)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][4]==1)
+                    {
+                        //pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][4]==1)
+                    {
+                        //pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][4]==1)
+                    {
+                        //pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][4]==1)
+                    {
+                        //pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][4]==1)
+                    {
+                        //pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==6)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][5]==1)
+                    {
+                        //pc.printf("1");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][5]==1)
+                    {
+                        //pc.printf("2");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][5]==1)
+                    {
+                        //pc.printf("3");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][5]==1)
+                    {
+                        //pc.printf("4");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][5]==1)
+                    {
+                        //pc.printf("5");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==7)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][0]==1)
+                    {
+                        //pc.printf("6");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][0]==1)
+                    {
+                        //pc.printf("6");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][0]==1)
+                    {
+                        //pc.printf("6");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][0]==1)
+                    {
+                        //pc.printf("6");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][0]==1)
+                    {
+                        //pc.printf("6");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
             else if(tracking==8)
             {
+                sn44tick.detach();
+                cl44tick.detach();
+                clhh44tick.detach();
+                ophh44tick.detach();
+                kick44tick.detach();
+                if(beat[0][0]==1)
+                    {
+                        //pc.printf("7");
+                        kick44tick.attach(&playkick,1.0 / 33075);
+                    }
+                if(beat[1][0]==1)
+                    {
+                        //pc.printf("7");
+                        sn44tick.attach(&playsnare,1.0 / 33075);
+                    }
+                if(beat[2][0]==1)
+                    {
+                        //pc.printf("7");
+                        cl44tick.attach(&playclap,1.0 /22050);
+                    }
+                if(beat[3][0]==1)
+                    {
+                        //pc.printf("7");
+                        clhh44tick.attach(&playclhh,1.0 / 33075);
+                    }
+                if(beat[4][0]==1)
+                    {
+                        //pc.printf("7");
+                        ophh44tick.attach(&playophh,1.0 / 33075);
+                    }                        
             
                 
             }
@@ -371,7 +625,9 @@
   sprintf(tempostr,"%1.1f",tempo);  
   sprintf(volumechar,"%1.1f",volume);  
   pc.printf("tempo =  %s \n\r",tempostr);  //print ในuart ถูก
-  pc.printf("volume =  %s\n\r",volumechar);  
+  pc.printf("volume =  %s\n\r",volumechar);
+  speed=floor(tempo); 
+  speed=(60/speed)/2;
     
 }