Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <cmath>
- #include <list>
- #include <map>
- bool IsPressed(int vkey) { return GetAsyncKeyState(vkey)>>15; }
- void build_frequency_map(std::map<char, int> & fm);
- void build_frequency_stream(std::list<int> & fs, std::map<char, int> & fm);
- int main()
- {
- std::map<char, int> fm;
- std::list<int> fs;
- int f;
- build_frequency_map(fm);
- build_frequency_stream(fs,fm);
- f=0;
- for (std::list<int>::iterator it=fs.begin(); it!=fs.end(); it++)
- {
- f=*it;
- if (!f) Sleep(25);
- else Beep(f,25);
- }
- while (true)
- {
- if (IsPressed(VK_ESCAPE)) break;
- if (IsPressed('Z')) f=fm['C'];
- else if (IsPressed('X')) f=fm['D'];
- else if (IsPressed('C')) f=fm['E'];
- else if (IsPressed('V')) f=fm['F'];
- else if (IsPressed('B')) f=fm['G'];
- else if (IsPressed('N')) f=fm['A'];
- else if (IsPressed('M')) f=fm['B'];
- else if (IsPressed('A')) f=2*fm['C'];
- else if (IsPressed('S')) f=2*fm['D'];
- else if (IsPressed('D')) f=2*fm['E'];
- else if (IsPressed('F')) f=2*fm['F'];
- else if (IsPressed('G')) f=2*fm['G'];
- else if (IsPressed('H')) f=2*fm['A'];
- else if (IsPressed('J')) f=2*fm['B'];
- else f=0;
- if (IsPressed(VK_RSHIFT)) f*=pow(2.0,1.0/12.0); // #
- if (IsPressed(VK_LSHIFT)) f*=pow(2.0,-1.0/12.0); // b
- if (f) Beep(f,25);
- }
- return 0;
- }
- void build_frequency_map(std::map<char, int> & fm)
- {
- const int A=440;
- fm['C']=pow(2.0,-9.0/12.0)*A+0.5;
- fm['D']=pow(2.0,-7.0/12.0)*A+0.5;
- fm['E']=pow(2.0,-5.0/12.0)*A+0.5;
- fm['F']=pow(2.0,-4.0/12.0)*A+0.5;
- fm['G']=pow(2.0,-2.0/12.0)*A+0.5;
- fm['A']=A;
- fm['B']=pow(2.0,2.0/12.0)*A+0.5;
- fm[0]=0;
- }
- void build_frequency_stream(std::list<int> & fs, std::map<char, int> & fm)
- {
- struct Local
- {
- std::list<int> * pfs;
- std::map<char, int> * pfm;
- const int tempo;
- Local(): tempo(6) {}
- Local & put(char ch, int oct=2, double tempo_mod=1)
- {
- int f=(*pfm)[ch]*oct;
- for (int i=0; i<tempo*tempo_mod+0.5; i++)
- pfs->push_back(f);
- return *this;
- }
- Local & CDEE() { return put('C').put('D').put('E').put(0).put('E').put(0); }
- Local & BCDD() { return put('B',1).put('C').put('D').put(0).put('D').put(0); }
- } local;
- local.pfs=&fs; local.pfm=&fm;
- local.CDEE().put('E').put(0).CDEE().put('E').put(0).CDEE().put('F').
- put(0,0,2).put('E').put('D').put(0,0,5).BCDD().put('D').put(0).
- BCDD().put('D').put(0).BCDD().put('E').put('D',2,2.0/3.0).
- put(0,0,1.0/3.0).put('D').put('E').put('C').put(0,0,5).CDEE().
- put('E').put(0).CDEE().put('E').put(0).CDEE().put('F').
- put(0,0,2).put('E').put('D').put(0,0,5).put('B',1).put('C').
- put('D').put(0).put('F').put(0).put('E').put(0).put('D').
- put(0).put('C').put(0).put('B',1).put(0).put('A',1).put(0).
- put('G',1).put(0).put('G').put(0,0,3).put('B',1,0.5).
- put('C',2,0.5).put('B',1).put('A',1).put('B',1).put('C');
- }
Advertisement
Add Comment
Please, Sign In to add comment