-florian66-

sing sing sing

Apr 17th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.06 KB | None | 0 0
  1. /*****************************************************************/
  2. /*                                                               */
  3. /*   CASIO fx-9860G SDK Library                                  */
  4. /*                                                               */
  5. /*   File name : [ProjectName].c                                 */
  6. /*                                                               */
  7. /*   Copyright (c) 2006 CASIO COMPUTER CO., LTD.                 */
  8. /*                                                               */
  9. /*****************************************************************/
  10.  
  11. #include "fxlib.h"
  12. #include "serial.h"
  13. #include "MonochromeLib.h"                                  // FICHIER DEFINISSANT LES FONCTIONS GRAPHIQUES
  14. #include "stdio.h"
  15. #include "stdlib.h"
  16. #include "string.h"
  17. #include "SH4comp.c"
  18. //#include "math.h"
  19.  
  20.  
  21. //****************************************************************************
  22. //  AddIn_main (Sample program main function)
  23. //
  24. //  param   :   isAppli   : 1 = This application is launched by MAIN MENU.
  25. //                        : 0 = This application is launched by a strip in eACT application.
  26. //
  27. //              OptionNum : Strip number (0~3)
  28. //                         (This parameter is only used when isAppli parameter is 0.)
  29. //
  30. //  retval  :   1 = No error / 0 = Error
  31. //
  32. //****************************************************************************
  33.  
  34.  // une fonction qui affiche la fequence=>son (faite) et autre qui fait note =>frequence (faite)
  35.  // *(array+octave*12+demi_ton)
  36.  
  37. //#define NB_DEMI_TON 12
  38. #define r 1.059463094
  39.  
  40. char *notes[12]=
  41. {
  42.         "do",
  43.         "do#",
  44.         "re",
  45.         "re#",
  46.         "mi",
  47.         "fa",
  48.         "fa#",
  49.         "sol",
  50.         "sol#",
  51.         "la",
  52.         "la#",
  53.         "si"
  54. };
  55.  
  56. float power(float x, int y)
  57. {
  58.     float temp;
  59.     if( y == 0)
  60.        return 1;
  61.     temp = power(x, y/2);      
  62.     if (y%2 == 0)
  63.         return temp*temp;
  64.     else
  65.     {
  66.         if(y > 0)
  67.             return x*temp*temp;
  68.         else
  69.             return (temp*temp)/x;
  70.     }
  71. }  
  72.  
  73. short note=9; //correspond au la 3
  74. short octave=3;
  75. double tempo=60.0;
  76.  
  77. double calcul_frequence()
  78. {
  79. double frequence;
  80. //short nb_de_demi_ton=0;
  81. char buffer[50];
  82. unsigned int key;
  83.  
  84. while(1)
  85. {
  86.      Bdisp_AllClr_DDVRAM();
  87.        
  88.     sprintf(buffer,"note : %s",notes[note]);
  89.     PrintMini(10,30,buffer,0);
  90.        
  91.     sprintf(buffer,"%d",octave);
  92.     PrintMini(37+5*strlen(notes[note]),32,buffer,0);
  93.        
  94.     sprintf(buffer,"freq : %.2f",440*power(r,12*octave+note-45));
  95.     PrintMini(10,20,buffer,0);
  96.    
  97.     sprintf(buffer,"tempo : %.0f",tempo);
  98.     PrintMini(10,40,buffer,0);
  99.        
  100.     GetKey(&key);
  101.  
  102.                
  103.  
  104.      switch(key)
  105.         {
  106.         case KEY_CTRL_UP :
  107.             {
  108.             if (note<11)
  109.                 {
  110.                 note++;
  111.                 }
  112.             else
  113.                 {
  114.                 note=0;
  115.                 octave=(octave<5 ? octave+1 : 0);
  116.                 }
  117.             break;
  118.             }
  119.         case KEY_CTRL_DOWN :
  120.             {
  121.             if(note>0)
  122.                 {
  123.                 note--;
  124.                 }
  125.             else
  126.                 {
  127.                 note=11;
  128.                 octave=(octave>0 ? octave-1 : 5);
  129.                 }
  130.             break;
  131.             }
  132.  
  133.         case KEY_CHAR_PLUS : tempo+=10; break;
  134.         case KEY_CHAR_MINUS : tempo-=10; break;
  135.        
  136.         case KEY_CTRL_EXE : return 440*power(r,12*octave+note-45);
  137.         }
  138.     }
  139. }
  140.  
  141. void play(double frequence , double length)
  142. {
  143. int i;
  144. double j;
  145. unsigned char config[] = {0, 5, 0, 0, 0, 0};
  146.  
  147. Serial_Open(config);
  148.  
  149. for(i=0;i<(int)(frequence*length);i++)
  150.         {
  151.         Serial_WriteByte(255);
  152.         for (j=0;j<17.5;j+=0.211892619*frequence/440); // 0.211892619 => 2*((12)/square(2))
  153.         }
  154.        
  155. Serial_Close(1);
  156.  
  157. }
  158.  
  159.  
  160.  
  161. int AddIn_main(int isAppli, unsigned short OptionNum)
  162. {
  163.         double length=1.0; // a 60 batt/minute
  164.         double freq=440;
  165.                
  166.        
  167.    
  168. while(1)
  169.         {
  170.                
  171.                 freq=calcul_frequence();
  172.                
  173.                 play(freq, 60/tempo);
  174.         }
  175.        
  176. }
  177.  
  178. //****************************************************************************
  179. //**************                                              ****************
  180. //**************                 Notice!                      ****************
  181. //**************                                              ****************
  182. //**************  Please do not change the following source.  ****************
  183. //**************                                              ****************
  184. //****************************************************************************
  185.  
  186.  
  187. #pragma section _BR_Size
  188. unsigned long BR_Size;
  189. #pragma section
  190.  
  191.  
  192. #pragma section _TOP
  193.  
  194. //****************************************************************************
  195. //  InitializeSystem
  196. //
  197. //  param   :   isAppli   : 1 = Application / 0 = eActivity
  198. //              OptionNum : Option Number (only eActivity)
  199. //
  200. //  retval  :   1 = No error / 0 = Error
  201. //
  202. //****************************************************************************
  203. int InitializeSystem(int isAppli, unsigned short OptionNum)
  204. {
  205.     return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  206. }
  207.  
  208. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment