Debian Codegraph @

src/smploaders/pt2_load_iff.c

raw symbols SCIP
1 /* Tnis IFF loader intentionally avoids reading the sample volume from the
2 ** IFF header as the IFF vol range can differ depending on the software used.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <math.h>
11 #include "../pt2_header.h"
12 #include "../pt2_config.h"
13 #include "../pt2_structs.h"
14 #include "../pt2_textout.h"
15 #include "../pt2_visuals.h"
16 #include "../pt2_helpers.h"
17 #include "../pt2_replayer.h"
18 #include "../pt2_askbox.h"
19 #include "../pt2_downsample2x.h"
20 #include "../pt2_audio.h"
21
22 bool loadIFFSample(FILE *f, uint32_t filesize, moduleSample_t *s)
23 {
24 char tmpCharBuf[23];
25 uint16_t sampleRate;
26 int32_t loopStart, loopLength;
27
28 // zero out chunk pointers and lengths
29 uint32_t vhdrPtr = 0; uint32_t vhdrLen = 0;
30 uint32_t bodyPtr = 0; uint32_t bodyLen = 0;
31 uint32_t namePtr = 0; int32_t nameLen = 0;
32
33 fseek(f, 8, SEEK_SET);
34 fread(tmpCharBuf, 1, 4, f);
35 bool is16Bit = !strncmp(tmpCharBuf, "16SV", 4);
36
37 int32_t sampleLength = 0;
38
39 fseek(f, 12, SEEK_SET);
40 while (!feof(f) && (uint32_t)ftell(f) < filesize-12)
41 {
42 uint32_t blockName, blockSize;
43
44 fread(&blockName, 4, 1, f); if (feof(f)) break;
45 fread(&blockSize, 4, 1, f); if (feof(f)) break;
46
47 blockName = SWAP32(blockName);
48 blockSize = SWAP32(blockSize);
49
50 switch (blockName)
51 {
52 case 0x56484452: // VHDR
53 {
54 vhdrPtr = ftell(f);
55 vhdrLen = blockSize;
56 }
57 break;
58
59 case 0x4E414D45: // NAME
60 {
61 namePtr = ftell(f);
62 nameLen = blockSize;
63 }
64 break;
65
66 case 0x424F4459: // BODY
67 {
68 bodyPtr = ftell(f);
69 bodyLen = blockSize;
70 }
71 break;
72
73 default: break;
74 }
75
76 fseek(f, blockSize + (blockSize & 1), SEEK_CUR);
77 }
78
79 if (vhdrPtr == 0 || vhdrLen < 20 || bodyPtr == 0)
80 {
81 displayErrorMsg("NOT A VALID IFF !");
82 return false;
83 }
84
85 // kludge for some really strange IFFs
86 if (bodyLen == 0)
87 bodyLen = filesize - bodyPtr;
88
89 if (bodyPtr+bodyLen > (uint32_t)filesize)
90 bodyLen = filesize - bodyPtr;
91
92 fseek(f, vhdrPtr, SEEK_SET);
93 fread(&loopStart, 4, 1, f); loopStart = SWAP32(loopStart);
94 fread(&loopLength, 4, 1, f); loopLength = SWAP32(loopLength);
95 fseek(f, 4, SEEK_CUR);
96 fread(&sampleRate, 2, 1, f); sampleRate = SWAP16(sampleRate);
97 fseek(f, 1, SEEK_CUR);
98
99 if (fgetc(f) != 0) // sample type
100 {
101 displayErrorMsg("UNSUPPORTED IFF !");
102 return false;
103 }
104
105 sampleLength = bodyLen;
106
107 if (sampleLength == 0)
108 {
109 displayErrorMsg("NOT A VALID IFF !");
110 return false;
111 }
112
113 bool downSample = false;
114 if (sampleRate > 22050 && !config.noDownsampleOnSmpLoad)
115 {
116 if (askBox(ASKBOX_DOWNSAMPLE, "DOWNSAMPLE ?"))
117 downSample = true;
118 }
119
120 int32_t maxSampleLength = config.maxSampleLength;
121 if (is16Bit)
122 maxSampleLength *= 2;
123
124 if (downSample)
125 maxSampleLength *= 2;
126
127 if (sampleLength > maxSampleLength)
128 sampleLength = maxSampleLength;
129
130 int8_t *sampleData = (int8_t *)malloc(sampleLength);
131 if (sampleData == NULL)
132 {
133 statusOutOfMemory();
134 return false;
135 }
136
137 if (is16Bit)
138 {
139 sampleLength >>= 1;
140 loopStart >>= 1;
141 loopLength >>= 1;
142 }
143
144 if (downSample)
145 {
146 loopStart >>= 1;
147 loopLength >>= 1;
148 }
149
150 turnOffVoices();
151
152 int8_t *smpDataPtr = &song->sampleData[s->offset];
153
154 fseek(f, bodyPtr, SEEK_SET);
155 if (is16Bit) // FT2-specific 16SV format (little-endian samples)
156 {
157 fread(sampleData, 1, sampleLength << 1, f);
158 int16_t *ptr16 = (int16_t *)sampleData;
159
160 // 2x downsampling
161 if (downSample)
162 {
163 downsample2x16Bit(ptr16, sampleLength);
164 sampleLength >>= 1;
165 }
166
167 if (sampleLength > config.maxSampleLength)
168 sampleLength = config.maxSampleLength;
169
170 double dAmp = 1.0;
171 if (downSample) // we already normalized
172 {
173 dAmp = INT8_MAX / (double)INT16_MAX;
174 }
175 else
176 {
177 const double dPeak = get16BitPeak(ptr16, sampleLength);
178 if (dPeak > 0.0)
179 dAmp = INT8_MAX / dPeak;
180 }
181
182 for (int32_t i = 0; i < sampleLength; i++)
183 {
184 int32_t smp32 = (int32_t)round(ptr16[i] * dAmp);
185 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
186 smpDataPtr[i] = (int8_t)smp32;
187 }
188 }
189 else
190 {
191 fread(sampleData, 1, sampleLength, f);
192
193 // 2x downsampling
194 if (downSample)
195 {
196 downsample2x8Bit(sampleData, sampleLength);
197 sampleLength >>= 1;
198 }
199
200 if (sampleLength > config.maxSampleLength)
201 sampleLength = config.maxSampleLength;
202
203 memcpy(smpDataPtr, sampleData, sampleLength);
204 }
205
206 free(sampleData);
207
208 if (sampleLength & 1)
209 {
210 if (++sampleLength > config.maxSampleLength)
211 sampleLength = config.maxSampleLength;
212 else
213 smpDataPtr[sampleLength-1] = 0;
214 }
215
216 loopStart &= ~1;
217 loopLength &= ~1;
218
219 if (loopLength < 2 || loopStart+loopLength > sampleLength)
220 {
221 loopStart = 0;
222 loopLength = 2;
223 }
224
225 // set sample attributes
226 s->volume = 64;
227 s->fineTune = 0;
228 s->length = sampleLength;
229 s->loopStart = loopStart;
230 s->loopLength = loopLength;
231
232 if (namePtr != 0 && nameLen > 0)
233 {
234 // read name
235
236 fseek(f, namePtr, SEEK_SET);
237 memset(tmpCharBuf, 0, sizeof (tmpCharBuf));
238
239 if (nameLen > 21)
240 {
241 fread(tmpCharBuf, 1, 21, f);
242 fseek(f, nameLen - 21, SEEK_CUR);
243 }
244 else
245 {
246 fread(tmpCharBuf, 1, nameLen, f);
247 }
248
249 // copy over sample name
250 memset(s->text, '\0', sizeof (s->text));
251
252 nameLen = (uint32_t)strlen(tmpCharBuf);
253 if (nameLen > 21)
254 nameLen = 21;
255
256 memcpy(s->text, tmpCharBuf, nameLen);
257 }
258
259 return true;
260 }