XM.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // Original MOD & XM playback code from JUDAS Soundsystem
  24. // Copyright (c) 1997 Lasse Öörni & Olli Niemitalo
  25. #include "Precompiled.h"
  26. #include "Audio.h"
  27. #include "Channel.h"
  28. #include "Deserializer.h"
  29. #include "Profiler.h"
  30. #include "Sound.h"
  31. #include "XM.h"
  32. #include <cstring>
  33. #include "DebugNew.h"
  34. static const int ENV_ON = 1;
  35. static const int ENV_SUSTAIN = 2;
  36. static const int ENV_LOOP = 4;
  37. static const int SMP_LOOP = 1;
  38. static const int SMP_PINGPONGLOOP = 2;
  39. static const int SMP_16BIT = 16;
  40. static const int MAX_CHANNELS = 32;
  41. static const int MAX_PATTERNS = 256;
  42. static const int MAX_INSTRUMENTS = 128;
  43. static const int MAX_SAMPLES = 16;
  44. static const int KEYOFF = 0x61;
  45. const unsigned XM::sLinearFreqTable[] =
  46. {
  47. 535232, 534749, 534266, 533784, 533303, 532822, 532341, 531861, 531381, 530902,
  48. 530423, 529944, 529466, 528988, 528511, 528034, 527558, 527082, 526607, 526131,
  49. 525657, 525183, 524709, 524236, 523763, 523290, 522818, 522346, 521875, 521404,
  50. 520934, 520464, 519994, 519525, 519057, 518588, 518121, 517653, 517186, 516720,
  51. 516253, 515788, 515322, 514858, 514393, 513929, 513465, 513002, 512539, 512077,
  52. 511615, 511154, 510692, 510232, 509771, 509312, 508852, 508393, 507934, 507476,
  53. 507018, 506561, 506104, 505647, 505191, 504735, 504280, 503825, 503371, 502917,
  54. 502463, 502010, 501557, 501104, 500652, 500201, 499749, 499298, 498848, 498398,
  55. 497948, 497499, 497050, 496602, 496154, 495706, 495259, 494812, 494366, 493920,
  56. 493474, 493029, 492585, 492140, 491696, 491253, 490809, 490367, 489924, 489482,
  57. 489041, 488600, 488159, 487718, 487278, 486839, 486400, 485961, 485522, 485084,
  58. 484647, 484210, 483773, 483336, 482900, 482465, 482029, 481595, 481160, 480726,
  59. 480292, 479859, 479426, 478994, 478562, 478130, 477699, 477268, 476837, 476407,
  60. 475977, 475548, 475119, 474690, 474262, 473834, 473407, 472979, 472553, 472126,
  61. 471701, 471275, 470850, 470425, 470001, 469577, 469153, 468730, 468307, 467884,
  62. 467462, 467041, 466619, 466198, 465778, 465358, 464938, 464518, 464099, 463681,
  63. 463262, 462844, 462427, 462010, 461593, 461177, 460760, 460345, 459930, 459515,
  64. 459100, 458686, 458272, 457859, 457446, 457033, 456621, 456209, 455797, 455386,
  65. 454975, 454565, 454155, 453745, 453336, 452927, 452518, 452110, 451702, 451294,
  66. 450887, 450481, 450074, 449668, 449262, 448857, 448452, 448048, 447644, 447240,
  67. 446836, 446433, 446030, 445628, 445226, 444824, 444423, 444022, 443622, 443221,
  68. 442821, 442422, 442023, 441624, 441226, 440828, 440430, 440033, 439636, 439239,
  69. 438843, 438447, 438051, 437656, 437261, 436867, 436473, 436079, 435686, 435293,
  70. 434900, 434508, 434116, 433724, 433333, 432942, 432551, 432161, 431771, 431382,
  71. 430992, 430604, 430215, 429827, 429439, 429052, 428665, 428278, 427892, 427506,
  72. 427120, 426735, 426350, 425965, 425581, 425197, 424813, 424430, 424047, 423665,
  73. 423283, 422901, 422519, 422138, 421757, 421377, 420997, 420617, 420237, 419858,
  74. 419479, 419101, 418723, 418345, 417968, 417591, 417214, 416838, 416462, 416086,
  75. 415711, 415336, 414961, 414586, 414212, 413839, 413465, 413092, 412720, 412347,
  76. 411975, 411604, 411232, 410862, 410491, 410121, 409751, 409381, 409012, 408643,
  77. 408274, 407906, 407538, 407170, 406803, 406436, 406069, 405703, 405337, 404971,
  78. 404606, 404241, 403876, 403512, 403148, 402784, 402421, 402058, 401695, 401333,
  79. 400970, 400609, 400247, 399886, 399525, 399165, 398805, 398445, 398086, 397727,
  80. 397368, 397009, 396651, 396293, 395936, 395579, 395222, 394865, 394509, 394153,
  81. 393798, 393442, 393087, 392733, 392378, 392024, 391671, 391317, 390964, 390612,
  82. 390259, 389907, 389556, 389204, 388853, 388502, 388152, 387802, 387452, 387102,
  83. 386753, 386404, 386056, 385707, 385359, 385012, 384664, 384317, 383971, 383624,
  84. 383278, 382932, 382587, 382242, 381897, 381552, 381208, 380864, 380521, 380177,
  85. 379834, 379492, 379149, 378807, 378466, 378124, 377783, 377442, 377102, 376762,
  86. 376422, 376082, 375743, 375404, 375065, 374727, 374389, 374051, 373714, 373377,
  87. 373040, 372703, 372367, 372031, 371695, 371360, 371025, 370690, 370356, 370022,
  88. 369688, 369355, 369021, 368688, 368356, 368023, 367691, 367360, 367028, 366697,
  89. 366366, 366036, 365706, 365376, 365046, 364717, 364388, 364059, 363731, 363403,
  90. 363075, 362747, 362420, 362093, 361766, 361440, 361114, 360788, 360463, 360137,
  91. 359813, 359488, 359164, 358840, 358516, 358193, 357869, 357547, 357224, 356902,
  92. 356580, 356258, 355937, 355616, 355295, 354974, 354654, 354334, 354014, 353695,
  93. 353376, 353057, 352739, 352420, 352103, 351785, 351468, 351150, 350834, 350517,
  94. 350201, 349885, 349569, 349254, 348939, 348624, 348310, 347995, 347682, 347368,
  95. 347055, 346741, 346429, 346116, 345804, 345492, 345180, 344869, 344558, 344247,
  96. 343936, 343626, 343316, 343006, 342697, 342388, 342079, 341770, 341462, 341154,
  97. 340846, 340539, 340231, 339924, 339618, 339311, 339005, 338700, 338394, 338089,
  98. 337784, 337479, 337175, 336870, 336566, 336263, 335959, 335656, 335354, 335051,
  99. 334749, 334447, 334145, 333844, 333542, 333242, 332941, 332641, 332341, 332041,
  100. 331741, 331442, 331143, 330844, 330546, 330247, 329950, 329652, 329355, 329057,
  101. 328761, 328464, 328168, 327872, 327576, 327280, 326985, 326690, 326395, 326101,
  102. 325807, 325513, 325219, 324926, 324633, 324340, 324047, 323755, 323463, 323171,
  103. 322879, 322588, 322297, 322006, 321716, 321426, 321136, 320846, 320557, 320267,
  104. 319978, 319690, 319401, 319113, 318825, 318538, 318250, 317963, 317676, 317390,
  105. 317103, 316817, 316532, 316246, 315961, 315676, 315391, 315106, 314822, 314538,
  106. 314254, 313971, 313688, 313405, 313122, 312839, 312557, 312275, 311994, 311712,
  107. 311431, 311150, 310869, 310589, 310309, 310029, 309749, 309470, 309190, 308911,
  108. 308633, 308354, 308076, 307798, 307521, 307243, 306966, 306689, 306412, 306136,
  109. 305860, 305584, 305308, 305033, 304758, 304483, 304208, 303934, 303659, 303385,
  110. 303112, 302838, 302565, 302292, 302019, 301747, 301475, 301203, 300931, 300660,
  111. 300388, 300117, 299847, 299576, 299306, 299036, 298766, 298497, 298227, 297958,
  112. 297689, 297421, 297153, 296884, 296617, 296349, 296082, 295815, 295548, 295281,
  113. 295015, 294749, 294483, 294217, 293952, 293686, 293421, 293157, 292892, 292628,
  114. 292364, 292100, 291837, 291574, 291311, 291048, 290785, 290523, 290261, 289999,
  115. 289737, 289476, 289215, 288954, 288693, 288433, 288173, 287913, 287653, 287393,
  116. 287134, 286875, 286616, 286358, 286099, 285841, 285583, 285326, 285068, 284811,
  117. 284554, 284298, 284041, 283785, 283529, 283273, 283017, 282762, 282507, 282252,
  118. 281998, 281743, 281489, 281235, 280981, 280728, 280475, 280222, 279969, 279716,
  119. 279464, 279212, 278960, 278708, 278457, 278206, 277955, 277704, 277453, 277203,
  120. 276953, 276703, 276453, 276204, 275955, 275706, 275457, 275209, 274960, 274712,
  121. 274465, 274217, 273970, 273722, 273476, 273229, 272982, 272736, 272490, 272244,
  122. 271999, 271753, 271508, 271263, 271018, 270774, 270530, 270286, 270042, 269798,
  123. 269555, 269312, 269069, 268826, 268583, 268341, 268099, 267857
  124. };
  125. const unsigned short XM::sAmigaFreqTable[] =
  126. {
  127. 907, 900, 894, 887, 881, 875, 868, 862, 856, 850, 844, 838, 832, 826, 820, 814,
  128. 808, 802, 796, 791, 785, 779, 774, 768, 762, 757, 752, 746, 741, 736, 730, 725,
  129. 720, 715, 709, 704, 699, 694, 689, 684, 678, 675, 670, 665, 660, 655, 651, 646,
  130. 640, 636, 632, 628, 623, 619, 614, 610, 604, 601, 597, 592, 588, 584, 580, 575,
  131. 570, 567, 563, 559, 555, 551, 547, 543, 538, 535, 532, 528, 524, 520, 516, 513,
  132. 508, 505, 502, 498, 494, 491, 487, 484, 480, 477, 474, 470, 467, 463, 460, 457,
  133. 453, 450, 447, 443, 440, 437, 434, 431, 428
  134. };
  135. const signed char XM::sRetrigAddTable[] =
  136. {
  137. 0, -1, -2, -4, -8, -16, 0, 0, 0, +1, +2, +4, +8, +16, 0, 0
  138. };
  139. const signed char XM::sRetrigMulTable[] =
  140. {
  141. 0, 0, 0, 0, 0, 0, 11, 8, 0, 0, 0, 0, 0, 0, 24, 32
  142. };
  143. const XMNote XM::sEmptyNote = {0, 0, 0, 0, 0};
  144. XM::XM(Audio* audio, const std::string& name) :
  145. Song(audio, name),
  146. mPos(0)
  147. {
  148. }
  149. XM::~XM()
  150. {
  151. release();
  152. }
  153. void XM::load(Deserializer& source, ResourceCache* cache)
  154. {
  155. PROFILE(XM_Load);
  156. release();
  157. unsigned pos;
  158. unsigned memoryUse = 0;
  159. // Read in the identification
  160. memset(&mXMId, 0, sizeof mXMId);
  161. source.read(&mXMId.mId, 17);
  162. source.read(&mXMId.mModName, 20);
  163. mXMId.mIdByte = source.readUByte();
  164. source.read(&mXMId.mTrackerName, 20);
  165. mXMId.mVersion = source.readUShort();
  166. memoryUse += sizeof mXMId;
  167. // Check that it's an XM and the version is correct
  168. if ((mXMId.mIdByte != 0x1a) || (memcmp("Extended Module:", mXMId.mId, 16)))
  169. EXCEPTION("Could not determine XM format from " + source.getName());
  170. mXMId.mIdByte = 0x0; // Serves as the songmName endzero from now on
  171. if (mXMId.mVersion < 0x103)
  172. EXCEPTION("Unsupported XM data version in " + source.getName());
  173. // Read in the header & orderlist
  174. pos = source.getPosition();
  175. mXMHeader.mHeaderSize = source.readUInt();
  176. mXMHeader.mSongLength = source.readUShort();
  177. mXMHeader.mRestartPos = source.readUShort();
  178. mXMHeader.mNumChannels = source.readUShort();
  179. mXMHeader.mNumPatterns = source.readUShort();
  180. mXMHeader.mNumInstruments = source.readUShort();
  181. mXMHeader.mUseLinear = source.readUShort();
  182. mXMHeader.mDefaultTickTempo = source.readUShort();
  183. mXMHeader.mDefaultBpmTempo = source.readUShort();
  184. source.read(mXMHeader.mOrder, 256);
  185. memoryUse += sizeof mXMHeader;
  186. // Create tracks
  187. mTracks.resize(mXMHeader.mNumChannels);
  188. // Skip to next section
  189. source.seek(pos + mXMHeader.mHeaderSize);
  190. // Load the patterns
  191. mPatterns.resize(mXMHeader.mNumPatterns);
  192. XMPackedPattern loadPattern;
  193. for (unsigned i = 0; i < mXMHeader.mNumPatterns; ++i)
  194. {
  195. // Load pattern header
  196. pos = source.getPosition();
  197. loadPattern.mHeaderSize = source.readUInt();
  198. loadPattern.mPackingType = source.readUByte();
  199. loadPattern.mRows = source.readUShort();
  200. loadPattern.mPackSize = source.readUShort();
  201. // Skip to next section
  202. source.seek(pos + loadPattern.mHeaderSize);
  203. // Allocate memory for unpacked pattern & clear
  204. mPatterns[i].mRows = loadPattern.mRows;
  205. unsigned patternSize = 5 * loadPattern.mRows * mXMHeader.mNumChannels;
  206. mPatterns[i].mData = new unsigned char[patternSize];
  207. memset(mPatterns[i].mData.getPtr(), 0, patternSize);
  208. memoryUse += patternSize;
  209. if (loadPattern.mPackSize)
  210. {
  211. SharedArrayPtr<unsigned char> packBuffer(new unsigned char[loadPattern.mPackSize]);
  212. source.read(packBuffer.getPtr(), loadPattern.mPackSize);
  213. unsigned char* packPtr = packBuffer.getPtr();
  214. unsigned char* unpackPtr = mPatterns[i].mData.getPtr();
  215. unsigned packLeft = loadPattern.mPackSize;
  216. while (packLeft)
  217. {
  218. unsigned char control = *packPtr++;
  219. --packLeft;
  220. // Packed?
  221. if (control & 0x80)
  222. {
  223. // Note?
  224. if (control & 0x01)
  225. {
  226. *unpackPtr++ = *packPtr++;
  227. --packLeft;
  228. }
  229. else ++unpackPtr;
  230. // Instrument?
  231. if (control & 0x02)
  232. {
  233. *unpackPtr++ = *packPtr++;
  234. --packLeft;
  235. }
  236. else ++unpackPtr;
  237. // Volume column?
  238. if (control & 0x04)
  239. {
  240. *unpackPtr++ = *packPtr++;
  241. --packLeft;
  242. }
  243. else ++unpackPtr;
  244. // Effect?
  245. if (control & 0x08)
  246. {
  247. *unpackPtr++ = *packPtr++;
  248. --packLeft;
  249. }
  250. else ++unpackPtr;
  251. // Effect parameter?
  252. if (control & 0x10)
  253. {
  254. *unpackPtr++ = *packPtr++;
  255. --packLeft;
  256. }
  257. else ++unpackPtr;
  258. }
  259. else
  260. {
  261. *unpackPtr++ = control; // Note
  262. *unpackPtr++ = *packPtr++; // Instrument
  263. *unpackPtr++ = *packPtr++; // Volume c.
  264. *unpackPtr++ = *packPtr++; // Effect
  265. *unpackPtr++ = *packPtr++; // Effect p.
  266. packLeft -= 4;
  267. }
  268. }
  269. }
  270. }
  271. // Load instruments
  272. mInstruments.resize(mXMHeader.mNumInstruments);
  273. memoryUse += mXMHeader.mNumInstruments * sizeof(XMInstrument);
  274. for (unsigned i = 0; i < mXMHeader.mNumInstruments; ++i)
  275. {
  276. XMInstrument* instrPtr = &mInstruments[i];
  277. // Read the part common to all instruments
  278. pos = source.getPosition();
  279. instrPtr->mHeaderSize = source.readUInt();
  280. source.read(instrPtr->mName, 22);
  281. instrPtr->mType = source.readUByte();
  282. instrPtr->mNumSamples = source.readUShort();
  283. if (instrPtr->mNumSamples)
  284. {
  285. instrPtr->mXMSamples.resize(instrPtr->mNumSamples);
  286. instrPtr->mSounds.resize(instrPtr->mNumSamples);
  287. int s;
  288. // There are samples; read the rest in
  289. instrPtr->mSoundHeaderSize = source.readUInt();
  290. source.read(&instrPtr->mSampleTable, 96);
  291. for (s = 0; s < 12; ++s)
  292. {
  293. instrPtr->mVolumeEnv[s].mX = source.readUShort();
  294. instrPtr->mVolumeEnv[s].mY = source.readUShort();
  295. }
  296. for (s = 0; s < 12; ++s)
  297. {
  298. instrPtr->mPanningEnv[s].mX = source.readUShort();
  299. instrPtr->mPanningEnv[s].mY = source.readUShort();
  300. }
  301. instrPtr->mVolumeEnvPoints = source.readUByte();
  302. instrPtr->mPanningEnvPoints = source.readUByte();
  303. instrPtr->mVolumeSustainPoint = source.readUByte();
  304. instrPtr->mVolumeLoopStart = source.readUByte();
  305. instrPtr->mVolumeLoopEnd = source.readUByte();
  306. instrPtr->mPanningSustainPoint = source.readUByte();
  307. instrPtr->mPanningLoopStart = source.readUByte();
  308. instrPtr->mPanningLoopEnd = source.readUByte();
  309. instrPtr->mVolumeEnvFlags = source.readUByte();
  310. instrPtr->mPanningEnvFlags = source.readUByte();
  311. instrPtr->mVibratoType = source.readUByte();
  312. instrPtr->mVibratoSweep = source.readUByte();
  313. instrPtr->mVibratoDepth = source.readUByte();
  314. instrPtr->mVibratoRate = source.readUByte();
  315. instrPtr->mFadeOut = source.readUShort();
  316. instrPtr->mReserved = source.readUShort();
  317. // Skip over extra data in instr. header
  318. source.seek(pos + instrPtr->mHeaderSize);
  319. // Precalculate envelopes
  320. if (instrPtr->mVolumeEnvFlags & ENV_ON)
  321. {
  322. for (s = 0; s < instrPtr->mVolumeEnvPoints; ++s)
  323. {
  324. // Paranoid check
  325. if (instrPtr->mVolumeEnv[s].mX > 324)
  326. instrPtr->mVolumeEnv[s].mX = 324;
  327. }
  328. for (s = 0; s < instrPtr->mVolumeEnvPoints - 1; ++s)
  329. {
  330. int x, y, dx, dy;
  331. y = instrPtr->mVolumeEnv[s].mY;
  332. dx = instrPtr->mVolumeEnv[s+1].mX - instrPtr->mVolumeEnv[s].mX;
  333. dy = instrPtr->mVolumeEnv[s+1].mY - instrPtr->mVolumeEnv[s].mY;
  334. if (dx)
  335. {
  336. for (x = 0; x < dx; ++x)
  337. {
  338. instrPtr->mVolumeEnvLookup[x + instrPtr->mVolumeEnv[s].mX] =
  339. y + dy * x / dx;
  340. }
  341. }
  342. if (s == instrPtr->mVolumeEnvPoints - 2)
  343. {
  344. for (x = instrPtr->mVolumeEnv[s+1].mX; x < 325; x++)
  345. instrPtr->mVolumeEnvLookup[x] = (unsigned char)instrPtr->mVolumeEnv[s+1].mY;
  346. }
  347. }
  348. instrPtr->mVolumeSustainPoint = instrPtr->mVolumeEnv[instrPtr->mVolumeSustainPoint].mX;
  349. instrPtr->mVolumeLoopStart = instrPtr->mVolumeEnv[instrPtr->mVolumeLoopStart].mX;
  350. instrPtr->mVolumeLoopEnd = instrPtr->mVolumeEnv[instrPtr->mVolumeLoopEnd].mY;
  351. // If zero length loop, must deactivate it
  352. if (instrPtr->mVolumeLoopStart == instrPtr->mVolumeLoopEnd)
  353. instrPtr->mVolumeEnvFlags &= ~ENV_LOOP;
  354. }
  355. if (instrPtr->mPanningEnvFlags & ENV_ON)
  356. {
  357. for (s = 0; s < instrPtr->mPanningEnvPoints; ++s)
  358. {
  359. // Paranoid check
  360. if (instrPtr->mPanningEnv[s].mX > 324)
  361. instrPtr->mPanningEnv[s].mX = 324;
  362. }
  363. for (s = 0; s < instrPtr->mPanningEnvPoints - 1; ++s)
  364. {
  365. int x, y, dx, dy;
  366. y = instrPtr->mPanningEnv[s].mY;
  367. dx = instrPtr->mPanningEnv[s+1].mX - instrPtr->mPanningEnv[s].mX;
  368. dy = instrPtr->mPanningEnv[s+1].mY - instrPtr->mPanningEnv[s].mY;
  369. if (dx)
  370. {
  371. for (x = 0; x < dx; ++x)
  372. {
  373. instrPtr->mPanningEnvLookup[x + instrPtr->mPanningEnv[s].mX] =
  374. y + dy * x / dx;
  375. }
  376. }
  377. if (s == instrPtr->mPanningEnvPoints - 2)
  378. {
  379. for (x = instrPtr->mPanningEnv[s+1].mX; x < 325; ++x)
  380. instrPtr->mPanningEnvLookup[x] = (unsigned char)instrPtr->mPanningEnv[s+1].mY;
  381. }
  382. }
  383. instrPtr->mPanningSustainPoint = instrPtr->mPanningEnv[instrPtr->mPanningSustainPoint].mX;
  384. instrPtr->mPanningLoopStart = instrPtr->mPanningEnv[instrPtr->mPanningLoopStart].mX;
  385. instrPtr->mPanningLoopEnd = instrPtr->mPanningEnv[instrPtr->mPanningLoopEnd].mX;
  386. // If zero length loop, must deactivate it
  387. if (instrPtr->mPanningLoopStart == instrPtr->mPanningLoopEnd)
  388. instrPtr->mPanningEnvFlags &= ~ENV_LOOP;
  389. }
  390. // Allocate xmsample & sample structures, but do not read any audio data yet
  391. for (s = 0; s < instrPtr->mNumSamples; ++s)
  392. {
  393. XMSample *xsp = &instrPtr->mXMSamples[s];
  394. SharedPtr<Sound> sp;
  395. unsigned reserve;
  396. // Read in sample header
  397. pos = source.getPosition();
  398. xsp->mLength = source.readUInt();
  399. xsp->mLoopStart = source.readUInt();
  400. xsp->mLoopLength = source.readUInt();
  401. xsp->mVolume = source.readUByte();
  402. xsp->mFineTune = source.readUByte();
  403. xsp->mSoundFlags = source.readUByte();
  404. xsp->mPanning = source.readUByte();
  405. xsp->mRelativeNote = source.readUByte();
  406. xsp->mReserved = source.readUByte();
  407. source.read(xsp->mName, 22);
  408. // Skip extra data in sample header
  409. source.seek(pos + instrPtr->mSoundHeaderSize);
  410. // If mLoopLength is zero, loop must be deactivated
  411. if (!xsp->mLoopLength)
  412. xsp->mSoundFlags &= ~(SMP_LOOP | SMP_PINGPONGLOOP);
  413. reserve = xsp->mLength;
  414. // There might be zero length samples
  415. if (reserve)
  416. {
  417. // Reserve sample
  418. if (xsp->mSoundFlags & SMP_PINGPONGLOOP) reserve += xsp->mLoopLength;
  419. sp = new Sound();
  420. sp->setSize(reserve);
  421. memoryUse += sp->getMemoryUse();
  422. }
  423. instrPtr->mSounds[s] = sp;
  424. }
  425. // Now load audio data
  426. for (s = 0; s < instrPtr->mNumSamples; ++s)
  427. {
  428. XMSample *xsp = &instrPtr->mXMSamples[s];
  429. Sound *sp = instrPtr->mSounds[s];
  430. if (sp)
  431. {
  432. // Read sample data (delta values)
  433. source.read(sp->getStart(), xsp->mLength);
  434. // Convert to normal signed data
  435. if (xsp->mSoundFlags & SMP_16BIT)
  436. {
  437. sp->setFormat(sp->getIntFrequency(), true, false);
  438. int cc = xsp->mLength >> 1;
  439. unsigned short old = 0;
  440. unsigned short *cptr = (unsigned short *)sp->getStart();
  441. while (cc--)
  442. {
  443. *cptr += old;
  444. old = *cptr++;
  445. }
  446. }
  447. else
  448. {
  449. int cc = xsp->mLength;
  450. signed char old = 0;
  451. signed char *cptr = sp->getStart();
  452. while (cc--)
  453. {
  454. *cptr += old;
  455. old = *cptr++;
  456. }
  457. }
  458. // "Unroll" pingpong-loop
  459. if (xsp->mSoundFlags & SMP_PINGPONGLOOP)
  460. {
  461. if (xsp->mSoundFlags & SMP_16BIT)
  462. {
  463. int uc = xsp->mLoopLength >> 1;
  464. short *source = (short *)(sp->getStart() + xsp->mLoopStart + xsp->mLoopLength - 2);
  465. short *dest = (short *)(sp->getStart() + xsp->mLoopStart + xsp->mLoopLength);
  466. while (uc--) *dest++ = *source--;
  467. }
  468. else
  469. {
  470. int uc = xsp->mLoopLength;
  471. signed char *source = sp->getStart() + xsp->mLoopStart + xsp->mLoopLength - 1;
  472. signed char *dest = sp->getStart() + xsp->mLoopStart + xsp->mLoopLength;
  473. while (uc--) *dest++ = *source--;
  474. }
  475. xsp->mLoopLength <<= 1;
  476. }
  477. // Set loop
  478. if (xsp->mSoundFlags & (SMP_LOOP|SMP_PINGPONGLOOP))
  479. sp->setLoop(xsp->mLoopStart, xsp->mLoopStart + xsp->mLoopLength);
  480. }
  481. }
  482. }
  483. else
  484. {
  485. // Header without samples; skip over extra if any
  486. source.seek(source.getPosition() + instrPtr->mHeaderSize - 29);
  487. }
  488. }
  489. setMemoryUse(memoryUse);
  490. mLoaded = true;
  491. }
  492. void XM::release()
  493. {
  494. stop();
  495. mLoaded = false;
  496. mTracks.clear();
  497. mPatterns.clear();
  498. mInstruments.clear();
  499. }
  500. void XM::play(unsigned position)
  501. {
  502. if (!mLoaded) return;
  503. stop();
  504. if (position >= mXMHeader.mSongLength)
  505. position = 0;
  506. mPos = position;
  507. mLine = 0;
  508. mTickCount = 0;
  509. mTickTempo = mXMHeader.mDefaultTickTempo;
  510. mGlobalVol = 64;
  511. mGlobalVolSpeedUp = 0;
  512. mGlobalVolSpeedDown = 0;
  513. mPatternDelay = 0;
  514. setBpmTempo(mXMHeader.mDefaultBpmTempo);
  515. setNumChannels(mXMHeader.mNumChannels);
  516. for (unsigned i = 0; i < mXMHeader.mNumChannels; ++i)
  517. {
  518. XMTrack* tptr = &mTracks[i];
  519. Channel* chptr = mChannels[i];
  520. memset(tptr, 0, sizeof(XMTrack));
  521. tptr->mInstr = &mInstruments[0];
  522. }
  523. if (mXMHeader.mUseLinear)
  524. {
  525. mLowPeriod = 7743;
  526. mHighPeriod = 64;
  527. }
  528. else
  529. {
  530. mLowPeriod = 29024;
  531. mHighPeriod = 28;
  532. }
  533. mPlaying = true;
  534. if (mAudio)
  535. mAudio->addSong(this);
  536. }
  537. void XM::updatePlayer()
  538. {
  539. XMPattern *pattPtr = &mPatterns[mXMHeader.mOrder[mPos]];
  540. // Set new notes or do something else?
  541. if ((!mTickCount) && (!mPatternDelay))
  542. {
  543. const XMNote* notePtr = (XMNote *)pattPtr->mData.getPtr();
  544. if (!notePtr) notePtr = &sEmptyNote;
  545. else notePtr += mXMHeader.mNumChannels * mLine;
  546. mPatternBreak = false;
  547. for (unsigned i = 0; i < mXMHeader.mNumChannels; ++i)
  548. {
  549. XMTrack* tptr = &mTracks[i];
  550. Channel* chptr = mChannels[i];
  551. tptr->mNewNote = 0;
  552. tptr->mRetrigCount = 0;
  553. // Get note (if any)
  554. if (notePtr->mNote)
  555. {
  556. tptr->mNote = notePtr->mNote - 1;
  557. tptr->mNewNote = 1;
  558. }
  559. // Get effect & data
  560. tptr->mVolEffect = notePtr->mVolEffect;
  561. tptr->mEffect = notePtr->mEffect;
  562. tptr->mEffectData = notePtr->mEffectData;
  563. tptr->mNybble1 = notePtr->mEffectData >> 4;
  564. tptr->mNybble2 = notePtr->mEffectData & 0xf;
  565. tptr->mNewInstrument = notePtr->mInstrument;
  566. // Set sampleoffset here
  567. if (tptr->mEffect == 0x9)
  568. {
  569. if (tptr->mEffectData)
  570. tptr->mSoundOffset = tptr->mEffectData;
  571. }
  572. // Start new note if there is one
  573. if ((tptr->mEffect != 0xe) || (tptr->mNybble1 != 0xd) || (tptr->mNybble2 == 0))
  574. {
  575. if (tptr->mNewNote) startNewNote(tptr, chptr);
  576. if (tptr->mNewInstrument) changeInstrument(tptr);
  577. }
  578. // Reset period if not vibrato or toneportamento
  579. if (((tptr->mEffect < 0x3) || (tptr->mEffect > 0x6)) && (tptr->mVolEffect != 0xb) && (tptr->mVolEffect != 0xf))
  580. tptr->mPeriod = tptr->mBasePeriod;
  581. // Reset volume if not tremolo / tremor
  582. if ((tptr->mEffect != 0x7) && (tptr->mEffect != 0x1d))
  583. tptr->mVolume = tptr->mNoteVolume;
  584. // Now check effects: volume column done first
  585. switch (tptr->mVolEffect >> 4)
  586. {
  587. case 0x0:
  588. break;
  589. // Set volume
  590. case 0x1:
  591. case 0x2:
  592. case 0x3:
  593. case 0x4:
  594. case 0x5:
  595. // Applies only if there isn't notedelay
  596. if ((tptr->mEffect != 0xe) || (tptr->mNybble1 != 0xd) || (tptr->mNybble2 == 0))
  597. {
  598. tptr->mNoteVolume = tptr->mVolEffect - 0x10;
  599. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  600. tptr->mVolume = tptr->mNoteVolume;
  601. }
  602. break;
  603. // Fine volslide down
  604. case 0x8:
  605. tptr->mNoteVolume -= tptr->mVolEffect & 0xf;
  606. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  607. tptr->mVolume = tptr->mNoteVolume;
  608. break;
  609. // Fine volslide up
  610. case 0x9:
  611. tptr->mNoteVolume += tptr->mVolEffect & 0xf;
  612. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  613. tptr->mVolume = tptr->mNoteVolume;
  614. break;
  615. // Set vibrato speed
  616. case 0xa:
  617. if (tptr->mVolEffect & 0xf)
  618. tptr->mVibratoSpeed = tptr->mVolEffect & 0xf;
  619. break;
  620. // Vibrato
  621. case 0xb:
  622. if (tptr->mVolEffect & 0xf)
  623. tptr->mVibratoDepth = tptr->mVolEffect & 0xf;
  624. break;
  625. // Set panning
  626. case 0xc:
  627. // Applies only if there isn't notedelay
  628. if ((tptr->mEffect != 0xe) || (tptr->mNybble1 != 0xd) || (tptr->mNybble2 == 0))
  629. tptr->mNotePanning = (tptr->mVolEffect & 0xf) << 4 | (tptr->mVolEffect & 0xf);
  630. break;
  631. // Toneportamento
  632. case 0xf:
  633. if (tptr->mVolEffect & 0xf)
  634. tptr->mTonePortaSpeed = (tptr->mVolEffect & 0xf) << 4;
  635. break;
  636. }
  637. // Then the regular effects
  638. switch (tptr->mEffect)
  639. {
  640. case 0x0:
  641. break;
  642. // Set portamento speed up
  643. case 0x1:
  644. if (tptr->mEffectData) tptr->mPortaSpeedUp = tptr->mEffectData;
  645. break;
  646. // Set portamento speed down
  647. case 0x2:
  648. if (tptr->mEffectData) tptr->mPortaSpeedDown = tptr->mEffectData;
  649. break;
  650. // Set TP. speed
  651. case 0x3:
  652. if (tptr->mEffectData) tptr->mTonePortaSpeed = tptr->mEffectData;
  653. break;
  654. // Set vibrato
  655. case 0x4:
  656. if (tptr->mNybble1) tptr->mVibratoSpeed = tptr->mNybble1;
  657. if (tptr->mNybble2) tptr->mVibratoDepth = tptr->mNybble2;
  658. break;
  659. // Set tremolo
  660. case 0x7:
  661. if (tptr->mNybble1) tptr->mTremoloSpeed = tptr->mNybble1;
  662. if (tptr->mNybble2) tptr->mTremoloDepth = tptr->mNybble2;
  663. break;
  664. // Set panning
  665. case 0x8:
  666. tptr->mNotePanning = tptr->mEffectData;
  667. break;
  668. // ´Set volume slide speed
  669. case 0x5:
  670. case 0x6:
  671. case 0xa:
  672. if (tptr->mEffectData)
  673. {
  674. tptr->mVolumeSpeedUp = tptr->mNybble1;
  675. tptr->mVolumeSpeedDown = tptr->mNybble2;
  676. }
  677. break;
  678. // Pos. jump
  679. case 0xb:
  680. mLine = pattPtr->mRows - 1;
  681. mPos = tptr->mEffectData - 1;
  682. break;
  683. // Set volume
  684. case 0xc:
  685. tptr->mNoteVolume = tptr->mEffectData;
  686. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  687. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  688. tptr->mVolume = tptr->mNoteVolume;
  689. break;
  690. // Pattern break
  691. case 0xd:
  692. if (!mPatternBreak)
  693. {
  694. mPatternBreak = true;
  695. mLine = tptr->mNybble1 * 10 + tptr->mNybble2 - 1;
  696. mPos++;
  697. }
  698. break;
  699. // Extended command
  700. case 0xe:
  701. extendedCommand(tptr, chptr);
  702. break;
  703. // Set tempo
  704. case 0xf:
  705. if (tptr->mEffectData < 32) mTickTempo = tptr->mEffectData;
  706. else setBpmTempo(tptr->mEffectData);
  707. break;
  708. // Global volume
  709. case 0x10:
  710. mGlobalVol = tptr->mEffectData;
  711. if (mGlobalVol > 64) mGlobalVol = 64;
  712. break;
  713. // Global volume slide
  714. case 0x11:
  715. if (tptr->mEffectData)
  716. {
  717. mGlobalVolSpeedUp = tptr->mNybble1;
  718. mGlobalVolSpeedDown = tptr->mNybble2;
  719. }
  720. break;
  721. // Keyoff
  722. case 0x14:
  723. tptr->mKeyOn = false;
  724. if (!(tptr->mInstr->mVolumeEnvFlags & ENV_ON))
  725. {
  726. tptr->mNoteVolume = 0;
  727. tptr->mVolume = 0;
  728. }
  729. break;
  730. // Set envpos
  731. case 0x15:
  732. tptr->mVolumeEnvPos = tptr->mEffectData;
  733. tptr->mPanningEnvPos = tptr->mEffectData;
  734. break;
  735. // Panning slide
  736. case 0x19:
  737. if (tptr->mEffectData)
  738. {
  739. tptr->mPanningSpeedRight = tptr->mNybble1;
  740. tptr->mPanningSpeedLeft = tptr->mNybble2;
  741. }
  742. break;
  743. // Multi retrig
  744. case 0x1b:
  745. if (tptr->mNybble1) tptr->mRetrigVolChange = tptr->mNybble1;
  746. if (tptr->mNybble2) tptr->mRetrigInterval = tptr->mNybble2;
  747. if (tptr->mMultiRetrigCount >= tptr->mRetrigInterval)
  748. {
  749. startNewNote(tptr, chptr);
  750. tptr->mMultiRetrigCount = 0;
  751. if (!sRetrigMulTable[tptr->mRetrigVolChange])
  752. {
  753. tptr->mNoteVolume += sRetrigAddTable[tptr->mRetrigVolChange];
  754. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  755. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  756. tptr->mVolume = tptr->mNoteVolume;
  757. }
  758. else
  759. {
  760. tptr->mNoteVolume = (tptr->mNoteVolume * sRetrigMulTable[tptr->mRetrigVolChange]) >> 4;
  761. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  762. tptr->mVolume = tptr->mNoteVolume;
  763. }
  764. }
  765. tptr->mMultiRetrigCount++;
  766. break;
  767. // Tremor
  768. case 0x1d:
  769. if (tptr->mEffectData)
  770. {
  771. tptr->mTremorOnTime = tptr->mNybble1;
  772. tptr->mTremorOffTime = tptr->mNybble2;
  773. }
  774. break;
  775. // Extra fine portamento
  776. case 0x21:
  777. switch (tptr->mNybble1)
  778. {
  779. case 1:
  780. if (tptr->mNybble2) tptr->mPortaSpeedUp = tptr->mNybble2;
  781. tptr->mBasePeriod -= tptr->mPortaSpeedUp;
  782. if (tptr->mBasePeriod < mHighPeriod) tptr->mBasePeriod = mHighPeriod;
  783. tptr->mPeriod = tptr->mBasePeriod;
  784. break;
  785. case 2:
  786. if (tptr->mNybble2) tptr->mPortaSpeedDown = tptr->mNybble2;
  787. tptr->mBasePeriod += tptr->mPortaSpeedDown;
  788. if (tptr->mBasePeriod > mLowPeriod) tptr->mBasePeriod = mLowPeriod;
  789. tptr->mPeriod = tptr->mBasePeriod;
  790. break;
  791. }
  792. break;
  793. }
  794. if (notePtr != &sEmptyNote) notePtr++;
  795. }
  796. }
  797. if (mTickCount)
  798. {
  799. // If tick isn't 0, update "continuous" effects
  800. for (unsigned i = 0; i < mXMHeader.mNumChannels; ++i)
  801. {
  802. XMTrack* tptr = &mTracks[i];
  803. Channel* chptr = mChannels[i];
  804. switch (tptr->mVolEffect >> 4)
  805. {
  806. case 0x0:
  807. break;
  808. // Volslide down
  809. case 0x6:
  810. tptr->mNoteVolume -= tptr->mVolEffect & 0xf;
  811. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  812. tptr->mVolume = tptr->mNoteVolume;
  813. break;
  814. // Volslide up
  815. case 0x7:
  816. tptr->mNoteVolume += tptr->mVolEffect & 0xf;
  817. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  818. tptr->mVolume = tptr->mNoteVolume;
  819. break;
  820. // Vibrato
  821. case 0xb:
  822. tptr->mVibratoPhase += tptr->mVibratoSpeed * 4;
  823. tptr->mPeriod = tptr->mBasePeriod + ((Song::sVibratoTable[tptr->mVibratoType & 3][tptr->mVibratoPhase] * tptr->mVibratoDepth) >> 3);
  824. if (tptr->mPeriod < mHighPeriod) tptr->mPeriod = mHighPeriod;
  825. if (tptr->mPeriod > mLowPeriod) tptr->mPeriod = mLowPeriod;
  826. break;
  827. // Panslide left
  828. case 0xd:
  829. {
  830. int newPan = tptr->mNotePanning;
  831. newPan -= tptr->mVolEffect & 0xf;
  832. if (newPan < 0) newPan = 0;
  833. tptr->mNotePanning = newPan;
  834. }
  835. break;
  836. // Panslide right
  837. case 0xe:
  838. {
  839. int newPan = tptr->mNotePanning;
  840. newPan += tptr->mVolEffect & 0xf;
  841. if (newPan > 255) newPan = 255;
  842. tptr->mNotePanning = newPan;
  843. }
  844. break;
  845. // Toneportamento
  846. case 0xf:
  847. tonePortamento(tptr, chptr);
  848. break;
  849. }
  850. // Regular effects
  851. switch (tptr->mEffect)
  852. {
  853. // Arpeggio
  854. case 0x0:
  855. if (tptr->mEffectData)
  856. {
  857. char phase = mTickCount % 3;
  858. switch (phase)
  859. {
  860. case 0:
  861. tptr->mPeriod = tptr->mBasePeriod;
  862. break;
  863. case 1:
  864. if (mXMHeader.mUseLinear) tptr->mPeriod = tptr->mBasePeriod - tptr->mNybble1 * 64;
  865. else tptr->mPeriod = getAmigaPeriod(tptr->mRealNote + tptr->mNybble1, tptr->mFineTune);
  866. if (tptr->mPeriod < mHighPeriod) tptr->mPeriod = mHighPeriod;
  867. break;
  868. case 2:
  869. if (mXMHeader.mUseLinear) tptr->mPeriod = tptr->mBasePeriod - tptr->mNybble2 * 64;
  870. else tptr->mPeriod = getAmigaPeriod(tptr->mRealNote + tptr->mNybble2, tptr->mFineTune);
  871. if (tptr->mPeriod < mHighPeriod) tptr->mPeriod = mHighPeriod;
  872. break;
  873. }
  874. }
  875. break;
  876. // Portamento up
  877. case 0x1:
  878. tptr->mBasePeriod -= tptr->mPortaSpeedUp * 4;
  879. if (tptr->mBasePeriod < mHighPeriod) tptr->mBasePeriod = mHighPeriod;
  880. tptr->mPeriod = tptr->mBasePeriod;
  881. break;
  882. // Portamento down
  883. case 0x2:
  884. tptr->mBasePeriod += tptr->mPortaSpeedDown * 4;
  885. if (tptr->mBasePeriod > mLowPeriod) tptr->mBasePeriod = mLowPeriod;
  886. tptr->mPeriod = tptr->mBasePeriod;
  887. break;
  888. // Toneportamento
  889. case 0x3:
  890. tonePortamento(tptr, chptr);
  891. break;
  892. // Vibrato
  893. case 0x4:
  894. vibrato(tptr, chptr);
  895. break;
  896. // Toneportamento + volslide
  897. case 0x5:
  898. tonePortamento(tptr, chptr);
  899. volumeSlide(tptr, chptr);
  900. break;
  901. // Vibrato + volslide
  902. case 0x6:
  903. vibrato(tptr, chptr);
  904. volumeSlide(tptr, chptr);
  905. break;
  906. // Tremolo
  907. case 0x7:
  908. tptr->mTremoloPhase += tptr->mTremoloSpeed * 4;
  909. tptr->mVolume = tptr->mNoteVolume + ((Song::sVibratoTable[tptr->mTremoloType & 3][tptr->mTremoloPhase] * tptr->mTremoloDepth) >> 4);
  910. if (tptr->mVolume < 0) tptr->mVolume = 0;
  911. if (tptr->mVolume > 64) tptr->mVolume = 64;
  912. break;
  913. // Volume slide
  914. case 0xa:
  915. volumeSlide(tptr, chptr);
  916. break;
  917. // Extended command
  918. case 0xe:
  919. extendedCommand(tptr, chptr);
  920. break;
  921. // Global volume slide
  922. case 0x11:
  923. if (mGlobalVolSpeedUp)
  924. {
  925. mGlobalVol += mGlobalVolSpeedUp;
  926. if (mGlobalVol > 64) mGlobalVol = 64;
  927. }
  928. else
  929. {
  930. mGlobalVol -= mGlobalVolSpeedDown;
  931. if (mGlobalVol < 0) mGlobalVol = 0;
  932. }
  933. break;
  934. // Panning slide
  935. case 0x19:
  936. {
  937. int newPan = tptr->mNotePanning;
  938. newPan += tptr->mPanningSpeedRight;
  939. if (newPan < 0) newPan = 0;
  940. newPan -= tptr->mPanningSpeedLeft;
  941. if (newPan > 255) newPan = 255;
  942. tptr->mNotePanning = newPan;
  943. }
  944. break;
  945. // Multi retrig
  946. case 0x1b:
  947. if (tptr->mMultiRetrigCount >= tptr->mRetrigInterval)
  948. {
  949. startNewNote(tptr, chptr);
  950. tptr->mMultiRetrigCount = 0;
  951. if (!sRetrigMulTable[tptr->mRetrigVolChange])
  952. {
  953. tptr->mNoteVolume += sRetrigAddTable[tptr->mRetrigVolChange];
  954. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  955. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  956. tptr->mVolume = tptr->mNoteVolume;
  957. }
  958. else
  959. {
  960. tptr->mNoteVolume = (tptr->mNoteVolume * sRetrigMulTable[tptr->mRetrigVolChange]) >> 4;
  961. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  962. tptr->mVolume = tptr->mNoteVolume;
  963. }
  964. }
  965. tptr->mMultiRetrigCount++;
  966. break;
  967. // Tremor
  968. case 0x1d:
  969. if (!tptr->mTremorCount)
  970. {
  971. tptr->mTremorStatus ^= 1;
  972. if (tptr->mTremorStatus) tptr->mTremorCount = tptr->mTremorOnTime + 1;
  973. else tptr->mTremorCount = tptr->mTremorOffTime + 1;
  974. }
  975. if (tptr->mTremorStatus) tptr->mVolume = tptr->mNoteVolume;
  976. else tptr->mVolume = 0;
  977. tptr->mTremorCount--;
  978. break;
  979. }
  980. chptr++;
  981. tptr++;
  982. }
  983. }
  984. // Update envelopes and set the frequency, volume & panning of the channel
  985. for (unsigned i = 0; i < mXMHeader.mNumChannels; ++i)
  986. {
  987. XMTrack* tptr = &mTracks[i];
  988. Channel* chptr = mChannels[i];
  989. XMInstrument *instrPtr = tptr->mInstr;
  990. short finalPeriod = tptr->mPeriod;
  991. // Update fadeout (must be done before before calculating the final volume)
  992. if (!tptr->mKeyOn)
  993. {
  994. tptr->mFadeOutValue -= instrPtr->mFadeOut;
  995. if (tptr->mFadeOutValue < 0) tptr->mFadeOutValue = 0;
  996. }
  997. // Calculate final channel values
  998. int finalVol;
  999. if (instrPtr->mVolumeEnvFlags & ENV_ON)
  1000. finalVol = (tptr->mVolume * mGlobalVol * instrPtr->mVolumeEnvLookup[tptr->mVolumeEnvPos] * (tptr->mFadeOutValue >> 9)) >> 18;
  1001. else
  1002. finalVol = (tptr->mVolume * mGlobalVol * (tptr->mFadeOutValue >> 9)) >> 12;
  1003. int finalPan;
  1004. if (instrPtr->mPanningEnvFlags & ENV_ON)
  1005. finalPan = tptr->mNotePanning + (instrPtr->mPanningEnvLookup[tptr->mPanningEnvPos] - 32) * (128 - abs(tptr->mNotePanning - 128)) / 32;
  1006. else
  1007. finalPan = tptr->mNotePanning;
  1008. chptr->setIntVolume(finalVol);
  1009. chptr->setIntPanning(finalPan);
  1010. // Update instrument vibrato
  1011. if ((instrPtr->mVibratoDepth) && (instrPtr->mVibratoRate))
  1012. {
  1013. if (instrPtr->mVibratoSweep)
  1014. {
  1015. if (tptr->mKeyOn)
  1016. {
  1017. tptr->mInstrVibratoDepth += (instrPtr->mVibratoDepth << 8) / instrPtr->mVibratoSweep;
  1018. if (tptr->mInstrVibratoDepth > (instrPtr->mVibratoDepth << 8)) tptr->mInstrVibratoDepth = instrPtr->mVibratoDepth << 8;
  1019. }
  1020. }
  1021. else tptr->mInstrVibratoDepth = instrPtr->mVibratoDepth << 8;
  1022. tptr->mInstrVibratoPhase += instrPtr->mVibratoRate;
  1023. finalPeriod += (Song::sVibratoTable[instrPtr->mVibratoType & 3][tptr->mInstrVibratoPhase] * tptr->mInstrVibratoDepth) >> 14;
  1024. }
  1025. if (finalPeriod < (mHighPeriod - 15)) finalPeriod = mHighPeriod - 15;
  1026. if (finalPeriod > (mLowPeriod + 15)) finalPeriod = mLowPeriod + 15;
  1027. if (mXMHeader.mUseLinear)
  1028. chptr->setIntFrequency(sLinearFreqTable[finalPeriod % 768] >> (finalPeriod / 768));
  1029. else
  1030. chptr->setIntFrequency(14317456 / finalPeriod);
  1031. // Update envelopes
  1032. if ((instrPtr->mVolumeEnvFlags & ENV_ON) && ((!(instrPtr->mVolumeEnvFlags & ENV_SUSTAIN)) || (!tptr->mKeyOn) || (tptr->mVolumeEnvPos != instrPtr->mVolumeSustainPoint)))
  1033. {
  1034. tptr->mVolumeEnvPos++;
  1035. if (tptr->mVolumeEnvPos > 324) tptr->mVolumeEnvPos = 324;
  1036. if (instrPtr->mVolumeEnvFlags & ENV_LOOP)
  1037. {
  1038. if (tptr->mVolumeEnvPos >= instrPtr->mVolumeLoopEnd)
  1039. tptr->mVolumeEnvPos = instrPtr->mVolumeLoopStart;
  1040. }
  1041. }
  1042. if ((instrPtr->mPanningEnvFlags & ENV_ON) && ((!(instrPtr->mPanningEnvFlags & ENV_SUSTAIN)) || (!tptr->mKeyOn) || (tptr->mPanningEnvPos != instrPtr->mPanningSustainPoint)))
  1043. {
  1044. tptr->mPanningEnvPos++;
  1045. if (tptr->mPanningEnvPos > 324) tptr->mPanningEnvPos = 324;
  1046. if (instrPtr->mPanningEnvFlags & ENV_LOOP)
  1047. {
  1048. if (tptr->mPanningEnvPos >= instrPtr->mPanningLoopEnd)
  1049. tptr->mPanningEnvPos = instrPtr->mPanningLoopStart;
  1050. }
  1051. }
  1052. }
  1053. // Advance song
  1054. if (mTickTempo)
  1055. {
  1056. mTickCount++;
  1057. if (mTickCount >= mTickTempo)
  1058. {
  1059. mTickCount = 0;
  1060. if (mPatternDelay)
  1061. mPatternDelay--;
  1062. if (!mPatternDelay)
  1063. {
  1064. mLine++;
  1065. if (mLine >= pattPtr->mRows)
  1066. {
  1067. mLine = 0;
  1068. mPos++;
  1069. }
  1070. if (mPos >= mXMHeader.mSongLength)
  1071. mPos = mXMHeader.mRestartPos;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. void XM::startNewNote(XMTrack* tptr, Channel* chptr)
  1077. {
  1078. if (tptr->mNewInstrument)
  1079. tptr->mInstrument = tptr->mNewInstrument - 1;
  1080. // Handle keyoff
  1081. if (tptr->mNote == KEYOFF - 1)
  1082. {
  1083. tptr->mKeyOn = false;
  1084. if (!(tptr->mInstr->mVolumeEnvFlags & ENV_ON))
  1085. {
  1086. tptr->mNoteVolume = 0;
  1087. tptr->mVolume = 0;
  1088. }
  1089. return;
  1090. }
  1091. // Now get instrptr & sampleptr, but don't change them if using toneportamento
  1092. if ((tptr->mEffect == 0x3) || (tptr->mEffect == 0x5) || ((tptr->mVolEffect >> 4) == 0xf))
  1093. tptr->mTonePortamento = true;
  1094. else
  1095. {
  1096. tptr->mTonePortamento = false;
  1097. if (tptr->mInstrument < mXMHeader.mNumInstruments)
  1098. tptr->mInstr = &mInstruments[tptr->mInstrument];
  1099. tptr->mSoundNumber = tptr->mInstr->mSampleTable[tptr->mNote];
  1100. tptr->mXMSample = &tptr->mInstr->mXMSamples[tptr->mSoundNumber];
  1101. tptr->mSound = tptr->mInstr->mSounds[tptr->mSoundNumber];
  1102. }
  1103. // Don't go further if sample doesn't exist
  1104. if ((tptr->mXMSample) && (tptr->mSound))
  1105. {
  1106. tptr->mFineTune = tptr->mXMSample->mFineTune;
  1107. tptr->mMultiRetrigCount = 1;
  1108. if (!(tptr->mVibratoType & 4)) tptr->mVibratoPhase = 0;
  1109. if (!(tptr->mTremoloType & 4)) tptr->mTremoloPhase = 0;
  1110. tptr->mTremorCount = 0;
  1111. tptr->mTremorStatus = 0;
  1112. tptr->mRealNote = tptr->mNote + tptr->mXMSample->mRelativeNote;
  1113. if (tptr->mRealNote < 0) tptr->mRealNote = 0;
  1114. if (tptr->mRealNote > 118) tptr->mRealNote = 118;
  1115. if (tptr->mTonePortamento)
  1116. {
  1117. // Toneportamento
  1118. if (mXMHeader.mUseLinear) tptr->mTargetPeriod = 7680 - tptr->mRealNote * 64 - tptr->mFineTune / 2;
  1119. else tptr->mTargetPeriod = getAmigaPeriod(tptr->mRealNote, tptr->mFineTune);
  1120. }
  1121. else
  1122. {
  1123. // Normal note start
  1124. if (mXMHeader.mUseLinear) tptr->mBasePeriod = 7680 - tptr->mRealNote * 64 - tptr->mFineTune / 2;
  1125. else tptr->mBasePeriod = getAmigaPeriod(tptr->mRealNote, tptr->mFineTune);
  1126. tptr->mPeriod = tptr->mBasePeriod;
  1127. chptr->playLockless(tptr->mSound);
  1128. if (tptr->mEffect == 0x9)
  1129. {
  1130. signed char *newPos;
  1131. if (tptr->mSound->isSixteenBit())
  1132. newPos = tptr->mSound->getStart() + (tptr->mSoundOffset << 9);
  1133. else
  1134. newPos = tptr->mSound->getStart() + (tptr->mSoundOffset << 8);
  1135. if (newPos >= tptr->mSound->getEnd())
  1136. chptr->stopLockless();
  1137. else
  1138. chptr->setPlayPositionLockless(newPos);
  1139. }
  1140. }
  1141. }
  1142. }
  1143. void XM::tonePortamento(XMTrack* tptr, Channel* chptr)
  1144. {
  1145. if (tptr->mTonePortamento)
  1146. {
  1147. if (tptr->mBasePeriod < tptr->mTargetPeriod)
  1148. {
  1149. tptr->mBasePeriod += tptr->mTonePortaSpeed * 4;
  1150. if (tptr->mBasePeriod >= tptr->mTargetPeriod)
  1151. {
  1152. tptr->mBasePeriod = tptr->mTargetPeriod;
  1153. tptr->mTonePortamento = false;
  1154. }
  1155. }
  1156. if (tptr->mBasePeriod > tptr->mTargetPeriod)
  1157. {
  1158. tptr->mBasePeriod -= tptr->mTonePortaSpeed * 4;
  1159. if (tptr->mBasePeriod <= tptr->mTargetPeriod)
  1160. {
  1161. tptr->mBasePeriod = tptr->mTargetPeriod;
  1162. tptr->mTonePortamento = false;
  1163. }
  1164. }
  1165. tptr->mPeriod = tptr->mBasePeriod;
  1166. if (tptr->mGlissando)
  1167. {
  1168. if (mXMHeader.mUseLinear)
  1169. {
  1170. tptr->mPeriod += tptr->mFineTune / 2;
  1171. tptr->mPeriod += 32;
  1172. tptr->mPeriod &= 0xffc0;
  1173. tptr->mPeriod -= tptr->mFineTune / 2;
  1174. }
  1175. else
  1176. {
  1177. int offset = M_MAX_INT;
  1178. int sc;
  1179. short bestperiod = 0;
  1180. for (sc = 0; sc <= 118; sc++)
  1181. {
  1182. int newoffset = abs((int)tptr->mPeriod - (int)getAmigaPeriod(sc, tptr->mFineTune));
  1183. if (newoffset < offset)
  1184. {
  1185. bestperiod = getAmigaPeriod(sc, tptr->mFineTune);
  1186. offset = newoffset;
  1187. }
  1188. }
  1189. tptr->mPeriod = bestperiod;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. void XM::vibrato(XMTrack* tptr, Channel* chptr)
  1195. {
  1196. tptr->mVibratoPhase += tptr->mVibratoSpeed * 4;
  1197. tptr->mPeriod = tptr->mBasePeriod + ((Song::sVibratoTable[tptr->mVibratoType & 3][tptr->mVibratoPhase] * tptr->mVibratoDepth) >> 3);
  1198. if (tptr->mPeriod < mHighPeriod) tptr->mPeriod = mHighPeriod;
  1199. if (tptr->mPeriod > mLowPeriod) tptr->mPeriod = mLowPeriod;
  1200. }
  1201. void XM::volumeSlide(XMTrack* tptr, Channel* chptr)
  1202. {
  1203. if (tptr->mVolumeSpeedUp)
  1204. {
  1205. tptr->mNoteVolume += tptr->mVolumeSpeedUp;
  1206. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  1207. }
  1208. else
  1209. {
  1210. tptr->mNoteVolume -= tptr->mVolumeSpeedDown;
  1211. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  1212. }
  1213. tptr->mVolume = tptr->mNoteVolume;
  1214. }
  1215. void XM::extendedCommand(XMTrack* tptr, Channel* chptr)
  1216. {
  1217. switch(tptr->mNybble1)
  1218. {
  1219. // Fine porta up
  1220. case 0x1:
  1221. if (!mTickCount)
  1222. {
  1223. if (tptr->mNybble2) tptr->mPortaSpeedUp = tptr->mNybble2;
  1224. tptr->mBasePeriod -= tptr->mPortaSpeedUp * 4;
  1225. if (tptr->mBasePeriod < mHighPeriod) tptr->mBasePeriod = mHighPeriod;
  1226. }
  1227. break;
  1228. // Fine porta down
  1229. case 0x2:
  1230. if (!mTickCount)
  1231. {
  1232. if (tptr->mNybble2) tptr->mPortaSpeedDown = tptr->mNybble2;
  1233. tptr->mBasePeriod += tptr->mPortaSpeedDown * 4;
  1234. if (tptr->mBasePeriod > mLowPeriod) tptr->mBasePeriod = mLowPeriod;
  1235. }
  1236. break;
  1237. // Set glissando
  1238. case 0x3:
  1239. if (!mTickCount) tptr->mGlissando = tptr->mNybble2;
  1240. break;
  1241. // Set vibrato waveform
  1242. case 0x4:
  1243. if (!mTickCount)
  1244. {
  1245. tptr->mVibratoType = Song::sVibratoTypeTable[tptr->mNybble2 & 3];
  1246. tptr->mVibratoType |= tptr->mNybble2 & 4;
  1247. }
  1248. break;
  1249. // Set finetune
  1250. case 0x5:
  1251. if ((!mTickCount) && (tptr->mNewNote))
  1252. {
  1253. tptr->mFineTune = (tptr->mNybble2 << 4) - 128;
  1254. if (mXMHeader.mUseLinear) tptr->mBasePeriod = 7680 - tptr->mRealNote * 64 - tptr->mFineTune / 2;
  1255. else tptr->mBasePeriod = getAmigaPeriod(tptr->mRealNote, tptr->mFineTune);
  1256. tptr->mPeriod = tptr->mBasePeriod;
  1257. }
  1258. break;
  1259. // Patternloop
  1260. case 0x6:
  1261. if (!mTickCount)
  1262. {
  1263. if (!tptr->mNybble2)
  1264. tptr->mPatternLoopLine = mLine;
  1265. else
  1266. {
  1267. if (!tptr->mPatternLoopCount)
  1268. {
  1269. tptr->mPatternLoopCount = tptr->mNybble2;
  1270. mLine = tptr->mPatternLoopLine - 1;
  1271. }
  1272. else
  1273. {
  1274. tptr->mPatternLoopCount--;
  1275. if (tptr->mPatternLoopCount) mLine = tptr->mPatternLoopLine - 1;
  1276. }
  1277. }
  1278. }
  1279. break;
  1280. // Set tremolo waveform
  1281. case 0x7:
  1282. if (!mTickCount)
  1283. {
  1284. tptr->mTremoloType = Song::sVibratoTypeTable[tptr->mNybble2 & 3];
  1285. tptr->mTremoloType |= tptr->mNybble2 & 4;
  1286. }
  1287. break;
  1288. // Retrig
  1289. case 0x9:
  1290. if (tptr->mNybble2)
  1291. {
  1292. if (tptr->mRetrigCount >= tptr->mNybble2)
  1293. {
  1294. tptr->mRetrigCount = 0;
  1295. // Don't retrig on tick 0
  1296. if (mTickCount)
  1297. {
  1298. startNewNote(tptr, chptr);
  1299. tptr->mKeyOn = true;
  1300. tptr->mFadeOutValue = 32768;
  1301. tptr->mVolumeEnvPos = 0;
  1302. tptr->mPanningEnvPos = 0;
  1303. tptr->mInstrVibratoPhase = 0;
  1304. tptr->mInstrVibratoDepth = 0;
  1305. }
  1306. }
  1307. }
  1308. else
  1309. {
  1310. // Special case e90: start the note at tick 0 but don't restart envelope
  1311. if (!mTickCount)
  1312. {
  1313. startNewNote(tptr, chptr);
  1314. }
  1315. }
  1316. tptr->mRetrigCount++;
  1317. break;
  1318. // Notedelay
  1319. case 0xd:
  1320. // Don't start on tick 0
  1321. if (!mTickCount) break;
  1322. if (mTickCount == tptr->mNybble2)
  1323. {
  1324. startNewNote(tptr, chptr);
  1325. if (tptr->mNewInstrument)
  1326. changeInstrument(tptr);
  1327. else
  1328. {
  1329. tptr->mKeyOn = true;
  1330. tptr->mFadeOutValue = 32768;
  1331. tptr->mVolumeEnvPos = 0;
  1332. tptr->mPanningEnvPos = 0;
  1333. tptr->mInstrVibratoPhase = 0;
  1334. tptr->mInstrVibratoDepth = 0;
  1335. }
  1336. if ((tptr->mVolEffect >= 0x10) && (tptr->mVolEffect <= 0x50))
  1337. {
  1338. tptr->mNoteVolume = tptr->mVolEffect - 0x10;
  1339. tptr->mVolume = tptr->mNoteVolume;
  1340. }
  1341. if ((tptr->mVolEffect >> 4) == 0xc)
  1342. {
  1343. tptr->mNotePanning = (tptr->mVolEffect & 0xf) << 4 | (tptr->mVolEffect & 0xf);
  1344. }
  1345. }
  1346. break;
  1347. // Cut note
  1348. case 0xc:
  1349. if (mTickCount == tptr->mNybble2) tptr->mNoteVolume = 0;
  1350. break;
  1351. // Fine volslide up
  1352. case 0xa:
  1353. if (!mTickCount)
  1354. {
  1355. if (tptr->mNybble2) tptr->mVolumeSpeedUp = tptr->mNybble2;
  1356. tptr->mNoteVolume += tptr->mVolumeSpeedUp;
  1357. if (tptr->mNoteVolume > 64) tptr->mNoteVolume = 64;
  1358. }
  1359. break;
  1360. // Fine volslide down
  1361. case 0xb:
  1362. if (!mTickCount)
  1363. {
  1364. if (tptr->mNybble2) tptr->mVolumeSpeedDown = tptr->mNybble2;
  1365. tptr->mNoteVolume -= tptr->mVolumeSpeedDown;
  1366. if (tptr->mNoteVolume < 0) tptr->mNoteVolume = 0;
  1367. }
  1368. break;
  1369. // Patterndelay
  1370. case 0xe:
  1371. if (!mTickCount)
  1372. {
  1373. mPatternDelay = tptr->mNybble2 + 1;
  1374. }
  1375. break;
  1376. }
  1377. }
  1378. void XM::changeInstrument(XMTrack* tptr)
  1379. {
  1380. tptr->mInstrument = tptr->mNewInstrument - 1;
  1381. if (tptr->mXMSample)
  1382. {
  1383. tptr->mNotePanning = tptr->mXMSample->mPanning;
  1384. tptr->mNoteVolume = tptr->mXMSample->mVolume;
  1385. tptr->mVolume = tptr->mNoteVolume;
  1386. }
  1387. // Must not restart envelopes if there's a keyoff just in that place
  1388. if ((!tptr->mNewNote) || (tptr->mNote != KEYOFF - 1))
  1389. {
  1390. tptr->mKeyOn = true;
  1391. tptr->mFadeOutValue = 32768;
  1392. tptr->mVolumeEnvPos = 0;
  1393. tptr->mPanningEnvPos = 0;
  1394. tptr->mInstrVibratoPhase = 0;
  1395. tptr->mInstrVibratoDepth = 0;
  1396. }
  1397. }
  1398. unsigned XM::getAmigaPeriod(int mNote, int fineTune)
  1399. {
  1400. int noteOffset = (mNote % 12) << 3;
  1401. if (fineTune >= 0)
  1402. {
  1403. int roughFineTune = fineTune >> 4;
  1404. int fineFineTune = fineTune & 15;
  1405. return ((sAmigaFreqTable[8 + noteOffset + roughFineTune] * (16 - fineFineTune) +
  1406. sAmigaFreqTable[9 + noteOffset + roughFineTune] * fineFineTune)
  1407. * 2) >> (mNote / 12);
  1408. }
  1409. else
  1410. {
  1411. int roughFineTune = (fineTune - 15) >> 4;
  1412. int fineFineTune = fineTune & 15;
  1413. return ((sAmigaFreqTable[8 + noteOffset + roughFineTune] * (16 - fineFineTune) +
  1414. sAmigaFreqTable[9 + noteOffset + roughFineTune] * fineFineTune)
  1415. * 2) >> (mNote / 12);
  1416. }
  1417. }