1 /*************************************************************************/
2 /* cp_loader_mod.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30 #include "cp_loader_mod.h"
31
tag_equal_to(const char * p_tag,const char * p_string)32 static bool tag_equal_to(const char *p_tag, const char *p_string) {
33
34 return (p_tag[0] == p_string[0] &&
35 p_tag[1] == p_string[1] &&
36 p_tag[2] == p_string[2] &&
37 p_tag[3] == p_string[3]);
38 }
39 /* ProTracker period table */
40 uint16_t period_table[6 * 12] = {
41 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 907,
42 856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
43 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
44 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
45 107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
46 53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28
47 };
48
load_song(const char * p_file,CPSong * p_song,bool p_sampleset)49 CPLoader::Error CPLoader_MOD::load_song(const char *p_file, CPSong *p_song, bool p_sampleset) {
50
51 if (file->open(p_file, CPFileAccessWrapper::READ)) {
52 //printf("Can't open file! %s\n",p_file);
53 return FILE_CANNOT_OPEN;
54 };
55
56 /* FIRST OF ALL, one needs to read the .mod file format tag */
57 file->seek(1080); //located at 1080
58
59 char format_tag[4];
60
61 file->get_byte_array((uint8_t *)format_tag, 4);
62
63 int channels = -1;
64
65 /** THE PAIN!! - COMPARE TAGS */
66
67 /* Classic 4-chan */
68 if (tag_equal_to(format_tag, "M.K."))
69 channels = 4;
70 if (tag_equal_to(format_tag, "FLT4"))
71 channels = 4;
72 if (tag_equal_to(format_tag, "M!K!"))
73 channels = 4;
74
75 /* 8 Channel MODS */
76
77 if (tag_equal_to(format_tag, "FLT8"))
78 channels = 2;
79
80 if (tag_equal_to(format_tag, "CD81"))
81 channels = 2;
82
83 /* Custom channel MODS */
84
85 for (int i = 1; i <= 32; i++) {
86
87 if (i < 10) { // up to 9 channels mods
88
89 /* Old Take Tracker */
90 char old_take_tracker[4] = { 'T', 'D', 'Z', char('0' + i) };
91
92 if (tag_equal_to(format_tag, old_take_tracker)) {
93
94 channels = i;
95 break;
96 }
97
98 /* Contemplates many XCHN Formats */
99 char xchn[4] = { char('0' + i), 'C', 'H', 'N' };
100
101 if (tag_equal_to(format_tag, xchn)) {
102
103 channels = i;
104 break;
105 }
106 }
107
108 /* Fast Tracker */
109 char fast_tracker[4] = { char('0' + (i / 10)), char('0' + (i % 10)), 'C', 'H' };
110
111 if (tag_equal_to(format_tag, fast_tracker)) {
112
113 channels = i;
114 break;
115 }
116 }
117
118 if (channels == -1) {
119
120 file->close();
121 return FILE_UNRECOGNIZED;
122 }
123
124 /** Load CPSong INFO */
125
126 file->seek(0); //go to begining of file
127
128 file->set_endian_conversion(true);
129 p_song->reset();
130 p_song->set_instruments(false);
131
132 char name[21];
133
134 file->get_byte_array((uint8_t *)name, 20);
135 name[20] = 0;
136
137 p_song->set_name(name);
138 p_song->set_old_effects(true);
139 p_song->set_linear_slides(false);
140 p_song->set_compatible_gxx(true);
141
142 CPSampleManager *sm = CPSampleManager::get_singleton();
143
144 int instruments = 31;
145
146 for (int i = 0; i < instruments; i++) {
147
148 char sample_name[23];
149 file->get_byte_array((uint8_t *)sample_name, 22);
150 sample_name[22] = 0;
151
152 uint32_t sample_len = file->get_word();
153 sample_len <<= 1;
154
155 uint8_t fine_nibble = file->get_byte() & 0xF;
156
157 //(int8_t)(fine_nibble & 7) - (int8_t)(fine_nibble & 8); //yesso's genius trick
158 // boo, I can't use it :( but i leave it here because of how cool it is
159 uint8_t linear_volume = file->get_byte(); //0 .. ?
160
161 uint32_t loop_begin = file->get_word(); //0 .. ?
162 loop_begin <<= 1;
163 uint32_t loop_end = file->get_word(); //0 .. ?
164 loop_end <<= 1;
165
166 if (sample_len > 0) {
167
168 CPSample_ID sid = sm->create(false, false, sample_len);
169
170 if (sid.is_null()) {
171
172 file->close();
173 return FILE_OUT_OF_MEMORY;
174 }
175
176 if (loop_end > 2) {
177 sm->set_loop_begin(sid, loop_begin);
178 sm->set_loop_end(sid, loop_end + loop_begin);
179 sm->set_loop_type(sid, CP_LOOP_FORWARD);
180 }
181 static const uint16_t fine_to_freq[16] = {
182 8363, 8413, 8463, 8529, 8581, 8651, 8723, 8757,
183 7895, 7941, 7985, 8046, 8107, 8169, 8232, 8280
184 };
185
186 sm->set_c5_freq(sid, fine_to_freq[fine_nibble]);
187 p_song->get_sample(i)->set_sample_data(sid);
188 }
189
190 p_song->get_sample(i)->set_name(sample_name);
191 p_song->get_sample(i)->set_default_volume(linear_volume);
192 }
193
194 /* pan for MODs */
195 for (int i = 0; i < channels; i++)
196 p_song->set_channel_pan(i, (((i & 3) == 1) || ((i & 3) == 2)) ? 0 : 64);
197
198 uint8_t order_count = file->get_byte();
199 // uint8_t loop_to=file->get_byte();
200
201 int pattern_count = 0;
202
203 for (int i = 0; i < 128; i++) {
204
205 uint8_t order = file->get_byte();
206
207 if (i < order_count) {
208 p_song->set_order(i, order);
209
210 /* Determine the amount of patterns */
211 if ((order + 1) > pattern_count)
212 pattern_count = order + 1;
213 } else
214 p_song->set_order(i, CP_ORDER_NONE);
215 }
216
217 if (instruments == 31)
218 file->get_dword(); // identiefier, now skip it
219
220 for (int i = 0; i < pattern_count; i++) {
221
222 for (int line = 0; line < 64; line++) {
223
224 for (int column = 0; column < channels; column++) {
225
226 uint32_t note_w = file->get_dword();
227
228 CPNote note;
229
230 note.instrument = (note_w >> 12) & 0xF;
231 note.instrument |= (note_w >> 24) & 0xF0;
232
233 if (note.instrument == 0)
234 note.instrument = CPNote::EMPTY;
235 else
236 note.instrument--;
237
238 note.parameter = note_w & 0xFF;
239
240 int cmd = (note_w >> 8) & 0xF;
241
242 uint32_t period = (note_w >> 16) & 0xFFF;
243
244 if (period > 0 && period < 0xFFF) {
245
246 //period>>=2;
247 //period<<=1;
248 for (int n = 0; n < 6 * 12; n++) {
249
250 if (period >= period_table[n]) {
251
252 if ((period != period_table[n]) && (n)) {
253 uint32_t p1 = period_table[n - 1];
254 uint32_t p2 = period_table[n];
255 if (p1 - period < (period - p2)) {
256
257 note.note = n + 36;
258 break;
259 }
260 }
261 note.note = n + 1 + 36;
262 break;
263 }
264 }
265 if (note.note == CPNote::EMPTY)
266 note.note = 6 * 12 + 36;
267
268 note.note--;
269 }
270
271 switch (cmd) {
272
273 case 0x0: {
274
275 if (note.parameter > 0)
276 note.command = 'J' - 'A';
277 } break;
278 case 0x1: {
279 note.command = 'F' - 'A';
280 } break;
281 case 0x2: {
282
283 note.command = 'E' - 'A';
284 } break;
285 case 0x3: {
286
287 note.command = 'G' - 'A';
288 } break;
289 case 0x4: {
290
291 note.command = 'H' - 'A';
292 } break;
293 case 0x5: {
294 note.command = 'L' - 'A';
295 } break;
296 case 0x6: {
297
298 note.command = 'K' - 'A';
299 } break;
300 case 0x7: {
301 note.command = 'R' - 'A';
302 } break;
303 case 0x8: {
304
305 note.command = 'X' - 'A';
306 } break;
307 case 0x9: {
308
309 note.command = 'O' - 'A';
310
311 } break;
312 case 0xA: {
313
314 note.command = 'D' - 'A';
315
316 } break;
317 case 0xB: {
318
319 note.command = 'B' - 'A';
320
321 } break;
322 case 0xC: {
323
324 note.volume = note.parameter;
325 if (note.volume > 64)
326 note.volume = 64;
327 note.parameter = 0;
328
329 } break;
330 case 0xD: {
331
332 note.command = 'C' - 'A';
333 note.parameter = (note.parameter >> 4) * 10 + (note.parameter & 0xF);
334
335 } break;
336 case 0xE: { //SPECIAL EFFECT!
337
338 note.command = 'S' - 'A';
339
340 switch (note.parameter >> 4) {
341
342 case 0x1: {
343
344 note.command = 'F' - 'A';
345 note.parameter = 0xF0 | (note.parameter & 0xF);
346 } break;
347 case 0x2: {
348
349 note.command = 'E' - 'A';
350 note.parameter = 0xF0 | (note.parameter & 0xF);
351 } break;
352 case 0x4: {
353
354 note.command = 'S' - 'A';
355 note.parameter = 0x30 | (note.parameter & 0x3);
356
357 } break;
358 case 0x6: {
359
360 note.command = 'S' - 'A';
361 note.parameter = 0xB0 | (note.parameter & 0xF);
362
363 } break;
364 case 0x7: {
365 note.command = 'S' - 'A';
366 note.parameter = 0x40 | (note.parameter & 0x3);
367
368 } break;
369 case 0x8: {
370
371 note.command = 'S' - 'A'; // wow, it's the same!
372
373 } break;
374 case 0x9: {
375 note.command = 'Q' - 'A';
376 note.parameter = (note.parameter & 0xF);
377
378 } break;
379 case 0xA: {
380
381 note.command = 'D' - 'A';
382 note.parameter = 0xF | ((note.parameter & 0xF) << 4);
383
384 } break;
385 case 0xB: {
386 note.command = 'D' - 'A';
387 note.parameter = 0xF0 | (note.parameter & 0xF);
388
389 } break;
390 case 0xC:
391 case 0xD: {
392
393 note.command = 'S' - 'A'; //wow, they are the same!
394
395 } break;
396 case 0xE: {
397 note.command = 'S' - 'A';
398 note.parameter = 0x60 | (note.parameter & 0xF);
399
400 } break;
401
402 default: {
403
404 note.command = CPNote::EMPTY;
405 note.parameter = 0;
406 } break;
407 }
408 } break;
409 case 0xF: {
410
411 if (note.parameter < 32)
412 note.command = 'A' - 'A';
413 else
414 note.command = 'T' - 'A';
415
416 } break;
417 }
418
419 p_song->get_pattern(i)->set_note(column, line, note);
420 }
421 }
422 }
423
424 for (int i = 0; i < instruments; i++) {
425
426 CPSample_ID sid = p_song->get_sample(i)->get_sample_data();
427 if (sid.is_null()) {
428 continue; //empty sample, not stored?
429 }
430 sm->lock_data(sid);
431 uint8_t *dataptr = (uint8_t *)sm->get_data(sid);
432
433 int len = sm->get_size(sid);
434 for (int s = 0; s < len; s++) {
435
436 uint8_t d = file->get_byte();
437 //d-=128; //convert to signed
438 int8_t *ds = (int8_t *)&d;
439 dataptr[s] = *ds;
440 }
441 sm->unlock_data(sid);
442 }
443
444 file->close();
445
446 return FILE_OK;
447 }
448
CPLoader_MOD(CPFileAccessWrapper * p_file)449 CPLoader_MOD::CPLoader_MOD(CPFileAccessWrapper *p_file) {
450
451 file = p_file;
452 }
453
~CPLoader_MOD()454 CPLoader_MOD::~CPLoader_MOD() {
455 }
456