#include "Utils.h" unsigned short MidiNoteToFrequency(int note) { static const unsigned short pFrequencies[] = { 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16, 16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,18,18,18,18,18, 18,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20, 20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21, 21,21,21,22,22,22,22,22,22,22,22,22,22,22,22,23, 23,23,23,23,23,23,23,23,23,23,23,24,24,24,24,24, 24,24,24,24,24,24,25,25,25,25,25,25,25,25,25,25, 25,26,26,26,26,26,26,26,26,26,26,27,27,27,27,27, 27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,29, 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30, 30,30,31,31,31,31,31,31,31,31,32,32,32,32,32,32, 32,32,32,33,33,33,33,33,33,33,33,34,34,34,34,34, 34,34,34,35,35,35,35,35,35,35,35,36,36,36,36,36, 36,36,36,37,37,37,37,37,37,37,38,38,38,38,38,38, 38,39,39,39,39,39,39,39,40,40,40,40,40,40,40,41, 41,41,41,41,41,41,42,42,42,42,42,42,43,43,43,43, 43,43,43,44,44,44,44,44,44,45,45,45,45,45,45,46, 46,46,46,46,46,47,47,47,47,47,47,48,48,48,48,48, 48,49,49,49,49,49,50,50,50,50,50,50,51,51,51,51, 51,52,52,52,52,52,53,53,53,53,53,54,54,54,54,54, 55,55,55,55,55,56,56,56,56,56,57,57,57,57,57,58, 58,58,58,58,59,59,59,59,59,60,60,60,60,61,61,61, 61,61,62,62,62,62,63,63,63,63,64,64,64,64,64,65, 65,65,65,66,66,66,66,67,67,67,67,68,68,68,68,69, 69,69,69,70,70,70,70,71,71,71,71,72,72,72,72,73, 73,73,73,74,74,74,75,75,75,75,76,76,76,76,77,77, 77,78,78,78,78,79,79,79,80,80,80,80,81,81,81,82, 82,82,83,83,83,83,84,84,84,85,85,85,86,86,86,86, 87,87,87,88,88,88,89,89,89,90,90,90,91,91,91,92, 92,92,93,93,93,94,94,94,95,95,95,96,96,96,97,97, 97,98,98,99,99,99,100,100,100,101,101,101,102,102,103,103, 103,104,104,104,105,105,106,106,106,107,107,108,108,108,109,109, 110,110,110,111,111,112,112,112,113,113,114,114,114,115,115,116, 116,116,117,117,118,118,119,119,119,120,120,121,121,122,122,123, 123,123,124,124,125,125,126,126,127,127,128,128,128,129,129,130, 130,131,131,132,132,133,133,134,134,135,135,136,136,137,137,138, 138,139,139,140,140,141,141,142,142,143,143,144,144,145,145,146, 146,147,147,148,148,149,150,150,151,151,152,152,153,153,154,155, 155,156,156,157,157,158,158,159,160,160,161,161,162,163,163,164, 164,165,166,166,167,167,168,169,169,170,170,171,172,172,173,173, 174,175,175,176,177,177,178,179,179,180,181,181,182,183,183,184, 184,185,186,187,187,188,189,189,190,191,191,192,193,193,194,195, 195,196,197,198,198,199,200,201,201,202,203,203,204,205,206,206, 207,208,209,209,210,211,212,212,213,214,215,216,216,217,218,219, 220,220,221,222,223,224,224,225,226,227,228,228,229,230,231,232, 233,233,234,235,236,237,238,239,239,240,241,242,243,244,245,246, 246,247,248,249,250,251,252,253,254,255,256,256,257,258,259,260, 261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276, 277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292, 293,294,295,296,297,299,300,301,302,303,304,305,306,307,308,310, 311,312,313,314,315,316,317,319,320,321,322,323,324,326,327,328, 329,330,332,333,334,335,336,338,339,340,341,342,344,345,346,347, 349,350,351,353,354,355,356,358,359,360,362,363,364,366,367,368, 369,371,372,374,375,376,378,379,380,382,383,384,386,387,389,390, 391,393,394,396,397,399,400,402,403,404,406,407,409,410,412,413, 415,416,418,419,421,422,424,425,427,429,430,432,433,435,436,438, 440,441,443,444,446,448,449,451,452,454,456,457,459,461,462,464, 466,467,469,471,472,474,476,478,479,481,483,485,486,488,490,492, 493,495,497,499,501,502,504,506,508,510,512,513,515,517,519,521, 523,525,527,528,530,532,534,536,538,540,542,544,546,548,550,552, 554,556,558,560,562,564,566,568,570,572,574,576,578,581,583,585, 587,589,591,593,595,598,600,602,604,606,608,611,613,615,617,620, 622,624,626,629,631,633,635,638,640,642,645,647,649,652,654,656, 659,661,664,666,668,671,673,676,678,681,683,685,688,690,693,695, 698,700,703,706,708,711,713,716,718,721,724,726,729,732,734,737, 739,742,745,748,750,753,756,758,761,764,767,769,772,775,778,781, 783,786,789,792,795,798,801,804,806,809,812,815,818,821,824,827, 830,833,836,839,842,845,848,851,854,858,861,864,867,870,873,876, 880,883,886,889,892,896,899,902,905,909,912,915,918,922,925,928, 932,935,939,942,945,949,952,956,959,963,966,970,973,977,980,984, 987,991,994,998,1002,1005,1009,1013,1016,1020,1024,1027,1031,1035,1038,1042, 1046,1050,1054,1057,1061,1065,1069,1073,1077,1081,1084,1088,1092,1096,1100,1104, 1108,1112,1116,1120,1124,1128,1133,1137,1141,1145,1149,1153,1157,1162,1166,1170, 1174,1178,1183,1187,1191,1196,1200,1204,1209,1213,1217,1222,1226,1231,1235,1240, 1244,1249,1253,1258,1262,1267,1271,1276,1280,1285,1290,1294,1299,1304,1309,1313, 1318,1323,1328,1332,1337,1342,1347,1352,1357,1362,1366,1371,1376,1381,1386,1391, 1396,1401,1407,1412,1417,1422,1427,1432,1437,1443,1448,1453,1458,1464,1469,1474, 1479,1485,1490,1496,1501,1506,1512,1517,1523,1528,1534,1539,1545,1551,1556,1562, 1567,1573,1579,1585,1590,1596,1602,1608,1613,1619,1625,1631,1637,1643,1649,1655, 1661,1667,1673,1679,1685,1691,1697,1703,1709,1716,1722,1728,1734,1741,1747,1753, 1760,1766,1772,1779,1785,1792,1798,1805,1811,1818,1824,1831,1837,1844,1851,1857, 1864,1871,1878,1884,1891,1898,1905,1912,1919,1926,1933,1940,1947,1954,1961,1968, 1975,1982,1989,1997,2004,2011,2018,2026,2033,2040,2048,2055,2062,2070,2077,2085, 2093,2100,2108,2115,2123,2131,2138,2146,2154,2162,2169,2177,2185,2193,2201,2209, 2217,2225,2233,2241,2249,2257,2266,2274,2282,2290,2298,2307,2315,2324,2332,2340, 2349,2357,2366,2374,2383,2392,2400,2409,2418,2426,2435,2444,2453,2462,2471,2480, 2489,2498,2507,2516,2525,2534,2543,2552,2561,2571,2580,2589,2599,2608,2618,2627, 2637,2646,2656,2665,2675,2685,2694,2704,2714,2724,2733,2743,2753,2763,2773,2783, 2793,2803,2814,2824,2834,2844,2855,2865,2875,2886,2896,2907,2917,2928,2938,2949, 2959,2970,2981,2992,3003,3013,3024,3035,3046,3057,3068,3079,3091,3102,3113,3124, 3135,3147,3158,3170,3181,3193,3204,3216,3227,3239,3251,3263,3274,3286,3298,3310, 3322,3334,3346,3358,3370,3382,3395,3407,3419,3432,3444,3457,3469,3482,3494,3507, 3520,3532,3545,3558,3571,3584,3597,3610,3623,3636,3649,3662,3675,3689,3702,3715, 3729,3742,3756,3769,3783,3797,3810,3824,3838,3852,3866,3880,3894,3908,3922,3936, 3951,3965,3979,3994,4008,4023,4037,4052,4066,4081,4096,4111,4125,4140,4155,4170, 4186,4201,4216,4231,4246,4262,4277,4293,4308,4324,4339,4355,4371,4387,4403,4418, 4434,4450,4467,4483,4499,4515,4532,4548,4564,4581,4597,4614,4631,4648,4664,4681, 4698,4715,4732,4749,4766,4784,4801,4818,4836,4853,4871,4888,4906,4924,4942,4960, 4978,4996,5014,5032,5050,5068,5087,5105,5123,5142,5161,5179,5198,5217,5236,5255, 5274,5293,5312,5331,5350,5370,5389,5409,5428,5448,5467,5487,5507,5527,5547,5567, 5587,5607,5628,5648,5668,5689,5710,5730,5751,5772,5793,5814,5835,5856,5877,5898, 5919,5941,5962,5984,6006,6027,6049,6071,6093,6115,6137,6159,6182,6204,6226,6249, 6271,6294,6317,6340,6363,6386,6409,6432,6455,6479,6502,6526,6549,6573,6597,6620, 6644,6668,6693,6717,6741,6765,6790,6814,6839,6864,6889,6914,6939,6964,6989,7014, 7040,7065,7091,7116,7142,7168,7194,7220,7246,7272,7298,7325,7351,7378,7404,7431, 7458,7485,7512,7539,7567,7594,7621,7649,7677,7704,7732,7760,7788,7817,7845,7873, 7902,7930,7959,7988,8017,8046,8075,8104,8133,8163,8192,8222,8251,8281,8311,8341, 8372,8402,8432,8463,8493,8524,8555,8586,8617,8648,8679,8711,8742,8774,8806,8837, 8869,8901,8934,8966,8998,9031,9064,9096,9129,9162,9195,9229,9262,9296,9329,9363, 9397,9431,9465,9499,9533,9568,9603,9637,9672,9707,9742,9777,9813,9848,9884,9920, 9956,9992,10028,10064,10100,10137,10174,10210,10247,10284,10322,10359,10396,10434,10472,10510, 10548,10586,10624,10662,10701,10740,10779,10818,10857,10896,10935,10975,11015,11054,11094,11135, 11175,11215,11256,11296,11337,11378,11420,11461,11502,11544,11586,11628,11670,11712,11754,11797, 11839,11882,11925,11968,12012,12055,12099,12142,12186,12230,12275,12319,12364,12408,12453,12498, 12543,12589,12634,12680,12726,12772,12818,12864,12911,12958,13004,13052,13099,13146,13194,13241, }; if ( note < 0 ) note = 0; if ( note > 127*16 ) note = 127*16; return pFrequencies[note]; } int Abs(int x) { if ( x < 0 ) return -x; return x; } int Sinus(int x) { // translate x into [-pi, pi].f12 while ( x > 12868 ) x -= 25736; while ( x < -12868 ) x += 25736; // compute sin(x) // see http://www.devmaster.net/forums/showthread.php?t=5784 const int B = 5215; const int C = -1660; return (B*x + ((C*x)>>12)*Abs(x))>>12; } void MemCopy(void * dst, void * src, int len) { unsigned char * d = (unsigned char *) dst; unsigned char * s = (unsigned char *) src; while ( len-- ) *d++ = *s++; }