Debian Codegraph @

src/modloaders/pt2_load_mod15.c

raw symbols SCIP
1 /* 15-sample Amiga MOD loader (The Ultimate Soundtracker, etc.)
2 **
3 ** Note: Data sanitation is done in the last stage
4 ** of module loading, so you don't need to do that here.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include "../pt2_header.h"
13 #include "../pt2_config.h"
14 #include "../pt2_structs.h"
15 #include "../pt2_replayer.h"
16 #include "../pt2_textout.h"
17 #include "../pt2_visuals.h"
18
19 static int32_t realSampleLengths[15];
20
21 module_t *loadMod15(uint8_t *buffer, uint32_t filesize)
22 {
23 (void)filesize;
24
25 module_t *m = createEmptyMod();
26 if (m == NULL)
27 {
28 statusOutOfMemory();
29 goto loadError;
30 }
31
32 bool veryLateSTKVerFlag = false; // "DFJ SoundTracker III" and later
33 bool lateSTKVerFlag = false; // "TJC SoundTracker II" and later
34
35 const uint8_t *p = buffer;
36 memcpy(m->header.name, p, 20); p += 20;
37
38 // read sample headers
39 moduleSample_t *s = m->samples;
40 for (int32_t i = 0; i < 15; i++, s++)
41 {
42 memcpy(s->text, p, 22); p += 22;
43
44 realSampleLengths[i] = ((p[0] << 8) | p[1]) * 2; p += 2;
45 s->length = (realSampleLengths[i] > config.maxSampleLength) ? config.maxSampleLength : realSampleLengths[i];
46
47 /* Only late versions of Ultimate SoundTracker can have samples larger than 9999 bytes.
48 ** If detected, we know for sure that this is a late STK module.
49 */
50 if (s->length > 9999)
51 lateSTKVerFlag = true;
52
53 p++; // hi-byte of volume, no finetune in STK/UST modules. Skip it.
54 s->volume = *p++;
55
56 // in The Ultimate SoundTracker, sample loop start is in bytes, not words
57 s->loopStart = (p[0] << 8) | p[1]; p += 2;
58 s->loopLength = ((p[0] << 8) | p[1]) * 2; p += 2;
59 }
60
61 m->header.songLength = *p++;
62
63 if (m->header.songLength == 0 || m->header.songLength > 128)
64 {
65 displayErrorMsg("NOT A MOD FILE !");
66 goto loadError;
67 }
68
69 uint8_t initTempo = *p++;
70 if (initTempo > 220)
71 {
72 displayErrorMsg("NOT A MOD FILE !");
73 goto loadError;
74 }
75
76 // 120 is a special case and means 50Hz (125BPM)
77 if (initTempo == 0)
78 initTempo = 120;
79
80 // jjk55.mod by Jesper Kyd has a bogus STK tempo value that should be ignored (XXX: hackish...)
81 if (!strcmp("jjk55", m->header.name))
82 initTempo = 120;
83
84 m->header.initialTempo = 125;
85 if (initTempo != 120)
86 {
87 if (initTempo > 220)
88 initTempo = 220;
89
90 // convert UST tempo to BPM
91 uint16_t ciaPeriod = (240 - initTempo) * 122;
92
93 const double dHz = (double)CIA_PAL_CLK / (ciaPeriod+1); // +1, CIA triggers on underflow
94
95 m->header.initialTempo = (uint16_t)((dHz * (125.0 / 50.0)) + 0.5);
96 }
97
98 // read orders and count number of patterns
99 int32_t numPatterns = 0;
100 for (int32_t i = 0; i < 128; i++)
101 {
102 m->header.patternTable[i] = *p++;
103 if (m->header.patternTable[i] > numPatterns)
104 numPatterns = m->header.patternTable[i];
105 }
106 numPatterns++;
107
108 if (numPatterns > MAX_PATTERNS)
109 {
110 displayErrorMsg("UNSUPPORTED MOD !");
111 goto loadError;
112 }
113
114 // load pattern data
115 for (int32_t i = 0; i < numPatterns; i++)
116 {
117 note_t *note = m->patterns[i];
118 for (int32_t j = 0; j < MOD_ROWS; j++)
119 {
120 for (int32_t k = 0; k < 4; k++, note++, p += 4)
121 {
122 note->period = ((p[0] & 0x0F) << 8) | p[1];
123 note->sample = (p[0] & 0xF0) | (p[2] >> 4);
124 note->command = p[2] & 0x0F;
125 note->param = p[3];
126
127 // added sanitation not present in original PT
128 if (note->sample > 31)
129 note->sample = 0;
130
131 if (note->command == 0xC || note->command == 0xD || note->command == 0xE)
132 {
133 // "TJC SoundTracker II" and later
134 lateSTKVerFlag = true;
135 }
136
137 if (note->command == 0xF)
138 {
139 // "DFJ SoundTracker III" and later
140 lateSTKVerFlag = true;
141 veryLateSTKVerFlag = true;
142 }
143 }
144 }
145 }
146
147 // pattern command conversion
148 for (int32_t i = 0; i < numPatterns; i++)
149 {
150 note_t *note = m->patterns[i];
151 for (int32_t j = 0; j < MOD_ROWS*4; j++, note++)
152 {
153 // convert STK effects to PT effects
154
155 if (!lateSTKVerFlag)
156 {
157 // old SoundTracker 1.x commands
158
159 if (note->command == 1)
160 {
161 // arpeggio
162 note->command = 0;
163 }
164 else if (note->command == 2)
165 {
166 // pitch slide
167 if (note->param & 0xF0)
168 {
169 // pitch slide down
170 note->command = 2;
171 note->param >>= 4;
172 }
173 else if (note->param & 0x0F)
174 {
175 // pitch slide up
176 note->command = 1;
177 }
178 }
179 }
180 else
181 {
182 // "DFJ SoundTracker II" or later
183
184 // TODO: This needs more detection and is NOT correct!
185 if (note->command == 0xD)
186 {
187 if (veryLateSTKVerFlag) // "DFJ SoundTracker III" or later
188 {
189 // pattern break w/ no param (param must be cleared to fix some songs)
190 note->param = 0;
191 }
192 else
193 {
194 // volume slide
195 note->command = 0xA;
196 }
197 }
198 }
199
200 // effect F with param 0x00 does nothing in UST/STK (I think?)
201 if (note->command == 0xF && note->param == 0)
202 note->command = 0;
203
204 // remove E8x (Karplus-Strong is only supported for ProTracker .MODs)
205 if (note->command == 0xE && (note->param >> 4) == 0x8)
206 {
207 note->command = 0;
208 note->param = 0;
209 }
210
211 // remove EFx (Invert Loop is only supported for ProTracker .MODs)
212 if (note->command == 0xE && (note->param >> 4) == 0xF)
213 {
214 note->command = 0;
215 note->param = 0;
216 }
217 }
218 }
219
220 // load sample data
221 s = m->samples;
222 for (int32_t i = 0; i < 15; i++, s++)
223 {
224 // for Ultimate SoundTracker modules, only the loop area of a looped sample is played.
225 if (s->loopStart > 0 && s->loopLength < s->length && s->loopStart < s->length)
226 {
227 s->length -= s->loopStart;
228 p += s->loopStart;
229 s->loopStart = 0;
230 }
231
232 int32_t bytesToSkip = 0;
233 if (realSampleLengths[i] > config.maxSampleLength)
234 bytesToSkip = realSampleLengths[i] - config.maxSampleLength;
235
236 // for Ultimate SoundTracker modules, don't load sample data after loop end
237 int32_t loopEnd = s->loopStart + s->loopLength;
238 if (loopEnd > 2 && s->length > loopEnd)
239 {
240 bytesToSkip += s->length-loopEnd;
241 s->length = loopEnd;
242 }
243
244 memcpy(&m->sampleData[s->offset], p, s->length); p += s->length;
245 if (bytesToSkip > 0)
246 p += bytesToSkip;
247 }
248
249 return m;
250
251 loadError:
252 if (m != NULL)
253 {
254 for (int32_t i = 0; i < MAX_PATTERNS; i++)
255 {
256 if (m->patterns[i] != NULL)
257 free(m->patterns[i]);
258 }
259
260 if (m->sampleData != NULL)
261 free(m->sampleData);
262
263 free(m);
264 }
265
266 return NULL;
267 }