1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /*
24 * This code is based on IBXM mod player
25 *
26 * Copyright (c) 2015, Martin Cameron
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or
30 * without modification, are permitted provided that the
31 * following conditions are met:
32 *
33 * * Redistributions of source code must retain the above
34 * copyright notice, this list of conditions and the
35 * following disclaimer.
36 *
37 * * Redistributions in binary form must reproduce the
38 * above copyright notice, this list of conditions and the
39 * following disclaimer in the documentation and/or other
40 * materials provided with the distribution.
41 *
42 * * Neither the name of the organization nor the names of
43 * its contributors may be used to endorse or promote
44 * products derived from this software without specific
45 * prior written permission.
46
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
48 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
55 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
56 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
57 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 *
62 */
63
64 #include "common/debug.h"
65 #include "common/endian.h"
66 #include "common/stream.h"
67 #include "common/textconsole.h"
68 #include "common/util.h"
69 #include "module_mod_xm_s3m.h"
70
71 namespace Modules {
72
73 const int ModuleModXmS3m::FP_SHIFT = 0xF;
74 const int ModuleModXmS3m::FP_ONE = 0x8000;
75 const int ModuleModXmS3m::FP_MASK = 0x7FFF;
76
77 const int ModuleModXmS3m::exp2table[] = {
78 32768, 32946, 33125, 33305, 33486, 33667, 33850, 34034,
79 34219, 34405, 34591, 34779, 34968, 35158, 35349, 35541,
80 35734, 35928, 36123, 36319, 36516, 36715, 36914, 37114,
81 37316, 37518, 37722, 37927, 38133, 38340, 38548, 38757,
82 38968, 39180, 39392, 39606, 39821, 40037, 40255, 40473,
83 40693, 40914, 41136, 41360, 41584, 41810, 42037, 42265,
84 42495, 42726, 42958, 43191, 43425, 43661, 43898, 44137,
85 44376, 44617, 44859, 45103, 45348, 45594, 45842, 46091,
86 46341, 46593, 46846, 47100, 47356, 47613, 47871, 48131,
87 48393, 48655, 48920, 49185, 49452, 49721, 49991, 50262,
88 50535, 50810, 51085, 51363, 51642, 51922, 52204, 52488,
89 52773, 53059, 53347, 53637, 53928, 54221, 54515, 54811,
90 55109, 55408, 55709, 56012, 56316, 56622, 56929, 57238,
91 57549, 57861, 58176, 58491, 58809, 59128, 59449, 59772,
92 60097, 60423, 60751, 61081, 61413, 61746, 62081, 62419,
93 62757, 63098, 63441, 63785, 64132, 64480, 64830, 65182,
94 65536
95 };
96
moduleExp2(int x)97 int ModuleModXmS3m::moduleExp2(int x) {
98 int c, m, y;
99 int x0 = (x & FP_MASK) >> (FP_SHIFT - 7);
100 c = exp2table[x0];
101 m = exp2table[x0 + 1] - c;
102 y = (m * (x & (FP_MASK >> 7)) >> 8) + c;
103 return (y << FP_SHIFT) >> (FP_SHIFT - (x >> FP_SHIFT));
104 }
105
moduleLog2(int x)106 int ModuleModXmS3m::moduleLog2(int x) {
107 int y = 16 << FP_SHIFT;
108 for (int step = y; step > 0; step >>= 1) {
109 if (moduleExp2(y - step) >= x) {
110 y -= step;
111 }
112 }
113 return y;
114 }
115
load(Common::SeekableReadStream & st)116 bool ModuleModXmS3m::load(Common::SeekableReadStream &st) {
117 int32 setPos = st.pos();
118
119 // xm file
120 char sigXm[18] = { 0 };
121 st.read(sigXm, 17);
122 if (!memcmp(sigXm, "Extended Module: ", 17)) {
123 return loadXm(st);
124 }
125 st.seek(setPos);
126
127 // s3m file
128 char sigS3m[4];
129 st.skip(44);
130 st.read(sigS3m, 4);
131 if (!memcmp(sigS3m, "SCRM", 4)) {
132 st.seek(setPos);
133 return loadS3m(st);
134 }
135 st.seek(setPos);
136
137 // amf file
138 char sigAmf[25] = {};
139 st.read(sigAmf, 24);
140 if (!memcmp(sigAmf, "ASYLUM Music Format V1.0", 24)) {
141 return loadAmf(st);
142 }
143 st.seek(setPos);
144
145 // mod file
146 return loadMod(st);
147 }
148
ModuleModXmS3m()149 ModuleModXmS3m::ModuleModXmS3m() {
150 sequenceLen = 1;
151 sequence = nullptr;
152 restartPos = 0;
153
154 // patterns
155 numChannels = 4;
156 numPatterns = 1;
157 patterns = nullptr;
158
159 // instruments
160 numInstruments = 1;
161 instruments = nullptr;
162
163 // others
164 defaultGvol = 64;
165 defaultSpeed = 6;
166 defaultTempo = 125;
167 c2Rate = 8287;
168 gain = 64;
169 linearPeriods = false;
170 fastVolSlides = false;
171 defaultPanning = nullptr; //{ 51, 204, 204, 51 };
172 }
173
~ModuleModXmS3m()174 ModuleModXmS3m::~ModuleModXmS3m() {
175 // free song position
176 if (sequence) {
177 delete[] sequence;
178 sequence = nullptr;
179 }
180
181 // free instruments
182 if (instruments) {
183 for (int i = 0; i <= numInstruments; ++i) {
184 // free samples
185 for (int j = 0; j < instruments[i].numSamples; ++j) {
186 if (instruments[i].samples[j].data) {
187 delete[] instruments[i].samples[j].data;
188 instruments[i].samples[j].data = nullptr;
189 }
190 }
191 delete[] instruments[i].samples;
192 instruments[i].samples = nullptr;
193 }
194 delete[] instruments;
195 instruments = nullptr;
196 }
197
198 // free patterns
199 if (patterns) {
200 for (int i = 0; i < numPatterns; ++i) {
201 delete []patterns[i].notes;
202 }
203 delete[] patterns;
204 patterns = nullptr;
205 }
206
207 // free default values
208 if (defaultPanning) {
209 delete[] defaultPanning;
210 defaultPanning = nullptr;
211 }
212 }
213
loadMod(Common::SeekableReadStream & st)214 bool ModuleModXmS3m::loadMod(Common::SeekableReadStream &st) {
215 // load song name
216 st.read(name, 20);
217 name[20] = '\0';
218
219 // load instruments
220 numInstruments = 31;
221 instruments = new Instrument[numInstruments + 1];
222 memset(instruments, 0, sizeof(Instrument) * (numInstruments + 1));
223 instruments[0].numSamples = 1;
224 instruments[0].samples = new Sample[1];
225 memset(&instruments[0].samples[0], 0, sizeof(Sample));
226
227 for (int i = 1; i <= numInstruments; ++i) {
228 instruments[i].numSamples = 1;
229 instruments[i].samples = new Sample[1];
230 memset(&instruments[i].samples[0], 0, sizeof(Sample));
231
232 // load sample
233 Sample &sample = instruments[i].samples[0];
234 st.read((byte *)sample.name, 22);
235 sample.name[22] = '\0';
236 sample.length = 2 * st.readUint16BE();
237
238 sample.finetune = st.readByte() & 0xF;
239
240 sample.volume = st.readByte();
241 sample.loopStart = 2 * st.readUint16BE();
242 sample.loopLength = 2 * st.readUint16BE();
243
244 if (sample.loopStart + sample.loopLength > sample.length) {
245 sample.loopLength = sample.length - sample.loopStart;
246 }
247 if (sample.loopLength < 4) {
248 sample.loopStart = sample.length;
249 sample.loopLength = 0;
250 }
251 }
252
253 sequenceLen = st.readByte();
254 if (sequenceLen > 128)
255 sequenceLen = 128;
256
257 restartPos = 0;
258 st.readByte(); // undefined byte, should be 127
259
260 sequence = new byte[128];
261 st.read(sequence, 128);
262
263 // check signature
264 byte xx[2];
265 st.read(xx, 2); // first 2 bytes of the signature
266 switch (st.readUint16BE()) {
267 case MKTAG16('K', '.'): /* M.K. */
268 // Fall Through intended
269 case MKTAG16('K', '!'): /* M!K! */
270 // Fall Through intended
271 case MKTAG16('T', '4'): /* FLT4 */
272 // Fall Through intended
273 numChannels = 4;
274 c2Rate = 8287;
275 gain = 64;
276 break;
277
278 case MKTAG16('H', 'N'): /* xCHN */
279 numChannels = xx[0] - '0';
280 c2Rate = 8363;
281 gain = 32;
282 break;
283
284 case MKTAG16('C', 'H'): /* xxCH */
285 numChannels = (xx[0] - '0') * 10 + xx[1] - '0';
286 c2Rate = 8363;
287 gain = 32;
288 break;
289
290 default:
291 warning("No known signature found in micromod module");
292 return false;
293
294 }
295
296 // default values
297 defaultGvol = 64;
298 defaultSpeed = 6;
299 defaultTempo = 125;
300 defaultPanning = new byte[numChannels];
301 for (int i = 0; i < numChannels; ++i) {
302 defaultPanning[i] = 51;
303 if ((i & 3) == 1 || (i & 3) == 2) {
304 defaultPanning[i] = 204;
305 }
306 }
307
308 // load patterns
309 numPatterns = 0;
310 for (int i = 0; i < 128; ++i)
311 if (numPatterns < sequence[i])
312 numPatterns = sequence[i];
313 ++numPatterns;
314
315 // load patterns
316 patterns = new Pattern[numPatterns];
317 for (int i = 0; i < numPatterns; ++i) {
318 patterns[i].numChannels = numChannels;
319 patterns[i].numRows = 64;
320
321 // load notes
322 /*
323 Old (amiga) noteinfo:
324
325 _____byte 1_____ byte2_ _____byte 3_____ byte4_
326 / \ / \ / \ / \
327 0000 0000-00000000 0000 0000-00000000
328
329 Upper four 12 bits for Lower four Effect command.
330 bits of sam- note period. bits of sam-
331 ple number. ple number.
332 */
333
334 int numNotes = patterns[i].numChannels * patterns[i].numRows;
335 patterns[i].notes = new Note[numNotes];
336 memset(patterns[i].notes, 0, numNotes * sizeof(Note));
337 for (int idx = 0; idx < numNotes; ++idx) {
338 byte first = st.readByte();
339 byte second = st.readByte();
340 byte third = st.readByte();
341 byte fourth = st.readByte();
342
343 // period, key
344 uint period = (first & 0xF) << 8;
345 period = (period | second) * 4;
346 if (period >= 112 && period <= 6848) {
347 int key = -12 * moduleLog2((period << FP_SHIFT) / 29021);
348 key = (key + (key & (FP_ONE >> 1))) >> FP_SHIFT;
349 patterns[i].notes[idx].key = key;
350 }
351
352 // instrument
353 uint ins = (third & 0xF0) >> 4;
354 ins = ins | (first & 0x10);
355 patterns[i].notes[idx].instrument = ins;
356
357 // effect, param
358 byte effect = third & 0x0F;
359 byte param = fourth & 0xff;
360 if (param == 0 && (effect < 3 || effect == 0xA)) {
361 effect = 0;
362 }
363 if (param == 0 && (effect == 5 || effect == 6)) {
364 effect -= 2;
365 }
366 if (effect == 8) {
367 if (numChannels == 4) {
368 effect = param = 0;
369 } else if (param > 128) {
370 param = 128;
371 } else {
372 param = (param * 255) >> 7;
373 }
374 }
375 patterns[i].notes[idx].effect = effect;
376 patterns[i].notes[idx].param = param;
377 }
378 }
379
380 // load data for the sample of instruments
381 for (int i = 1; i <= numInstruments; ++i) {
382 Sample &sample = instruments[i].samples[0];
383 if (!sample.length) {
384 sample.data = 0;
385 } else {
386 sample.data = new int16[sample.length + 1];
387 readSampleSint8(st, sample.length, sample.data);
388 sample.data[sample.loopStart + sample.loopLength] = sample.data[sample.loopStart];
389 }
390 }
391
392 return true;
393 }
394
loadXm(Common::SeekableReadStream & st)395 bool ModuleModXmS3m::loadXm(Common::SeekableReadStream &st) {
396 st.read(name, 20);
397 name[20] = '\0';
398 st.readByte(); // reserved byte
399
400 byte trackername[20];
401 st.read(trackername, 20);
402 bool deltaEnv = !memcmp(trackername, "DigiBooster Pro", 15);
403
404 uint16 version = st.readUint16LE();
405 if (version != 0x0104) {
406 warning("XM format version must be 0x0104!");
407 return false;
408 }
409
410 uint offset = st.pos() + st.readUint32LE();
411
412 sequenceLen = st.readUint16LE();
413 restartPos = st.readUint16LE();
414
415 numChannels = st.readUint16LE();
416 numPatterns = st.readUint16LE();
417 numInstruments = st.readUint16LE();
418 linearPeriods = st.readUint16LE() & 0x1;
419 defaultGvol = 64;
420 defaultSpeed = st.readUint16LE();
421 defaultTempo = st.readUint16LE();
422 c2Rate = 8363;
423 gain = 64;
424
425 defaultPanning = new byte[numChannels];
426 for (int i = 0; i < numChannels; ++i) {
427 defaultPanning[i] = 128;
428 }
429
430 sequence = new byte[sequenceLen];
431 for (int i = 0; i < sequenceLen; ++i) {
432 int entry = st.readByte();
433 sequence[i] = entry < numPatterns ? entry : 0;
434 }
435
436 // load patterns
437 patterns = new Pattern[numPatterns];
438 for (int i = 0; i < numPatterns; ++i) {
439 st.seek(offset, SEEK_SET);
440 offset += st.readUint32LE();
441 if (st.readByte()) {
442 warning("Unknown pattern packing type!");
443 return false;
444 }
445 patterns[i].numRows = st.readUint16LE();
446 if (patterns[i].numRows < 1)
447 patterns[i].numRows = 1;
448 uint16 patDataLength = st.readUint16LE();
449 offset += patDataLength;
450
451 // load notes
452 patterns[i].numChannels = numChannels;
453 int numNotes = patterns[i].numRows * numChannels;
454 patterns[i].notes = new Note[numNotes];
455 memset(patterns[i].notes, 0, numNotes * sizeof(Note));
456
457 if (patDataLength > 0) {
458 for (int j = 0; j < numNotes; ++j) {
459 Note ¬e = patterns[i].notes[j];
460 byte cmp = st.readByte();
461 if (cmp & 0x80) {
462 if (cmp & 1)
463 note.key = st.readByte();
464 if (cmp & 2)
465 note.instrument = st.readByte();
466 if (cmp & 4)
467 note.volume = st.readByte();
468 if (cmp & 8)
469 note.effect = st.readByte();
470 if (cmp & 16)
471 note.param = st.readByte();
472 } else {
473 note.key = cmp;
474 note.instrument = st.readByte();
475 note.volume = st.readByte();
476 note.effect = st.readByte();
477 note.param = st.readByte();
478 }
479 if( note.effect >= 0x40 ) {
480 note.effect = note.param = 0;
481 }
482 }
483 }
484 }
485
486 // load instruments
487 instruments = new Instrument[numInstruments + 1];
488 memset(instruments, 0, (numInstruments + 1) * sizeof(Instrument));
489 instruments[0].samples = new Sample[1];
490 memset(instruments[0].samples, 0, sizeof(Sample));
491 for (int i = 1; i <= numInstruments; ++i) {
492 st.seek(offset, SEEK_SET);
493 offset += st.readUint32LE();
494
495 Instrument &ins = instruments[i];
496 st.read(ins.name, 22);
497 ins.name[22] = '\0';
498
499 st.readByte(); // Instrument type (always 0)
500
501 // load sample number
502 int nSamples = st.readUint16LE();
503 ins.numSamples = nSamples > 0 ? nSamples : 1;
504 ins.samples = new Sample[ins.numSamples];
505 memset(ins.samples, 0, ins.numSamples * sizeof(Sample));
506 st.readUint32LE(); // skip 4 byte
507
508 // load instrument informations
509 if (nSamples > 0) {
510 for (int k = 0; k < 96; ++k) {
511 ins.keyToSample[k + 1] = st.readByte();
512 }
513 int pointTick = 0;
514 for (int p = 0; p < 12; ++p) {
515 pointTick = (deltaEnv ? pointTick : 0) + st.readUint16LE();
516 ins.volEnv.pointsTick[p] = pointTick;
517 ins.volEnv.pointsAmpl[p] = st.readUint16LE();
518 }
519 pointTick = 0;
520 for (int p = 0; p < 12; ++p) {
521 pointTick = (deltaEnv ? pointTick : 0) + st.readUint16LE();
522 ins.panEnv.pointsTick[p] = pointTick;
523 ins.panEnv.pointsAmpl[p] = st.readUint16LE();
524 }
525 ins.volEnv.numPoints = st.readByte();
526 if (ins.volEnv.numPoints > 12)
527 ins.volEnv.numPoints = 0;
528 ins.panEnv.numPoints = st.readByte();
529 if (ins.panEnv.numPoints > 12)
530 ins.panEnv.numPoints = 0;
531 ins.volEnv.sustainTick = ins.volEnv.pointsTick[st.readByte() & 0xF];
532 ins.volEnv.loopStartTick = ins.volEnv.pointsTick[st.readByte() & 0xF];
533 ins.volEnv.loopEndTick = ins.volEnv.pointsTick[st.readByte() & 0xF];
534 ins.panEnv.sustainTick = ins.panEnv.pointsTick[st.readByte() & 0xF];
535 ins.panEnv.loopStartTick = ins.panEnv.pointsTick[st.readByte() & 0xF];
536 ins.panEnv.loopEndTick = ins.panEnv.pointsTick[st.readByte() & 0xF];
537 byte volParam = st.readByte();
538 ins.volEnv.enabled = ins.volEnv.numPoints > 0 && (volParam & 0x1);
539 ins.volEnv.sustain = (volParam & 0x2) > 0;
540 ins.volEnv.looped = (volParam & 0x4) > 0;
541 byte panParam = st.readByte();
542 ins.panEnv.enabled = ins.panEnv.numPoints > 0 && (panParam & 0x1);
543 ins.panEnv.sustain = (panParam & 0x2) > 0;
544 ins.panEnv.looped = (panParam & 0x4) > 0;
545 ins.vibType = st.readByte();
546 ins.vibSweep = st.readByte();
547 ins.vibDepth = st.readByte();
548 ins.vibRate = st.readByte();
549 ins.volFadeout = st.readUint16LE();
550 }
551
552 // load samples
553 uint samHeadOffset = offset;
554 offset += nSamples * 40; // offset for sample data
555 for (int j = 0; j < nSamples; ++j) {
556 // load sample head
557 st.seek(samHeadOffset, SEEK_SET);
558 samHeadOffset += 40; // increment
559 Sample &sample = ins.samples[j];
560 uint samDataBytes = st.readUint32LE();
561 uint samLoopStart = st.readUint32LE();
562 uint samLoopLength = st.readUint32LE();
563 sample.volume = st.readByte();
564 sample.finetune = st.readSByte();
565 byte loopType = st.readByte();
566 bool looped = (loopType & 0x3) > 0;
567 bool pingPong = (loopType & 0x2) > 0;
568 bool sixteenBit = (loopType & 0x10) > 0;
569 sample.panning = st.readByte() + 1;
570 sample.relNote = st.readSByte();
571 st.readByte(); // reserved byte
572 st.read(sample.name, 22);
573 sample.name[22] = '\0';
574
575 uint samDataSamples = samDataBytes;
576 if (sixteenBit) {
577 samDataSamples = samDataSamples >> 1;
578 samLoopStart = samLoopStart >> 1;
579 samLoopLength = samLoopLength >> 1;
580 }
581 if (!looped || (samLoopStart + samLoopLength) > samDataSamples) {
582 samLoopStart = samDataSamples;
583 samLoopLength = 0;
584 }
585 sample.loopStart = samLoopStart;
586 sample.loopLength = samLoopLength;
587
588 // load sample data
589 st.seek(offset, SEEK_SET);
590 offset += samDataBytes; // increment
591 sample.data = new int16[samDataSamples + 1];
592 if (sixteenBit) {
593 readSampleSint16LE(st, samDataSamples, sample.data);
594 } else {
595 readSampleSint8(st, samDataSamples, sample.data);
596 }
597 int amp = 0;
598 for (uint idx = 0; idx < samDataSamples; idx++) {
599 amp = amp + sample.data[idx];
600 amp = (amp & 0x7FFF) - (amp & 0x8000);
601 sample.data[idx] = amp;
602 }
603 sample.data[samLoopStart + samLoopLength] = sample.data[samLoopStart];
604 if (pingPong) {
605 SamplePingPong(sample);
606 }
607 }
608 }
609 return true;
610 }
611
loadS3m(Common::SeekableReadStream & st)612 bool ModuleModXmS3m::loadS3m(Common::SeekableReadStream &st) {
613 st.read(name, 28);
614 name[28] = '\0';
615 st.skip(4); // skip 4 bytes
616
617 sequenceLen = st.readUint16LE();
618 numInstruments = st.readUint16LE();
619 numPatterns = st.readUint16LE();
620 uint16 flags = st.readUint16LE();
621 uint16 version = st.readUint16LE();
622 fastVolSlides = ((flags & 0x40) == 0x40) || version == 0x1300;
623 bool signedSamples = st.readUint16LE() == 1;
624
625 // check signature
626 if (st.readUint32BE() != MKTAG('S', 'C', 'R', 'M')) {
627 warning("Not an S3M file!");
628 return false;
629 }
630
631 defaultGvol = st.readByte();
632 defaultSpeed = st.readByte();
633 defaultTempo = st.readByte();
634 c2Rate = 8363;
635 byte mastermult = st.readByte();
636 gain = mastermult & 0x7F;
637 bool stereoMode = (mastermult & 0x80) == 0x80;
638 st.readByte(); // skip ultra-click
639 bool defaultPan = st.readByte() == 0xFC;
640 st.skip(10); // skip 10 bytes
641
642 // load channel map
643 numChannels = 0;
644 int channelMap[32];
645 for (int i = 0; i < 32; ++i) {
646 channelMap[i] = -1;
647 if (st.readByte() < 16) {
648 channelMap[i] = numChannels++;
649 }
650 }
651
652 // load sequence
653 sequence = new byte[sequenceLen];
654 st.read(sequence, sequenceLen);
655
656 int moduleDataIndex = st.pos();
657
658 // load instruments
659 instruments = new Instrument[numInstruments + 1];
660 memset(instruments, 0, sizeof(Instrument) * (numInstruments + 1));
661 instruments[0].numSamples = 1;
662 instruments[0].samples = new Sample[1];
663 memset(instruments[0].samples, 0, sizeof(Sample));
664 for (int i = 1; i <= numInstruments; ++i) {
665 Instrument &instrum = instruments[i];
666 instrum.numSamples = 1;
667 instrum.samples = new Sample[1];
668 memset(instrum.samples, 0, sizeof(Sample));
669 Sample &sample = instrum.samples[0];
670
671 // get instrument offset
672 st.seek(moduleDataIndex, SEEK_SET);
673 int instOffset = st.readUint16LE() << 4;
674 moduleDataIndex += 2;
675 st.seek(instOffset, SEEK_SET);
676
677 // load instrument, sample
678 if (st.readByte() == 1) { // type
679 st.skip(12); // skip file name
680 int sampleOffset = (st.readByte() << 20) + (st.readUint16LE() << 4);
681 uint sampleLength = st.readUint32LE();
682 uint loopStart = st.readUint32LE();
683 uint loopLength = st.readUint32LE() - loopStart;
684 sample.volume = st.readByte();
685 st.skip(1); // skip dsk
686 if (st.readByte() != 0) {
687 warning("Packed samples not supported for S3M files");
688 return false;
689 }
690 byte samParam = st.readByte();
691
692 if (loopStart + loopLength > sampleLength) {
693 loopLength = sampleLength - loopStart;
694 }
695 if (loopLength < 1 || !(samParam & 0x1)) {
696 loopStart = sampleLength;
697 loopLength = 0;
698 }
699
700 sample.loopStart = loopStart;
701 sample.loopLength = loopLength;
702
703 bool sixteenBit = samParam & 0x4;
704 int tune = (moduleLog2(st.readUint32LE()) - moduleLog2(c2Rate)) * 12;
705 sample.relNote = tune >> FP_SHIFT;
706 sample.finetune = (tune & FP_MASK) >> (FP_SHIFT - 7);
707 st.skip(12); // skip unused bytes
708 st.read(instrum.name, 28);
709
710 // load sample data
711 sample.data = new int16[sampleLength + 1];
712 st.seek(sampleOffset, SEEK_SET);
713 if (sixteenBit) {
714 readSampleSint16LE(st, sampleLength, sample.data);
715 } else {
716 readSampleSint8(st, sampleLength, sample.data);
717 }
718 if (!signedSamples) {
719 for (uint idx = 0; idx < sampleLength; ++idx) {
720 sample.data[idx] = (sample.data[idx] & 0xFFFF) - 32768;
721 }
722 }
723 sample.data[loopStart + loopLength] = sample.data[loopStart];
724 }
725 }
726
727 // load patterns
728 patterns = new Pattern[numPatterns];
729 memset(patterns, 0, numPatterns * sizeof(Pattern));
730 for (int i = 0; i < numPatterns; ++i) {
731 patterns[i].numChannels = numChannels;
732 patterns[i].numRows = 64;
733
734 // get pattern data offset
735 st.seek(moduleDataIndex, SEEK_SET);
736 int patOffset = (st.readUint16LE() << 4) + 2;
737 st.seek(patOffset, SEEK_SET);
738
739 // load notes
740 patterns[i].notes = new Note[numChannels * 64];
741 memset(patterns[i].notes, 0, numChannels * 64 * sizeof(Note));
742 int row = 0;
743 while (row < 64) {
744 byte token = st.readByte();
745 if (token) {
746 byte key = 0;
747 byte ins = 0;
748 if ((token & 0x20) == 0x20) {
749 /* Key + Instrument.*/
750 key = st.readByte();
751 ins = st.readByte();
752 if (key < 0xFE) {
753 key = (key >> 4) * 12 + (key & 0xF) + 1;
754 } else if (key == 0xFF) {
755 key = 0;
756 }
757 }
758 byte volume = 0;
759 if ((token & 0x40) == 0x40) {
760 /* Volume Column.*/
761 volume = (st.readByte() & 0x7F) + 0x10;
762 if (volume > 0x50) {
763 volume = 0;
764 }
765 }
766 byte effect = 0;
767 byte param = 0;
768 if ((token & 0x80) == 0x80) {
769 /* Effect + Param.*/
770 effect = st.readByte();
771 param = st.readByte();
772 if (effect < 1 || effect >= 0x40) {
773 effect = param = 0;
774 } else if (effect > 0) {
775 effect += 0x80;
776 }
777 }
778 int chan = channelMap[token & 0x1F];
779 if (chan >= 0) {
780 int noteIndex = row * numChannels + chan;
781 patterns[i].notes[noteIndex].key = key;
782 patterns[i].notes[noteIndex].instrument = ins;
783 patterns[i].notes[noteIndex].volume = volume;
784 patterns[i].notes[noteIndex].effect = effect;
785 patterns[i].notes[noteIndex].param = param;
786 }
787 } else {
788 row++;
789 }
790 }
791
792 // increment index
793 moduleDataIndex += 2;
794 }
795
796 // load default panning
797 defaultPanning = new byte[numChannels];
798 memset(defaultPanning, 0, numChannels);
799 for (int chan = 0; chan < 32; ++chan) {
800 if (channelMap[chan] >= 0) {
801 byte panning = 7;
802 if (stereoMode) {
803 panning = 12;
804 st.seek(64 + chan, SEEK_SET);
805 if (st.readByte() < 8) {
806 panning = 3;
807 }
808 }
809 if (defaultPan) {
810 st.seek(moduleDataIndex + chan, SEEK_SET);
811 flags = st.readByte();
812 if ((flags & 0x20) == 0x20) {
813 panning = flags & 0xF;
814 }
815 }
816 defaultPanning[channelMap[chan]] = panning * 17;
817 }
818 }
819 return true;
820 }
821
loadAmf(Common::SeekableReadStream & st)822 bool ModuleModXmS3m::loadAmf(Common::SeekableReadStream &st) {
823 // already skipped the signature ("ASYLUM Music Format V1.0")
824 // total signature length is 32 bytes (the rest are null)
825 st.skip(8);
826 memcpy(name, "Asylum Module", 14);
827
828 numChannels = 8;
829 defaultSpeed = st.readByte();
830 defaultTempo = st.readByte();
831 numInstruments = st.readByte(); // actually number of samples, but we'll do 1:1 mapping
832 numPatterns = st.readByte();
833 sequenceLen = st.readByte();
834 restartPos = st.readByte();
835
836 sequence = new byte[256];
837 st.read(sequence, 256); // Always 256 bytes in the file.
838
839 // Read sample headers..
840 instruments = new Instrument[numInstruments + 1];
841 memset(instruments, 0, sizeof(Instrument) * (numInstruments + 1));
842 instruments[0].numSamples = 1;
843 instruments[0].samples = new Sample[1];
844 memset(&instruments[0].samples[0], 0, sizeof(Sample));
845
846 for (int i = 1; i <= numInstruments; ++i) {
847 instruments[i].numSamples = 1;
848 instruments[i].samples = new Sample[1];
849 memset(&instruments[i].samples[0], 0, sizeof(Sample));
850
851 // load sample
852 Sample &sample = instruments[i].samples[0];
853 st.read((byte *)sample.name, 22);
854 sample.name[22] = '\0';
855
856 sample.finetune = st.readSByte();
857 sample.volume = st.readByte();
858 sample.relNote = st.readSByte(); // aka "transpose"
859 sample.length = st.readUint32LE();
860 sample.loopStart = st.readUint32LE();
861 sample.loopLength = st.readUint32LE();
862
863 if (sample.loopStart + sample.loopLength > sample.length) {
864 sample.loopLength = sample.length - sample.loopStart;
865 }
866 if (sample.loopLength < 4) {
867 sample.loopStart = sample.length;
868 sample.loopLength = 0;
869 }
870
871 // Sample data comes later.
872 }
873
874 st.skip((64 - numInstruments) * 37); // 37 == sample header len
875
876 // load patterns
877 patterns = new Pattern[numPatterns];
878 memset(patterns, 0, numPatterns * sizeof(Pattern));
879 for (int i = 0; i < numPatterns; ++i) {
880 // Always 8 channels, 64 rows.
881 patterns[i].numChannels = 8;
882 patterns[i].numRows = 64;
883
884 // load notes
885 patterns[i].notes = new Note[8 * 64];
886 memset(patterns[i].notes, 0, 8 * 64 * sizeof(Note));
887 for (int row = 0; row < 64; row++) {
888 for (int channel = 0; channel < 8; channel++) {
889 Note &n = patterns[i].notes[row * 8 + channel];
890 uint8 note = st.readByte();
891 if (note != 0) {
892 note = note + 1;
893 }
894 n.key = note;
895 n.instrument = st.readByte();
896 n.effect = st.readByte();
897 n.param = st.readByte();
898 // TODO: copied from libmodplug .. is this needed?
899 if (n.effect < 1 || n.effect > 0x0f) {
900 n.effect = n.param = 0;
901 }
902 // TODO: copied from mod loader.. is this needed?
903 if (n.param == 0 && (n.effect < 3 || n.effect == 0xA))
904 n.effect = 0;
905 if (n.param == 0 && (n.effect == 5 || n.effect == 6))
906 n.effect -= 2;
907 if (n.effect == 8) {
908 if (n.param > 128) {
909 n.param = 128;
910 } else {
911 n.param = (n.param * 255) >> 7;
912 }
913 }
914 }
915 }
916 }
917
918 // Load sample data
919 for (int i = 1; i <= numInstruments; ++i) {
920 Sample &sample = instruments[i].samples[0];
921 sample.data = new int16[sample.length];
922 readSampleSint8(st, sample.length, sample.data);
923 }
924
925 // default to panning to middle?
926 defaultPanning = new byte[numChannels];
927 for (int i = 0; i < numChannels; ++i) {
928 defaultPanning[i] = 128;
929 }
930
931 return true;
932 }
933
934
readSampleSint8(Common::SeekableReadStream & stream,int length,int16 * dest)935 void ModuleModXmS3m::readSampleSint8(Common::SeekableReadStream &stream, int length, int16 *dest) {
936 for (int i = 0; i < length; ++i) {
937 dest[i] = static_cast<int16>(stream.readSByte() * 256);
938 dest[i] = (dest[i] & 0x7FFF) - (dest[i] & 0x8000);
939 }
940 }
941
readSampleSint16LE(Common::SeekableReadStream & stream,int length,int16 * dest)942 void ModuleModXmS3m::readSampleSint16LE(Common::SeekableReadStream &stream, int length, int16 *dest) {
943 for (int i = 0; i < length; ++i) {
944 dest[i] = stream.readSint16LE();
945 dest[i] = (dest[i] & 0x7FFF) - (dest[i] & 0x8000);
946 }
947 }
948
SamplePingPong(Sample & sample)949 void ModuleModXmS3m::SamplePingPong(Sample &sample) {
950 int loopStart = sample.loopStart;
951 int loopLength = sample.loopLength;
952 int loopEnd = loopStart + loopLength;
953 int16 *sampleData = sample.data;
954 int16 *newData = new int16[loopEnd + loopLength + 1];
955 if (newData) {
956 memcpy(newData, sampleData, loopEnd * sizeof(int16));
957 for (int idx = 0; idx < loopLength; idx++) {
958 newData[loopEnd + idx] = sampleData[loopEnd - idx - 1];
959 }
960 delete []sample.data;
961 sample.data = newData;
962 sample.loopLength *= 2;
963 sample.data[loopStart + sample.loopLength] = sample.data[loopStart];
964 }
965 }
966
967 } // End of namespace Modules
968