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