xref: /btstack/3rd-party/hxcmod-player/hxcmod.c (revision 4aa7d471d326f94fa7e80af8b1ccb77ec47dde0c)
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(channel * 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, channel * cptr,char t,modcontext * mod )
480 {
481 	// BK4BSTACK_CHANGE START
482 	(void) t;
483 	// BK4BSTACK_CHANGE END
484 
485 	muint sample, period, effect, operiod;
486 	muint curnote, arpnote;
487 	muchar effect_op;
488 	muchar effect_param,effect_param_l,effect_param_h;
489 	muint enable_nxt_smp;
490 	const short * period_table_ptr;
491 
492 	sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4);
493 	period = ((nptr->sampperiod & 0xF) << 8) | nptr->period;
494 	effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect;
495 	effect_op = nptr->sampeffect & 0xF;
496 	effect_param = nptr->effect;
497 	effect_param_l = effect_param & 0x0F;
498 	effect_param_h = effect_param >> 4;
499 
500 	enable_nxt_smp = 0;
501 
502 	operiod = cptr->period;
503 
504 	if ( period || sample )
505 	{
506 		if( sample && ( sample < 32 ) )
507 		{
508 			cptr->sampnum = sample - 1;
509 		}
510 
511 		if( period || sample )
512 		{
513 			if( period )
514 			{
515 				if( ( effect_op != EFFECT_TONE_PORTAMENTO ) || ( ( effect_op == EFFECT_TONE_PORTAMENTO ) && !cptr->sampdata ) )
516 				{
517 					// Not a Tone Partamento effect or no sound currently played :
518 					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 ) )
519 					{
520 						// Immediately (re)trigger the new note
521 						cptr->sampdata = mod->sampledata[cptr->sampnum];
522 						cptr->length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
523 						cptr->reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
524 						cptr->replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
525 
526 						cptr->lst_sampdata = cptr->sampdata;
527 						cptr->lst_length = cptr->length;
528 						cptr->lst_reppnt = cptr->reppnt;
529 						cptr->lst_replen = cptr->replen;
530 					}
531 					else
532 					{
533 						cptr->dly_sampdata = mod->sampledata[cptr->sampnum];
534 						cptr->dly_length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
535 						cptr->dly_reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
536 						cptr->dly_replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
537 						cptr->note_delay = effect_param_l;
538 					}
539 					// Cancel any delayed note...
540 					cptr->update_nxt_repeat = 0;
541 				}
542 				else
543 				{
544 					// Partamento effect - Play the new note after the current sample.
545 					if( effect_op == EFFECT_TONE_PORTAMENTO )
546 						enable_nxt_smp = 1;
547 				}
548 			}
549 			else // Note without period : Trigger it after the current sample.
550 				enable_nxt_smp = 1;
551 
552 			if ( enable_nxt_smp )
553 			{
554 				// Prepare the next sample retrigger after the current one
555 				cptr->nxt_sampdata = mod->sampledata[cptr->sampnum];
556 				cptr->nxt_length = GET_BGI_W( mod->song.samples[cptr->sampnum].length );
557 				cptr->nxt_reppnt = GET_BGI_W( mod->song.samples[cptr->sampnum].reppnt );
558 				cptr->nxt_replen = GET_BGI_W( mod->song.samples[cptr->sampnum].replen );
559 
560 				if(cptr->nxt_replen < 2)   // Protracker : don't play the sample if not looped...
561 					cptr->nxt_sampdata = 0;
562 
563 				cptr->update_nxt_repeat = 1;
564 			}
565 
566 			cptr->finetune = (mod->song.samples[cptr->sampnum].finetune) & 0xF;
567 
568 			if( effect_op != EFFECT_VIBRATO && effect_op != EFFECT_VOLSLIDE_VIBRATO )
569 			{
570 				cptr->vibraperiod = 0;
571 				cptr->vibrapointeur = 0;
572 			}
573 		}
574 
575 		if( (sample != 0) && ( effect_op != EFFECT_VOLSLIDE_TONEPORTA ) )
576 		{
577 			cptr->volume = mod->song.samples[cptr->sampnum].volume;
578 			cptr->volumeslide = 0;
579 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
580 			cptr->volume_table = mod->volume_selection_table[cptr->volume];
581 #endif
582 		}
583 
584 		if( ( effect_op != EFFECT_TONE_PORTAMENTO ) && ( effect_op != EFFECT_VOLSLIDE_TONEPORTA ) )
585 		{
586 			if ( period != 0 )
587 				cptr->samppos = 0;
588 		}
589 
590 		cptr->decalperiod = 0;
591 		if( period )
592 		{
593 			if( cptr->finetune )
594 			{
595 				period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
596 				period = period_table_ptr[getnote(mod,period)];
597 			}
598 
599 			cptr->period = period;
600 		}
601 	}
602 
603 	cptr->effect = 0;
604 	cptr->parameffect = 0;
605 	cptr->effect_code = effect;
606 
607 #ifdef EFFECTS_USAGE_STATE
608 	if(effect_op || ((effect_op==EFFECT_ARPEGGIO) && effect_param))
609 	{
610 		mod->effects_event_counts[ effect_op ]++;
611 	}
612 
613 	if(effect_op == 0xE)
614 		mod->effects_event_counts[ 0x10 + effect_param_h ]++;
615 #endif
616 
617 	switch ( effect_op )
618 	{
619 		case EFFECT_ARPEGGIO:
620 			/*
621 			[0]: Arpeggio
622 			Where [0][x][y] means "play note, note+x semitones, note+y
623 			semitones, then return to original note". The fluctuations are
624 			carried out evenly spaced in one pattern division. They are usually
625 			used to simulate chords, but this doesn't work too well. They are
626 			also used to produce heavy vibrato. A major chord is when x=4, y=7.
627 			A minor chord is when x=3, y=7.
628 			*/
629 
630 			if( effect_param )
631 			{
632 				cptr->effect = EFFECT_ARPEGGIO;
633 				cptr->parameffect = effect_param;
634 
635 				cptr->ArpIndex = 0;
636 
637 				curnote = getnote(mod,cptr->period);
638 
639 				cptr->Arpperiods[0] = cptr->period;
640 
641 				period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
642 
643 				arpnote = curnote + (((cptr->parameffect>>4)&0xF));
644 				if( arpnote >= MAXNOTES )
645 					arpnote = (MAXNOTES) - 1;
646 
647 				cptr->Arpperiods[1] = period_table_ptr[arpnote];
648 
649 				arpnote = curnote + (((cptr->parameffect)&0xF));
650 				if( arpnote >= MAXNOTES )
651 					arpnote = (MAXNOTES) - 1;
652 
653 				cptr->Arpperiods[2] = period_table_ptr[arpnote];
654 			}
655 		break;
656 
657 		case EFFECT_PORTAMENTO_UP:
658 			/*
659 			[1]: Slide up
660 			Where [1][x][y] means "smoothly decrease the period of current
661 			sample by x*16+y after each tick in the division". The
662 			ticks/division are set with the 'set speed' effect (see below). If
663 			the period of the note being played is z, then the final period
664 			will be z - (x*16 + y)*(ticks - 1). As the slide rate depends on
665 			the speed, changing the speed will change the slide. You cannot
666 			slide beyond the note B3 (period 113).
667 			*/
668 
669 			cptr->effect = EFFECT_PORTAMENTO_UP;
670 			cptr->parameffect = effect_param;
671 		break;
672 
673 		case EFFECT_PORTAMENTO_DOWN:
674 			/*
675 			[2]: Slide down
676 			Where [2][x][y] means "smoothly increase the period of current
677 			sample by x*16+y after each tick in the division". Similar to [1],
678 			but lowers the pitch. You cannot slide beyond the note C1 (period
679 			856).
680 			*/
681 
682 			cptr->effect = EFFECT_PORTAMENTO_DOWN;
683 			cptr->parameffect = effect_param;
684 		break;
685 
686 		case EFFECT_TONE_PORTAMENTO:
687 			/*
688 			[3]: Slide to note
689 			Where [3][x][y] means "smoothly change the period of current sample
690 			by x*16+y after each tick in the division, never sliding beyond
691 			current period". The period-length in this channel's division is a
692 			parameter to this effect, and hence is not played. Sliding to a
693 			note is similar to effects [1] and [2], but the slide will not go
694 			beyond the given period, and the direction is implied by that
695 			period. If x and y are both 0, then the old slide will continue.
696 			*/
697 
698 			cptr->effect = EFFECT_TONE_PORTAMENTO;
699 			if( effect_param != 0 )
700 			{
701 				cptr->portaspeed = (short)( effect_param );
702 			}
703 
704 			if(period!=0)
705 			{
706 				cptr->portaperiod = period;
707 				cptr->period = operiod;
708 			}
709 		break;
710 
711 		case EFFECT_VIBRATO:
712 			/*
713 			[4]: Vibrato
714 			Where [4][x][y] means "oscillate the sample pitch using a
715 			particular waveform with amplitude y/16 semitones, such that (x *
716 			ticks)/64 cycles occur in the division". The waveform is set using
717 			effect [14][4]. By placing vibrato effects on consecutive
718 			divisions, the vibrato effect can be maintained. If either x or y
719 			are 0, then the old vibrato values will be used.
720 			*/
721 
722 			cptr->effect = EFFECT_VIBRATO;
723 			if( effect_param_l != 0 ) // Depth continue or change ?
724 				cptr->vibraparam = ( cptr->vibraparam & 0xF0 ) | effect_param_l;
725 			if( effect_param_h != 0 ) // Speed continue or change ?
726 				cptr->vibraparam = ( cptr->vibraparam & 0x0F ) | ( effect_param_h << 4 );
727 
728 		break;
729 
730 		case EFFECT_VOLSLIDE_TONEPORTA:
731 			/*
732 			[5]: Continue 'Slide to note', but also do Volume slide
733 			Where [5][x][y] means "either slide the volume up x*(ticks - 1) or
734 			slide the volume down y*(ticks - 1), at the same time as continuing
735 			the last 'Slide to note'". It is illegal for both x and y to be
736 			non-zero. You cannot slide outside the volume range 0..64. The
737 			period-length in this channel's division is a parameter to this
738 			effect, and hence is not played.
739 			*/
740 
741 			if( period != 0 )
742 			{
743 				cptr->portaperiod = period;
744 				cptr->period = operiod;
745 			}
746 
747 			cptr->effect = EFFECT_VOLSLIDE_TONEPORTA;
748 			if( effect_param != 0 )
749 				cptr->volumeslide = effect_param;
750 
751 		break;
752 
753 		case EFFECT_VOLSLIDE_VIBRATO:
754 			/*
755 			[6]: Continue 'Vibrato', but also do Volume slide
756 			Where [6][x][y] means "either slide the volume up x*(ticks - 1) or
757 			slide the volume down y*(ticks - 1), at the same time as continuing
758 			the last 'Vibrato'". It is illegal for both x and y to be non-zero.
759 			You cannot slide outside the volume range 0..64.
760 			*/
761 
762 			cptr->effect = EFFECT_VOLSLIDE_VIBRATO;
763 			if( effect_param != 0 )
764 				cptr->volumeslide = effect_param;
765 		break;
766 
767 		case EFFECT_SET_OFFSET:
768 			/*
769 			[9]: Set sample offset
770 			Where [9][x][y] means "play the sample from offset x*4096 + y*256".
771 			The offset is measured in words. If no sample is given, yet one is
772 			still playing on this channel, it should be retriggered to the new
773 			offset using the current volume.
774 			If xy is 00, the previous value is used.
775 			*/
776 
777 			cptr->samppos = ( ( ((muint)effect_param_h) << 12) + ( (((muint)effect_param_l) << 8) ) ) << 10;
778 
779 			if(!cptr->samppos)
780 				cptr->samppos = cptr->last_set_offset;
781 
782 			cptr->last_set_offset = cptr->samppos;
783 		break;
784 
785 		case EFFECT_VOLUME_SLIDE:
786 			/*
787 			[10]: Volume slide
788 			Where [10][x][y] means "either slide the volume up x*(ticks - 1) or
789 			slide the volume down y*(ticks - 1)". If both x and y are non-zero,
790 			then the y value is ignored (assumed to be 0). You cannot slide
791 			outside the volume range 0..64.
792 			*/
793 
794 			cptr->effect = EFFECT_VOLUME_SLIDE;
795 			cptr->volumeslide = effect_param;
796 		break;
797 
798 		case EFFECT_JUMP_POSITION:
799 			/*
800 			[11]: Position Jump
801 			Where [11][x][y] means "stop the pattern after this division, and
802 			continue the song at song-position x*16+y". This shifts the
803 			'pattern-cursor' in the pattern table (see above). Legal values for
804 			x*16+y are from 0 to 127.
805 			*/
806 
807 			mod->tablepos = effect_param;
808 			if(mod->tablepos >= mod->song.length)
809 				mod->tablepos = 0;
810 			mod->patternpos = 0;
811 			mod->jump_loop_effect = 1;
812 
813 		break;
814 
815 		case EFFECT_SET_VOLUME:
816 			/*
817 			[12]: Set volume
818 			Where [12][x][y] means "set current sample's volume to x*16+y".
819 			Legal volumes are 0..64.
820 			*/
821 
822 			cptr->volume = effect_param;
823 
824 			if(cptr->volume > 64)
825 				cptr->volume = 64;
826 
827 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
828 			cptr->volume_table = mod->volume_selection_table[cptr->volume];
829 #endif
830 		break;
831 
832 		case EFFECT_PATTERN_BREAK:
833 			/*
834 			[13]: Pattern Break
835 			Where [13][x][y] means "stop the pattern after this division, and
836 			continue the song at the next pattern at division x*10+y" (the 10
837 			is not a typo). Legal divisions are from 0 to 63 (note Protracker
838 			exception above).
839 			*/
840 
841 			mod->patternpos = ( ((muint)(effect_param_h) * 10) + effect_param_l );
842 
843 			if(mod->patternpos >= 64)
844 				mod->patternpos = 63;
845 
846 			mod->patternpos *= mod->number_of_channels;
847 
848 			if(!mod->jump_loop_effect)
849 			{
850 				mod->tablepos++;
851 				if(mod->tablepos >= mod->song.length)
852 					mod->tablepos = 0;
853 			}
854 
855 			mod->jump_loop_effect = 1;
856 		break;
857 
858 		case EFFECT_EXTENDED:
859 			switch( effect_param_h )
860 			{
861 				case EFFECT_E_FINE_PORTA_UP:
862 					/*
863 					[14][1]: Fineslide up
864 					Where [14][1][x] means "decrement the period of the current sample
865 					by x". The incrementing takes place at the beginning of the
866 					division, and hence there is no actual sliding. You cannot slide
867 					beyond the note B3 (period 113).
868 					*/
869 
870 					cptr->period -= effect_param_l;
871 					if( cptr->period < 113 )
872 						cptr->period = 113;
873 				break;
874 
875 				case EFFECT_E_FINE_PORTA_DOWN:
876 					/*
877 					[14][2]: Fineslide down
878 					Where [14][2][x] means "increment the period of the current sample
879 					by x". Similar to [14][1] but shifts the pitch down. You cannot
880 					slide beyond the note C1 (period 856).
881 					*/
882 
883 					cptr->period += effect_param_l;
884 					if( cptr->period > 856 )
885 						cptr->period = 856;
886 				break;
887 
888 				case EFFECT_E_GLISSANDO_CTRL:
889 					/*
890 					[14][3]: Set glissando on/off
891 					Where [14][3][x] means "set glissando ON if x is 1, OFF if x is 0".
892 					Used in conjunction with [3] ('Slide to note'). If glissando is on,
893 					then 'Slide to note' will slide in semitones, otherwise will
894 					perform the default smooth slide.
895 					*/
896 
897 					cptr->glissando = effect_param_l;
898 				break;
899 
900 				case EFFECT_E_FINE_VOLSLIDE_UP:
901 					/*
902 					[14][10]: Fine volume slide up
903 					Where [14][10][x] means "increment the volume of the current sample
904 					by x". The incrementing takes place at the beginning of the
905 					division, and hence there is no sliding. You cannot slide beyond
906 					volume 64.
907 					*/
908 
909 					cptr->volume += effect_param_l;
910 					if( cptr->volume > 64 )
911 						cptr->volume = 64;
912 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
913 					cptr->volume_table = mod->volume_selection_table[cptr->volume];
914 #endif
915 				break;
916 
917 				case EFFECT_E_FINE_VOLSLIDE_DOWN:
918 					/*
919 					[14][11]: Fine volume slide down
920 					Where [14][11][x] means "decrement the volume of the current sample
921 					by x". Similar to [14][10] but lowers volume. You cannot slide
922 					beyond volume 0.
923 					*/
924 
925 					cptr->volume -= effect_param_l;
926 					if( cptr->volume > 200 )
927 						cptr->volume = 0;
928 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
929 					cptr->volume_table = mod->volume_selection_table[cptr->volume];
930 #endif
931 				break;
932 
933 				case EFFECT_E_SET_FINETUNE:
934 					/*
935 					[14][5]: Set finetune value
936 					Where [14][5][x] means "sets the finetune value of the current
937 					sample to the signed nibble x". x has legal values of 0..15,
938 					corresponding to signed nibbles 0..7,-8..-1 (see start of text for
939 					more info on finetune values).
940 					*/
941 
942 					cptr->finetune = effect_param_l;
943 
944 					if( period )
945 					{
946 						period_table_ptr = periodtable_finetune_ptr[cptr->finetune&0xF];
947 						period = period_table_ptr[getnote(mod,period)];
948 						cptr->period = period;
949 					}
950 
951 				break;
952 
953 				case EFFECT_E_PATTERN_LOOP:
954 					/*
955 					[14][6]: Loop pattern
956 					Where [14][6][x] means "set the start of a loop to this division if
957 					x is 0, otherwise after this division, jump back to the start of a
958 					loop and play it another x times before continuing". If the start
959 					of the loop was not set, it will default to the start of the
960 					current pattern. Hence 'loop pattern' cannot be performed across
961 					multiple patterns. Note that loops do not support nesting, and you
962 					may generate an infinite loop if you try to nest 'loop pattern's.
963 					*/
964 
965 					if( effect_param_l )
966 					{
967 						if( cptr->patternloopcnt )
968 						{
969 							cptr->patternloopcnt--;
970 							if( cptr->patternloopcnt )
971 							{
972 								mod->patternpos = cptr->patternloopstartpoint;
973 								mod->jump_loop_effect = 1;
974 							}
975 							else
976 							{
977 								cptr->patternloopstartpoint = mod->patternpos ;
978 							}
979 						}
980 						else
981 						{
982 							cptr->patternloopcnt = effect_param_l;
983 							mod->patternpos = cptr->patternloopstartpoint;
984 							mod->jump_loop_effect = 1;
985 						}
986 					}
987 					else // Start point
988 					{
989 						cptr->patternloopstartpoint = mod->patternpos;
990 					}
991 
992 				break;
993 
994 				case EFFECT_E_PATTERN_DELAY:
995 					/*
996 					[14][14]: Delay pattern
997 					Where [14][14][x] means "after this division there will be a delay
998 					equivalent to the time taken to play x divisions after which the
999 					pattern will be resumed". The delay only relates to the
1000 					interpreting of new divisions, and all effects and previous notes
1001 					continue during delay.
1002 					*/
1003 
1004 					mod->patterndelay = effect_param_l;
1005 				break;
1006 
1007 				case EFFECT_E_RETRIGGER_NOTE:
1008 					/*
1009 					[14][9]: Retrigger sample
1010 					 Where [14][9][x] means "trigger current sample every x ticks in
1011 					 this division". If x is 0, then no retriggering is done (acts as if
1012 					 no effect was chosen), otherwise the retriggering begins on the
1013 					 first tick and then x ticks after that, etc.
1014 					*/
1015 
1016 					if( effect_param_l )
1017 					{
1018 						cptr->effect = EFFECT_EXTENDED;
1019 						cptr->parameffect = (EFFECT_E_RETRIGGER_NOTE<<4);
1020 						cptr->retrig_param = effect_param_l;
1021 						cptr->retrig_cnt = 0;
1022 					}
1023 				break;
1024 
1025 				case EFFECT_E_NOTE_CUT:
1026 					/*
1027 					[14][12]: Cut sample
1028 					Where [14][12][x] means "after the current sample has been played
1029 					for x ticks in this division, its volume will be set to 0". This
1030 					implies that if x is 0, then you will not hear any of the sample.
1031 					If you wish to insert "silence" in a pattern, it is better to use a
1032 					"silence"-sample (see above) due to the lack of proper support for
1033 					this effect.
1034 					*/
1035 
1036 					cptr->effect = EFFECT_E_NOTE_CUT;
1037 					cptr->cut_param = effect_param_l;
1038 					if( !cptr->cut_param )
1039 					{
1040 						cptr->volume = 0;
1041 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1042 						cptr->volume_table = mod->volume_selection_table[cptr->volume];
1043 #endif
1044 					}
1045 				break;
1046 
1047 				case EFFECT_E_NOTE_DELAY:
1048 					/*
1049 					 Where [14][13][x] means "do not start this division's sample for
1050 					 the first x ticks in this division, play the sample after this".
1051 					 This implies that if x is 0, then you will hear no delay, but
1052 					 actually there will be a VERY small delay. Note that this effect
1053 					 only influences a sample if it was started in this division.
1054 					*/
1055 
1056 					cptr->effect = EFFECT_EXTENDED;
1057 					cptr->parameffect = (EFFECT_E_NOTE_DELAY<<4);
1058 				break;
1059 
1060 				case EFFECT_E_INVERT_LOOP:
1061 					/*
1062 					Where [14][15][x] means "if x is greater than 0, then play the
1063 					current sample's loop upside down at speed x". Each byte in the
1064 					sample's loop will have its sign changed (negated). It will only
1065 					work if the sample's loop (defined previously) is not too big. The
1066 					speed is based on an internal table.
1067 					*/
1068 
1069 					cptr->funkspeed = effect_param_l;
1070 
1071 					doFunk(cptr);
1072 
1073 				break;
1074 
1075 				default:
1076 
1077 				break;
1078 			}
1079 		break;
1080 
1081 		case 0xF:
1082 			/*
1083 			[15]: Set speed
1084 			Where [15][x][y] means "set speed to x*16+y". Though it is nowhere
1085 			near that simple. Let z = x*16+y. Depending on what values z takes,
1086 			different units of speed are set, there being two: ticks/division
1087 			and beats/minute (though this one is only a label and not strictly
1088 			true). If z=0, then what should technically happen is that the
1089 			module stops, but in practice it is treated as if z=1, because
1090 			there is already a method for stopping the module (running out of
1091 			patterns). If z<=32, then it means "set ticks/division to z"
1092 			otherwise it means "set beats/minute to z" (convention says that
1093 			this should read "If z<32.." but there are some composers out there
1094 			that defy conventions). Default values are 6 ticks/division, and
1095 			125 beats/minute (4 divisions = 1 beat). The beats/minute tag is
1096 			only meaningful for 6 ticks/division. To get a more accurate view
1097 			of how things work, use the following formula:
1098 									 24 * beats/minute
1099 				  divisions/minute = -----------------
1100 									  ticks/division
1101 			Hence divisions/minute range from 24.75 to 6120, eg. to get a value
1102 			of 2000 divisions/minute use 3 ticks/division and 250 beats/minute.
1103 			If multiple "set speed" effects are performed in a single division,
1104 			the ones on higher-numbered channels take precedence over the ones
1105 			on lower-numbered channels. This effect has a large number of
1106 			different implementations, but the one described here has the
1107 			widest usage.
1108 			*/
1109 
1110 
1111 			if( effect_param )
1112 			{
1113 
1114 				if( effect_param < 0x20 )
1115 				{
1116 					mod->song.speed = effect_param;
1117 				}
1118 				else
1119 				{   // effect_param >= 0x20
1120 					///	 HZ = 2 * BPM / 5
1121 					mod->bpm = effect_param;
1122 				}
1123 
1124 #ifdef HXCMOD_16BITS_TARGET
1125 				// song.speed = 1 <> 31
1126 				// playrate = 8000 <> 22050
1127 				// bpm = 32 <> 255
1128 
1129 				mod->patternticksem = (muint)( ( (mulong)mod->playrate * 5 ) / ( (muint)mod->bpm * 2 ) );
1130 #else
1131 				// song.speed = 1 <> 31
1132 				// playrate = 8000 <> 96000
1133 				// bpm = 32 <> 255
1134 
1135 				mod->patternticksem = ( ( mod->playrate * 5 ) / ( (mulong)mod->bpm * 2 ) );
1136 #endif
1137 				mod->patternticksaim = mod->song.speed * mod->patternticksem;
1138 			}
1139 
1140 		break;
1141 
1142 		default:
1143 		// Unsupported effect
1144 		break;
1145 
1146 	}
1147 
1148 }
1149 
1150 static void workeffect( modcontext * modctx, note * nptr, channel * cptr )
1151 {
1152 	// BK4BSTACK_CHANGE START
1153 	(void) nptr;
1154 	// BK4BSTACK_CHANGE END
1155 
1156 	doFunk(cptr);
1157 
1158 	switch(cptr->effect)
1159 	{
1160 		case EFFECT_ARPEGGIO:
1161 
1162 			if( cptr->parameffect )
1163 			{
1164 				cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex];
1165 
1166 				cptr->ArpIndex++;
1167 				if( cptr->ArpIndex>2 )
1168 					cptr->ArpIndex = 0;
1169 			}
1170 		break;
1171 
1172 		case EFFECT_PORTAMENTO_UP:
1173 
1174 			if( cptr->period )
1175 			{
1176 				cptr->period -= cptr->parameffect;
1177 
1178 				if( cptr->period < 113 || cptr->period > 20000 )
1179 					cptr->period = 113;
1180 			}
1181 
1182 		break;
1183 
1184 		case EFFECT_PORTAMENTO_DOWN:
1185 
1186 			if( cptr->period )
1187 			{
1188 				cptr->period += cptr->parameffect;
1189 
1190 				if( cptr->period > 20000 )
1191 					cptr->period = 20000;
1192 			}
1193 
1194 		break;
1195 
1196 		case EFFECT_VOLSLIDE_TONEPORTA:
1197 		case EFFECT_TONE_PORTAMENTO:
1198 
1199 			if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod )
1200 			{
1201 				if( cptr->period > cptr->portaperiod )
1202 				{
1203 					if( cptr->period - cptr->portaperiod >= cptr->portaspeed )
1204 					{
1205 						cptr->period -= cptr->portaspeed;
1206 					}
1207 					else
1208 					{
1209 						cptr->period = cptr->portaperiod;
1210 					}
1211 				}
1212 				else
1213 				{
1214 					if( cptr->portaperiod - cptr->period >= cptr->portaspeed )
1215 					{
1216 						cptr->period += cptr->portaspeed;
1217 					}
1218 					else
1219 					{
1220 						cptr->period = cptr->portaperiod;
1221 					}
1222 				}
1223 
1224 				if( cptr->period == cptr->portaperiod )
1225 				{
1226 					// If the slide is over, don't let it to be retriggered.
1227 					cptr->portaperiod = 0;
1228 				}
1229 			}
1230 
1231 			if( cptr->glissando )
1232 			{
1233 				// TODO : Glissando effect.
1234 			}
1235 
1236 			if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA )
1237 			{
1238 				if( cptr->volumeslide & 0xF0 )
1239 				{
1240 					cptr->volume += ( cptr->volumeslide >> 4 );
1241 
1242 					if( cptr->volume > 63 )
1243 						cptr->volume = 63;
1244 				}
1245 				else
1246 				{
1247 					cptr->volume -= ( cptr->volumeslide & 0x0F );
1248 
1249 					if( cptr->volume > 63 )
1250 						cptr->volume = 0;
1251 				}
1252 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1253 				cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1254 #endif
1255 			}
1256 		break;
1257 
1258 		case EFFECT_VOLSLIDE_VIBRATO:
1259 		case EFFECT_VIBRATO:
1260 
1261 			cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7;
1262 
1263 			if( cptr->vibrapointeur > 31 )
1264 				cptr->vibraperiod = -cptr->vibraperiod;
1265 
1266 			cptr->vibrapointeur = ( cptr->vibrapointeur + ( ( cptr->vibraparam>>4 ) & 0x0F) ) & 0x3F;
1267 
1268 			if( cptr->effect == EFFECT_VOLSLIDE_VIBRATO )
1269 			{
1270 				if( cptr->volumeslide & 0xF0 )
1271 				{
1272 					cptr->volume += ( cptr->volumeslide >> 4 );
1273 
1274 					if( cptr->volume > 64 )
1275 						cptr->volume = 64;
1276 				}
1277 				else
1278 				{
1279 					cptr->volume -= cptr->volumeslide;
1280 
1281 					if( cptr->volume > 64 )
1282 						cptr->volume = 0;
1283 				}
1284 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1285 				cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1286 #endif
1287 			}
1288 
1289 		break;
1290 
1291 		case EFFECT_VOLUME_SLIDE:
1292 
1293 			if( cptr->volumeslide & 0xF0 )
1294 			{
1295 				cptr->volume += ( cptr->volumeslide >> 4 );
1296 
1297 				if( cptr->volume > 64 )
1298 					cptr->volume = 64;
1299 			}
1300 			else
1301 			{
1302 				cptr->volume -= cptr->volumeslide;
1303 
1304 				if( cptr->volume > 64 )
1305 					cptr->volume = 0;
1306 			}
1307 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1308 			cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1309 #endif
1310 		break;
1311 
1312 		case EFFECT_EXTENDED:
1313 			switch( cptr->parameffect >> 4 )
1314 			{
1315 
1316 				case EFFECT_E_NOTE_CUT:
1317 					if( cptr->cut_param )
1318 						cptr->cut_param--;
1319 
1320 					if( !cptr->cut_param )
1321 					{
1322 						cptr->volume = 0;
1323 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1324 						cptr->volume_table = modctx->volume_selection_table[cptr->volume];
1325 #endif
1326 					}
1327 				break;
1328 
1329 				case EFFECT_E_RETRIGGER_NOTE:
1330 					cptr->retrig_cnt++;
1331 					if( cptr->retrig_cnt >= cptr->retrig_param )
1332 					{
1333 						cptr->retrig_cnt = 0;
1334 
1335 						cptr->sampdata = cptr->lst_sampdata;
1336 						cptr->length = cptr->lst_length;
1337 						cptr->reppnt = cptr->lst_reppnt;
1338 						cptr->replen = cptr->lst_replen;
1339 						cptr->samppos = 0;
1340 					}
1341 				break;
1342 
1343 				case EFFECT_E_NOTE_DELAY:
1344 					if( cptr->note_delay )
1345 					{
1346 						if( (unsigned char)( cptr->note_delay - 1 ) == modctx->tick_cnt )
1347 						{
1348 							cptr->sampdata = cptr->dly_sampdata;
1349 							cptr->length = cptr->dly_length;
1350 							cptr->reppnt = cptr->dly_reppnt;
1351 							cptr->replen = cptr->dly_replen;
1352 
1353 							cptr->lst_sampdata = cptr->sampdata;
1354 							cptr->lst_length = cptr->length;
1355 							cptr->lst_reppnt = cptr->reppnt;
1356 							cptr->lst_replen = cptr->replen;
1357 							cptr->note_delay = 0;
1358 						}
1359 					}
1360 				break;
1361 				default:
1362 				break;
1363 			}
1364 		break;
1365 
1366 		default:
1367 		break;
1368 
1369 	}
1370 
1371 }
1372 
1373 ///////////////////////////////////////////////////////////////////////////////////
1374 int hxcmod_init(modcontext * modctx)
1375 {
1376 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1377 	muint c;
1378 	mint  i,j;
1379 #endif
1380 	if( modctx )
1381 	{
1382 		memclear(modctx,0,sizeof(modcontext));
1383 		modctx->playrate = 44100;
1384 		modctx->stereo = 1;
1385 		modctx->stereo_separation = 1;
1386 		modctx->bits = 16;
1387 		modctx->filter = 1;
1388 
1389 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1390 		c = 0;
1391 		for(i=0;i<65;i++)
1392 		{
1393 			for(j=-128;j<128;j++)
1394 			{
1395 				modctx->precalc_volume_array[c] = i * j;
1396 				c++;
1397 			}
1398 
1399 			modctx->volume_selection_table[i] = &modctx->precalc_volume_array[(i*256) + 128];
1400 		}
1401 #endif
1402 
1403 		return 1;
1404 	}
1405 
1406 	return 0;
1407 }
1408 
1409 int hxcmod_setcfg(modcontext * modctx, int samplerate, int stereo_separation, int filter)
1410 {
1411 	if( modctx )
1412 	{
1413 		modctx->playrate = samplerate;
1414 
1415 		if(stereo_separation < 4)
1416 		{
1417 			modctx->stereo_separation = stereo_separation;
1418 		}
1419 
1420 		if( filter )
1421 			modctx->filter = 1;
1422 		else
1423 			modctx->filter = 0;
1424 
1425 		return 1;
1426 	}
1427 
1428 	return 0;
1429 }
1430 
1431 int hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size )
1432 {
1433 	muint i, j, max, digitfactor;
1434 	sample *sptr;
1435 	unsigned char * modmemory,* endmodmemory;
1436 
1437 	modmemory = (unsigned char *)mod_data;
1438 	endmodmemory = modmemory + mod_data_size;
1439 
1440 	if( modmemory )
1441 	{
1442 		if( modctx )
1443 		{
1444 #ifdef FULL_STATE
1445 			memclear(&(modctx->effects_event_counts),0,sizeof(modctx->effects_event_counts));
1446 #endif
1447 			memcopy(&(modctx->song),modmemory,1084);
1448 
1449 			i = 0;
1450 			modctx->number_of_channels = 0;
1451 			while(modlist[i].numberofchannels && !modctx->number_of_channels)
1452 			{
1453 				digitfactor = 0;
1454 
1455 				j = 0;
1456 				while( j < 4 )
1457 				{
1458 					if( modlist[i].signature[j] == '$' )
1459 					{
1460 						if(digitfactor)
1461 							digitfactor *= 10;
1462 						else
1463 							digitfactor = 1;
1464 					}
1465 					j++;
1466 				}
1467 
1468 				modctx->number_of_channels = 0;
1469 
1470 				j = 0;
1471 				while( j < 4 )
1472 				{
1473 					if( (modlist[i].signature[j] == modctx->song.signature[j]) || modlist[i].signature[j] == '$' )
1474 					{
1475 						if( modlist[i].signature[j] == '$' )
1476 						{
1477 							if(modctx->song.signature[j] >= '0' && modctx->song.signature[j] <= '9')
1478 							{
1479 								modctx->number_of_channels += (modctx->song.signature[j] - '0') * digitfactor;
1480 								digitfactor /= 10;
1481 							}
1482 							else
1483 							{
1484 								modctx->number_of_channels = 0;
1485 								break;
1486 							}
1487 						}
1488 						j++;
1489 					}
1490 					else
1491 					{
1492 						modctx->number_of_channels = 0;
1493 						break;
1494 					}
1495 				}
1496 
1497 				if( j == 4 )
1498 				{
1499 					if(!modctx->number_of_channels)
1500 						modctx->number_of_channels = modlist[i].numberofchannels;
1501 				}
1502 
1503 				i++;
1504 			}
1505 
1506 			if( !modctx->number_of_channels )
1507 			{
1508 				// 15 Samples modules support
1509 				// Shift the whole datas to make it look likes a standard 4 channels mod.
1510 				memcopy(&(modctx->song.signature), "M.K.", 4);
1511 				memcopy(&(modctx->song.length), &(modctx->song.samples[15]), 130);
1512 				memclear(&(modctx->song.samples[15]), 0, 480);
1513 				modmemory += 600;
1514 				modctx->number_of_channels = 4;
1515 			}
1516 			else
1517 			{
1518 				modmemory += 1084;
1519 			}
1520 
1521 			if( modctx->number_of_channels > NUMMAXCHANNELS )
1522 				return 0; // Too much channels ! - Increase/define HXCMOD_MAXCHANNELS !
1523 
1524 			if( modmemory >= endmodmemory )
1525 				return 0; // End passed ? - Probably a bad file !
1526 
1527 			// Patterns loading
1528 			for (i = max = 0; i < 128; i++)
1529 			{
1530 				while (max <= modctx->song.patterntable[i])
1531 				{
1532 					modctx->patterndata[max] = (note*)modmemory;
1533 					modmemory += (256*modctx->number_of_channels);
1534 					max++;
1535 
1536 					if( modmemory >= endmodmemory )
1537 						return 0; // End passed ? - Probably a bad file !
1538 				}
1539 			}
1540 
1541 			for (i = 0; i < 31; i++)
1542 				modctx->sampledata[i]=0;
1543 
1544 			// Samples loading
1545 			for (i = 0, sptr = modctx->song.samples; i <31; i++, sptr++)
1546 			{
1547 				if (sptr->length == 0) continue;
1548 
1549 				modctx->sampledata[i] = (mchar*)modmemory;
1550 				modmemory += (GET_BGI_W(sptr->length)*2);
1551 
1552 				if (GET_BGI_W(sptr->replen) + GET_BGI_W(sptr->reppnt) > GET_BGI_W(sptr->length))
1553 					sptr->replen = GET_BGI_W((GET_BGI_W(sptr->length) - GET_BGI_W(sptr->reppnt)));
1554 
1555 				if( modmemory > endmodmemory )
1556 					return 0; // End passed ? - Probably a bad file !
1557 			}
1558 
1559 			// States init
1560 
1561 			modctx->tablepos = 0;
1562 			modctx->patternpos = 0;
1563 			modctx->song.speed = 6;
1564 			modctx->bpm = 125;
1565 
1566 #ifdef HXCMOD_16BITS_TARGET
1567 			// song.speed = 1 <> 31
1568 			// playrate = 8000 <> 22050
1569 			// bpm = 32 <> 255
1570 
1571 			modctx->patternticksem = (muint)( ( (mulong)modctx->playrate * 5 ) / ( (muint)modctx->bpm * 2 ) );
1572 #else
1573 			// song.speed = 1 <> 31
1574 			// playrate = 8000 <> 96000
1575 			// bpm = 32 <> 255
1576 
1577 			modctx->patternticksem = ( ( modctx->playrate * 5 ) / ( (mulong)modctx->bpm * 2 ) );
1578 #endif
1579 			modctx->patternticksaim = modctx->song.speed * modctx->patternticksem;
1580 
1581 			modctx->patternticks = modctx->patternticksaim + 1;
1582 
1583 			modctx->sampleticksconst = ((3546894UL * 16) / modctx->playrate) << 6; //8448*428/playrate;
1584 
1585 			for(i=0; i < modctx->number_of_channels; i++)
1586 			{
1587 				modctx->channels[i].volume = 0;
1588 				modctx->channels[i].period = 0;
1589 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1590 				modctx->channels[i].volume_table = modctx->volume_selection_table[0];
1591 #endif
1592 			}
1593 
1594 			modctx->mod_loaded = 1;
1595 
1596 			return 1;
1597 		}
1598 	}
1599 
1600 	return 0;
1601 }
1602 
1603 void hxcmod_fillbuffer(modcontext * modctx, msample * outbuffer, mssize nbsample, tracker_buffer_state * trkbuf)
1604 {
1605 	mssize i;
1606 	muint  j;
1607 	muint c;
1608 
1609 	unsigned long k;
1610 	unsigned int state_remaining_steps;
1611 
1612 #ifdef HXCMOD_OUTPUT_FILTER
1613 #ifndef HXCMOD_MONO_OUTPUT
1614 	int ll,tl;
1615 #endif
1616 	int lr,tr;
1617 #endif
1618 
1619 #ifndef HXCMOD_MONO_OUTPUT
1620 	int l;
1621 #endif
1622 	int r;
1623 
1624 	short finalperiod;
1625 	note	*nptr;
1626 	channel *cptr;
1627 
1628 	if( modctx && outbuffer )
1629 	{
1630 		if(modctx->mod_loaded)
1631 		{
1632 			state_remaining_steps = 0;
1633 
1634 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1635 			if( trkbuf )
1636 			{
1637 				trkbuf->cur_rd_index = 0;
1638 
1639 				memcopy(trkbuf->name,modctx->song.title,sizeof(modctx->song.title));
1640 
1641 				for(i=0;i<31;i++)
1642 				{
1643 					memcopy(trkbuf->instruments[i].name,modctx->song.samples[i].name,sizeof(trkbuf->instruments[i].name));
1644 				}
1645 			}
1646 #endif
1647 
1648 #ifdef HXCMOD_OUTPUT_FILTER
1649 	#ifndef HXCMOD_MONO_OUTPUT
1650 			ll = modctx->last_l_sample;
1651 	#endif
1652 			lr = modctx->last_r_sample;
1653 #endif
1654 
1655 			for (i = 0; i < nbsample; i++)
1656 			{
1657 				//---------------------------------------
1658 				if( modctx->patternticks++ > modctx->patternticksaim )
1659 				{
1660 					if( !modctx->patterndelay )
1661 					{
1662 						nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1663 						nptr = nptr + modctx->patternpos;
1664 						cptr = modctx->channels;
1665 
1666 						modctx->tick_cnt = 0;
1667 
1668 						modctx->patternticks = 0;
1669 						modctx->patterntickse = 0;
1670 
1671 						for(c=0;c<modctx->number_of_channels;c++)
1672 						{
1673 							worknote((note*)(nptr), (channel*)(cptr),(char)(c+1),modctx);
1674 
1675 							if (cptr->period != 0)
1676 							{
1677 								finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod;
1678 								if (finalperiod)
1679 								{
1680 									cptr->sampinc = ((modctx->sampleticksconst) / finalperiod);
1681 								}
1682 								else
1683 								{
1684 									cptr->sampinc = 0;
1685 								}
1686 							}
1687 							else
1688 								cptr->sampinc = 0;
1689 
1690 							nptr++;
1691 							cptr++;
1692 						}
1693 
1694 						if( !modctx->jump_loop_effect )
1695 							modctx->patternpos += modctx->number_of_channels;
1696 						else
1697 							modctx->jump_loop_effect = 0;
1698 
1699 						if( modctx->patternpos == 64*modctx->number_of_channels )
1700 						{
1701 							modctx->tablepos++;
1702 							modctx->patternpos = 0;
1703 							if(modctx->tablepos >= modctx->song.length)
1704 								modctx->tablepos = 0;
1705 						}
1706 					}
1707 					else
1708 					{
1709 						modctx->patterndelay--;
1710 						modctx->patternticks = 0;
1711 						modctx->patterntickse = 0;
1712 						modctx->tick_cnt = 0;
1713 					}
1714 
1715 				}
1716 
1717 				if (modctx->patterntickse++ > modctx->patternticksem)
1718 				{
1719 					nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1720 					nptr = nptr + modctx->patternpos;
1721 					cptr = modctx->channels;
1722 
1723 					for(c=0;c<modctx->number_of_channels;c++)
1724 					{
1725 						workeffect( modctx, nptr, cptr );
1726 
1727 						if (cptr->period != 0)
1728 						{
1729 							finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod;
1730 							if (finalperiod)
1731 							{
1732 								cptr->sampinc = ((modctx->sampleticksconst) / finalperiod);
1733 							}
1734 							else
1735 							{
1736 								cptr->sampinc = 0;
1737 							}
1738 						}
1739 						else
1740 							cptr->sampinc = 0;
1741 
1742 						nptr++;
1743 						cptr++;
1744 					}
1745 
1746 					modctx->tick_cnt++;
1747 					modctx->patterntickse = 0;
1748 				}
1749 
1750 				//---------------------------------------
1751 
1752 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1753 				if( trkbuf && !state_remaining_steps )
1754 				{
1755 					if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1756 					{
1757 						memclear(&trkbuf->track_state_buf[trkbuf->nb_of_state],0,sizeof(tracker_state));
1758 					}
1759 				}
1760 #endif
1761 
1762 #ifndef HXCMOD_MONO_OUTPUT
1763 				l=0;
1764 #endif
1765 				r=0;
1766 
1767 				for( j = 0, cptr = modctx->channels; j < modctx->number_of_channels ; j++, cptr++)
1768 				{
1769 					if( cptr->period != 0 )
1770 					{
1771 						cptr->samppos += cptr->sampinc;
1772 
1773 						if( cptr->replen < 2 )
1774 						{
1775 							if( ( cptr->samppos >> 11) >= cptr->length )
1776 							{
1777 								cptr->length = 0;
1778 								cptr->reppnt = 0;
1779 
1780 								if(cptr->update_nxt_repeat)
1781 								{
1782 									cptr->replen = cptr->nxt_replen;
1783 									cptr->reppnt = cptr->nxt_reppnt;
1784 									cptr->sampdata = cptr->nxt_sampdata;
1785 									cptr->length = cptr->nxt_length;
1786 
1787 									cptr->lst_sampdata = cptr->sampdata;
1788 									cptr->lst_length = cptr->length;
1789 									cptr->lst_reppnt = cptr->reppnt;
1790 									cptr->lst_replen = cptr->replen;
1791 
1792 									cptr->update_nxt_repeat = 0;
1793 								}
1794 
1795 								if( cptr->length )
1796 									cptr->samppos = cptr->samppos % (((unsigned long)cptr->length)<<11);
1797 								else
1798 									cptr->samppos = 0;
1799 							}
1800 						}
1801 						else
1802 						{
1803 							if( ( cptr->samppos >> 11 ) >= (unsigned long)(cptr->replen+cptr->reppnt) )
1804 							{
1805 								if( cptr->update_nxt_repeat )
1806 								{
1807 									cptr->replen = cptr->nxt_replen;
1808 									cptr->reppnt = cptr->nxt_reppnt;
1809 									cptr->sampdata = cptr->nxt_sampdata;
1810 									cptr->length = cptr->nxt_length;
1811 
1812 									cptr->lst_sampdata = cptr->sampdata;
1813 									cptr->lst_length = cptr->length;
1814 									cptr->lst_reppnt = cptr->reppnt;
1815 									cptr->lst_replen = cptr->replen;
1816 
1817 									cptr->update_nxt_repeat = 0;
1818 								}
1819 
1820 								if( cptr->sampdata )
1821 								{
1822 									cptr->samppos = ((unsigned long)(cptr->reppnt)<<11) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<11));
1823 								}
1824 							}
1825 						}
1826 
1827 						k = cptr->samppos >> 10;
1828 
1829 #ifdef HXCMOD_MONO_OUTPUT
1830 						if( cptr->sampdata!=0 )
1831 						{
1832 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1833 							r += cptr->volume_table[cptr->sampdata[k]];
1834 #else
1835 							r += ( cptr->sampdata[k] *  cptr->volume );
1836 #endif
1837 						}
1838 #else
1839 						if (cptr->sampdata != 0)
1840 						{
1841 							if ( !(j & 3) || ((j & 3) == 3) )
1842 							{
1843 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1844 								l += cptr->volume_table[cptr->sampdata[k]];
1845 #else
1846 								l += (cptr->sampdata[k] * cptr->volume);
1847 #endif
1848 							}
1849 							else
1850 							{
1851 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
1852 								r += cptr->volume_table[cptr->sampdata[k]];
1853 #else
1854 								r += (cptr->sampdata[k] * cptr->volume);
1855 #endif
1856 							}
1857 						}
1858 #endif
1859 
1860 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1861 						if( trkbuf && !state_remaining_steps )
1862 						{
1863 							if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1864 							{
1865 								trkbuf->track_state_buf[trkbuf->nb_of_state].number_of_tracks = modctx->number_of_channels;
1866 								trkbuf->track_state_buf[trkbuf->nb_of_state].buf_index = i;
1867 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern = modctx->song.patterntable[modctx->tablepos];
1868 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_pos = modctx->patternpos / modctx->number_of_channels;
1869 								trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_table_pos = modctx->tablepos;
1870 								trkbuf->track_state_buf[trkbuf->nb_of_state].bpm = modctx->bpm;
1871 								trkbuf->track_state_buf[trkbuf->nb_of_state].speed = modctx->song.speed;
1872 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_effect = cptr->effect_code;
1873 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_parameffect = cptr->parameffect;
1874 								if(cptr->sampinc)
1875 									trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = (muint)(modctx->sampleticksconst / cptr->sampinc);
1876 								else
1877 									trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = 0;
1878 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_volume = cptr->volume;
1879 								trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].instrument_number = (unsigned char)cptr->sampnum;
1880 							}
1881 						}
1882 #endif
1883 					}
1884 				}
1885 
1886 #ifdef HXCMOD_STATE_REPORT_SUPPORT
1887 				if( trkbuf && !state_remaining_steps )
1888 				{
1889 					state_remaining_steps = trkbuf->sample_step;
1890 
1891 					if(trkbuf->nb_of_state < trkbuf->nb_max_of_state)
1892 						trkbuf->nb_of_state++;
1893 				}
1894 				else
1895 #endif
1896 				{
1897 					state_remaining_steps--;
1898 				}
1899 
1900 #ifdef HXCMOD_MONO_OUTPUT
1901 
1902 	#ifdef HXCMOD_OUTPUT_FILTER
1903 				tr = (short)r;
1904 
1905 				if ( modctx->filter )
1906 				{
1907 					// Filter
1908 					r = (r+lr)>>1;
1909 				}
1910 	#endif
1911 
1912 	#ifdef HXCMOD_CLIPPING_CHECK
1913 				// Level limitation
1914 				if( r > 32767 ) r = 32767;
1915 				if( r < -32768 ) r = -32768;
1916 	#endif
1917 				// Store the final sample.
1918 	#ifdef HXCMOD_8BITS_OUTPUT
1919 
1920 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1921 				*outbuffer++ = (r >> 8) + 127;
1922 		#else
1923 				*outbuffer++ = r >> 8;
1924 		#endif
1925 
1926 	#else
1927 
1928 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1929 				*outbuffer++ = r + 32767;
1930 		#else
1931 				*outbuffer++ = r;
1932 		#endif
1933 
1934 	#endif
1935 
1936 	#ifdef HXCMOD_OUTPUT_FILTER
1937 				lr = tr;
1938 	#endif
1939 
1940 #else
1941 
1942 	#ifdef HXCMOD_OUTPUT_FILTER
1943 				tl = (short)l;
1944 				tr = (short)r;
1945 
1946 				if ( modctx->filter )
1947 				{
1948 					// Filter
1949 					l = (l+ll)>>1;
1950 					r = (r+lr)>>1;
1951 				}
1952 	#endif
1953 
1954 	#ifdef HXCMOD_OUTPUT_STEREO_MIX
1955 				if ( modctx->stereo_separation == 1 )
1956 				{
1957 					// Left & Right Stereo panning
1958 					l = (l+(r>>1));
1959 					r = (r+(l>>1));
1960 				}
1961 	#endif
1962 
1963 	#ifdef HXCMOD_CLIPPING_CHECK
1964 				// Level limitation
1965 				if( l > 32767 ) l = 32767;
1966 				if( l < -32768 ) l = -32768;
1967 				if( r > 32767 ) r = 32767;
1968 				if( r < -32768 ) r = -32768;
1969 	#endif
1970 				// Store the final sample.
1971 
1972 
1973 	#ifdef HXCMOD_8BITS_OUTPUT
1974 
1975 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1976 				*outbuffer++ = ( l >> 8 ) + 127;
1977 				*outbuffer++ = ( r >> 8 ) + 127;
1978 		#else
1979 				*outbuffer++ = l >> 8;
1980 				*outbuffer++ = r >> 8;
1981 		#endif
1982 
1983 	#else
1984 
1985 		#ifdef HXCMOD_UNSIGNED_OUTPUT
1986 				*outbuffer++ = l + 32767;
1987 				*outbuffer++ = r + 32767;
1988 		#else
1989 				*outbuffer++ = l;
1990 				*outbuffer++ = r;
1991 		#endif
1992 
1993 	#endif
1994 
1995 	#ifdef HXCMOD_OUTPUT_FILTER
1996 				ll = tl;
1997 				lr = tr;
1998 	#endif
1999 
2000 #endif // HXCMOD_MONO_OUTPUT
2001 
2002 			}
2003 
2004 #ifdef HXCMOD_OUTPUT_FILTER
2005 	#ifndef HXCMOD_MONO_OUTPUT
2006 			modctx->last_l_sample = ll;
2007 	#endif
2008 			modctx->last_r_sample = lr;
2009 #endif
2010 		}
2011 		else
2012 		{
2013 			for (i = 0; i < nbsample; i++)
2014 			{
2015 				// Mod not loaded. Return blank buffer.
2016 #ifdef HXCMOD_MONO_OUTPUT
2017 				outbuffer[i] = 0;
2018 #else
2019 				*outbuffer++ = 0;
2020 				*outbuffer++ = 0;
2021 #endif
2022 			}
2023 
2024 #ifdef HXCMOD_STATE_REPORT_SUPPORT
2025 			if(trkbuf)
2026 			{
2027 				trkbuf->nb_of_state = 0;
2028 				trkbuf->cur_rd_index = 0;
2029 				trkbuf->name[0] = 0;
2030 				memclear(trkbuf->track_state_buf,0,sizeof(tracker_state) * trkbuf->nb_max_of_state);
2031 				memclear(trkbuf->instruments,0,sizeof(trkbuf->instruments));
2032 			}
2033 #endif
2034 		}
2035 	}
2036 }
2037 
2038 void hxcmod_unload( modcontext * modctx )
2039 {
2040 	if(modctx)
2041 	{
2042 		memclear(&modctx->song,0,sizeof(modctx->song));
2043 		memclear(&modctx->sampledata,0,sizeof(modctx->sampledata));
2044 		memclear(&modctx->patterndata,0,sizeof(modctx->patterndata));
2045 		modctx->tablepos = 0;
2046 		modctx->patternpos = 0;
2047 		modctx->patterndelay  = 0;
2048 		modctx->jump_loop_effect = 0;
2049 		modctx->bpm = 0;
2050 		modctx->patternticks = 0;
2051 		modctx->patterntickse = 0;
2052 		modctx->patternticksaim = 0;
2053 		modctx->sampleticksconst = 0;
2054 
2055 		memclear(modctx->channels,0,sizeof(modctx->channels));
2056 
2057 		modctx->number_of_channels = 0;
2058 
2059 		modctx->mod_loaded = 0;
2060 
2061 		modctx->last_r_sample = 0;
2062 		modctx->last_l_sample = 0;
2063 	}
2064 }
2065