xref: /btstack/3rd-party/hxcmod-player/hxcmod.c (revision a68a4a75928f21921da209ab44a77c7bbbd48c55)
1 ///////////////////////////////////////////////////////////////////////////////////
2 //-------------------------------------------------------------------------------//
3 //-------------------------------------------------------------------------------//
4 //-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------//
5 //----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------//
6 //---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------//
7 //--------H----H----X--X----C----------2-------0----0---0----0-----1-------------//
8 //-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------//
9 //-------------------------------------------------------------------------------//
10 //----------------------------------------------------- http://hxc2001.free.fr --//
11 ///////////////////////////////////////////////////////////////////////////////////
12 // File : hxcmod.c
13 // Contains: a tiny mod player
14 //
15 // Written by: Jean Francois DEL NERO
16 //
17 // You are free to do what you want with this code.
18 // A credit is always appreciated if you include it into your prod :)
19 //
20 // This file include some parts of the Noisetracker/Soundtracker/Protracker
21 // Module Format documentation written by Andrew Scott (Adrenalin Software)
22 // (modformat.txt)
23 //
24 // The core (hxcmod.c/hxcmod.h) is designed to have the least external dependency.
25 // So it should be usable on almost all OS and systems.
26 // Please also note that no dynamic allocation is done into the HxCMOD core.
27 //
28 // Change History (most recent first):
29 ///////////////////////////////////////////////////////////////////////////////////
30 // HxCMOD Core API:
31 // -------------------------------------------
32 // int  hxcmod_init(modcontext * modctx)
33 //
34 // - Initialize the modcontext buffer. Must be called before doing anything else.
35 //   Return 1 if success. 0 in case of error.
36 // -------------------------------------------
37 // int  hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size )
38 //
39 // - "Load" a MOD from memory (from "mod_data" with size "mod_data_size").
40 //   Return 1 if success. 0 in case of error.
41 // -------------------------------------------
42 // void hxcmod_fillbuffer( modcontext * modctx, unsigned short * outbuffer, mssize nbsample, tracker_buffer_state * trkbuf )
43 //
44 // - Generate and return the next samples chunk to outbuffer.
45 //   nbsample specify the number of stereo 16bits samples you want.
46 //   The output format is signed 44100Hz 16-bit Stereo PCM samples.
47 //   The output buffer size in byte must be equal to ( nbsample * 2 * 2 ).
48 //   The optional trkbuf parameter can be used to get detailed status of the player. Put NULL/0 is unused.
49 // -------------------------------------------
50 // void hxcmod_unload( modcontext * modctx )
51 // - "Unload" / clear the player status.
52 // -------------------------------------------
53 ///////////////////////////////////////////////////////////////////////////////////
54 
55 #include "hxcmod.h"
56 
57 // BK4BSTACK_CHANGE START
58 
59 // MODs ar either in ROM (MCUs) or in .text segment
60 #define HXCMOD_MOD_FILE_IN_ROM
61 
62 #ifdef __GNUC__
63 #ifndef	__clang__
64 #pragma GCC diagnostic push
65 // -Wstringop-overflow unknown to Clang 16.0.0
66 #pragma GCC diagnostic ignored "-Wstringop-overflow"
67 #endif
68 #endif
69 
70 // BK4BSTACK_CHANGE END
71 
72 ///////////////////////////////////////////////////////////////////////////////////
73 
74 // Effects list
75 #define EFFECT_ARPEGGIO              0x0 // Supported
76 #define EFFECT_PORTAMENTO_UP         0x1 // Supported
77 #define EFFECT_PORTAMENTO_DOWN       0x2 // Supported
78 #define EFFECT_TONE_PORTAMENTO       0x3 // Supported
79 #define EFFECT_VIBRATO               0x4 // Supported
80 #define EFFECT_VOLSLIDE_TONEPORTA    0x5 // Supported
81 #define EFFECT_VOLSLIDE_VIBRATO      0x6 // Supported
82 #define EFFECT_VOLSLIDE_TREMOLO      0x7 // - TO BE DONE -
83 #define EFFECT_SET_PANNING           0x8 // - TO BE DONE -
84 #define EFFECT_SET_OFFSET            0x9 // Supported
85 #define EFFECT_VOLUME_SLIDE          0xA // Supported
86 #define EFFECT_JUMP_POSITION         0xB // Supported
87 #define EFFECT_SET_VOLUME            0xC // Supported
88 #define EFFECT_PATTERN_BREAK         0xD // Supported
89 
90 #define EFFECT_EXTENDED              0xE
91 #define EFFECT_E_FINE_PORTA_UP       0x1 // Supported
92 #define EFFECT_E_FINE_PORTA_DOWN     0x2 // Supported
93 #define EFFECT_E_GLISSANDO_CTRL      0x3 // - TO BE DONE -
94 #define EFFECT_E_VIBRATO_WAVEFORM    0x4 // - TO BE DONE -
95 #define EFFECT_E_SET_FINETUNE        0x5 // Supported
96 #define EFFECT_E_PATTERN_LOOP        0x6 // Supported
97 #define EFFECT_E_TREMOLO_WAVEFORM    0x7 // - TO BE DONE -
98 #define EFFECT_E_SET_PANNING_2       0x8 // - TO BE DONE -
99 #define EFFECT_E_RETRIGGER_NOTE      0x9 // Supported
100 #define EFFECT_E_FINE_VOLSLIDE_UP    0xA // Supported
101 #define EFFECT_E_FINE_VOLSLIDE_DOWN  0xB // Supported
102 #define EFFECT_E_NOTE_CUT            0xC // Supported
103 #define EFFECT_E_NOTE_DELAY          0xD // Supported
104 #define EFFECT_E_PATTERN_DELAY       0xE // Supported
105 #define EFFECT_E_INVERT_LOOP         0xF // Supported (W.I.P)
106 #define EFFECT_SET_SPEED             0xF0 // Supported
107 #define EFFECT_SET_TEMPO             0xF2 // Supported
108 
109 #define PERIOD_TABLE_LENGTH  MAXNOTES
110 
111 
112 //
113 // Finetuning periods -> Amiga period * 2^(-finetune/12/8)
114 //
115 
116 static const short periodtable[]=
117 {
118 	// Finetune 0 (* 1.000000), Offset 0x0000
119 	27392, 25856, 24384, 23040, 21696, 20480, 19328, 18240, 17216, 16256, 15360, 14496,
120 	13696, 12928, 12192, 11520, 10848, 10240,  9664,  9120,  8606,  8128,  7680,  7248,
121 	 6848,  6464,  6096,  5760,  5424,  5120,  4832,  4560,  4304,  4064,  3840,  3624,
122 	 3424,  3232,  3048,  2880,  2712,  2560,  2416,  2280,  2152,  2032,  1920,  1812,
123 	 1712,  1616,  1524,  1440,  1356,  1280,  1208,  1140,  1076,  1016,   960,   906,
124 	  856,   808,   762,   720,   678,   640,   604,   570,   538,   508,   480,   453,
125 	  428,   404,   381,   360,   339,   320,   302,   285,   269,   254,   240,   226,
126 	  214,   202,   190,   180,   170,   160,   151,   143,   135,   127,   120,   113,
127 	  107,   101,    95,    90,    85,    80,    75,    71,    67,    63,    60,    56,
128 	   53,    50,    47,    45,    42,    40,    37,    35,    33,    31,    30,    28,
129 	   27,    25,    24,    22,    21,    20,    19,    18,    17,    16,    15,    14,
130 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
131 
132 	// Finetune 1 (* 0.992806), Offset 0x0120
133 	27195, 25670, 24209, 22874, 21540, 20333, 19189, 18109, 17092, 16139, 15249, 14392,
134 	13597, 12835, 12104, 11437, 10770, 10166,  9594,  9054,  8544,  8070,  7625,  7196,
135 	 6799,  6417,  6052,  5719,  5385,  5083,  4797,  4527,  4273,  4035,  3812,  3598,
136 	 3399,  3209,  3026,  2859,  2692,  2542,  2399,  2264,  2137,  2017,  1906,  1799,
137 	 1700,  1604,  1513,  1430,  1346,  1271,  1199,  1132,  1068,  1009,   953,   899,
138 	  850,   802,   757,   715,   673,   635,   600,   566,   534,   504,   477,   450,
139 	  425,   401,   378,   357,   337,   318,   300,   283,   267,   252,   238,   224,
140 	  212,   201,   189,   179,   169,   159,   150,   142,   134,   126,   119,   112,
141 	  106,   100,    94,    89,    84,    79,    74,    70,    67,    63,    60,    56,
142 	   53,    50,    47,    45,    42,    40,    37,    35,    33,    31,    30,    28,
143 	   27,    25,    24,    22,    21,    20,    19,    18,    17,    16,    15,    14,
144 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
145 
146 	// Finetune 2 (* 0.985663), Offset 0x0240
147 	26999, 25485, 24034, 22710, 21385, 20186, 19051, 17978, 16969, 16023, 15140, 14288,
148 	13500, 12743, 12017, 11355, 10692, 10093,  9525,  8989,  8483,  8011,  7570,  7144,
149 	 6750,  6371,  6009,  5677,  5346,  5047,  4763,  4495,  4242,  4006,  3785,  3572,
150 	 3375,  3186,  3004,  2839,  2673,  2523,  2381,  2247,  2121,  2003,  1892,  1786,
151 	 1687,  1593,  1502,  1419,  1337,  1262,  1191,  1124,  1061,  1001,   946,   893,
152 	  844,   796,   751,   710,   668,   631,   595,   562,   530,   501,   473,   447,
153 	  422,   398,   376,   355,   334,   315,   298,   281,   265,   250,   237,   223,
154 	  211,   199,   187,   177,   168,   158,   149,   141,   133,   125,   118,   111,
155 	  105,   100,    94,    89,    84,    79,    74,    70,    66,    62,    59,    55,
156 	   52,    49,    46,    44,    41,    39,    36,    34,    33,    31,    30,    28,
157 	   27,    25,    24,    22,    21,    20,    19,    18,    17,    16,    15,    14,
158 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
159 
160 	// Finetune 3 (* 0.978572), Offset 0x0360
161 	26805, 25302, 23862, 22546, 21231, 20041, 18914, 17849, 16847, 15908, 15031, 14185,
162 	13403, 12651, 11931, 11273, 10616, 10021,  9457,  8925,  8422,  7954,  7515,  7093,
163 	 6701,  6325,  5965,  5637,  5308,  5010,  4728,  4462,  4212,  3977,  3758,  3546,
164 	 3351,  3163,  2983,  2818,  2654,  2505,  2364,  2231,  2106,  1988,  1879,  1773,
165 	 1675,  1581,  1491,  1409,  1327,  1253,  1182,  1116,  1053,   994,   939,   887,
166 	  838,   791,   746,   705,   663,   626,   591,   558,   526,   497,   470,   443,
167 	  419,   395,   373,   352,   332,   313,   296,   279,   263,   249,   235,   221,
168 	  209,   198,   186,   176,   166,   157,   148,   140,   132,   124,   117,   111,
169 	  105,    99,    93,    88,    83,    78,    73,    69,    66,    62,    59,    55,
170 	   52,    49,    46,    44,    41,    39,    36,    34,    32,    30,    29,    27,
171 	   26,    24,    23,    22,    21,    20,    19,    18,    17,    16,    15,    14,
172 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
173 
174 	// Finetune 4 (* 0.971532), Offset 0x0480
175 	26612, 25120, 23690, 22384, 21078, 19897, 18778, 17721, 16726, 15793, 14923, 14083,
176 	13306, 12560, 11845, 11192, 10539,  9948,  9389,  8860,  8361,  7897,  7461,  7042,
177 	 6653,  6280,  5922,  5596,  5270,  4974,  4694,  4430,  4181,  3948,  3731,  3521,
178 	 3327,  3140,  2961,  2798,  2635,  2487,  2347,  2215,  2091,  1974,  1865,  1760,
179 	 1663,  1570,  1481,  1399,  1317,  1244,  1174,  1108,  1045,   987,   933,   880,
180 	  832,   785,   740,   700,   659,   622,   587,   554,   523,   494,   466,   440,
181 	  416,   392,   370,   350,   329,   311,   293,   277,   261,   247,   233,   220,
182 	  208,   196,   185,   175,   165,   155,   147,   139,   131,   123,   117,   110,
183 	  104,    98,    92,    87,    83,    78,    73,    69,    65,    61,    58,    54,
184 	   51,    49,    46,    44,    41,    39,    36,    34,    32,    30,    29,    27,
185 	   26,    24,    23,    21,    20,    19,    18,    17,    17,    16,    15,    14,
186 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
187 
188 	// Finetune 5 (* 0.964542), Offset 0x05a0
189 	26421, 24939, 23519, 22223, 20927, 19754, 18643, 17593, 16606, 15680, 14815, 13982,
190 	13210, 12470, 11760, 11112, 10463,  9877,  9321,  8797,  8301,  7840,  7408,  6991,
191 	 6605,  6235,  5880,  5556,  5232,  4938,  4661,  4398,  4151,  3920,  3704,  3496,
192 	 3303,  3117,  2940,  2778,  2616,  2469,  2330,  2199,  2076,  1960,  1852,  1748,
193 	 1651,  1559,  1470,  1389,  1308,  1235,  1165,  1100,  1038,   980,   926,   874,
194 	  826,   779,   735,   694,   654,   617,   583,   550,   519,   490,   463,   437,
195 	  413,   390,   367,   347,   327,   309,   291,   275,   259,   245,   231,   218,
196 	  206,   195,   183,   174,   164,   154,   146,   138,   130,   122,   116,   109,
197 	  103,    97,    92,    87,    82,    77,    72,    68,    65,    61,    58,    54,
198 	   51,    48,    45,    43,    41,    39,    36,    34,    32,    30,    29,    27,
199 	   26,    24,    23,    21,    20,    19,    18,    17,    16,    15,    14,    14,
200 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
201 
202 	// Finetune 6 (* 0.957603), Offset 0x06c0
203 	26231, 24760, 23350, 22063, 20776, 19612, 18509, 17467, 16486, 15567, 14709, 13881,
204 	13115, 12380, 11675, 11032, 10388,  9806,  9254,  8733,  8241,  7783,  7354,  6941,
205 	 6558,  6190,  5838,  5516,  5194,  4903,  4627,  4367,  4122,  3892,  3677,  3470,
206 	 3279,  3095,  2919,  2758,  2597,  2451,  2314,  2183,  2061,  1946,  1839,  1735,
207 	 1639,  1547,  1459,  1379,  1299,  1226,  1157,  1092,  1030,   973,   919,   868,
208 	  820,   774,   730,   689,   649,   613,   578,   546,   515,   486,   460,   434,
209 	  410,   387,   365,   345,   325,   306,   289,   273,   258,   243,   230,   216,
210 	  205,   193,   182,   172,   163,   153,   145,   137,   129,   122,   115,   108,
211 	  102,    97,    91,    86,    81,    77,    72,    68,    64,    60,    57,    54,
212 	   51,    48,    45,    43,    40,    38,    35,    34,    32,    30,    29,    27,
213 	   26,    24,    23,    21,    20,    19,    18,    17,    16,    15,    14,    13,
214 	   12,    12,    11,    11,    11,    10,     9,     9,     8,     8,     7,     7,
215 
216 	// Finetune 7 (* 0.950714), Offset 0x07e0
217 	26042, 24582, 23182, 21904, 20627, 19471, 18375, 17341, 16367, 15455, 14603, 13782,
218 	13021, 12291, 11591, 10952, 10313,  9735,  9188,  8671,  8182,  7727,  7301,  6891,
219 	 6510,  6145,  5796,  5476,  5157,  4868,  4594,  4335,  4092,  3864,  3651,  3445,
220 	 3255,  3073,  2898,  2738,  2578,  2434,  2297,  2168,  2046,  1932,  1825,  1723,
221 	 1628,  1536,  1449,  1369,  1289,  1217,  1148,  1084,  1023,   966,   913,   861,
222 	  814,   768,   724,   685,   645,   608,   574,   542,   511,   483,   456,   431,
223 	  407,   384,   362,   342,   322,   304,   287,   271,   256,   241,   228,   215,
224 	  203,   192,   181,   171,   162,   152,   144,   136,   128,   121,   114,   107,
225 	  102,    96,    90,    86,    81,    76,    71,    68,    64,    60,    57,    53,
226 	   50,    48,    45,    43,    40,    38,    35,    33,    31,    29,    29,    27,
227 	   26,    24,    23,    21,    20,    19,    18,    17,    16,    15,    14,    13,
228 	   12,    12,    11,    10,    10,    10,     9,     9,     8,     8,     7,     7,
229 
230 	// Finetune -8 (* 1.059463), Offset 0x0900
231 	29021, 27393, 25834, 24410, 22986, 21698, 20477, 19325, 18240, 17223, 16273, 15358,
232 	14510, 13697, 12917, 12205, 11493, 10849, 10239,  9662,  9118,  8611,  8137,  7679,
233 	 7255,  6848,  6458,  6103,  5747,  5424,  5119,  4831,  4560,  4306,  4068,  3839,
234 	 3628,  3424,  3229,  3051,  2873,  2712,  2560,  2416,  2280,  2153,  2034,  1920,
235 	 1814,  1712,  1615,  1526,  1437,  1356,  1280,  1208,  1140,  1076,  1017,   960,
236 	  907,   856,   807,   763,   718,   678,   640,   604,   570,   538,   509,   480,
237 	  453,   428,   404,   381,   359,   339,   320,   302,   285,   269,   254,   239,
238 	  227,   214,   201,   191,   180,   170,   160,   152,   143,   135,   127,   120,
239 	  113,   107,   101,    95,    90,    85,    79,    75,    71,    67,    64,    59,
240 	   56,    53,    50,    48,    44,    42,    39,    37,    35,    33,    32,    30,
241 	   29,    26,    25,    23,    22,    21,    20,    19,    18,    17,    16,    15,
242 	   14,    14,    13,    12,    12,    11,    10,    10,     8,     8,     7,     7,
243 
244 	// Finetune -7 (* 1.051841), Offset 0x0a20
245 	28812, 27196, 25648, 24234, 22821, 21542, 20330, 19186, 18108, 17099, 16156, 15247,
246 	14406, 13598, 12824, 12117, 11410, 10771, 10165,  9593,  9052,  8549,  8078,  7624,
247 	 7203,  6799,  6412,  6059,  5705,  5385,  5082,  4796,  4527,  4275,  4039,  3812,
248 	 3602,  3400,  3206,  3029,  2853,  2693,  2541,  2398,  2264,  2137,  2020,  1906,
249 	 1801,  1700,  1603,  1515,  1426,  1346,  1271,  1199,  1132,  1069,  1010,   953,
250 	  900,   850,   802,   757,   713,   673,   635,   600,   566,   534,   505,   476,
251 	  450,   425,   401,   379,   357,   337,   318,   300,   283,   267,   252,   238,
252 	  225,   212,   200,   189,   179,   168,   159,   150,   142,   134,   126,   119,
253 	  113,   106,   100,    95,    89,    84,    79,    75,    70,    66,    63,    59,
254 	   56,    53,    49,    47,    44,    42,    39,    37,    35,    33,    32,    29,
255 	   28,    26,    25,    23,    22,    21,    20,    19,    18,    17,    16,    15,
256 	   14,    14,    13,    12,    12,    11,     9,     9,     8,     8,     7,     7,
257 
258 	// Finetune -6 (* 1.044274), Offset 0x0b40
259 	28605, 27001, 25464, 24060, 22657, 21387, 20184, 19048, 17978, 16976, 16040, 15138,
260 	14302, 13500, 12732, 12030, 11328, 10693, 10092,  9524,  8987,  8488,  8020,  7569,
261 	 7151,  6750,  6366,  6015,  5664,  5347,  5046,  4762,  4495,  4244,  4010,  3784,
262 	 3576,  3375,  3183,  3008,  2832,  2673,  2523,  2381,  2247,  2122,  2005,  1892,
263 	 1788,  1688,  1591,  1504,  1416,  1337,  1261,  1190,  1124,  1061,  1003,   946,
264 	  894,   844,   796,   752,   708,   668,   631,   595,   562,   530,   501,   473,
265 	  447,   422,   398,   376,   354,   334,   315,   298,   281,   265,   251,   236,
266 	  223,   211,   198,   188,   178,   167,   158,   149,   141,   133,   125,   118,
267 	  112,   105,    99,    94,    89,    84,    78,    74,    70,    66,    63,    58,
268 	   55,    52,    49,    47,    44,    42,    39,    37,    34,    32,    31,    29,
269 	   28,    26,    25,    23,    22,    21,    20,    19,    18,    17,    16,    15,
270 	   14,    14,    13,    11,    11,    10,     9,     9,     8,     8,     7,     7,
271 
272 	// Finetune -5 (* 1.036761), Offset 0x0c60
273 	28399, 26806, 25280, 23887, 22494, 21233, 20039, 18911, 17849, 16854, 15925, 15029,
274 	14199, 13403, 12640, 11943, 11247, 10616, 10019,  9455,  8922,  8427,  7962,  7514,
275 	 7100,  6702,  6320,  5972,  5623,  5308,  5010,  4728,  4462,  4213,  3981,  3757,
276 	 3550,  3351,  3160,  2986,  2812,  2654,  2505,  2364,  2231,  2107,  1991,  1879,
277 	 1775,  1675,  1580,  1493,  1406,  1327,  1252,  1182,  1116,  1053,   995,   939,
278 	  887,   838,   790,   746,   703,   664,   626,   591,   558,   527,   498,   470,
279 	  444,   419,   395,   373,   351,   332,   313,   295,   279,   263,   249,   234,
280 	  222,   209,   197,   187,   176,   166,   157,   148,   140,   132,   124,   117,
281 	  111,   105,    98,    93,    88,    83,    78,    74,    69,    65,    62,    58,
282 	   55,    52,    49,    47,    44,    41,    38,    36,    34,    32,    31,    29,
283 	   28,    26,    25,    23,    22,    21,    20,    19,    18,    17,    16,    15,
284 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
285 
286 	// Finetune -4 (* 1.029302), Offset 0x0d80
287 	28195, 26614, 25099, 23715, 22332, 21080, 19894, 18774, 17720, 16732, 15810, 14921,
288 	14097, 13307, 12549, 11858, 11166, 10540,  9947,  9387,  8858,  8366,  7905,  7460,
289 	 7049,  6653,  6275,  5929,  5583,  5270,  4974,  4694,  4430,  4183,  3953,  3730,
290 	 3524,  3327,  3137,  2964,  2791,  2635,  2487,  2347,  2215,  2092,  1976,  1865,
291 	 1762,  1663,  1569,  1482,  1396,  1318,  1243,  1173,  1108,  1046,   988,   933,
292 	  881,   832,   784,   741,   698,   659,   622,   587,   554,   523,   494,   466,
293 	  441,   416,   392,   371,   349,   329,   311,   293,   277,   261,   247,   233,
294 	  220,   208,   196,   185,   175,   165,   155,   147,   139,   131,   124,   116,
295 	  110,   104,    98,    93,    87,    82,    77,    73,    69,    65,    62,    58,
296 	   55,    51,    48,    46,    43,    41,    38,    36,    34,    32,    31,    29,
297 	   28,    26,    25,    23,    22,    21,    20,    19,    17,    16,    15,    14,
298 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
299 
300 	// Finetune -3 (* 1.021897), Offset 0x0ea0
301 	27992, 26422, 24918, 23545, 22171, 20928, 19751, 18639, 17593, 16612, 15696, 14813,
302 	13996, 13211, 12459, 11772, 11086, 10464,  9876,  9320,  8794,  8306,  7848,  7407,
303 	 6998,  6606,  6229,  5886,  5543,  5232,  4938,  4660,  4398,  4153,  3924,  3703,
304 	 3499,  3303,  3115,  2943,  2771,  2616,  2469,  2330,  2199,  2076,  1962,  1852,
305 	 1749,  1651,  1557,  1472,  1386,  1308,  1234,  1165,  1100,  1038,   981,   926,
306 	  875,   826,   779,   736,   693,   654,   617,   582,   550,   519,   491,   463,
307 	  437,   413,   389,   368,   346,   327,   309,   291,   275,   260,   245,   231,
308 	  219,   206,   194,   184,   174,   164,   154,   146,   138,   130,   123,   115,
309 	  109,   103,    97,    92,    87,    82,    77,    73,    68,    64,    61,    57,
310 	   54,    51,    48,    46,    43,    41,    38,    36,    34,    32,    31,    29,
311 	   28,    26,    25,    22,    21,    20,    19,    18,    17,    16,    15,    14,
312 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
313 
314 	// Finetune -2 (* 1.014545), Offset 0x0fc0
315 	27790, 26232, 24739, 23375, 22012, 20778, 19609, 18505, 17466, 16492, 15583, 14707,
316 	13895, 13116, 12369, 11688, 11006, 10389,  9805,  9253,  8731,  8246,  7792,  7353,
317 	 6948,  6558,  6185,  5844,  5503,  5194,  4902,  4626,  4367,  4123,  3896,  3677,
318 	 3474,  3279,  3092,  2922,  2751,  2597,  2451,  2313,  2183,  2062,  1948,  1838,
319 	 1737,  1640,  1546,  1461,  1376,  1299,  1226,  1157,  1092,  1031,   974,   919,
320 	  868,   820,   773,   730,   688,   649,   613,   578,   546,   515,   487,   460,
321 	  434,   410,   387,   365,   344,   325,   306,   289,   273,   258,   243,   229,
322 	  217,   205,   193,   183,   172,   162,   153,   145,   137,   129,   122,   115,
323 	  109,   102,    96,    91,    86,    81,    76,    72,    68,    64,    61,    57,
324 	   54,    51,    48,    46,    43,    41,    38,    36,    33,    31,    30,    28,
325 	   27,    25,    24,    22,    21,    20,    19,    18,    17,    16,    15,    14,
326 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7,
327 
328 	// Finetune -1 (* 1.007246), Offset 0x10e0
329 	27590, 26043, 24561, 23207, 21853, 20628, 19468, 18372, 17341, 16374, 15471, 14601,
330 	13795, 13022, 12280, 11603, 10927, 10314,  9734,  9186,  8668,  8187,  7736,  7301,
331 	 6898,  6511,  6140,  5802,  5463,  5157,  4867,  4593,  4335,  4093,  3868,  3650,
332 	 3449,  3255,  3070,  2901,  2732,  2579,  2434,  2297,  2168,  2047,  1934,  1825,
333 	 1724,  1628,  1535,  1450,  1366,  1289,  1217,  1148,  1084,  1023,   967,   913,
334 	  862,   814,   768,   725,   683,   645,   608,   574,   542,   512,   483,   456,
335 	  431,   407,   384,   363,   341,   322,   304,   287,   271,   256,   242,   228,
336 	  216,   203,   191,   181,   171,   161,   152,   144,   136,   128,   121,   114,
337 	  108,   102,    96,    91,    86,    81,    76,    72,    67,    63,    60,    56,
338 	   53,    50,    47,    45,    42,    40,    37,    35,    33,    31,    30,    28,
339 	   27,    25,    24,    22,    21,    20,    19,    18,    17,    16,    15,    14,
340 	   13,    13,    12,    11,    11,    10,     9,     9,     8,     8,     7,     7
341 };
342 
343 static const short * periodtable_finetune_ptr[]=
344 {
345 	&periodtable[0x0000], &periodtable[0x0090], &periodtable[0x0120], &periodtable[0x01B0],
346 	&periodtable[0x0240], &periodtable[0x02D0], &periodtable[0x0360], &periodtable[0x03F0],
347 	&periodtable[0x0480], &periodtable[0x0510], &periodtable[0x05A0], &periodtable[0x0630],
348 	&periodtable[0x06C0], &periodtable[0x0750], &periodtable[0x07E0], &periodtable[0x0870]
349 };
350 
351 static const short sintable[]={
352 	  0,  24,  49,  74,  97, 120, 141, 161,
353 	180, 197, 212, 224, 235, 244, 250, 253,
354 	255, 253, 250, 244, 235, 224, 212, 197,
355 	180, 161, 141, 120,  97,  74,  49,  24
356 };
357 
358 static const muchar InvertLoopTable[]={
359 	  0,   5,   6,   7,   8,  10,  11, 13,
360 	 16,  19,  22,  26,  32,  43,  64, 128
361 };
362 
363 typedef struct modtype_
364 {
365 	unsigned char signature[5];
366 	int numberofchannels;
367 }modtype;
368 
369 static modtype modlist[]=
370 {
371 	{ "M!K!",4},
372 	{ "M.K.",4},
373 	{ "M&K!",4},
374 	{ "PATT",4},
375 	{ "NSMS",4},
376 	{ "LARD",4},
377 	{ "FEST",4},
378 	{ "FIST",4},
379 	{ "N.T.",4},
380 	{ "OKTA",8},
381 	{ "OCTA",8},
382 	{ "$CHN",-1},
383 	{ "$$CH",-1},
384 	{ "$$CN",-1},
385 	{ "$$$C",-1},
386 	{ "FLT$",-1},
387 	{ "EXO$",-1},
388 	{ "CD$1",-1},
389 	{ "TDZ$",-1},
390 	{ "FA0$",-1},
391 	{ "",0}
392 };
393 
394 #ifdef HXCMOD_BIGENDIAN_MACHINE
395 
396 #define GET_BGI_W( big_endian_word ) ( big_endian_word )
397 
398 #else
399 
400 #define GET_BGI_W( big_endian_word ) ( (big_endian_word >> 8) | ((big_endian_word&0xFF) << 8) )
401 
402 #endif
403 
404 
405 ///////////////////////////////////////////////////////////////////////////////////
406 
407 static void memcopy( void * dest, void *source, unsigned long size )
408 {
409 	unsigned long i;
410 	unsigned char * d,*s;
411 
412 	d=(unsigned char*)dest;
413 	s=(unsigned char*)source;
414 	for(i=0;i<size;i++)
415 	{
416 		d[i]=s[i];
417 	}
418 }
419 
420 static void memclear( void * dest, unsigned char value, unsigned long size )
421 {
422 	unsigned long i;
423 	unsigned char * d;
424 
425 	d = (unsigned char*)dest;
426 	for(i=0;i<size;i++)
427 	{
428 		d[i]=value;
429 	}
430 }
431 
432 static int getnote( modcontext * mod, unsigned short period )
433 {
434 // BK4BSTACK_CHANGE START
435 	(void) mod;
436 // BK4BSTACK_CHANGE END
437 
438 	int i;
439 	const short * ptr;
440 
441 	ptr = periodtable_finetune_ptr[0];
442 
443 	for(i = 0; i < MAXNOTES; i++)
444 	{
445 		if(period >= ptr[i])
446 		{
447 			return i;
448 		}
449 	}
450 
451 	return MAXNOTES;
452 }
453 
454 static void doFunk(hxcmod_channel_t * cptr)
455 {
456 	if(cptr->funkspeed)
457 	{
458 		cptr->funkoffset += InvertLoopTable[cptr->funkspeed];
459 		if( cptr->funkoffset > 128 )
460 		{
461 			cptr->funkoffset = 0;
462 			if( cptr->sampdata && cptr->length && (cptr->replen > 1) )
463 			{
464 				if( ( (cptr->samppos) >> 11 ) >= (unsigned long)(cptr->replen+cptr->reppnt) )
465 				{
466 					cptr->samppos = ((unsigned long)(cptr->reppnt)<<11) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<11));
467 				}
468 
469 #ifndef HXCMOD_MOD_FILE_IN_ROM
470 				// Note : Directly modify the sample in the mod buffer...
471 				// The current Invert Loop effect implementation can't be played from ROM.
472 				cptr->sampdata[cptr->samppos >> 10] = -1 - cptr->sampdata[cptr->samppos >> 10];
473 #endif
474 			}
475 		}
476 	}
477 }
478 
479 static void worknote( note * nptr, hxcmod_channel_t * cptr,char t,modcontext * mod )
480 {
481 	// BK4BSTACK_CHANGE START
482 	(void) t;
483 	// we rename sample into a_sample to avoid shadowing typedef struct { .. } sample; from hxcmod.h
484 	// BK4BSTACK_CHANGE END
485 
486 	muint a_sample, period, effect, operiod;
487 	muint curnote, arpnote;
488 	muchar effect_op;
489 	muchar effect_param,effect_param_l,effect_param_h;
490 	muint enable_nxt_smp;
491 	const short * period_table_ptr;
492 
493 	a_sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4);
494 	period = ((nptr->sampperiod & 0xF) << 8) | nptr->period;
495 	effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect;
496 	effect_op = nptr->sampeffect & 0xF;
497 	effect_param = nptr->effect;
498 	effect_param_l = effect_param & 0x0F;
499 	effect_param_h = effect_param >> 4;
500 
501 	enable_nxt_smp = 0;
502 
503 	operiod = cptr->period;
504 
505 	if ( period || a_sample )
506 	{
507 		if( a_sample && ( a_sample < 32 ) )
508 		{
509 			cptr->sampnum = a_sample - 1;
510 		}
511 
512 		if( period || a_sample )
513 		{
514 			if( period )
515 			{
516 				if( ( effect_op != EFFECT_TONE_PORTAMENTO ) || ( ( effect_op == EFFECT_TONE_PORTAMENTO ) && !cptr->sampdata ) )
517 				{
518 					// Not a Tone Partamento effect or no sound currently played :
519 					if ( ( effect_op != EFFECT_EXTENDED || effect_param_h != EFFECT_E_NOTE_DELAY ) || ( ( effect_op == EFFECT_EXTENDED && effect_param_h == EFFECT_E_NOTE_DELAY ) && !effect_param_l ) )
520 					{
521 						// Immediately (re)trigger the new note
522 						cptr->sampdata = mod->sampledata[cptr->sampnum];
523 						cptr->length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
524 						cptr->reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
525 						cptr->replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
526 
527 						cptr->lst_sampdata = cptr->sampdata;
528 						cptr->lst_length = cptr->length;
529 						cptr->lst_reppnt = cptr->reppnt;
530 						cptr->lst_replen = cptr->replen;
531 					}
532 					else
533 					{
534 						cptr->dly_sampdata = mod->sampledata[cptr->sampnum];
535 						cptr->dly_length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
536 						cptr->dly_reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
537 						cptr->dly_replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
538 						cptr->note_delay = effect_param_l;
539 					}
540 					// Cancel any delayed note...
541 					cptr->update_nxt_repeat = 0;
542 				}
543 				else
544 				{
545 					// Partamento effect - Play the new note after the current sample.
546 					if( effect_op == EFFECT_TONE_PORTAMENTO )
547 						enable_nxt_smp = 1;
548 				}
549 			}
550 			else // Note without period : Trigger it after the current sample.
551 				enable_nxt_smp = 1;
552 
553 			if ( enable_nxt_smp )
554 			{
555 				// Prepare the next sample retrigger after the current one
556 				cptr->nxt_sampdata = mod->sampledata[cptr->sampnum];
557 				cptr->nxt_length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
558 				cptr->nxt_reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
559 				cptr->nxt_replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
560 
561 				if(cptr->nxt_replen < 2)   // Protracker : don't play the sample if not looped...
562 					cptr->nxt_sampdata = 0;
563 
564 				cptr->update_nxt_repeat = 1;
565 			}
566 
567 			cptr->finetune = (mod->song.samples[cptr->sampnum].finetune) & 0xF;
568 
569 			if( effect_op != EFFECT_VIBRATO && effect_op != EFFECT_VOLSLIDE_VIBRATO )
570 			{
571 				cptr->vibraperiod = 0;
572 				cptr->vibrapointeur = 0;
573 			}
574 		}
575 
576 		if( (a_sample != 0) && ( effect_op != EFFECT_VOLSLIDE_TONEPORTA ) )
577 		{
578 			cptr->volume = mod->song.samples[cptr->sampnum].volume;
579 			cptr->volumeslide = 0;
580 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
581 			cptr->volume_table = mod->volume_selection_table[cptr->volume];
582 #endif
583 		}
584 
585 		if( ( effect_op != EFFECT_TONE_PORTAMENTO ) && ( effect_op != EFFECT_VOLSLIDE_TONEPORTA ) )
586 		{
587 			if ( period != 0 )
588 				cptr->samppos = 0;
589 		}
590 
591 		cptr->decalperiod = 0;
592 		if( period )
593 		{
594 			if( cptr->finetune )
595 			{
596 				period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
597 				period = period_table_ptr[getnote(mod,period)];
598 			}
599 
600 			cptr->period = period;
601 		}
602 	}
603 
604 	cptr->effect = 0;
605 	cptr->parameffect = 0;
606 	cptr->effect_code = effect;
607 
608 #ifdef EFFECTS_USAGE_STATE
609 	if(effect_op || ((effect_op==EFFECT_ARPEGGIO) && effect_param))
610 	{
611 		mod->effects_event_counts[ effect_op ]++;
612 	}
613 
614 	if(effect_op == 0xE)
615 		mod->effects_event_counts[ 0x10 + effect_param_h ]++;
616 #endif
617 
618 	switch ( effect_op )
619 	{
620 		case EFFECT_ARPEGGIO:
621 			/*
622 			[0]: Arpeggio
623 			Where [0][x][y] means "play note, note+x semitones, note+y
624 			semitones, then return to original note". The fluctuations are
625 			carried out evenly spaced in one pattern division. They are usually
626 			used to simulate chords, but this doesn't work too well. They are
627 			also used to produce heavy vibrato. A major chord is when x=4, y=7.
628 			A minor chord is when x=3, y=7.
629 			*/
630 
631 			if( effect_param )
632 			{
633 				cptr->effect = EFFECT_ARPEGGIO;
634 				cptr->parameffect = effect_param;
635 
636 				cptr->ArpIndex = 0;
637 
638 				curnote = getnote(mod,cptr->period);
639 
640 				cptr->Arpperiods[0] = cptr->period;
641 
642 				period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
643 
644 				arpnote = curnote + (((cptr->parameffect>>4)&0xF));
645 				if( arpnote >= MAXNOTES )
646 					arpnote = (MAXNOTES) - 1;
647 
648 				cptr->Arpperiods[1] = period_table_ptr[arpnote];
649 
650 				arpnote = curnote + (((cptr->parameffect)&0xF));
651 				if( arpnote >= MAXNOTES )
652 					arpnote = (MAXNOTES) - 1;
653 
654 				cptr->Arpperiods[2] = period_table_ptr[arpnote];
655 			}
656 		break;
657 
658 		case EFFECT_PORTAMENTO_UP:
659 			/*
660 			[1]: Slide up
661 			Where [1][x][y] means "smoothly decrease the period of current
662 			sample by x*16+y after each tick in the division". The
663 			ticks/division are set with the 'set speed' effect (see below). If
664 			the period of the note being played is z, then the final period
665 			will be z - (x*16 + y)*(ticks - 1). As the slide rate depends on
666 			the speed, changing the speed will change the slide. You cannot
667 			slide beyond the note B3 (period 113).
668 			*/
669 
670 			cptr->effect = EFFECT_PORTAMENTO_UP;
671 			cptr->parameffect = effect_param;
672 		break;
673 
674 		case EFFECT_PORTAMENTO_DOWN:
675 			/*
676 			[2]: Slide down
677 			Where [2][x][y] means "smoothly increase the period of current
678 			sample by x*16+y after each tick in the division". Similar to [1],
679 			but lowers the pitch. You cannot slide beyond the note C1 (period
680 			856).
681 			*/
682 
683 			cptr->effect = EFFECT_PORTAMENTO_DOWN;
684 			cptr->parameffect = effect_param;
685 		break;
686 
687 		case EFFECT_TONE_PORTAMENTO:
688 			/*
689 			[3]: Slide to note
690 			Where [3][x][y] means "smoothly change the period of current sample
691 			by x*16+y after each tick in the division, never sliding beyond
692 			current period". The period-length in this channel's division is a
693 			parameter to this effect, and hence is not played. Sliding to a
694 			note is similar to effects [1] and [2], but the slide will not go
695 			beyond the given period, and the direction is implied by that
696 			period. If x and y are both 0, then the old slide will continue.
697 			*/
698 
699 			cptr->effect = EFFECT_TONE_PORTAMENTO;
700 			if( effect_param != 0 )
701 			{
702 				cptr->portaspeed = (short)( effect_param );
703 			}
704 
705 			if(period!=0)
706 			{
707 				cptr->portaperiod = period;
708 				cptr->period = operiod;
709 			}
710 		break;
711 
712 		case EFFECT_VIBRATO:
713 			/*
714 			[4]: Vibrato
715 			Where [4][x][y] means "oscillate the sample pitch using a
716 			particular waveform with amplitude y/16 semitones, such that (x *
717 			ticks)/64 cycles occur in the division". The waveform is set using
718 			effect [14][4]. By placing vibrato effects on consecutive
719 			divisions, the vibrato effect can be maintained. If either x or y
720 			are 0, then the old vibrato values will be used.
721 			*/
722 
723 			cptr->effect = EFFECT_VIBRATO;
724 			if( effect_param_l != 0 ) // Depth continue or change ?
725 				cptr->vibraparam = ( cptr->vibraparam & 0xF0 ) | effect_param_l;
726 			if( effect_param_h != 0 ) // Speed continue or change ?
727 				cptr->vibraparam = ( cptr->vibraparam & 0x0F ) | ( effect_param_h << 4 );
728 
729 		break;
730 
731 		case EFFECT_VOLSLIDE_TONEPORTA:
732 			/*
733 			[5]: Continue 'Slide to note', but also do Volume slide
734 			Where [5][x][y] means "either slide the volume up x*(ticks - 1) or
735 			slide the volume down y*(ticks - 1), at the same time as continuing
736 			the last 'Slide to note'". It is illegal for both x and y to be
737 			non-zero. You cannot slide outside the volume range 0..64. The
738 			period-length in this channel's division is a parameter to this
739 			effect, and hence is not played.
740 			*/
741 
742 			if( period != 0 )
743 			{
744 				cptr->portaperiod = period;
745 				cptr->period = operiod;
746 			}
747 
748 			cptr->effect = EFFECT_VOLSLIDE_TONEPORTA;
749 			if( effect_param != 0 )
750 				cptr->volumeslide = effect_param;
751 
752 		break;
753 
754 		case EFFECT_VOLSLIDE_VIBRATO:
755 			/*
756 			[6]: Continue 'Vibrato', but also do Volume slide
757 			Where [6][x][y] means "either slide the volume up x*(ticks - 1) or
758 			slide the volume down y*(ticks - 1), at the same time as continuing
759 			the last 'Vibrato'". It is illegal for both x and y to be non-zero.
760 			You cannot slide outside the volume range 0..64.
761 			*/
762 
763 			cptr->effect = EFFECT_VOLSLIDE_VIBRATO;
764 			if( effect_param != 0 )
765 				cptr->volumeslide = effect_param;
766 		break;
767 
768 		case EFFECT_SET_OFFSET:
769 			/*
770 			[9]: Set sample offset
771 			Where [9][x][y] means "play the sample from offset x*4096 + y*256".
772 			The offset is measured in words. If no sample is given, yet one is
773 			still playing on this channel, it should be retriggered to the new
774 			offset using the current volume.
775 			If xy is 00, the previous value is used.
776 			*/
777 
778 			cptr->samppos = ( ( ((muint)effect_param_h) << 12) + ( (((muint)effect_param_l) << 8) ) ) << 10;
779 
780 			if(!cptr->samppos)
781 				cptr->samppos = cptr->last_set_offset;
782 
783 			cptr->last_set_offset = cptr->samppos;
784 		break;
785 
786 		case EFFECT_VOLUME_SLIDE:
787 			/*
788 			[10]: Volume slide
789 			Where [10][x][y] means "either slide the volume up x*(ticks - 1) or
790 			slide the volume down y*(ticks - 1)". If both x and y are non-zero,
791 			then the y value is ignored (assumed to be 0). You cannot slide
792 			outside the volume range 0..64.
793 			*/
794 
795 			cptr->effect = EFFECT_VOLUME_SLIDE;
796 			cptr->volumeslide = effect_param;
797 		break;
798 
799 		case EFFECT_JUMP_POSITION:
800 			/*
801 			[11]: Position Jump
802 			Where [11][x][y] means "stop the pattern after this division, and
803 			continue the song at song-position x*16+y". This shifts the
804 			'pattern-cursor' in the pattern table (see above). Legal values for
805 			x*16+y are from 0 to 127.
806 			*/
807 
808 			mod->tablepos = effect_param;
809 			if(mod->tablepos >= mod->song.length)
810 				mod->tablepos = 0;
811 			mod->patternpos = 0;
812 			mod->jump_loop_effect = 1;
813 
814 		break;
815 
816 		case EFFECT_SET_VOLUME:
817 			/*
818 			[12]: Set volume
819 			Where [12][x][y] means "set current sample's volume to x*16+y".
820 			Legal volumes are 0..64.
821 			*/
822 
823 			cptr->volume = effect_param;
824 
825 			if(cptr->volume > 64)
826 				cptr->volume = 64;
827 
828 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
829 			cptr->volume_table = mod->volume_selection_table[cptr->volume];
830 #endif
831 		break;
832 
833 		case EFFECT_PATTERN_BREAK:
834 			/*
835 			[13]: Pattern Break
836 			Where [13][x][y] means "stop the pattern after this division, and
837 			continue the song at the next pattern at division x*10+y" (the 10
838 			is not a typo). Legal divisions are from 0 to 63 (note Protracker
839 			exception above).
840 			*/
841 
842 			mod->patternpos = ( ((muint)(effect_param_h) * 10) + effect_param_l );
843 
844 			if(mod->patternpos >= 64)
845 				mod->patternpos = 63;
846 
847 			mod->patternpos *= mod->number_of_channels;
848 
849 			if(!mod->jump_loop_effect)
850 			{
851 				mod->tablepos++;
852 				if(mod->tablepos >= mod->song.length)
853 					mod->tablepos = 0;
854 			}
855 
856 			mod->jump_loop_effect = 1;
857 		break;
858 
859 		case EFFECT_EXTENDED:
860 			switch( effect_param_h )
861 			{
862 				case EFFECT_E_FINE_PORTA_UP:
863 					/*
864 					[14][1]: Fineslide up
865 					Where [14][1][x] means "decrement the period of the current sample
866 					by x". The incrementing takes place at the beginning of the
867 					division, and hence there is no actual sliding. You cannot slide
868 					beyond the note B3 (period 113).
869 					*/
870 
871 					cptr->period -= effect_param_l;
872 					if( cptr->period < 113 )
873 						cptr->period = 113;
874 				break;
875 
876 				case EFFECT_E_FINE_PORTA_DOWN:
877 					/*
878 					[14][2]: Fineslide down
879 					Where [14][2][x] means "increment the period of the current sample
880 					by x". Similar to [14][1] but shifts the pitch down. You cannot
881 					slide beyond the note C1 (period 856).
882 					*/
883 
884 					cptr->period += effect_param_l;
885 					if( cptr->period > 856 )
886 						cptr->period = 856;
887 				break;
888 
889 				case EFFECT_E_GLISSANDO_CTRL:
890 					/*
891 					[14][3]: Set glissando on/off
892 					Where [14][3][x] means "set glissando ON if x is 1, OFF if x is 0".
893 					Used in conjunction with [3] ('Slide to note'). If glissando is on,
894 					then 'Slide to note' will slide in semitones, otherwise will
895 					perform the default smooth slide.
896 					*/
897 
898 					cptr->glissando = effect_param_l;
899 				break;
900 
901 				case EFFECT_E_FINE_VOLSLIDE_UP:
902 					/*
903 					[14][10]: Fine volume slide up
904 					Where [14][10][x] means "increment the volume of the current sample
905 					by x". The incrementing takes place at the beginning of the
906 					division, and hence there is no sliding. You cannot slide beyond
907 					volume 64.
908 					*/
909 
910 					cptr->volume += effect_param_l;
911 					if( cptr->volume > 64 )
912 						cptr->volume = 64;
913 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
914 					cptr->volume_table = mod->volume_selection_table[cptr->volume];
915 #endif
916 				break;
917 
918 				case EFFECT_E_FINE_VOLSLIDE_DOWN:
919 					/*
920 					[14][11]: Fine volume slide down
921 					Where [14][11][x] means "decrement the volume of the current sample
922 					by x". Similar to [14][10] but lowers volume. You cannot slide
923 					beyond volume 0.
924 					*/
925 
926 					cptr->volume -= effect_param_l;
927 					if( cptr->volume > 200 )
928 						cptr->volume = 0;
929 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
930 					cptr->volume_table = mod->volume_selection_table[cptr->volume];
931 #endif
932 				break;
933 
934 				case EFFECT_E_SET_FINETUNE:
935 					/*
936 					[14][5]: Set finetune value
937 					Where [14][5][x] means "sets the finetune value of the current
938 					sample to the signed nibble x". x has legal values of 0..15,
939 					corresponding to signed nibbles 0..7,-8..-1 (see start of text for
940 					more info on finetune values).
941 					*/
942 
943 					cptr->finetune = effect_param_l;
944 
945 					if( period )
946 					{
947 						period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
948 						period = period_table_ptr[getnote(mod,period)];
949 						cptr->period = period;
950 					}
951 
952 				break;
953 
954 				case EFFECT_E_PATTERN_LOOP:
955 					/*
956 					[14][6]: Loop pattern
957 					Where [14][6][x] means "set the start of a loop to this division if
958 					x is 0, otherwise after this division, jump back to the start of a
959 					loop and play it another x times before continuing". If the start
960 					of the loop was not set, it will default to the start of the
961 					current pattern. Hence 'loop pattern' cannot be performed across
962 					multiple patterns. Note that loops do not support nesting, and you
963 					may generate an infinite loop if you try to nest 'loop pattern's.
964 					*/
965 
966 					if( effect_param_l )
967 					{
968 						if( cptr->patternloopcnt )
969 						{
970 							cptr->patternloopcnt--;
971 							if( cptr->patternloopcnt )
972 							{
973 								mod->patternpos = cptr->patternloopstartpoint;
974 								mod->jump_loop_effect = 1;
975 							}
976 							else
977 							{
978 								cptr->patternloopstartpoint = mod->patternpos ;
979 							}
980 						}
981 						else
982 						{
983 							cptr->patternloopcnt = effect_param_l;
984 							mod->patternpos = cptr->patternloopstartpoint;
985 							mod->jump_loop_effect = 1;
986 						}
987 					}
988 					else // Start point
989 					{
990 						cptr->patternloopstartpoint = mod->patternpos;
991 					}
992 
993 				break;
994 
995 				case EFFECT_E_PATTERN_DELAY:
996 					/*
997 					[14][14]: Delay pattern
998 					Where [14][14][x] means "after this division there will be a delay
999 					equivalent to the time taken to play x divisions after which the
1000 					pattern will be resumed". The delay only relates to the
1001 					interpreting of new divisions, and all effects and previous notes
1002 					continue during delay.
1003 					*/
1004 
1005 					mod->patterndelay = effect_param_l;
1006 				break;
1007 
1008 				case EFFECT_E_RETRIGGER_NOTE:
1009 					/*
1010 					[14][9]: Retrigger sample
1011 					 Where [14][9][x] means "trigger current sample every x ticks in
1012 					 this division". If x is 0, then no retriggering is done (acts as if
1013 					 no effect was chosen), otherwise the retriggering begins on the
1014 					 first tick and then x ticks after that, etc.
1015 					*/
1016 
1017 					if( effect_param_l )
1018 					{
1019 						cptr->effect = EFFECT_EXTENDED;
1020 						cptr->parameffect = (EFFECT_E_RETRIGGER_NOTE<<4);
1021 						cptr->retrig_param = effect_param_l;
1022 						cptr->retrig_cnt = 0;
1023 					}
1024 				break;
1025 
1026 				case EFFECT_E_NOTE_CUT:
1027 					/*
1028 					[14][12]: Cut sample
1029 					Where [14][12][x] means "after the current sample has been played
1030 					for x ticks in this division, its volume will be set to 0". This
1031 					implies that if x is 0, then you will not hear any of the sample.
1032 					If you wish to insert "silence" in a pattern, it is better to use a
1033 					"silence"-sample (see above) due to the lack of proper support for
1034 					this effect.
1035 					*/
1036 
1037 					cptr->effect = EFFECT_E_NOTE_CUT;
1038 					cptr->cut_param = effect_param_l;
1039 					if( !cptr->cut_param )
1040 					{
1041 						cptr->volume = 0;
1042 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1043 						cptr->volume_table = mod->volume_selection_table[cptr->volume];
1044 #endif
1045 					}
1046 				break;
1047 
1048 				case EFFECT_E_NOTE_DELAY:
1049 					/*
1050 					 Where [14][13][x] means "do not start this division's sample for
1051 					 the first x ticks in this division, play the sample after this".
1052 					 This implies that if x is 0, then you will hear no delay, but
1053 					 actually there will be a VERY small delay. Note that this effect
1054 					 only influences a sample if it was started in this division.
1055 					*/
1056 
1057 					cptr->effect = EFFECT_EXTENDED;
1058 					cptr->parameffect = (EFFECT_E_NOTE_DELAY<<4);
1059 				break;
1060 
1061 				case EFFECT_E_INVERT_LOOP:
1062 					/*
1063 					Where [14][15][x] means "if x is greater than 0, then play the
1064 					current sample's loop upside down at speed x". Each byte in the
1065 					sample's loop will have its sign changed (negated). It will only
1066 					work if the sample's loop (defined previously) is not too big. The
1067 					speed is based on an internal table.
1068 					*/
1069 
1070 					cptr->funkspeed = effect_param_l;
1071 
1072 					doFunk(cptr);
1073 
1074 				break;
1075 
1076 				default:
1077 
1078 				break;
1079 			}
1080 		break;
1081 
1082 		case 0xF:
1083 			/*
1084 			[15]: Set speed
1085 			Where [15][x][y] means "set speed to x*16+y". Though it is nowhere
1086 			near that simple. Let z = x*16+y. Depending on what values z takes,
1087 			different units of speed are set, there being two: ticks/division
1088 			and beats/minute (though this one is only a label and not strictly
1089 			true). If z=0, then what should technically happen is that the
1090 			module stops, but in practice it is treated as if z=1, because
1091 			there is already a method for stopping the module (running out of
1092 			patterns). If z<=32, then it means "set ticks/division to z"
1093 			otherwise it means "set beats/minute to z" (convention says that
1094 			this should read "If z<32.." but there are some composers out there
1095 			that defy conventions). Default values are 6 ticks/division, and
1096 			125 beats/minute (4 divisions = 1 beat). The beats/minute tag is
1097 			only meaningful for 6 ticks/division. To get a more accurate view
1098 			of how things work, use the following formula:
1099 									 24 * beats/minute
1100 				  divisions/minute = -----------------
1101 									  ticks/division
1102 			Hence divisions/minute range from 24.75 to 6120, eg. to get a value
1103 			of 2000 divisions/minute use 3 ticks/division and 250 beats/minute.
1104 			If multiple "set speed" effects are performed in a single division,
1105 			the ones on higher-numbered channels take precedence over the ones
1106 			on lower-numbered channels. This effect has a large number of
1107 			different implementations, but the one described here has the
1108 			widest usage.
1109 			*/
1110 
1111 
1112 			if( effect_param )
1113 			{
1114 
1115 				if( effect_param < 0x20 )
1116 				{
1117 					mod->song.speed = effect_param;
1118 				}
1119 				else
1120 				{   // effect_param >= 0x20
1121 					///	 HZ = 2 * BPM / 5
1122 					mod->bpm = effect_param;
1123 				}
1124 
1125 #ifdef HXCMOD_16BITS_TARGET
1126 				// song.speed = 1 <> 31
1127 				// playrate = 8000 <> 22050
1128 				// bpm = 32 <> 255
1129 
1130 				mod->patternticksem = (muint)( ( (mulong)mod->playrate * 5 ) / ( (muint)mod->bpm * 2 ) );
1131 #else
1132 				// song.speed = 1 <> 31
1133 				// playrate = 8000 <> 96000
1134 				// bpm = 32 <> 255
1135 
1136 				mod->patternticksem = ( ( mod->playrate * 5 ) / ( (mulong)mod->bpm * 2 ) );
1137 #endif
1138 				mod->patternticksaim = mod->song.speed * mod->patternticksem;
1139 			}
1140 
1141 		break;
1142 
1143 		default:
1144 		// Unsupported effect
1145 		break;
1146 
1147 	}
1148 
1149 }
1150 
1151 static void workeffect( modcontext * modctx, note * nptr, hxcmod_channel_t * cptr )
1152 {
1153 	// BK4BSTACK_CHANGE START
1154 	(void) nptr;
1155 	// BK4BSTACK_CHANGE END
1156 
1157 	doFunk(cptr);
1158 
1159 	switch(cptr->effect)
1160 	{
1161 		case EFFECT_ARPEGGIO:
1162 
1163 			if( cptr->parameffect )
1164 			{
1165 				cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex];
1166 
1167 				cptr->ArpIndex++;
1168 				if( cptr->ArpIndex>2 )
1169 					cptr->ArpIndex = 0;
1170 			}
1171 		break;
1172 
1173 		case EFFECT_PORTAMENTO_UP:
1174 
1175 			if( cptr->period )
1176 			{
1177 				cptr->period -= cptr->parameffect;
1178 
1179 				if( cptr->period < 113 || cptr->period > 20000 )
1180 					cptr->period = 113;
1181 			}
1182 
1183 		break;
1184 
1185 		case EFFECT_PORTAMENTO_DOWN:
1186 
1187 			if( cptr->period )
1188 			{
1189 				cptr->period += cptr->parameffect;
1190 
1191 				if( cptr->period > 20000 )
1192 					cptr->period = 20000;
1193 			}
1194 
1195 		break;
1196 
1197 		case EFFECT_VOLSLIDE_TONEPORTA:
1198 		case EFFECT_TONE_PORTAMENTO:
1199 
1200 			if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod )
1201 			{
1202 				if( cptr->period > cptr->portaperiod )
1203 				{
1204 					if( cptr->period - cptr->portaperiod >= cptr->portaspeed )
1205 					{
1206 						cptr->period -= cptr->portaspeed;
1207 					}
1208 					else
1209 					{
1210 						cptr->period = cptr->portaperiod;
1211 					}
1212 				}
1213 				else
1214 				{
1215 					if( cptr->portaperiod - cptr->period >= cptr->portaspeed )
1216 					{
1217 						cptr->period += cptr->portaspeed;
1218 					}
1219 					else
1220 					{
1221 						cptr->period = cptr->portaperiod;
1222 					}
1223 				}
1224 
1225 				if( cptr->period == cptr->portaperiod )
1226 				{
1227 					// If the slide is over, don't let it to be retriggered.
1228 					cptr->portaperiod = 0;
1229 				}
1230 			}
1231 
1232 			if( cptr->glissando )
1233 			{
1234 				// TODO : Glissando effect.
1235 			}
1236 
1237 			if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA )
1238 			{
1239 				if( cptr->volumeslide & 0xF0 )
1240 				{
1241 					cptr->volume += ( cptr->volumeslide >> 4 );
1242 
1243 					if( cptr->volume > 63 )
1244 						cptr->volume = 63;
1245 				}
1246 				else
1247 				{
1248 					cptr->volume -= ( cptr->volumeslide & 0x0F );
1249 
1250 					if( cptr->volume > 63 )
1251 						cptr->volume = 0;
1252 				}
1253 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1254 				cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1255 #endif
1256 			}
1257 		break;
1258 
1259 		case EFFECT_VOLSLIDE_VIBRATO:
1260 		case EFFECT_VIBRATO:
1261 
1262 			cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7;
1263 
1264 			if( cptr->vibrapointeur > 31 )
1265 				cptr->vibraperiod = -cptr->vibraperiod;
1266 
1267 			cptr->vibrapointeur = ( cptr->vibrapointeur + ( ( cptr->vibraparam>>4 ) & 0x0F) ) & 0x3F;
1268 
1269 			if( cptr->effect == EFFECT_VOLSLIDE_VIBRATO )
1270 			{
1271 				if( cptr->volumeslide & 0xF0 )
1272 				{
1273 					cptr->volume += ( cptr->volumeslide >> 4 );
1274 
1275 					if( cptr->volume > 64 )
1276 						cptr->volume = 64;
1277 				}
1278 				else
1279 				{
1280 					cptr->volume -= cptr->volumeslide;
1281 
1282 					if( cptr->volume > 64 )
1283 						cptr->volume = 0;
1284 				}
1285 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1286 				cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1287 #endif
1288 			}
1289 
1290 		break;
1291 
1292 		case EFFECT_VOLUME_SLIDE:
1293 
1294 			if( cptr->volumeslide & 0xF0 )
1295 			{
1296 				cptr->volume += ( cptr->volumeslide >> 4 );
1297 
1298 				if( cptr->volume > 64 )
1299 					cptr->volume = 64;
1300 			}
1301 			else
1302 			{
1303 				cptr->volume -= cptr->volumeslide;
1304 
1305 				if( cptr->volume > 64 )
1306 					cptr->volume = 0;
1307 			}
1308 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1309 			cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1310 #endif
1311 		break;
1312 
1313 		case EFFECT_EXTENDED:
1314 			switch( cptr->parameffect >> 4 )
1315 			{
1316 
1317 				case EFFECT_E_NOTE_CUT:
1318 					if( cptr->cut_param )
1319 						cptr->cut_param--;
1320 
1321 					if( !cptr->cut_param )
1322 					{
1323 						cptr->volume = 0;
1324 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1325 						cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1326 #endif
1327 					}
1328 				break;
1329 
1330 				case EFFECT_E_RETRIGGER_NOTE:
1331 					cptr->retrig_cnt++;
1332 					if( cptr->retrig_cnt >= cptr->retrig_param )
1333 					{
1334 						cptr->retrig_cnt = 0;
1335 
1336 						cptr->sampdata = cptr->lst_sampdata;
1337 						cptr->length = cptr->lst_length;
1338 						cptr->reppnt = cptr->lst_reppnt;
1339 						cptr->replen = cptr->lst_replen;
1340 						cptr->samppos = 0;
1341 					}
1342 				break;
1343 
1344 				case EFFECT_E_NOTE_DELAY:
1345 					if( cptr->note_delay )
1346 					{
1347 						if( (unsigned char)( cptr->note_delay - 1 ) == modctx->tick_cnt )
1348 						{
1349 							cptr->sampdata = cptr->dly_sampdata;
1350 							cptr->length = cptr->dly_length;
1351 							cptr->reppnt = cptr->dly_reppnt;
1352 							cptr->replen = cptr->dly_replen;
1353 
1354 							cptr->lst_sampdata = cptr->sampdata;
1355 							cptr->lst_length = cptr->length;
1356 							cptr->lst_reppnt = cptr->reppnt;
1357 							cptr->lst_replen = cptr->replen;
1358 							cptr->note_delay = 0;
1359 						}
1360 					}
1361 				break;
1362 				default:
1363 				break;
1364 			}
1365 		break;
1366 
1367 		default:
1368 		break;
1369 
1370 	}
1371 
1372 }
1373 
1374 ///////////////////////////////////////////////////////////////////////////////////
1375 int hxcmod_init(modcontext * modctx)
1376 {
1377 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1378 	muint c;
1379 	mint  i,j;
1380 #endif
1381 	if( modctx )
1382 	{
1383 		memclear(modctx,0,sizeof(modcontext));
1384 		modctx->playrate = 44100;
1385 		modctx->stereo = 1;
1386 		modctx->stereo_separation = 1;
1387 		modctx->bits = 16;
1388 		modctx->filter = 1;
1389 
1390 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1391 		c = 0;
1392 		for(i=0;i<65;i++)
1393 		{
1394 			for(j=-128;j<128;j++)
1395 			{
1396 				modctx->precalc_volume_array[c] = i * j;
1397 				c++;
1398 			}
1399 
1400 			modctx->volume_selection_table[i] = &modctx->precalc_volume_array[(i*256) + 128];
1401 		}
1402 #endif
1403 
1404 		return 1;
1405 	}
1406 
1407 	return 0;
1408 }
1409 
1410 int hxcmod_setcfg(modcontext * modctx, int samplerate, int stereo_separation, int filter)
1411 {
1412 	if( modctx )
1413 	{
1414 		modctx->playrate = samplerate;
1415 
1416 		if(stereo_separation < 4)
1417 		{
1418 			modctx->stereo_separation = stereo_separation;
1419 		}
1420 
1421 		if( filter )
1422 			modctx->filter = 1;
1423 		else
1424 			modctx->filter = 0;
1425 
1426 		return 1;
1427 	}
1428 
1429 	return 0;
1430 }
1431 
1432 int hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size )
1433 {
1434 	muint i, j, max, digitfactor;
1435 	sample *sptr;
1436 	unsigned char * modmemory,* endmodmemory;
1437 
1438 	modmemory = (unsigned char *)mod_data;
1439 	endmodmemory = modmemory + mod_data_size;
1440 
1441 	if( modmemory )
1442 	{
1443 		if( modctx )
1444 		{
1445 #ifdef FULL_STATE
1446 			memclear(&(modctx->effects_event_counts),0,sizeof(modctx->effects_event_counts));
1447 #endif
1448 			memcopy(&(modctx->song),modmemory,1084);
1449 
1450 			i = 0;
1451 			modctx->number_of_channels = 0;
1452 			while(modlist[i].numberofchannels && !modctx->number_of_channels)
1453 			{
1454 				digitfactor = 0;
1455 
1456 				j = 0;
1457 				while( j < 4 )
1458 				{
1459 					if( modlist[i].signature[j] == '$' )
1460 					{
1461 						if(digitfactor)
1462 							digitfactor *= 10;
1463 						else
1464 							digitfactor = 1;
1465 					}
1466 					j++;
1467 				}
1468 
1469 				modctx->number_of_channels = 0;
1470 
1471 				j = 0;
1472 				while( j < 4 )
1473 				{
1474 					if( (modlist[i].signature[j] == modctx->song.signature[j]) || modlist[i].signature[j] == '$' )
1475 					{
1476 						if( modlist[i].signature[j] == '$' )
1477 						{
1478 							if(modctx->song.signature[j] >= '0' && modctx->song.signature[j] <= '9')
1479 							{
1480 								modctx->number_of_channels += (modctx->song.signature[j] - '0') * digitfactor;
1481 								digitfactor /= 10;
1482 							}
1483 							else
1484 							{
1485 								modctx->number_of_channels = 0;
1486 								break;
1487 							}
1488 						}
1489 						j++;
1490 					}
1491 					else
1492 					{
1493 						modctx->number_of_channels = 0;
1494 						break;
1495 					}
1496 				}
1497 
1498 				if( j == 4 )
1499 				{
1500 					if(!modctx->number_of_channels)
1501 						modctx->number_of_channels = modlist[i].numberofchannels;
1502 				}
1503 
1504 				i++;
1505 			}
1506 
1507 			if( !modctx->number_of_channels )
1508 			{
1509 				// 15 Samples modules support
1510 				// Shift the whole datas to make it look likes a standard 4 channels mod.
1511 				memcopy(&(modctx->song.signature), "M.K.", 4);
1512 				memcopy(&(modctx->song.length), &(modctx->song.samples[15]), 130);
1513 				memclear(&(modctx->song.samples[15]), 0, 480);
1514 				modmemory += 600;
1515 				modctx->number_of_channels = 4;
1516 			}
1517 			else
1518 			{
1519 				modmemory += 1084;
1520 			}
1521 
1522 			if( modctx->number_of_channels > NUMMAXCHANNELS )
1523 				return 0; // Too much channels ! - Increase/define HXCMOD_MAXCHANNELS !
1524 
1525 			if( modmemory >= endmodmemory )
1526 				return 0; // End passed ? - Probably a bad file !
1527 
1528 			// Patterns loading
1529 			for (i = max = 0; i < 128; i++)
1530 			{
1531 				while (max <= modctx->song.patterntable[i])
1532 				{
1533 					modctx->patterndata[max] = (note*)modmemory;
1534 					modmemory += (256*modctx->number_of_channels);
1535 					max++;
1536 
1537 					if( modmemory >= endmodmemory )
1538 						return 0; // End passed ? - Probably a bad file !
1539 				}
1540 			}
1541 
1542 			for (i = 0; i < 31; i++)
1543 				modctx->sampledata[i]=0;
1544 
1545 			// Samples loading
1546 			for (i = 0, sptr = modctx->song.samples; i <31; i++, sptr++)
1547 			{
1548 				if (sptr->length == 0) continue;
1549 
1550 				modctx->sampledata[i] = (mchar*)modmemory;
1551 				modmemory += (GET_BGI_W(sptr->length)*2);
1552 
1553 				if (GET_BGI_W(sptr->replen) + GET_BGI_W(sptr->reppnt) > GET_BGI_W(sptr->length))
1554 					sptr->replen = GET_BGI_W((GET_BGI_W(sptr->length) - GET_BGI_W(sptr->reppnt)));
1555 
1556 				if( modmemory > endmodmemory )
1557 					return 0; // End passed ? - Probably a bad file !
1558 			}
1559 
1560 			// States init
1561 
1562 			modctx->tablepos = 0;
1563 			modctx->patternpos = 0;
1564 			modctx->song.speed = 6;
1565 			modctx->bpm = 125;
1566 
1567 #ifdef HXCMOD_16BITS_TARGET
1568 			// song.speed = 1 <> 31
1569 			// playrate = 8000 <> 22050
1570 			// bpm = 32 <> 255
1571 
1572 			modctx->patternticksem = (muint)( ( (mulong)modctx->playrate * 5 ) / ( (muint)modctx->bpm * 2 ) );
1573 #else
1574 			// song.speed = 1 <> 31
1575 			// playrate = 8000 <> 96000
1576 			// bpm = 32 <> 255
1577 
1578 			modctx->patternticksem = ( ( modctx->playrate * 5 ) / ( (mulong)modctx->bpm * 2 ) );
1579 #endif
1580 			modctx->patternticksaim = modctx->song.speed * modctx->patternticksem;
1581 
1582 			modctx->patternticks = modctx->patternticksaim + 1;
1583 
1584 			modctx->sampleticksconst = ((3546894UL * 16) / modctx->playrate) << 6; //8448*428/playrate;
1585 
1586 			for(i=0; i < modctx->number_of_channels; i++)
1587 			{
1588 				modctx->channels[i].volume = 0;
1589 				modctx->channels[i].period = 0;
1590 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1591 				modctx->channels[i].volume_table = modctx->volume_selection_table[0];
1592 #endif
1593 			}
1594 
1595 			modctx->mod_loaded = 1;
1596 
1597 			return 1;
1598 		}
1599 	}
1600 
1601 	return 0;
1602 }
1603 
1604 void hxcmod_fillbuffer(modcontext * modctx, msample * outbuffer, mssize nbsample, tracker_buffer_state * trkbuf)
1605 {
1606 	mssize i;
1607 	muint  j;
1608 	muint c;
1609 
1610 	unsigned long k;
1611 	unsigned int state_remaining_steps;
1612 
1613 #ifdef HXCMOD_OUTPUT_FILTER
1614 #ifndef HXCMOD_MONO_OUTPUT
1615 	int ll,tl;
1616 #endif
1617 	int lr,tr;
1618 #endif
1619 
1620 #ifndef HXCMOD_MONO_OUTPUT
1621 	int l;
1622 #endif
1623 	int r;
1624 
1625 	short finalperiod;
1626 	note	*nptr;
1627 	hxcmod_channel_t *cptr;
1628 
1629 	if( modctx && outbuffer )
1630 	{
1631 		if(modctx->mod_loaded)
1632 		{
1633 			state_remaining_steps = 0;
1634 
1635 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1636 			if( trkbuf )
1637 			{
1638 				trkbuf->cur_rd_index = 0;
1639 
1640 				memcopy(trkbuf->name,modctx->song.title,sizeof(modctx->song.title));
1641 
1642 				for(i=0;i<31;i++)
1643 				{
1644 					memcopy(trkbuf->instruments[i].name,modctx->song.samples[i].name,sizeof(trkbuf->instruments[i].name));
1645 				}
1646 			}
1647 #endif
1648 
1649 #ifdef HXCMOD_OUTPUT_FILTER
1650 	#ifndef HXCMOD_MONO_OUTPUT
1651 			ll = modctx->last_l_sample;
1652 	#endif
1653 			lr = modctx->last_r_sample;
1654 #endif
1655 
1656 			for (i = 0; i < nbsample; i++)
1657 			{
1658 				//---------------------------------------
1659 				if( modctx->patternticks++ > modctx->patternticksaim )
1660 				{
1661 					if( !modctx->patterndelay )
1662 					{
1663 						nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1664 						nptr = nptr + modctx->patternpos;
1665 						cptr = modctx->channels;
1666 
1667 						modctx->tick_cnt = 0;
1668 
1669 						modctx->patternticks = 0;
1670 						modctx->patterntickse = 0;
1671 
1672 						for(c=0;c<modctx->number_of_channels;c++)
1673 						{
1674 							worknote((note*)(nptr), (hxcmod_channel_t*)(cptr),(char)(c+1),modctx);
1675 
1676 							if (cptr->period != 0)
1677 							{
1678 								finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod;
1679 								if (finalperiod)
1680 								{
1681 									cptr->sampinc = ((modctx->sampleticksconst) / finalperiod);
1682 								}
1683 								else
1684 								{
1685 									cptr->sampinc = 0;
1686 								}
1687 							}
1688 							else
1689 								cptr->sampinc = 0;
1690 
1691 							nptr++;
1692 							cptr++;
1693 						}
1694 
1695 						if( !modctx->jump_loop_effect )
1696 							modctx->patternpos += modctx->number_of_channels;
1697 						else
1698 							modctx->jump_loop_effect = 0;
1699 
1700 						if( modctx->patternpos == 64*modctx->number_of_channels )
1701 						{
1702 							modctx->tablepos++;
1703 							modctx->patternpos = 0;
1704 							if(modctx->tablepos >= modctx->song.length)
1705 								modctx->tablepos = 0;
1706 						}
1707 					}
1708 					else
1709 					{
1710 						modctx->patterndelay--;
1711 						modctx->patternticks = 0;
1712 						modctx->patterntickse = 0;
1713 						modctx->tick_cnt = 0;
1714 					}
1715 
1716 				}
1717 
1718 				if (modctx->patterntickse++ > modctx->patternticksem)
1719 				{
1720 					nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1721 					nptr = nptr + modctx->patternpos;
1722 					cptr = modctx->channels;
1723 
1724 					for(c=0;c<modctx->number_of_channels;c++)
1725 					{
1726 						workeffect( modctx, nptr, cptr );
1727 
1728 						if (cptr->period != 0)
1729 						{
1730 							finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod;
1731 							if (finalperiod)
1732 							{
1733 								cptr->sampinc = ((modctx->sampleticksconst) / finalperiod);
1734 							}
1735 							else
1736 							{
1737 								cptr->sampinc = 0;
1738 							}
1739 						}
1740 						else
1741 							cptr->sampinc = 0;
1742 
1743 						nptr++;
1744 						cptr++;
1745 					}
1746 
1747 					modctx->tick_cnt++;
1748 					modctx->patterntickse = 0;
1749 				}
1750 
1751 				//---------------------------------------
1752 
1753 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1754 				if( trkbuf && !state_remaining_steps )
1755 				{
1756 					if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1757 					{
1758 						memclear(&trkbuf->track_state_buf[trkbuf->nb_of_state],0,sizeof(tracker_state));
1759 					}
1760 				}
1761 #endif
1762 
1763 #ifndef HXCMOD_MONO_OUTPUT
1764 				l=0;
1765 #endif
1766 				r=0;
1767 
1768 				for( j = 0, cptr = modctx->channels; j < modctx->number_of_channels ; j++, cptr++)
1769 				{
1770 					if( cptr->period != 0 )
1771 					{
1772 						cptr->samppos += cptr->sampinc;
1773 
1774 						if( cptr->replen < 2 )
1775 						{
1776 							if( ( cptr->samppos >> 11) >= cptr->length )
1777 							{
1778 								cptr->length = 0;
1779 								cptr->reppnt = 0;
1780 
1781 								if(cptr->update_nxt_repeat)
1782 								{
1783 									cptr->replen = cptr->nxt_replen;
1784 									cptr->reppnt = cptr->nxt_reppnt;
1785 									cptr->sampdata = cptr->nxt_sampdata;
1786 									cptr->length = cptr->nxt_length;
1787 
1788 									cptr->lst_sampdata = cptr->sampdata;
1789 									cptr->lst_length = cptr->length;
1790 									cptr->lst_reppnt = cptr->reppnt;
1791 									cptr->lst_replen = cptr->replen;
1792 
1793 									cptr->update_nxt_repeat = 0;
1794 								}
1795 
1796 								if( cptr->length )
1797 									cptr->samppos = cptr->samppos % (((unsigned long)cptr->length)<<11);
1798 								else
1799 									cptr->samppos = 0;
1800 							}
1801 						}
1802 						else
1803 						{
1804 							if( ( cptr->samppos >> 11 ) >= (unsigned long)(cptr->replen+cptr->reppnt) )
1805 							{
1806 								if( cptr->update_nxt_repeat )
1807 								{
1808 									cptr->replen = cptr->nxt_replen;
1809 									cptr->reppnt = cptr->nxt_reppnt;
1810 									cptr->sampdata = cptr->nxt_sampdata;
1811 									cptr->length = cptr->nxt_length;
1812 
1813 									cptr->lst_sampdata = cptr->sampdata;
1814 									cptr->lst_length = cptr->length;
1815 									cptr->lst_reppnt = cptr->reppnt;
1816 									cptr->lst_replen = cptr->replen;
1817 
1818 									cptr->update_nxt_repeat = 0;
1819 								}
1820 
1821 								if( cptr->sampdata )
1822 								{
1823 									cptr->samppos = ((unsigned long)(cptr->reppnt)<<11) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<11));
1824 								}
1825 							}
1826 						}
1827 
1828 						k = cptr->samppos >> 10;
1829 
1830 #ifdef HXCMOD_MONO_OUTPUT
1831 						if( cptr->sampdata!=0 )
1832 						{
1833 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1834 							r += cptr->volume_table[cptr->sampdata[k]];
1835 #else
1836 							r += ( cptr->sampdata[k] *  cptr->volume );
1837 #endif
1838 						}
1839 #else
1840 						if (cptr->sampdata != 0)
1841 						{
1842 							if ( !(j & 3) || ((j & 3) == 3) )
1843 							{
1844 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1845 								l += cptr->volume_table[cptr->sampdata[k]];
1846 #else
1847 								l += (cptr->sampdata[k] * cptr->volume);
1848 #endif
1849 							}
1850 							else
1851 							{
1852 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1853 								r += cptr->volume_table[cptr->sampdata[k]];
1854 #else
1855 								r += (cptr->sampdata[k] * cptr->volume);
1856 #endif
1857 							}
1858 						}
1859 #endif
1860 
1861 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1862 						if( trkbuf && !state_remaining_steps )
1863 						{
1864 							if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1865 							{
1866 								trkbuf->track_state_buf[trkbuf->nb_of_state].number_of_tracks = modctx->number_of_channels;
1867 								trkbuf->track_state_buf[trkbuf->nb_of_state].buf_index = i;
1868 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern = modctx->song.patterntable[modctx->tablepos];
1869 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_pos = modctx->patternpos / modctx->number_of_channels;
1870 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_table_pos = modctx->tablepos;
1871 								trkbuf->track_state_buf[trkbuf->nb_of_state].bpm = modctx->bpm;
1872 								trkbuf->track_state_buf[trkbuf->nb_of_state].speed = modctx->song.speed;
1873 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_effect = cptr->effect_code;
1874 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_parameffect = cptr->parameffect;
1875 								if(cptr->sampinc)
1876 									trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = (muint)(modctx->sampleticksconst / cptr->sampinc);
1877 								else
1878 									trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = 0;
1879 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_volume = cptr->volume;
1880 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].instrument_number = (unsigned char)cptr->sampnum;
1881 							}
1882 						}
1883 #endif
1884 					}
1885 				}
1886 
1887 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1888 				if( trkbuf && !state_remaining_steps )
1889 				{
1890 					state_remaining_steps = trkbuf->sample_step;
1891 
1892 					if(trkbuf->nb_of_state < trkbuf->nb_max_of_state)
1893 						trkbuf->nb_of_state++;
1894 				}
1895 				else
1896 #endif
1897 				{
1898 					state_remaining_steps--;
1899 				}
1900 
1901 #ifdef HXCMOD_MONO_OUTPUT
1902 
1903 	#ifdef HXCMOD_OUTPUT_FILTER
1904 				tr = (short)r;
1905 
1906 				if ( modctx->filter )
1907 				{
1908 					// Filter
1909 					r = (r+lr)>>1;
1910 				}
1911 	#endif
1912 
1913 	#ifdef HXCMOD_CLIPPING_CHECK
1914 				// Level limitation
1915 				if( r > 32767 ) r = 32767;
1916 				if( r < -32768 ) r = -32768;
1917 	#endif
1918 				// Store the final sample.
1919 	#ifdef HXCMOD_8BITS_OUTPUT
1920 
1921 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1922 				*outbuffer++ = (r >> 8) + 127;
1923 		#else
1924 				*outbuffer++ = r >> 8;
1925 		#endif
1926 
1927 	#else
1928 
1929 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1930 				*outbuffer++ = r + 32767;
1931 		#else
1932 				*outbuffer++ = r;
1933 		#endif
1934 
1935 	#endif
1936 
1937 	#ifdef HXCMOD_OUTPUT_FILTER
1938 				lr = tr;
1939 	#endif
1940 
1941 #else
1942 
1943 	#ifdef HXCMOD_OUTPUT_FILTER
1944 				tl = (short)l;
1945 				tr = (short)r;
1946 
1947 				if ( modctx->filter )
1948 				{
1949 					// Filter
1950 					l = (l+ll)>>1;
1951 					r = (r+lr)>>1;
1952 				}
1953 	#endif
1954 
1955 	#ifdef HXCMOD_OUTPUT_STEREO_MIX
1956 				if ( modctx->stereo_separation == 1 )
1957 				{
1958 					// Left & Right Stereo panning
1959 					l = (l+(r>>1));
1960 					r = (r+(l>>1));
1961 				}
1962 	#endif
1963 
1964 	#ifdef HXCMOD_CLIPPING_CHECK
1965 				// Level limitation
1966 				if( l > 32767 ) l = 32767;
1967 				if( l < -32768 ) l = -32768;
1968 				if( r > 32767 ) r = 32767;
1969 				if( r < -32768 ) r = -32768;
1970 	#endif
1971 				// Store the final sample.
1972 
1973 
1974 	#ifdef HXCMOD_8BITS_OUTPUT
1975 
1976 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1977 				*outbuffer++ = ( l >> 8 ) + 127;
1978 				*outbuffer++ = ( r >> 8 ) + 127;
1979 		#else
1980 				*outbuffer++ = l >> 8;
1981 				*outbuffer++ = r >> 8;
1982 		#endif
1983 
1984 	#else
1985 
1986 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1987 				*outbuffer++ = l + 32767;
1988 				*outbuffer++ = r + 32767;
1989 		#else
1990 				*outbuffer++ = l;
1991 				*outbuffer++ = r;
1992 		#endif
1993 
1994 	#endif
1995 
1996 	#ifdef HXCMOD_OUTPUT_FILTER
1997 				ll = tl;
1998 				lr = tr;
1999 	#endif
2000 
2001 #endif // HXCMOD_MONO_OUTPUT
2002 
2003 			}
2004 
2005 #ifdef HXCMOD_OUTPUT_FILTER
2006 	#ifndef HXCMOD_MONO_OUTPUT
2007 			modctx->last_l_sample = ll;
2008 	#endif
2009 			modctx->last_r_sample = lr;
2010 #endif
2011 		}
2012 		else
2013 		{
2014 			for (i = 0; i < nbsample; i++)
2015 			{
2016 				// Mod not loaded. Return blank buffer.
2017 #ifdef HXCMOD_MONO_OUTPUT
2018 				outbuffer[i] = 0;
2019 #else
2020 				*outbuffer++ = 0;
2021 				*outbuffer++ = 0;
2022 #endif
2023 			}
2024 
2025 #ifdef HXCMOD_STATE_REPORT_SUPPORT
2026 			if(trkbuf)
2027 			{
2028 				trkbuf->nb_of_state = 0;
2029 				trkbuf->cur_rd_index = 0;
2030 				trkbuf->name[0] = 0;
2031 				memclear(trkbuf->track_state_buf,0,sizeof(tracker_state) * trkbuf->nb_max_of_state);
2032 				memclear(trkbuf->instruments,0,sizeof(trkbuf->instruments));
2033 			}
2034 #endif
2035 		}
2036 	}
2037 }
2038 
2039 void hxcmod_unload( modcontext * modctx )
2040 {
2041 	if(modctx)
2042 	{
2043 		memclear(&modctx->song,0,sizeof(modctx->song));
2044 		memclear(&modctx->sampledata,0,sizeof(modctx->sampledata));
2045 		memclear(&modctx->patterndata,0,sizeof(modctx->patterndata));
2046 		modctx->tablepos = 0;
2047 		modctx->patternpos = 0;
2048 		modctx->patterndelay  = 0;
2049 		modctx->jump_loop_effect = 0;
2050 		modctx->bpm = 0;
2051 		modctx->patternticks = 0;
2052 		modctx->patterntickse = 0;
2053 		modctx->patternticksaim = 0;
2054 		modctx->sampleticksconst = 0;
2055 
2056 		memclear(modctx->channels,0,sizeof(modctx->channels));
2057 
2058 		modctx->number_of_channels = 0;
2059 
2060 		modctx->mod_loaded = 0;
2061 
2062 		modctx->last_r_sample = 0;
2063 		modctx->last_l_sample = 0;
2064 	}
2065 }
2066