Mercurial > audlegacy-plugins
comparison src/modplug/load_s3m.cxx @ 136:6b5a52635b3b trunk
[svn] - like with so many other things, modplug is now maintained by us.
author | nenolod |
---|---|
date | Sun, 29 Oct 2006 01:04:52 -0700 |
parents | |
children | 032053ca08ab 3673c7ec4ea2 |
comparison
equal
deleted
inserted
replaced
135:33d24bd94ccc | 136:6b5a52635b3b |
---|---|
1 /* | |
2 * This source code is public domain. | |
3 * | |
4 * Authors: Olivier Lapicque <olivierl@jps.net>, | |
5 * Adam Goode <adam@evdebs.org> (endian and char fixes for PPC) | |
6 */ | |
7 | |
8 #include "stdafx.h" | |
9 #include "sndfile.h" | |
10 | |
11 //#pragma warning(disable:4244) | |
12 | |
13 extern WORD S3MFineTuneTable[16]; | |
14 | |
15 ////////////////////////////////////////////////////// | |
16 // ScreamTracker S3M file support | |
17 | |
18 typedef struct tagS3MSAMPLESTRUCT | |
19 { | |
20 BYTE type; | |
21 CHAR dosname[12]; | |
22 BYTE hmem; | |
23 WORD memseg; | |
24 DWORD length; | |
25 DWORD loopbegin; | |
26 DWORD loopend; | |
27 BYTE vol; | |
28 BYTE bReserved; | |
29 BYTE pack; | |
30 BYTE flags; | |
31 DWORD finetune; | |
32 DWORD dwReserved; | |
33 WORD intgp; | |
34 WORD int512; | |
35 DWORD lastused; | |
36 CHAR name[28]; | |
37 CHAR scrs[4]; | |
38 } S3MSAMPLESTRUCT; | |
39 | |
40 | |
41 typedef struct tagS3MFILEHEADER | |
42 { | |
43 CHAR name[28]; | |
44 BYTE b1A; | |
45 BYTE type; | |
46 WORD reserved1; | |
47 WORD ordnum; | |
48 WORD insnum; | |
49 WORD patnum; | |
50 WORD flags; | |
51 WORD cwtv; | |
52 WORD version; | |
53 DWORD scrm; // "SCRM" = 0x4D524353 | |
54 BYTE globalvol; | |
55 BYTE speed; | |
56 BYTE tempo; | |
57 BYTE mastervol; | |
58 BYTE ultraclicks; | |
59 BYTE panning_present; | |
60 BYTE reserved2[8]; | |
61 WORD special; | |
62 BYTE channels[32]; | |
63 } S3MFILEHEADER; | |
64 | |
65 | |
66 void CSoundFile::S3MConvert(MODCOMMAND *m, BOOL bIT) const | |
67 //-------------------------------------------------------- | |
68 { | |
69 UINT command = m->command; | |
70 UINT param = m->param; | |
71 switch (command + 0x40) | |
72 { | |
73 case 'A': command = CMD_SPEED; break; | |
74 case 'B': command = CMD_POSITIONJUMP; break; | |
75 case 'C': command = CMD_PATTERNBREAK; if (!bIT) param = (param >> 4) * 10 + (param & 0x0F); break; | |
76 case 'D': command = CMD_VOLUMESLIDE; break; | |
77 case 'E': command = CMD_PORTAMENTODOWN; break; | |
78 case 'F': command = CMD_PORTAMENTOUP; break; | |
79 case 'G': command = CMD_TONEPORTAMENTO; break; | |
80 case 'H': command = CMD_VIBRATO; break; | |
81 case 'I': command = CMD_TREMOR; break; | |
82 case 'J': command = CMD_ARPEGGIO; break; | |
83 case 'K': command = CMD_VIBRATOVOL; break; | |
84 case 'L': command = CMD_TONEPORTAVOL; break; | |
85 case 'M': command = CMD_CHANNELVOLUME; break; | |
86 case 'N': command = CMD_CHANNELVOLSLIDE; break; | |
87 case 'O': command = CMD_OFFSET; break; | |
88 case 'P': command = CMD_PANNINGSLIDE; break; | |
89 case 'Q': command = CMD_RETRIG; break; | |
90 case 'R': command = CMD_TREMOLO; break; | |
91 case 'S': command = CMD_S3MCMDEX; break; | |
92 case 'T': command = CMD_TEMPO; break; | |
93 case 'U': command = CMD_FINEVIBRATO; break; | |
94 case 'V': command = CMD_GLOBALVOLUME; break; | |
95 case 'W': command = CMD_GLOBALVOLSLIDE; break; | |
96 case 'X': command = CMD_PANNING8; break; | |
97 case 'Y': command = CMD_PANBRELLO; break; | |
98 case 'Z': command = CMD_MIDI; break; | |
99 default: command = 0; | |
100 } | |
101 m->command = command; | |
102 m->param = param; | |
103 } | |
104 | |
105 | |
106 void CSoundFile::S3MSaveConvert(UINT *pcmd, UINT *pprm, BOOL bIT) const | |
107 //--------------------------------------------------------------------- | |
108 { | |
109 UINT command = *pcmd; | |
110 UINT param = *pprm; | |
111 switch(command) | |
112 { | |
113 case CMD_SPEED: command = 'A'; break; | |
114 case CMD_POSITIONJUMP: command = 'B'; break; | |
115 case CMD_PATTERNBREAK: command = 'C'; if (!bIT) param = ((param / 10) << 4) + (param % 10); break; | |
116 case CMD_VOLUMESLIDE: command = 'D'; break; | |
117 case CMD_PORTAMENTODOWN: command = 'E'; if ((param >= 0xE0) && (m_nType & (MOD_TYPE_MOD|MOD_TYPE_XM))) param = 0xDF; break; | |
118 case CMD_PORTAMENTOUP: command = 'F'; if ((param >= 0xE0) && (m_nType & (MOD_TYPE_MOD|MOD_TYPE_XM))) param = 0xDF; break; | |
119 case CMD_TONEPORTAMENTO: command = 'G'; break; | |
120 case CMD_VIBRATO: command = 'H'; break; | |
121 case CMD_TREMOR: command = 'I'; break; | |
122 case CMD_ARPEGGIO: command = 'J'; break; | |
123 case CMD_VIBRATOVOL: command = 'K'; break; | |
124 case CMD_TONEPORTAVOL: command = 'L'; break; | |
125 case CMD_CHANNELVOLUME: command = 'M'; break; | |
126 case CMD_CHANNELVOLSLIDE: command = 'N'; break; | |
127 case CMD_OFFSET: command = 'O'; break; | |
128 case CMD_PANNINGSLIDE: command = 'P'; break; | |
129 case CMD_RETRIG: command = 'Q'; break; | |
130 case CMD_TREMOLO: command = 'R'; break; | |
131 case CMD_S3MCMDEX: command = 'S'; break; | |
132 case CMD_TEMPO: command = 'T'; break; | |
133 case CMD_FINEVIBRATO: command = 'U'; break; | |
134 case CMD_GLOBALVOLUME: command = 'V'; break; | |
135 case CMD_GLOBALVOLSLIDE: command = 'W'; break; | |
136 case CMD_PANNING8: | |
137 command = 'X'; | |
138 if ((bIT) && (m_nType != MOD_TYPE_IT) && (m_nType != MOD_TYPE_XM)) | |
139 { | |
140 if (param == 0xA4) { command = 'S'; param = 0x91; } else | |
141 if (param <= 0x80) { param <<= 1; if (param > 255) param = 255; } else | |
142 command = param = 0; | |
143 } else | |
144 if ((!bIT) && ((m_nType == MOD_TYPE_IT) || (m_nType == MOD_TYPE_XM))) | |
145 { | |
146 param >>= 1; | |
147 } | |
148 break; | |
149 case CMD_PANBRELLO: command = 'Y'; break; | |
150 case CMD_MIDI: command = 'Z'; break; | |
151 case CMD_XFINEPORTAUPDOWN: | |
152 if (param & 0x0F) switch(param & 0xF0) | |
153 { | |
154 case 0x10: command = 'F'; param = (param & 0x0F) | 0xE0; break; | |
155 case 0x20: command = 'E'; param = (param & 0x0F) | 0xE0; break; | |
156 case 0x90: command = 'S'; break; | |
157 default: command = param = 0; | |
158 } else command = param = 0; | |
159 break; | |
160 case CMD_MODCMDEX: | |
161 command = 'S'; | |
162 switch(param & 0xF0) | |
163 { | |
164 case 0x00: command = param = 0; break; | |
165 case 0x10: command = 'F'; param |= 0xF0; break; | |
166 case 0x20: command = 'E'; param |= 0xF0; break; | |
167 case 0x30: param = (param & 0x0F) | 0x10; break; | |
168 case 0x40: param = (param & 0x0F) | 0x30; break; | |
169 case 0x50: param = (param & 0x0F) | 0x20; break; | |
170 case 0x60: param = (param & 0x0F) | 0xB0; break; | |
171 case 0x70: param = (param & 0x0F) | 0x40; break; | |
172 case 0x90: command = 'Q'; param &= 0x0F; break; | |
173 case 0xA0: if (param & 0x0F) { command = 'D'; param = (param << 4) | 0x0F; } else command=param=0; break; | |
174 case 0xB0: if (param & 0x0F) { command = 'D'; param |= 0xF0; } else command=param=0; break; | |
175 } | |
176 break; | |
177 default: command = param = 0; | |
178 } | |
179 command &= ~0x40; | |
180 *pcmd = command; | |
181 *pprm = param; | |
182 } | |
183 | |
184 | |
185 BOOL CSoundFile::ReadS3M(const BYTE *lpStream, DWORD dwMemLength) | |
186 //--------------------------------------------------------------- | |
187 { | |
188 UINT insnum,patnum,nins,npat; | |
189 DWORD insfile[128]; | |
190 WORD ptr[256]; | |
191 BYTE s[1024]; | |
192 DWORD dwMemPos; | |
193 BYTE insflags[128], inspack[128]; | |
194 S3MFILEHEADER psfh = *(S3MFILEHEADER *)lpStream; | |
195 | |
196 psfh.reserved1 = bswapLE16(psfh.reserved1); | |
197 psfh.ordnum = bswapLE16(psfh.ordnum); | |
198 psfh.insnum = bswapLE16(psfh.insnum); | |
199 psfh.patnum = bswapLE16(psfh.patnum); | |
200 psfh.flags = bswapLE16(psfh.flags); | |
201 psfh.cwtv = bswapLE16(psfh.cwtv); | |
202 psfh.version = bswapLE16(psfh.version); | |
203 psfh.scrm = bswapLE32(psfh.scrm); | |
204 psfh.special = bswapLE16(psfh.special); | |
205 | |
206 if ((!lpStream) || (dwMemLength <= sizeof(S3MFILEHEADER)+sizeof(S3MSAMPLESTRUCT)+64)) return FALSE; | |
207 if (psfh.scrm != 0x4D524353) return FALSE; | |
208 dwMemPos = 0x60; | |
209 m_nType = MOD_TYPE_S3M; | |
210 memset(m_szNames,0,sizeof(m_szNames)); | |
211 memcpy(m_szNames[0], psfh.name, 28); | |
212 // Speed | |
213 m_nDefaultSpeed = psfh.speed; | |
214 if (m_nDefaultSpeed < 1) m_nDefaultSpeed = 6; | |
215 if (m_nDefaultSpeed > 0x1F) m_nDefaultSpeed = 0x1F; | |
216 // Tempo | |
217 m_nDefaultTempo = psfh.tempo; | |
218 if (m_nDefaultTempo < 40) m_nDefaultTempo = 40; | |
219 if (m_nDefaultTempo > 240) m_nDefaultTempo = 240; | |
220 // Global Volume | |
221 m_nDefaultGlobalVolume = psfh.globalvol << 2; | |
222 if ((!m_nDefaultGlobalVolume) || (m_nDefaultGlobalVolume > 256)) m_nDefaultGlobalVolume = 256; | |
223 m_nSongPreAmp = psfh.mastervol & 0x7F; | |
224 // Channels | |
225 m_nChannels = 4; | |
226 for (UINT ich=0; ich<32; ich++) | |
227 { | |
228 ChnSettings[ich].nPan = 128; | |
229 ChnSettings[ich].nVolume = 64; | |
230 | |
231 ChnSettings[ich].dwFlags = CHN_MUTE; | |
232 if (psfh.channels[ich] != 0xFF) | |
233 { | |
234 m_nChannels = ich+1; | |
235 UINT b = psfh.channels[ich] & 0x0F; | |
236 ChnSettings[ich].nPan = (b & 8) ? 0xC0 : 0x40; | |
237 ChnSettings[ich].dwFlags = 0; | |
238 } | |
239 } | |
240 if (m_nChannels < 4) m_nChannels = 4; | |
241 if ((psfh.cwtv < 0x1320) || (psfh.flags & 0x40)) m_dwSongFlags |= SONG_FASTVOLSLIDES; | |
242 // Reading pattern order | |
243 UINT iord = psfh.ordnum; | |
244 if (iord<1) iord = 1; | |
245 if (iord > MAX_ORDERS) iord = MAX_ORDERS; | |
246 if (iord) | |
247 { | |
248 memcpy(Order, lpStream+dwMemPos, iord); | |
249 dwMemPos += iord; | |
250 } | |
251 if ((iord & 1) && (lpStream[dwMemPos] == 0xFF)) dwMemPos++; | |
252 // Reading file pointers | |
253 insnum = nins = psfh.insnum; | |
254 if (insnum >= MAX_SAMPLES) insnum = MAX_SAMPLES-1; | |
255 m_nSamples = insnum; | |
256 patnum = npat = psfh.patnum; | |
257 if (patnum > MAX_PATTERNS) patnum = MAX_PATTERNS; | |
258 memset(ptr, 0, sizeof(ptr)); | |
259 if (nins+npat) | |
260 { | |
261 memcpy(ptr, lpStream+dwMemPos, 2*(nins+npat)); | |
262 dwMemPos += 2*(nins+npat); | |
263 for (UINT j = 0; j < (nins+npat); ++j) { | |
264 ptr[j] = bswapLE16(ptr[j]); | |
265 } | |
266 if (psfh.panning_present == 252) | |
267 { | |
268 const BYTE *chnpan = lpStream+dwMemPos; | |
269 for (UINT i=0; i<32; i++) if (chnpan[i] & 0x20) | |
270 { | |
271 ChnSettings[i].nPan = ((chnpan[i] & 0x0F) << 4) + 8; | |
272 } | |
273 } | |
274 } | |
275 if (!m_nChannels) return TRUE; | |
276 // Reading instrument headers | |
277 memset(insfile, 0, sizeof(insfile)); | |
278 for (UINT iSmp=1; iSmp<=insnum; iSmp++) | |
279 { | |
280 UINT nInd = ((DWORD)ptr[iSmp-1])*16; | |
281 if ((!nInd) || (nInd + 0x50 > dwMemLength)) continue; | |
282 memcpy(s, lpStream+nInd, 0x50); | |
283 memcpy(Ins[iSmp].name, s+1, 12); | |
284 insflags[iSmp-1] = s[0x1F]; | |
285 inspack[iSmp-1] = s[0x1E]; | |
286 s[0x4C] = 0; | |
287 lstrcpy(m_szNames[iSmp], (LPCSTR)&s[0x30]); | |
288 if ((s[0]==1) && (s[0x4E]=='R') && (s[0x4F]=='S')) | |
289 { | |
290 UINT j = bswapLE32(*((LPDWORD)(s+0x10))); | |
291 if (j > MAX_SAMPLE_LENGTH) j = MAX_SAMPLE_LENGTH; | |
292 if (j < 4) j = 0; | |
293 Ins[iSmp].nLength = j; | |
294 j = bswapLE32(*((LPDWORD)(s+0x14))); | |
295 if (j >= Ins[iSmp].nLength) j = Ins[iSmp].nLength - 1; | |
296 Ins[iSmp].nLoopStart = j; | |
297 j = bswapLE32(*((LPDWORD)(s+0x18))); | |
298 if (j > MAX_SAMPLE_LENGTH) j = MAX_SAMPLE_LENGTH; | |
299 if (j < 4) j = 0; | |
300 if (j > Ins[iSmp].nLength) j = Ins[iSmp].nLength; | |
301 Ins[iSmp].nLoopEnd = j; | |
302 j = s[0x1C]; | |
303 if (j > 64) j = 64; | |
304 Ins[iSmp].nVolume = j << 2; | |
305 Ins[iSmp].nGlobalVol = 64; | |
306 if (s[0x1F]&1) Ins[iSmp].uFlags |= CHN_LOOP; | |
307 j = bswapLE32(*((LPDWORD)(s+0x20))); | |
308 if (!j) j = 8363; | |
309 if (j < 1024) j = 1024; | |
310 Ins[iSmp].nC4Speed = j; | |
311 insfile[iSmp] = ((DWORD)bswapLE16(*((LPWORD)(s+0x0E)))) << 4; | |
312 insfile[iSmp] += ((DWORD)(BYTE)s[0x0D]) << 20; | |
313 if (insfile[iSmp] > dwMemLength) insfile[iSmp] &= 0xFFFF; | |
314 if ((Ins[iSmp].nLoopStart >= Ins[iSmp].nLoopEnd) || (Ins[iSmp].nLoopEnd - Ins[iSmp].nLoopStart < 8)) | |
315 Ins[iSmp].nLoopStart = Ins[iSmp].nLoopEnd = 0; | |
316 Ins[iSmp].nPan = 0x80; | |
317 } | |
318 } | |
319 // Reading patterns | |
320 for (UINT iPat=0; iPat<patnum; iPat++) | |
321 { | |
322 UINT nInd = ((DWORD)ptr[nins+iPat]) << 4; | |
323 if (nInd + 0x40 > dwMemLength) continue; | |
324 WORD len = bswapLE16(*((WORD *)(lpStream+nInd))); | |
325 nInd += 2; | |
326 PatternSize[iPat] = 64; | |
327 if ((!len) || (nInd + len > dwMemLength - 6) | |
328 || ((Patterns[iPat] = AllocatePattern(64, m_nChannels)) == NULL)) continue; | |
329 LPBYTE src = (LPBYTE)(lpStream+nInd); | |
330 // Unpacking pattern | |
331 MODCOMMAND *p = Patterns[iPat]; | |
332 UINT row = 0; | |
333 UINT j = 0; | |
334 while (j < len) | |
335 { | |
336 BYTE b = src[j++]; | |
337 if (!b) | |
338 { | |
339 if (++row >= 64) break; | |
340 } else | |
341 { | |
342 UINT chn = b & 0x1F; | |
343 if (chn < m_nChannels) | |
344 { | |
345 MODCOMMAND *m = &p[row*m_nChannels+chn]; | |
346 if (b & 0x20) | |
347 { | |
348 m->note = src[j++]; | |
349 if (m->note < 0xF0) m->note = (m->note & 0x0F) + 12*(m->note >> 4) + 13; | |
350 else if (m->note == 0xFF) m->note = 0; | |
351 m->instr = src[j++]; | |
352 } | |
353 if (b & 0x40) | |
354 { | |
355 UINT vol = src[j++]; | |
356 if ((vol >= 128) && (vol <= 192)) | |
357 { | |
358 vol -= 128; | |
359 m->volcmd = VOLCMD_PANNING; | |
360 } else | |
361 { | |
362 if (vol > 64) vol = 64; | |
363 m->volcmd = VOLCMD_VOLUME; | |
364 } | |
365 m->vol = vol; | |
366 } | |
367 if (b & 0x80) | |
368 { | |
369 m->command = src[j++]; | |
370 m->param = src[j++]; | |
371 if (m->command) S3MConvert(m, FALSE); | |
372 } | |
373 } else | |
374 { | |
375 if (b & 0x20) j += 2; | |
376 if (b & 0x40) j++; | |
377 if (b & 0x80) j += 2; | |
378 } | |
379 if (j >= len) break; | |
380 } | |
381 } | |
382 } | |
383 // Reading samples | |
384 for (UINT iRaw=1; iRaw<=insnum; iRaw++) if ((Ins[iRaw].nLength) && (insfile[iRaw])) | |
385 { | |
386 UINT flags = (psfh.version == 1) ? RS_PCM8S : RS_PCM8U; | |
387 if (insflags[iRaw-1] & 4) flags += 5; | |
388 if (insflags[iRaw-1] & 2) flags |= RSF_STEREO; | |
389 if (inspack[iRaw-1] == 4) flags = RS_ADPCM4; | |
390 dwMemPos = insfile[iRaw]; | |
391 dwMemPos += ReadSample(&Ins[iRaw], flags, (LPSTR)(lpStream + dwMemPos), dwMemLength - dwMemPos); | |
392 } | |
393 m_nMinPeriod = 64; | |
394 m_nMaxPeriod = 32767; | |
395 if (psfh.flags & 0x10) m_dwSongFlags |= SONG_AMIGALIMITS; | |
396 return TRUE; | |
397 } | |
398 | |
399 | |
400 #ifndef MODPLUG_NO_FILESAVE | |
401 #pragma warning(disable:4100) | |
402 | |
403 static BYTE S3MFiller[16] = | |
404 { | |
405 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, | |
406 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 | |
407 }; | |
408 | |
409 | |
410 BOOL CSoundFile::SaveS3M(LPCSTR lpszFileName, UINT nPacking) | |
411 //---------------------------------------------------------- | |
412 { | |
413 FILE *f; | |
414 BYTE header[0x60]; | |
415 UINT nbo,nbi,nbp,i; | |
416 WORD patptr[128]; | |
417 WORD insptr[128]; | |
418 BYTE buffer[5*1024]; | |
419 S3MSAMPLESTRUCT insex[128]; | |
420 | |
421 if ((!m_nChannels) || (!lpszFileName)) return FALSE; | |
422 if ((f = fopen(lpszFileName, "wb")) == NULL) return FALSE; | |
423 // Writing S3M header | |
424 memset(header, 0, sizeof(header)); | |
425 memset(insex, 0, sizeof(insex)); | |
426 memcpy(header, m_szNames[0], 0x1C); | |
427 header[0x1B] = 0; | |
428 header[0x1C] = 0x1A; | |
429 header[0x1D] = 0x10; | |
430 nbo = (GetNumPatterns() + 15) & 0xF0; | |
431 if (!nbo) nbo = 16; | |
432 header[0x20] = nbo & 0xFF; | |
433 header[0x21] = nbo >> 8; | |
434 nbi = m_nInstruments; | |
435 if (!nbi) nbi = m_nSamples; | |
436 if (nbi > 99) nbi = 99; | |
437 header[0x22] = nbi & 0xFF; | |
438 header[0x23] = nbi >> 8; | |
439 nbp = 0; | |
440 for (i=0; Patterns[i]; i++) { nbp = i+1; if (nbp >= MAX_PATTERNS) break; } | |
441 for (i=0; i<MAX_ORDERS; i++) if ((Order[i] < MAX_PATTERNS) && (Order[i] >= nbp)) nbp = Order[i] + 1; | |
442 header[0x24] = nbp & 0xFF; | |
443 header[0x25] = nbp >> 8; | |
444 if (m_dwSongFlags & SONG_FASTVOLSLIDES) header[0x26] |= 0x40; | |
445 if ((m_nMaxPeriod < 20000) || (m_dwSongFlags & SONG_AMIGALIMITS)) header[0x26] |= 0x10; | |
446 header[0x28] = 0x20; | |
447 header[0x29] = 0x13; | |
448 header[0x2A] = 0x02; // Version = 1 => Signed samples | |
449 header[0x2B] = 0x00; | |
450 header[0x2C] = 'S'; | |
451 header[0x2D] = 'C'; | |
452 header[0x2E] = 'R'; | |
453 header[0x2F] = 'M'; | |
454 header[0x30] = m_nDefaultGlobalVolume >> 2; | |
455 header[0x31] = m_nDefaultSpeed; | |
456 header[0x32] = m_nDefaultTempo; | |
457 header[0x33] = ((m_nSongPreAmp < 0x20) ? 0x20 : m_nSongPreAmp) | 0x80; // Stereo | |
458 header[0x35] = 0xFC; | |
459 for (i=0; i<32; i++) | |
460 { | |
461 if (i < m_nChannels) | |
462 { | |
463 UINT tmp = (i & 0x0F) >> 1; | |
464 header[0x40+i] = (i & 0x10) | ((i & 1) ? 8+tmp : tmp); | |
465 } else header[0x40+i] = 0xFF; | |
466 } | |
467 fwrite(header, 0x60, 1, f); | |
468 fwrite(Order, nbo, 1, f); | |
469 memset(patptr, 0, sizeof(patptr)); | |
470 memset(insptr, 0, sizeof(insptr)); | |
471 UINT ofs0 = 0x60 + nbo; | |
472 UINT ofs1 = ((0x60 + nbo + nbi*2 + nbp*2 + 15) & 0xFFF0) + 0x20; | |
473 UINT ofs = ofs1; | |
474 | |
475 for (i=0; i<nbi; i++) insptr[i] = (WORD)((ofs + i*0x50) / 16); | |
476 for (i=0; i<nbp; i++) patptr[i] = (WORD)((ofs + nbi*0x50) / 16); | |
477 fwrite(insptr, nbi, 2, f); | |
478 fwrite(patptr, nbp, 2, f); | |
479 if (header[0x35] == 0xFC) | |
480 { | |
481 BYTE chnpan[32]; | |
482 for (i=0; i<32; i++) | |
483 { | |
484 chnpan[i] = 0x20 | (ChnSettings[i].nPan >> 4); | |
485 } | |
486 fwrite(chnpan, 0x20, 1, f); | |
487 } | |
488 if ((nbi*2+nbp*2) & 0x0F) | |
489 { | |
490 fwrite(S3MFiller, 0x10 - ((nbi*2+nbp*2) & 0x0F), 1, f); | |
491 } | |
492 ofs1 = ftell(f); | |
493 fwrite(insex, nbi, 0x50, f); | |
494 // Packing patterns | |
495 ofs += nbi*0x50; | |
496 for (i=0; i<nbp; i++) | |
497 { | |
498 WORD len = 64; | |
499 memset(buffer, 0, sizeof(buffer)); | |
500 patptr[i] = ofs / 16; | |
501 if (Patterns[i]) | |
502 { | |
503 len = 2; | |
504 MODCOMMAND *p = Patterns[i]; | |
505 for (int row=0; row<64; row++) if (row < PatternSize[i]) | |
506 { | |
507 for (UINT j=0; j<m_nChannels; j++) | |
508 { | |
509 UINT b = j; | |
510 MODCOMMAND *m = &p[row*m_nChannels+j]; | |
511 UINT note = m->note; | |
512 UINT volcmd = m->volcmd; | |
513 UINT vol = m->vol; | |
514 UINT command = m->command; | |
515 UINT param = m->param; | |
516 | |
517 if ((note) || (m->instr)) b |= 0x20; | |
518 if (!note) note = 0xFF; else | |
519 if (note >= 0xFE) note = 0xFE; else | |
520 if (note < 13) note = 0; else note -= 13; | |
521 if (note < 0xFE) note = (note % 12) + ((note / 12) << 4); | |
522 if (command == CMD_VOLUME) | |
523 { | |
524 command = 0; | |
525 if (param > 64) param = 64; | |
526 volcmd = VOLCMD_VOLUME; | |
527 vol = param; | |
528 } | |
529 if (volcmd == VOLCMD_VOLUME) b |= 0x40; else | |
530 if (volcmd == VOLCMD_PANNING) { vol |= 0x80; b |= 0x40; } | |
531 if (command) | |
532 { | |
533 S3MSaveConvert(&command, ¶m, FALSE); | |
534 if (command) b |= 0x80; | |
535 } | |
536 if (b & 0xE0) | |
537 { | |
538 buffer[len++] = b; | |
539 if (b & 0x20) | |
540 { | |
541 buffer[len++] = note; | |
542 buffer[len++] = m->instr; | |
543 } | |
544 if (b & 0x40) | |
545 { | |
546 buffer[len++] = vol; | |
547 } | |
548 if (b & 0x80) | |
549 { | |
550 buffer[len++] = command; | |
551 buffer[len++] = param; | |
552 } | |
553 if (len > sizeof(buffer) - 20) break; | |
554 } | |
555 } | |
556 buffer[len++] = 0; | |
557 if (len > sizeof(buffer) - 20) break; | |
558 } | |
559 } | |
560 buffer[0] = (len - 2) & 0xFF; | |
561 buffer[1] = (len - 2) >> 8; | |
562 len = (len+15) & (~0x0F); | |
563 fwrite(buffer, len, 1, f); | |
564 ofs += len; | |
565 } | |
566 // Writing samples | |
567 for (i=1; i<=nbi; i++) | |
568 { | |
569 MODINSTRUMENT *pins = &Ins[i]; | |
570 if (m_nInstruments) | |
571 { | |
572 pins = Ins; | |
573 if (Headers[i]) | |
574 { | |
575 for (UINT j=0; j<128; j++) | |
576 { | |
577 UINT n = Headers[i]->Keyboard[j]; | |
578 if ((n) && (n < MAX_INSTRUMENTS)) | |
579 { | |
580 pins = &Ins[n]; | |
581 break; | |
582 } | |
583 } | |
584 } | |
585 } | |
586 memcpy(insex[i-1].dosname, pins->name, 12); | |
587 memcpy(insex[i-1].name, m_szNames[i], 28); | |
588 memcpy(insex[i-1].scrs, "SCRS", 4); | |
589 insex[i-1].hmem = (BYTE)((DWORD)ofs >> 20); | |
590 insex[i-1].memseg = (WORD)((DWORD)ofs >> 4); | |
591 if (pins->pSample) | |
592 { | |
593 insex[i-1].type = 1; | |
594 insex[i-1].length = pins->nLength; | |
595 insex[i-1].loopbegin = pins->nLoopStart; | |
596 insex[i-1].loopend = pins->nLoopEnd; | |
597 insex[i-1].vol = pins->nVolume / 4; | |
598 insex[i-1].flags = (pins->uFlags & CHN_LOOP) ? 1 : 0; | |
599 if (pins->nC4Speed) | |
600 insex[i-1].finetune = pins->nC4Speed; | |
601 else | |
602 insex[i-1].finetune = TransposeToFrequency(pins->RelativeTone, pins->nFineTune); | |
603 UINT flags = RS_PCM8U; | |
604 #ifndef NO_PACKING | |
605 if (nPacking) | |
606 { | |
607 if ((!(pins->uFlags & (CHN_16BIT|CHN_STEREO))) | |
608 && (CanPackSample((char *)pins->pSample, pins->nLength, nPacking))) | |
609 { | |
610 insex[i-1].pack = 4; | |
611 flags = RS_ADPCM4; | |
612 } | |
613 } else | |
614 #endif // NO_PACKING | |
615 { | |
616 if (pins->uFlags & CHN_16BIT) | |
617 { | |
618 insex[i-1].flags |= 4; | |
619 flags = RS_PCM16U; | |
620 } | |
621 if (pins->uFlags & CHN_STEREO) | |
622 { | |
623 insex[i-1].flags |= 2; | |
624 flags = (pins->uFlags & CHN_16BIT) ? RS_STPCM16U : RS_STPCM8U; | |
625 } | |
626 } | |
627 DWORD len = WriteSample(f, pins, flags); | |
628 if (len & 0x0F) | |
629 { | |
630 fwrite(S3MFiller, 0x10 - (len & 0x0F), 1, f); | |
631 } | |
632 ofs += (len + 15) & (~0x0F); | |
633 } else | |
634 { | |
635 insex[i-1].length = 0; | |
636 } | |
637 } | |
638 // Updating parapointers | |
639 fseek(f, ofs0, SEEK_SET); | |
640 fwrite(insptr, nbi, 2, f); | |
641 fwrite(patptr, nbp, 2, f); | |
642 fseek(f, ofs1, SEEK_SET); | |
643 fwrite(insex, 0x50, nbi, f); | |
644 fclose(f); | |
645 return TRUE; | |
646 } | |
647 | |
648 #pragma warning(default:4100) | |
649 #endif // MODPLUG_NO_FILESAVE | |
650 |