Guest User

Untitled

a guest
May 16th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <windows.h>
  2. #include <cmath>
  3. #include <list>
  4. #include <map>
  5.  
  6. bool IsPressed(int vkey) { return GetAsyncKeyState(vkey)>>15; }
  7.  
  8. void build_frequency_map(std::map<char, int> & fm);
  9. void build_frequency_stream(std::list<int> & fs, std::map<char, int> & fm);
  10.  
  11. int main()
  12. {
  13.     std::map<char, int> fm;
  14.     std::list<int> fs;
  15.     int f;
  16.  
  17.     build_frequency_map(fm);
  18.     build_frequency_stream(fs,fm);
  19.     f=0;
  20.  
  21.     for (std::list<int>::iterator it=fs.begin(); it!=fs.end(); it++)
  22.     {
  23.         f=*it;
  24.  
  25.         if (!f) Sleep(25);
  26.         else Beep(f,25);
  27.     }
  28.  
  29.     while (true)
  30.     {
  31.         if (IsPressed(VK_ESCAPE)) break;
  32.  
  33.         if      (IsPressed('Z')) f=fm['C'];
  34.         else if (IsPressed('X')) f=fm['D'];
  35.         else if (IsPressed('C')) f=fm['E'];
  36.         else if (IsPressed('V')) f=fm['F'];
  37.         else if (IsPressed('B')) f=fm['G'];
  38.         else if (IsPressed('N')) f=fm['A'];
  39.         else if (IsPressed('M')) f=fm['B'];
  40.         else if (IsPressed('A')) f=2*fm['C'];
  41.         else if (IsPressed('S')) f=2*fm['D'];
  42.         else if (IsPressed('D')) f=2*fm['E'];
  43.         else if (IsPressed('F')) f=2*fm['F'];
  44.         else if (IsPressed('G')) f=2*fm['G'];
  45.         else if (IsPressed('H')) f=2*fm['A'];
  46.         else if (IsPressed('J')) f=2*fm['B'];
  47.         else                     f=0;
  48.  
  49.         if (IsPressed(VK_RSHIFT)) f*=pow(2.0,1.0/12.0); // #
  50.         if (IsPressed(VK_LSHIFT)) f*=pow(2.0,-1.0/12.0); // b
  51.  
  52.         if (f) Beep(f,25);
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58. void build_frequency_map(std::map<char, int> & fm)
  59. {
  60.     const int A=440;
  61.  
  62.     fm['C']=pow(2.0,-9.0/12.0)*A+0.5;
  63.     fm['D']=pow(2.0,-7.0/12.0)*A+0.5;
  64.     fm['E']=pow(2.0,-5.0/12.0)*A+0.5;
  65.     fm['F']=pow(2.0,-4.0/12.0)*A+0.5;
  66.     fm['G']=pow(2.0,-2.0/12.0)*A+0.5;
  67.     fm['A']=A;
  68.     fm['B']=pow(2.0,2.0/12.0)*A+0.5;
  69.  
  70.     fm[0]=0;
  71. }
  72.  
  73. void build_frequency_stream(std::list<int> & fs, std::map<char, int> & fm)
  74. {
  75.     struct Local
  76.     {
  77.         std::list<int> * pfs;
  78.         std::map<char, int> * pfm;
  79.         const int tempo;
  80.  
  81.         Local(): tempo(6) {}
  82.  
  83.         Local & put(char ch, int oct=2, double tempo_mod=1)
  84.         {
  85.             int f=(*pfm)[ch]*oct;
  86.  
  87.             for (int i=0; i<tempo*tempo_mod+0.5; i++)
  88.                 pfs->push_back(f);
  89.  
  90.             return *this;
  91.         }
  92.  
  93.         Local & CDEE() { return put('C').put('D').put('E').put(0).put('E').put(0); }
  94.         Local & BCDD() { return put('B',1).put('C').put('D').put(0).put('D').put(0); }
  95.  
  96.     } local;
  97.  
  98.     local.pfs=&fs; local.pfm=&fm;
  99.  
  100.     local.CDEE().put('E').put(0).CDEE().put('E').put(0).CDEE().put('F').
  101.         put(0,0,2).put('E').put('D').put(0,0,5).BCDD().put('D').put(0).
  102.         BCDD().put('D').put(0).BCDD().put('E').put('D',2,2.0/3.0).
  103.         put(0,0,1.0/3.0).put('D').put('E').put('C').put(0,0,5).CDEE().
  104.         put('E').put(0).CDEE().put('E').put(0).CDEE().put('F').
  105.         put(0,0,2).put('E').put('D').put(0,0,5).put('B',1).put('C').
  106.         put('D').put(0).put('F').put(0).put('E').put(0).put('D').
  107.         put(0).put('C').put(0).put('B',1).put(0).put('A',1).put(0).
  108.         put('G',1).put(0).put('G').put(0,0,3).put('B',1,0.5).
  109.         put('C',2,0.5).put('B',1).put('A',1).put('B',1).put('C');
  110. }
Advertisement
Add Comment
Please, Sign In to add comment