cp_tables.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* cp_tables.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "cp_tables.h"
  31. int32_t CPTables::linear_period_to_freq_tab[768] = {
  32. 535232, 534749, 534266, 533784, 533303, 532822, 532341, 531861,
  33. 531381, 530902, 530423, 529944, 529466, 528988, 528511, 528034,
  34. 527558, 527082, 526607, 526131, 525657, 525183, 524709, 524236,
  35. 523763, 523290, 522818, 522346, 521875, 521404, 520934, 520464,
  36. 519994, 519525, 519057, 518588, 518121, 517653, 517186, 516720,
  37. 516253, 515788, 515322, 514858, 514393, 513929, 513465, 513002,
  38. 512539, 512077, 511615, 511154, 510692, 510232, 509771, 509312,
  39. 508852, 508393, 507934, 507476, 507018, 506561, 506104, 505647,
  40. 505191, 504735, 504280, 503825, 503371, 502917, 502463, 502010,
  41. 501557, 501104, 500652, 500201, 499749, 499298, 498848, 498398,
  42. 497948, 497499, 497050, 496602, 496154, 495706, 495259, 494812,
  43. 494366, 493920, 493474, 493029, 492585, 492140, 491696, 491253,
  44. 490809, 490367, 489924, 489482, 489041, 488600, 488159, 487718,
  45. 487278, 486839, 486400, 485961, 485522, 485084, 484647, 484210,
  46. 483773, 483336, 482900, 482465, 482029, 481595, 481160, 480726,
  47. 480292, 479859, 479426, 478994, 478562, 478130, 477699, 477268,
  48. 476837, 476407, 475977, 475548, 475119, 474690, 474262, 473834,
  49. 473407, 472979, 472553, 472126, 471701, 471275, 470850, 470425,
  50. 470001, 469577, 469153, 468730, 468307, 467884, 467462, 467041,
  51. 466619, 466198, 465778, 465358, 464938, 464518, 464099, 463681,
  52. 463262, 462844, 462427, 462010, 461593, 461177, 460760, 460345,
  53. 459930, 459515, 459100, 458686, 458272, 457859, 457446, 457033,
  54. 456621, 456209, 455797, 455386, 454975, 454565, 454155, 453745,
  55. 453336, 452927, 452518, 452110, 451702, 451294, 450887, 450481,
  56. 450074, 449668, 449262, 448857, 448452, 448048, 447644, 447240,
  57. 446836, 446433, 446030, 445628, 445226, 444824, 444423, 444022,
  58. 443622, 443221, 442821, 442422, 442023, 441624, 441226, 440828,
  59. 440430, 440033, 439636, 439239, 438843, 438447, 438051, 437656,
  60. 437261, 436867, 436473, 436079, 435686, 435293, 434900, 434508,
  61. 434116, 433724, 433333, 432942, 432551, 432161, 431771, 431382,
  62. 430992, 430604, 430215, 429827, 429439, 429052, 428665, 428278,
  63. 427892, 427506, 427120, 426735, 426350, 425965, 425581, 425197,
  64. 424813, 424430, 424047, 423665, 423283, 422901, 422519, 422138,
  65. 421757, 421377, 420997, 420617, 420237, 419858, 419479, 419101,
  66. 418723, 418345, 417968, 417591, 417214, 416838, 416462, 416086,
  67. 415711, 415336, 414961, 414586, 414212, 413839, 413465, 413092,
  68. 412720, 412347, 411975, 411604, 411232, 410862, 410491, 410121,
  69. 409751, 409381, 409012, 408643, 408274, 407906, 407538, 407170,
  70. 406803, 406436, 406069, 405703, 405337, 404971, 404606, 404241,
  71. 403876, 403512, 403148, 402784, 402421, 402058, 401695, 401333,
  72. 400970, 400609, 400247, 399886, 399525, 399165, 398805, 398445,
  73. 398086, 397727, 397368, 397009, 396651, 396293, 395936, 395579,
  74. 395222, 394865, 394509, 394153, 393798, 393442, 393087, 392733,
  75. 392378, 392024, 391671, 391317, 390964, 390612, 390259, 389907,
  76. 389556, 389204, 388853, 388502, 388152, 387802, 387452, 387102,
  77. 386753, 386404, 386056, 385707, 385359, 385012, 384664, 384317,
  78. 383971, 383624, 383278, 382932, 382587, 382242, 381897, 381552,
  79. 381208, 380864, 380521, 380177, 379834, 379492, 379149, 378807,
  80. 378466, 378124, 377783, 377442, 377102, 376762, 376422, 376082,
  81. 375743, 375404, 375065, 374727, 374389, 374051, 373714, 373377,
  82. 373040, 372703, 372367, 372031, 371695, 371360, 371025, 370690,
  83. 370356, 370022, 369688, 369355, 369021, 368688, 368356, 368023,
  84. 367691, 367360, 367028, 366697, 366366, 366036, 365706, 365376,
  85. 365046, 364717, 364388, 364059, 363731, 363403, 363075, 362747,
  86. 362420, 362093, 361766, 361440, 361114, 360788, 360463, 360137,
  87. 359813, 359488, 359164, 358840, 358516, 358193, 357869, 357547,
  88. 357224, 356902, 356580, 356258, 355937, 355616, 355295, 354974,
  89. 354654, 354334, 354014, 353695, 353376, 353057, 352739, 352420,
  90. 352103, 351785, 351468, 351150, 350834, 350517, 350201, 349885,
  91. 349569, 349254, 348939, 348624, 348310, 347995, 347682, 347368,
  92. 347055, 346741, 346429, 346116, 345804, 345492, 345180, 344869,
  93. 344558, 344247, 343936, 343626, 343316, 343006, 342697, 342388,
  94. 342079, 341770, 341462, 341154, 340846, 340539, 340231, 339924,
  95. 339618, 339311, 339005, 338700, 338394, 338089, 337784, 337479,
  96. 337175, 336870, 336566, 336263, 335959, 335656, 335354, 335051,
  97. 334749, 334447, 334145, 333844, 333542, 333242, 332941, 332641,
  98. 332341, 332041, 331741, 331442, 331143, 330844, 330546, 330247,
  99. 329950, 329652, 329355, 329057, 328761, 328464, 328168, 327872,
  100. 327576, 327280, 326985, 326690, 326395, 326101, 325807, 325513,
  101. 325219, 324926, 324633, 324340, 324047, 323755, 323463, 323171,
  102. 322879, 322588, 322297, 322006, 321716, 321426, 321136, 320846,
  103. 320557, 320267, 319978, 319690, 319401, 319113, 318825, 318538,
  104. 318250, 317963, 317676, 317390, 317103, 316817, 316532, 316246,
  105. 315961, 315676, 315391, 315106, 314822, 314538, 314254, 313971,
  106. 313688, 313405, 313122, 312839, 312557, 312275, 311994, 311712,
  107. 311431, 311150, 310869, 310589, 310309, 310029, 309749, 309470,
  108. 309190, 308911, 308633, 308354, 308076, 307798, 307521, 307243,
  109. 306966, 306689, 306412, 306136, 305860, 305584, 305308, 305033,
  110. 304758, 304483, 304208, 303934, 303659, 303385, 303112, 302838,
  111. 302565, 302292, 302019, 301747, 301475, 301203, 300931, 300660,
  112. 300388, 300117, 299847, 299576, 299306, 299036, 298766, 298497,
  113. 298227, 297958, 297689, 297421, 297153, 296884, 296617, 296349,
  114. 296082, 295815, 295548, 295281, 295015, 294749, 294483, 294217,
  115. 293952, 293686, 293421, 293157, 292892, 292628, 292364, 292100,
  116. 291837, 291574, 291311, 291048, 290785, 290523, 290261, 289999,
  117. 289737, 289476, 289215, 288954, 288693, 288433, 288173, 287913,
  118. 287653, 287393, 287134, 286875, 286616, 286358, 286099, 285841,
  119. 285583, 285326, 285068, 284811, 284554, 284298, 284041, 283785,
  120. 283529, 283273, 283017, 282762, 282507, 282252, 281998, 281743,
  121. 281489, 281235, 280981, 280728, 280475, 280222, 279969, 279716,
  122. 279464, 279212, 278960, 278708, 278457, 278206, 277955, 277704,
  123. 277453, 277203, 276953, 276703, 276453, 276204, 275955, 275706,
  124. 275457, 275209, 274960, 274712, 274465, 274217, 273970, 273722,
  125. 273476, 273229, 272982, 272736, 272490, 272244, 271999, 271753,
  126. 271508, 271263, 271018, 270774, 270530, 270286, 270042, 269798,
  127. 269555, 269312, 269069, 268826, 268583, 268341, 268099, 267857
  128. };
  129. uint16_t CPTables::old_period_table[OCTAVE * 2] = {
  130. 0x6b00, 0x6800, 0x6500, 0x6220, 0x5f50, 0x5c80,
  131. 0x5a00, 0x5740, 0x54d0, 0x5260, 0x5010, 0x4dc0,
  132. 0x4b90, 0x4960, 0x4750, 0x4540, 0x4350, 0x4160,
  133. 0x3f90, 0x3dc0, 0x3c10, 0x3a40, 0x38b0, 0x3700
  134. };
  135. #define LOGFAC 2 * 16
  136. uint16_t CPTables::log_table[104] = {
  137. LOGFAC * 907, LOGFAC * 900, LOGFAC * 894, LOGFAC * 887,
  138. LOGFAC * 881, LOGFAC * 875, LOGFAC * 868, LOGFAC * 862,
  139. LOGFAC * 856, LOGFAC * 850, LOGFAC * 844, LOGFAC * 838,
  140. LOGFAC * 832, LOGFAC * 826, LOGFAC * 820, LOGFAC * 814,
  141. LOGFAC * 808, LOGFAC * 802, LOGFAC * 796, LOGFAC * 791,
  142. LOGFAC * 785, LOGFAC * 779, LOGFAC * 774, LOGFAC * 768,
  143. LOGFAC * 762, LOGFAC * 757, LOGFAC * 752, LOGFAC * 746,
  144. LOGFAC * 741, LOGFAC * 736, LOGFAC * 730, LOGFAC * 725,
  145. LOGFAC * 720, LOGFAC * 715, LOGFAC * 709, LOGFAC * 704,
  146. LOGFAC * 699, LOGFAC * 694, LOGFAC * 689, LOGFAC * 684,
  147. LOGFAC * 678, LOGFAC * 675, LOGFAC * 670, LOGFAC * 665,
  148. LOGFAC * 660, LOGFAC * 655, LOGFAC * 651, LOGFAC * 646,
  149. LOGFAC * 640, LOGFAC * 636, LOGFAC * 632, LOGFAC * 628,
  150. LOGFAC * 623, LOGFAC * 619, LOGFAC * 614, LOGFAC * 610,
  151. LOGFAC * 604, LOGFAC * 601, LOGFAC * 597, LOGFAC * 592,
  152. LOGFAC * 588, LOGFAC * 584, LOGFAC * 580, LOGFAC * 575,
  153. LOGFAC * 570, LOGFAC * 567, LOGFAC * 563, LOGFAC * 559,
  154. LOGFAC * 555, LOGFAC * 551, LOGFAC * 547, LOGFAC * 543,
  155. LOGFAC * 538, LOGFAC * 535, LOGFAC * 532, LOGFAC * 528,
  156. LOGFAC * 524, LOGFAC * 520, LOGFAC * 516, LOGFAC * 513,
  157. LOGFAC * 508, LOGFAC * 505, LOGFAC * 502, LOGFAC * 498,
  158. LOGFAC * 494, LOGFAC * 491, LOGFAC * 487, LOGFAC * 484,
  159. LOGFAC * 480, LOGFAC * 477, LOGFAC * 474, LOGFAC * 470,
  160. LOGFAC * 467, LOGFAC * 463, LOGFAC * 460, LOGFAC * 457,
  161. LOGFAC * 453, LOGFAC * 450, LOGFAC * 447, LOGFAC * 443,
  162. LOGFAC * 440, LOGFAC * 437, LOGFAC * 434, LOGFAC * 431
  163. };
  164. int32_t CPTables::get_linear_period(uint16_t note, int32_t fine) {
  165. int32_t t;
  166. t = (24L * OCTAVE - (int32_t)note) * 32L - (fine >> 1);
  167. return t;
  168. }
  169. static int s3m_period_table[12] = { 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 907 };
  170. int32_t CPTables::get_log_period(uint16_t note, int32_t p_c5freq) {
  171. return (8363L * 16 * s3m_period_table[note % 12] >> (note / 12)) / p_c5freq;
  172. }
  173. /*
  174. int32_t CPTables::get_log_period(uint16_t note,int32_t p_c5freq)
  175. {
  176. uint16_t n,o;
  177. uint16_t p1,p2;
  178. int32_t i;
  179. n=note%(2*OCTAVE);
  180. o=note/(2*OCTAVE);
  181. i=(n<<2); // n*8 + fine/16
  182. if (i<0)
  183. i=0;
  184. if (i>102)
  185. i=102;
  186. p1=log_table[i];
  187. p2=log_table[i+1];
  188. return (Interpolate(fine>>4,0,15,p1,p2)>>o);
  189. } */
  190. int32_t CPTables::get_old_period(uint16_t note, int32_t speed) {
  191. uint16_t n, o, res;
  192. // if (!speed) {
  193. // return 4242; /* <- prevent divide overflow */
  194. // }
  195. n = note % (2 * OCTAVE);
  196. o = note / (2 * OCTAVE);
  197. res = ((8363L * (int32_t)old_period_table[n]) >> o) / ((old_period_table[17] >> 1) + (speed << 2)); /*/(128-speed)*/
  198. ;
  199. return res;
  200. }
  201. int32_t CPTables::get_linear_frequency(int32_t period) {
  202. int32_t shift_value = (((int32_t)period / 768) - 2);
  203. if (shift_value > 0) {
  204. return linear_period_to_freq_tab[period % 768] >> shift_value;
  205. } else {
  206. shift_value = 0 - shift_value;
  207. return linear_period_to_freq_tab[period % 768] << shift_value;
  208. }
  209. }
  210. int32_t CPTables::get_old_frequency(int32_t period) {
  211. return (8363L * 1712L) / (period ? period : 1);
  212. }
  213. CPTables::CPTables() {
  214. }
  215. CPTables::~CPTables() {
  216. }