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"cxx . . $ `<file>/src/pt2_header.h`/
13 #include "../pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/
14 #include "../pt2_structs.h"cxx . . $ `<file>/src/pt2_structs.h`/
15 #include "../pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/
16 #include "../pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/
17 #include "../pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/
18
19 static int32_tcxx . . $ int32_t# realSampleLengthscxx . . $ realSampleLengths.[15];
20
21 module_tcxx . . $ module_t# *loadMod15cxx . . $ loadMod15(abcad8371f3628e2).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize)
22 {
23 (void)filesize;
24
25 module_tcxx . . $ module_t# *m = createEmptyModcxx . . $ createEmptyMod(51965c0e12ed79c4).();
26 if (m == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
27 {
28 statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).();
29 goto loadError;
30 }
31
32 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! veryLateSTKVerFlag = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; // "DFJ SoundTracker III" and later
33 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! lateSTKVerFlag = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; // "TJC SoundTracker II" and later
34
35 const uint8_tcxx . . $ uint8_t# *p = buffer;
36 memcpycxx . . $ memcpy(cc817cb175f282d).(m->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name., p, 20); p += 20;
37
38 // read sample headers
39 moduleSample_tcxx . . $ moduleSample_t# *s = m->samplescxx . . $ module_t#samples.;
40 for (int32_tcxx . . $ int32_t# i = 0; i < 15; i++, s++)
41 {
42 memcpycxx . . $ memcpy(cc817cb175f282d).(s->textcxx . . $ moduleSample_t#text., p, 22); p += 22;
43
44 realSampleLengthscxx . . $ realSampleLengths.[i] = ((p[0] << 8) | p[1]) * 2; p += 2;
45 s->lengthcxx . . $ moduleSample_t#length. = (realSampleLengthscxx . . $ realSampleLengths.[i] > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) ? configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength. : realSampleLengthscxx . . $ 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->lengthcxx . . $ moduleSample_t#length. > 9999)
51 lateSTKVerFlag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
52
53 p++; // hi-byte of volume, no finetune in STK/UST modules. Skip it.
54 s->volumecxx . . $ moduleSample_t#volume. = *p++;
55
56 // in The Ultimate SoundTracker, sample loop start is in bytes, not words
57 s->loopStartcxx . . $ moduleSample_t#loopStart. = (p[0] << 8) | p[1]; p += 2;
58 s->loopLengthcxx . . $ moduleSample_t#loopLength. = ((p[0] << 8) | p[1]) * 2; p += 2;
59 }
60
61 m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. = *p++;
62
63 if (m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. == 0 || m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. > 128)
64 {
65 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("NOT A MOD FILE !");
66 goto loadError;
67 }
68
69 uint8_tcxx . . $ uint8_t# initTempo = *p++;
70 if (initTempo > 220)
71 {
72 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("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 (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).("jjk55", m->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name.))
82 initTempo = 120;
83
84 m->headercxx . . $ module_t#header..initialTempocxx . . $ moduleHeader_t#initialTempo. = 125;
85 if (initTempo != 120)
86 {
87 if (initTempo > 220)
88 initTempo = 220;
89
90 // convert UST tempo to BPM
91 uint16_tcxx . . $ uint16_t# ciaPeriod = (240 - initTempo) * 122;
92
93 const double dHz = (double)CIA_PAL_CLKcxx . . $ `src/pt2_header.h:50:9`! / (ciaPeriod+1); // +1, CIA triggers on underflow
94
95 m->headercxx . . $ module_t#header..initialTempocxx . . $ moduleHeader_t#initialTempo. = (uint16_tcxx . . $ uint16_t#)((dHz * (125.0 / 50.0)) + 0.5);
96 }
97
98 // read orders and count number of patterns
99 int32_tcxx . . $ int32_t# numPatterns = 0;
100 for (int32_tcxx . . $ int32_t# i = 0; i < 128; i++)
101 {
102 m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i] = *p++;
103 if (m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i] > numPatterns)
104 numPatterns = m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i];
105 }
106 numPatterns++;
107
108 if (numPatterns > MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!)
109 {
110 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("UNSUPPORTED MOD !");
111 goto loadError;
112 }
113
114 // load pattern data
115 for (int32_tcxx . . $ int32_t# i = 0; i < numPatterns; i++)
116 {
117 note_tcxx . . $ note_t# *note = m->patternscxx . . $ module_t#patterns.[i];
118 for (int32_tcxx . . $ int32_t# j = 0; j < MOD_ROWScxx . . $ `src/pt2_header.h:73:9`!; j++)
119 {
120 for (int32_tcxx . . $ int32_t# k = 0; k < 4; k++, note++, p += 4)
121 {
122 note->periodcxx . . $ note_t#period. = ((p[0] & 0x0F) << 8) | p[1];
123 note->samplecxx . . $ note_t#sample. = (p[0] & 0xF0) | (p[2] >> 4);
124 note->commandcxx . . $ note_t#command. = p[2] & 0x0F;
125 note->paramcxx . . $ note_t#param. = p[3];
126
127 // added sanitation not present in original PT
128 if (note->samplecxx . . $ note_t#sample. > 31)
129 note->samplecxx . . $ note_t#sample. = 0;
130
131 if (note->commandcxx . . $ note_t#command. == 0xC || note->commandcxx . . $ note_t#command. == 0xD || note->commandcxx . . $ note_t#command. == 0xE)
132 {
133 // "TJC SoundTracker II" and later
134 lateSTKVerFlag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
135 }
136
137 if (note->commandcxx . . $ note_t#command. == 0xF)
138 {
139 // "DFJ SoundTracker III" and later
140 lateSTKVerFlag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
141 veryLateSTKVerFlag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
142 }
143 }
144 }
145 }
146
147 // pattern command conversion
148 for (int32_tcxx . . $ int32_t# i = 0; i < numPatterns; i++)
149 {
150 note_tcxx . . $ note_t# *note = m->patternscxx . . $ module_t#patterns.[i];
151 for (int32_tcxx . . $ int32_t# j = 0; j < MOD_ROWScxx . . $ `src/pt2_header.h:73:9`!*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->commandcxx . . $ note_t#command. == 1)
160 {
161 // arpeggio
162 note->commandcxx . . $ note_t#command. = 0;
163 }
164 else if (note->commandcxx . . $ note_t#command. == 2)
165 {
166 // pitch slide
167 if (note->paramcxx . . $ note_t#param. & 0xF0)
168 {
169 // pitch slide down
170 note->commandcxx . . $ note_t#command. = 2;
171 note->paramcxx . . $ note_t#param. >>= 4;
172 }
173 else if (note->paramcxx . . $ note_t#param. & 0x0F)
174 {
175 // pitch slide up
176 note->commandcxx . . $ note_t#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->commandcxx . . $ note_t#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->paramcxx . . $ note_t#param. = 0;
191 }
192 else
193 {
194 // volume slide
195 note->commandcxx . . $ note_t#command. = 0xA;
196 }
197 }
198 }
199
200 // effect F with param 0x00 does nothing in UST/STK (I think?)
201 if (note->commandcxx . . $ note_t#command. == 0xF && note->paramcxx . . $ note_t#param. == 0)
202 note->commandcxx . . $ note_t#command. = 0;
203
204 // remove E8x (Karplus-Strong is only supported for ProTracker .MODs)
205 if (note->commandcxx . . $ note_t#command. == 0xE && (note->paramcxx . . $ note_t#param. >> 4) == 0x8)
206 {
207 note->commandcxx . . $ note_t#command. = 0;
208 note->paramcxx . . $ note_t#param. = 0;
209 }
210
211 // remove EFx (Invert Loop is only supported for ProTracker .MODs)
212 if (note->commandcxx . . $ note_t#command. == 0xE && (note->paramcxx . . $ note_t#param. >> 4) == 0xF)
213 {
214 note->commandcxx . . $ note_t#command. = 0;
215 note->paramcxx . . $ note_t#param. = 0;
216 }
217 }
218 }
219
220 // load sample data
221 s = m->samplescxx . . $ module_t#samples.;
222 for (int32_tcxx . . $ 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->loopStartcxx . . $ moduleSample_t#loopStart. > 0 && s->loopLengthcxx . . $ moduleSample_t#loopLength. < s->lengthcxx . . $ moduleSample_t#length. && s->loopStartcxx . . $ moduleSample_t#loopStart. < s->lengthcxx . . $ moduleSample_t#length.)
226 {
227 s->lengthcxx . . $ moduleSample_t#length. -= s->loopStartcxx . . $ moduleSample_t#loopStart.;
228 p += s->loopStartcxx . . $ moduleSample_t#loopStart.;
229 s->loopStartcxx . . $ moduleSample_t#loopStart. = 0;
230 }
231
232 int32_tcxx . . $ int32_t# bytesToSkip = 0;
233 if (realSampleLengthscxx . . $ realSampleLengths.[i] > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.)
234 bytesToSkip = realSampleLengthscxx . . $ realSampleLengths.[i] - configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.;
235
236 // for Ultimate SoundTracker modules, don't load sample data after loop end
237 int32_tcxx . . $ int32_t# loopEnd = s->loopStartcxx . . $ moduleSample_t#loopStart. + s->loopLengthcxx . . $ moduleSample_t#loopLength.;
238 if (loopEnd > 2 && s->lengthcxx . . $ moduleSample_t#length. > loopEnd)
239 {
240 bytesToSkip += s->lengthcxx . . $ moduleSample_t#length.-loopEnd;
241 s->lengthcxx . . $ moduleSample_t#length. = loopEnd;
242 }
243
244 memcpycxx . . $ memcpy(cc817cb175f282d).(&m->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.], p, s->lengthcxx . . $ moduleSample_t#length.); p += s->lengthcxx . . $ moduleSample_t#length.;
245 if (bytesToSkip > 0)
246 p += bytesToSkip;
247 }
248
249 return m;
250
251 loadError:
252 if (m != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
253 {
254 for (int32_tcxx . . $ int32_t# i = 0; i < MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!; i++)
255 {
256 if (m->patternscxx . . $ module_t#patterns.[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
257 freecxx . . $ free(5d22bdacc48458e8).(m->patternscxx . . $ module_t#patterns.[i]);
258 }
259
260 if (m->sampleDatacxx . . $ module_t#sampleData. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
261 freecxx . . $ free(5d22bdacc48458e8).(m->sampleDatacxx . . $ module_t#sampleData.);
262
263 freecxx . . $ free(5d22bdacc48458e8).(m);
264 }
265
266 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
267 }