comparison src/modplug/load_it.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 * Marco Trillo <toad@arsystel.com> (Endian fixes for SaveIT, XM->IT Sample Converter)
7 *
8 */
9
10 #include "stdafx.h"
11 #include "sndfile.h"
12 #include "it_defs.h"
13
14 #ifdef MSC_VER
15 #pragma warning(disable:4244)
16 #endif
17
18 BYTE autovibit2xm[8] =
19 { 0, 3, 1, 4, 2, 0, 0, 0 };
20
21 BYTE autovibxm2it[8] =
22 { 0, 2, 4, 1, 3, 0, 0, 0 };
23
24 //////////////////////////////////////////////////////////
25 // Impulse Tracker IT file support
26
27 // for conversion of XM samples
28 extern WORD XMPeriodTable[96+8];
29 extern UINT XMLinearTable[768];
30
31 static inline UINT ConvertVolParam(UINT value)
32 //--------------------------------------------
33 {
34 return (value > 9) ? 9 : value;
35 }
36
37
38 BOOL CSoundFile::ITInstrToMPT(const void *p, INSTRUMENTHEADER *penv, UINT trkvers)
39 //--------------------------------------------------------------------------------
40 {
41 if (trkvers < 0x0200)
42 {
43 const ITOLDINSTRUMENT *pis = (const ITOLDINSTRUMENT *)p;
44 memcpy(penv->name, pis->name, 26);
45 memcpy(penv->filename, pis->filename, 12);
46 penv->nFadeOut = bswapLE16(pis->fadeout) << 6;
47 penv->nGlobalVol = 64;
48 for (UINT j=0; j<120; j++)
49 {
50 UINT note = pis->keyboard[j*2];
51 UINT ins = pis->keyboard[j*2+1];
52 if (ins < MAX_SAMPLES) penv->Keyboard[j] = ins;
53 if (note < 128) penv->NoteMap[j] = note+1;
54 else if (note >= 0xFE) penv->NoteMap[j] = note;
55 }
56 if (pis->flags & 0x01) penv->dwFlags |= ENV_VOLUME;
57 if (pis->flags & 0x02) penv->dwFlags |= ENV_VOLLOOP;
58 if (pis->flags & 0x04) penv->dwFlags |= ENV_VOLSUSTAIN;
59 penv->nVolLoopStart = pis->vls;
60 penv->nVolLoopEnd = pis->vle;
61 penv->nVolSustainBegin = pis->sls;
62 penv->nVolSustainEnd = pis->sle;
63 penv->nVolEnv = 25;
64 for (UINT ev=0; ev<25; ev++)
65 {
66 if ((penv->VolPoints[ev] = pis->nodes[ev*2]) == 0xFF)
67 {
68 penv->nVolEnv = ev;
69 break;
70 }
71 penv->VolEnv[ev] = pis->nodes[ev*2+1];
72 }
73 penv->nNNA = pis->nna;
74 penv->nDCT = pis->dnc;
75 penv->nPan = 0x80;
76 } else
77 {
78 const ITINSTRUMENT *pis = (const ITINSTRUMENT *)p;
79 memcpy(penv->name, pis->name, 26);
80 memcpy(penv->filename, pis->filename, 12);
81 penv->nMidiProgram = pis->mpr;
82 penv->nMidiChannel = pis->mch;
83 penv->wMidiBank = bswapLE16(pis->mbank);
84 penv->nFadeOut = bswapLE16(pis->fadeout) << 5;
85 penv->nGlobalVol = pis->gbv >> 1;
86 if (penv->nGlobalVol > 64) penv->nGlobalVol = 64;
87 for (UINT j=0; j<120; j++)
88 {
89 UINT note = pis->keyboard[j*2];
90 UINT ins = pis->keyboard[j*2+1];
91 if (ins < MAX_SAMPLES) penv->Keyboard[j] = ins;
92 if (note < 128) penv->NoteMap[j] = note+1;
93 else if (note >= 0xFE) penv->NoteMap[j] = note;
94 }
95 // Volume Envelope
96 if (pis->volenv.flags & 1) penv->dwFlags |= ENV_VOLUME;
97 if (pis->volenv.flags & 2) penv->dwFlags |= ENV_VOLLOOP;
98 if (pis->volenv.flags & 4) penv->dwFlags |= ENV_VOLSUSTAIN;
99 if (pis->volenv.flags & 8) penv->dwFlags |= ENV_VOLCARRY;
100 penv->nVolEnv = pis->volenv.num;
101 if (penv->nVolEnv > 25) penv->nVolEnv = 25;
102
103 penv->nVolLoopStart = pis->volenv.lpb;
104 penv->nVolLoopEnd = pis->volenv.lpe;
105 penv->nVolSustainBegin = pis->volenv.slb;
106 penv->nVolSustainEnd = pis->volenv.sle;
107 // Panning Envelope
108 if (pis->panenv.flags & 1) penv->dwFlags |= ENV_PANNING;
109 if (pis->panenv.flags & 2) penv->dwFlags |= ENV_PANLOOP;
110 if (pis->panenv.flags & 4) penv->dwFlags |= ENV_PANSUSTAIN;
111 if (pis->panenv.flags & 8) penv->dwFlags |= ENV_PANCARRY;
112 penv->nPanEnv = pis->panenv.num;
113 if (penv->nPanEnv > 25) penv->nPanEnv = 25;
114 penv->nPanLoopStart = pis->panenv.lpb;
115 penv->nPanLoopEnd = pis->panenv.lpe;
116 penv->nPanSustainBegin = pis->panenv.slb;
117 penv->nPanSustainEnd = pis->panenv.sle;
118 // Pitch Envelope
119 if (pis->pitchenv.flags & 1) penv->dwFlags |= ENV_PITCH;
120 if (pis->pitchenv.flags & 2) penv->dwFlags |= ENV_PITCHLOOP;
121 if (pis->pitchenv.flags & 4) penv->dwFlags |= ENV_PITCHSUSTAIN;
122 if (pis->pitchenv.flags & 8) penv->dwFlags |= ENV_PITCHCARRY;
123 if (pis->pitchenv.flags & 0x80) penv->dwFlags |= ENV_FILTER;
124 penv->nPitchEnv = pis->pitchenv.num;
125 if (penv->nPitchEnv > 25) penv->nPitchEnv = 25;
126 penv->nPitchLoopStart = pis->pitchenv.lpb;
127 penv->nPitchLoopEnd = pis->pitchenv.lpe;
128 penv->nPitchSustainBegin = pis->pitchenv.slb;
129 penv->nPitchSustainEnd = pis->pitchenv.sle;
130 // Envelopes Data
131 for (UINT ev=0; ev<25; ev++)
132 {
133 penv->VolEnv[ev] = pis->volenv.data[ev*3];
134 penv->VolPoints[ev] = (pis->volenv.data[ev*3+2] << 8) | (pis->volenv.data[ev*3+1]);
135 penv->PanEnv[ev] = pis->panenv.data[ev*3] + 32;
136 penv->PanPoints[ev] = (pis->panenv.data[ev*3+2] << 8) | (pis->panenv.data[ev*3+1]);
137 penv->PitchEnv[ev] = pis->pitchenv.data[ev*3] + 32;
138 penv->PitchPoints[ev] = (pis->pitchenv.data[ev*3+2] << 8) | (pis->pitchenv.data[ev*3+1]);
139 }
140 penv->nNNA = pis->nna;
141 penv->nDCT = pis->dct;
142 penv->nDNA = pis->dca;
143 penv->nPPS = pis->pps;
144 penv->nPPC = pis->ppc;
145 penv->nIFC = pis->ifc;
146 penv->nIFR = pis->ifr;
147 penv->nVolSwing = pis->rv;
148 penv->nPanSwing = pis->rp;
149 penv->nPan = (pis->dfp & 0x7F) << 2;
150 if (penv->nPan > 256) penv->nPan = 128;
151 if (pis->dfp < 0x80) penv->dwFlags |= ENV_SETPANNING;
152 }
153 if ((penv->nVolLoopStart >= 25) || (penv->nVolLoopEnd >= 25)) penv->dwFlags &= ~ENV_VOLLOOP;
154 if ((penv->nVolSustainBegin >= 25) || (penv->nVolSustainEnd >= 25)) penv->dwFlags &= ~ENV_VOLSUSTAIN;
155 return TRUE;
156 }
157
158
159 BOOL CSoundFile::ReadIT(const BYTE *lpStream, DWORD dwMemLength)
160 //--------------------------------------------------------------
161 {
162 ITFILEHEADER pifh = *(ITFILEHEADER *)lpStream;
163 DWORD dwMemPos = sizeof(ITFILEHEADER);
164 DWORD inspos[MAX_INSTRUMENTS];
165 DWORD smppos[MAX_SAMPLES];
166 DWORD patpos[MAX_PATTERNS];
167 BYTE chnmask[64], channels_used[64];
168 MODCOMMAND lastvalue[64];
169
170 pifh.id = bswapLE32(pifh.id);
171 pifh.reserved1 = bswapLE16(pifh.reserved1);
172 pifh.ordnum = bswapLE16(pifh.ordnum);
173 pifh.insnum = bswapLE16(pifh.insnum);
174 pifh.smpnum = bswapLE16(pifh.smpnum);
175 pifh.patnum = bswapLE16(pifh.patnum);
176 pifh.cwtv = bswapLE16(pifh.cwtv);
177 pifh.cmwt = bswapLE16(pifh.cmwt);
178 pifh.flags = bswapLE16(pifh.flags);
179 pifh.special = bswapLE16(pifh.special);
180 pifh.msglength = bswapLE16(pifh.msglength);
181 pifh.msgoffset = bswapLE32(pifh.msgoffset);
182 pifh.reserved2 = bswapLE32(pifh.reserved2);
183
184 if ((!lpStream) || (dwMemLength < 0x100)) return FALSE;
185 if ((pifh.id != 0x4D504D49) || (pifh.insnum >= MAX_INSTRUMENTS)
186 || (!pifh.smpnum) || (pifh.smpnum >= MAX_INSTRUMENTS) || (!pifh.ordnum)) return FALSE;
187 if (dwMemPos + pifh.ordnum + pifh.insnum*4
188 + pifh.smpnum*4 + pifh.patnum*4 > dwMemLength) return FALSE;
189 m_nType = MOD_TYPE_IT;
190 if (pifh.flags & 0x08) m_dwSongFlags |= SONG_LINEARSLIDES;
191 if (pifh.flags & 0x10) m_dwSongFlags |= SONG_ITOLDEFFECTS;
192 if (pifh.flags & 0x20) m_dwSongFlags |= SONG_ITCOMPATMODE;
193 if (pifh.flags & 0x80) m_dwSongFlags |= SONG_EMBEDMIDICFG;
194 if (pifh.flags & 0x1000) m_dwSongFlags |= SONG_EXFILTERRANGE;
195 memcpy(m_szNames[0], pifh.songname, 26);
196 m_szNames[0][26] = 0;
197 // Global Volume
198 if (pifh.globalvol)
199 {
200 m_nDefaultGlobalVolume = pifh.globalvol << 1;
201 if (!m_nDefaultGlobalVolume) m_nDefaultGlobalVolume = 256;
202 if (m_nDefaultGlobalVolume > 256) m_nDefaultGlobalVolume = 256;
203 }
204 if (pifh.speed) m_nDefaultSpeed = pifh.speed;
205 if (pifh.tempo) m_nDefaultTempo = pifh.tempo;
206 m_nSongPreAmp = pifh.mv & 0x7F;
207 // Reading Channels Pan Positions
208 for (int ipan=0; ipan<64; ipan++) if (pifh.chnpan[ipan] != 0xFF)
209 {
210 ChnSettings[ipan].nVolume = pifh.chnvol[ipan];
211 ChnSettings[ipan].nPan = 128;
212 if (pifh.chnpan[ipan] & 0x80) ChnSettings[ipan].dwFlags |= CHN_MUTE;
213 UINT n = pifh.chnpan[ipan] & 0x7F;
214 if (n <= 64) ChnSettings[ipan].nPan = n << 2;
215 if (n == 100) ChnSettings[ipan].dwFlags |= CHN_SURROUND;
216 }
217 if (m_nChannels < 4) m_nChannels = 4;
218 // Reading Song Message
219 if ((pifh.special & 0x01) && (pifh.msglength) && (pifh.msgoffset + pifh.msglength < dwMemLength))
220 {
221 m_lpszSongComments = new char[pifh.msglength+1];
222 if (m_lpszSongComments)
223 {
224 memcpy(m_lpszSongComments, lpStream+pifh.msgoffset, pifh.msglength);
225 m_lpszSongComments[pifh.msglength] = 0;
226 }
227 }
228 // Reading orders
229 UINT nordsize = pifh.ordnum;
230 if (nordsize > MAX_ORDERS) nordsize = MAX_ORDERS;
231 memcpy(Order, lpStream+dwMemPos, nordsize);
232 dwMemPos += pifh.ordnum;
233 // Reading Instrument Offsets
234 memset(inspos, 0, sizeof(inspos));
235 UINT inspossize = pifh.insnum;
236 if (inspossize > MAX_INSTRUMENTS) inspossize = MAX_INSTRUMENTS;
237 inspossize <<= 2;
238 memcpy(inspos, lpStream+dwMemPos, inspossize);
239 for (UINT j=0; j < (inspossize>>2); j++)
240 {
241 inspos[j] = bswapLE32(inspos[j]);
242 }
243 dwMemPos += pifh.insnum * 4;
244 // Reading Samples Offsets
245 memset(smppos, 0, sizeof(smppos));
246 UINT smppossize = pifh.smpnum;
247 if (smppossize > MAX_SAMPLES) smppossize = MAX_SAMPLES;
248 smppossize <<= 2;
249 memcpy(smppos, lpStream+dwMemPos, smppossize);
250 for (UINT j=0; j < (smppossize>>2); j++)
251 {
252 smppos[j] = bswapLE32(smppos[j]);
253 }
254 dwMemPos += pifh.smpnum * 4;
255 // Reading Patterns Offsets
256 memset(patpos, 0, sizeof(patpos));
257 UINT patpossize = pifh.patnum;
258 if (patpossize > MAX_PATTERNS) patpossize = MAX_PATTERNS;
259 patpossize <<= 2;
260 memcpy(patpos, lpStream+dwMemPos, patpossize);
261 for (UINT j=0; j < (patpossize>>2); j++)
262 {
263 patpos[j] = bswapLE32(patpos[j]);
264 }
265 dwMemPos += pifh.patnum * 4;
266 // Reading IT Extra Info
267 if (dwMemPos + 2 < dwMemLength)
268 {
269 UINT nflt = bswapLE16(*((WORD *)(lpStream + dwMemPos)));
270 dwMemPos += 2;
271 if (dwMemPos + nflt * 8 < dwMemLength) dwMemPos += nflt * 8;
272 }
273 // Reading Midi Output & Macros
274 if (m_dwSongFlags & SONG_EMBEDMIDICFG)
275 {
276 if (dwMemPos + sizeof(MODMIDICFG) < dwMemLength)
277 {
278 memcpy(&m_MidiCfg, lpStream+dwMemPos, sizeof(MODMIDICFG));
279 dwMemPos += sizeof(MODMIDICFG);
280 }
281 }
282 // Read pattern names: "PNAM"
283 if ((dwMemPos + 8 < dwMemLength) && (bswapLE32(*((DWORD *)(lpStream+dwMemPos))) == 0x4d414e50))
284 {
285 UINT len = bswapLE32(*((DWORD *)(lpStream+dwMemPos+4)));
286 dwMemPos += 8;
287 if ((dwMemPos + len <= dwMemLength) && (len <= MAX_PATTERNS*MAX_PATTERNNAME) && (len >= MAX_PATTERNNAME))
288 {
289 m_lpszPatternNames = new char[len];
290 if (m_lpszPatternNames)
291 {
292 m_nPatternNames = len / MAX_PATTERNNAME;
293 memcpy(m_lpszPatternNames, lpStream+dwMemPos, len);
294 }
295 dwMemPos += len;
296 }
297 }
298 // 4-channels minimum
299 m_nChannels = 4;
300 // Read channel names: "CNAM"
301 if ((dwMemPos + 8 < dwMemLength) && (bswapLE32(*((DWORD *)(lpStream+dwMemPos))) == 0x4d414e43))
302 {
303 UINT len = bswapLE32(*((DWORD *)(lpStream+dwMemPos+4)));
304 dwMemPos += 8;
305 if ((dwMemPos + len <= dwMemLength) && (len <= 64*MAX_CHANNELNAME))
306 {
307 UINT n = len / MAX_CHANNELNAME;
308 if (n > m_nChannels) m_nChannels = n;
309 for (UINT i=0; i<n; i++)
310 {
311 memcpy(ChnSettings[i].szName, (lpStream+dwMemPos+i*MAX_CHANNELNAME), MAX_CHANNELNAME);
312 ChnSettings[i].szName[MAX_CHANNELNAME-1] = 0;
313 }
314 dwMemPos += len;
315 }
316 }
317 // Read mix plugins information
318 if (dwMemPos + 8 < dwMemLength)
319 {
320 dwMemPos += LoadMixPlugins(lpStream+dwMemPos, dwMemLength-dwMemPos);
321 }
322 // Checking for unused channels
323 UINT npatterns = pifh.patnum;
324 if (npatterns > MAX_PATTERNS) npatterns = MAX_PATTERNS;
325 for (UINT patchk=0; patchk<npatterns; patchk++)
326 {
327 memset(chnmask, 0, sizeof(chnmask));
328 if ((!patpos[patchk]) || ((DWORD)patpos[patchk] + 4 >= dwMemLength)) continue;
329 UINT len = bswapLE16(*((WORD *)(lpStream+patpos[patchk])));
330 UINT rows = bswapLE16(*((WORD *)(lpStream+patpos[patchk]+2)));
331 if ((rows < 4) || (rows > 256)) continue;
332 if (patpos[patchk]+8+len > dwMemLength) continue;
333 UINT i = 0;
334 const BYTE *p = lpStream+patpos[patchk]+8;
335 UINT nrow = 0;
336 while (nrow<rows)
337 {
338 if (i >= len) break;
339 BYTE b = p[i++];
340 if (!b)
341 {
342 nrow++;
343 continue;
344 }
345 UINT ch = b & 0x7F;
346 if (ch) ch = (ch - 1) & 0x3F;
347 if (b & 0x80)
348 {
349 if (i >= len) break;
350 chnmask[ch] = p[i++];
351 }
352 // Channel used
353 if (chnmask[ch] & 0x0F)
354 {
355 if ((ch >= m_nChannels) && (ch < 64)) m_nChannels = ch+1;
356 }
357 // Note
358 if (chnmask[ch] & 1) i++;
359 // Instrument
360 if (chnmask[ch] & 2) i++;
361 // Volume
362 if (chnmask[ch] & 4) i++;
363 // Effect
364 if (chnmask[ch] & 8) i += 2;
365 if (i >= len) break;
366 }
367 }
368 // Reading Instruments
369 m_nInstruments = 0;
370 if (pifh.flags & 0x04) m_nInstruments = pifh.insnum;
371 if (m_nInstruments >= MAX_INSTRUMENTS) m_nInstruments = MAX_INSTRUMENTS-1;
372 for (UINT nins=0; nins<m_nInstruments; nins++)
373 {
374 if ((inspos[nins] > 0) && (inspos[nins] < dwMemLength - sizeof(ITOLDINSTRUMENT)))
375 {
376 INSTRUMENTHEADER *penv = new INSTRUMENTHEADER;
377 if (!penv) continue;
378 Headers[nins+1] = penv;
379 memset(penv, 0, sizeof(INSTRUMENTHEADER));
380 ITInstrToMPT(lpStream + inspos[nins], penv, pifh.cmwt);
381 }
382 }
383 // Reading Samples
384 m_nSamples = pifh.smpnum;
385 if (m_nSamples >= MAX_SAMPLES) m_nSamples = MAX_SAMPLES-1;
386 for (UINT nsmp=0; nsmp<pifh.smpnum; nsmp++) if ((smppos[nsmp]) && (smppos[nsmp] + sizeof(ITSAMPLESTRUCT) <= dwMemLength))
387 {
388 ITSAMPLESTRUCT pis = *(ITSAMPLESTRUCT *)(lpStream+smppos[nsmp]);
389 pis.id = bswapLE32(pis.id);
390 pis.length = bswapLE32(pis.length);
391 pis.loopbegin = bswapLE32(pis.loopbegin);
392 pis.loopend = bswapLE32(pis.loopend);
393 pis.C5Speed = bswapLE32(pis.C5Speed);
394 pis.susloopbegin = bswapLE32(pis.susloopbegin);
395 pis.susloopend = bswapLE32(pis.susloopend);
396 pis.samplepointer = bswapLE32(pis.samplepointer);
397
398 if (pis.id == 0x53504D49)
399 {
400 MODINSTRUMENT *pins = &Ins[nsmp+1];
401 memcpy(pins->name, pis.filename, 12);
402 pins->uFlags = 0;
403 pins->nLength = 0;
404 pins->nLoopStart = pis.loopbegin;
405 pins->nLoopEnd = pis.loopend;
406 pins->nSustainStart = pis.susloopbegin;
407 pins->nSustainEnd = pis.susloopend;
408 pins->nC4Speed = pis.C5Speed;
409 if (!pins->nC4Speed) pins->nC4Speed = 8363;
410 if (pis.C5Speed < 256) pins->nC4Speed = 256;
411 pins->nVolume = pis.vol << 2;
412 if (pins->nVolume > 256) pins->nVolume = 256;
413 pins->nGlobalVol = pis.gvl;
414 if (pins->nGlobalVol > 64) pins->nGlobalVol = 64;
415 if (pis.flags & 0x10) pins->uFlags |= CHN_LOOP;
416 if (pis.flags & 0x20) pins->uFlags |= CHN_SUSTAINLOOP;
417 if (pis.flags & 0x40) pins->uFlags |= CHN_PINGPONGLOOP;
418 if (pis.flags & 0x80) pins->uFlags |= CHN_PINGPONGSUSTAIN;
419 pins->nPan = (pis.dfp & 0x7F) << 2;
420 if (pins->nPan > 256) pins->nPan = 256;
421 if (pis.dfp & 0x80) pins->uFlags |= CHN_PANNING;
422 pins->nVibType = autovibit2xm[pis.vit & 7];
423 pins->nVibRate = pis.vis;
424 pins->nVibDepth = pis.vid & 0x7F;
425 pins->nVibSweep = (pis.vir + 3) / 4;
426 if ((pis.samplepointer) && (pis.samplepointer < dwMemLength) && (pis.length))
427 {
428 pins->nLength = pis.length;
429 if (pins->nLength > MAX_SAMPLE_LENGTH) pins->nLength = MAX_SAMPLE_LENGTH;
430 UINT flags = (pis.cvt & 1) ? RS_PCM8S : RS_PCM8U;
431 if (pis.flags & 2)
432 {
433 flags += 5;
434 if (pis.flags & 4) flags |= RSF_STEREO;
435 pins->uFlags |= CHN_16BIT;
436 // IT 2.14 16-bit packed sample ?
437 if (pis.flags & 8) flags = ((pifh.cmwt >= 0x215) && (pis.cvt & 4)) ? RS_IT21516 : RS_IT21416;
438 } else
439 {
440 if (pis.flags & 4) flags |= RSF_STEREO;
441 if (pis.cvt == 0xFF) flags = RS_ADPCM4; else
442 // IT 2.14 8-bit packed sample ?
443 if (pis.flags & 8) flags = ((pifh.cmwt >= 0x215) && (pis.cvt & 4)) ? RS_IT2158 : RS_IT2148;
444 }
445 ReadSample(&Ins[nsmp+1], flags, (LPSTR)(lpStream+pis.samplepointer), dwMemLength - pis.samplepointer);
446 }
447 }
448 memcpy(m_szNames[nsmp+1], pis.name, 26);
449 }
450 // Reading Patterns
451 for (UINT npat=0; npat<npatterns; npat++)
452 {
453 if ((!patpos[npat]) || ((DWORD)patpos[npat] + 4 >= dwMemLength))
454 {
455 PatternSize[npat] = 64;
456 Patterns[npat] = AllocatePattern(64, m_nChannels);
457 continue;
458 }
459
460 UINT len = bswapLE16(*((WORD *)(lpStream+patpos[npat])));
461 UINT rows = bswapLE16(*((WORD *)(lpStream+patpos[npat]+2)));
462 if ((rows < 4) || (rows > 256)) continue;
463 if (patpos[npat]+8+len > dwMemLength) continue;
464 PatternSize[npat] = rows;
465 if ((Patterns[npat] = AllocatePattern(rows, m_nChannels)) == NULL) continue;
466 memset(lastvalue, 0, sizeof(lastvalue));
467 memset(chnmask, 0, sizeof(chnmask));
468 MODCOMMAND *m = Patterns[npat];
469 UINT i = 0;
470 const BYTE *p = lpStream+patpos[npat]+8;
471 UINT nrow = 0;
472 while (nrow<rows)
473 {
474 if (i >= len) break;
475 BYTE b = p[i++];
476 if (!b)
477 {
478 nrow++;
479 m+=m_nChannels;
480 continue;
481 }
482 UINT ch = b & 0x7F;
483 if (ch) ch = (ch - 1) & 0x3F;
484 if (b & 0x80)
485 {
486 if (i >= len) break;
487 chnmask[ch] = p[i++];
488 }
489 if ((chnmask[ch] & 0x10) && (ch < m_nChannels))
490 {
491 m[ch].note = lastvalue[ch].note;
492 }
493 if ((chnmask[ch] & 0x20) && (ch < m_nChannels))
494 {
495 m[ch].instr = lastvalue[ch].instr;
496 }
497 if ((chnmask[ch] & 0x40) && (ch < m_nChannels))
498 {
499 m[ch].volcmd = lastvalue[ch].volcmd;
500 m[ch].vol = lastvalue[ch].vol;
501 }
502 if ((chnmask[ch] & 0x80) && (ch < m_nChannels))
503 {
504 m[ch].command = lastvalue[ch].command;
505 m[ch].param = lastvalue[ch].param;
506 }
507 if (chnmask[ch] & 1) // Note
508 {
509 if (i >= len) break;
510 UINT note = p[i++];
511 if (ch < m_nChannels)
512 {
513 if (note < 0x80) note++;
514 m[ch].note = note;
515 lastvalue[ch].note = note;
516 channels_used[ch] = TRUE;
517 }
518 }
519 if (chnmask[ch] & 2)
520 {
521 if (i >= len) break;
522 UINT instr = p[i++];
523 if (ch < m_nChannels)
524 {
525 m[ch].instr = instr;
526 lastvalue[ch].instr = instr;
527 }
528 }
529 if (chnmask[ch] & 4)
530 {
531 if (i >= len) break;
532 UINT vol = p[i++];
533 if (ch < m_nChannels)
534 {
535 // 0-64: Set Volume
536 if (vol <= 64) { m[ch].volcmd = VOLCMD_VOLUME; m[ch].vol = vol; } else
537 // 128-192: Set Panning
538 if ((vol >= 128) && (vol <= 192)) { m[ch].volcmd = VOLCMD_PANNING; m[ch].vol = vol - 128; } else
539 // 65-74: Fine Volume Up
540 if (vol < 75) { m[ch].volcmd = VOLCMD_FINEVOLUP; m[ch].vol = vol - 65; } else
541 // 75-84: Fine Volume Down
542 if (vol < 85) { m[ch].volcmd = VOLCMD_FINEVOLDOWN; m[ch].vol = vol - 75; } else
543 // 85-94: Volume Slide Up
544 if (vol < 95) { m[ch].volcmd = VOLCMD_VOLSLIDEUP; m[ch].vol = vol - 85; } else
545 // 95-104: Volume Slide Down
546 if (vol < 105) { m[ch].volcmd = VOLCMD_VOLSLIDEDOWN; m[ch].vol = vol - 95; } else
547 // 105-114: Pitch Slide Up
548 if (vol < 115) { m[ch].volcmd = VOLCMD_PORTADOWN; m[ch].vol = vol - 105; } else
549 // 115-124: Pitch Slide Down
550 if (vol < 125) { m[ch].volcmd = VOLCMD_PORTAUP; m[ch].vol = vol - 115; } else
551 // 193-202: Portamento To
552 if ((vol >= 193) && (vol <= 202)) { m[ch].volcmd = VOLCMD_TONEPORTAMENTO; m[ch].vol = vol - 193; } else
553 // 203-212: Vibrato
554 if ((vol >= 203) && (vol <= 212)) { m[ch].volcmd = VOLCMD_VIBRATOSPEED; m[ch].vol = vol - 203; }
555 lastvalue[ch].volcmd = m[ch].volcmd;
556 lastvalue[ch].vol = m[ch].vol;
557 }
558 }
559 // Reading command/param
560 if (chnmask[ch] & 8)
561 {
562 if (i > len - 2) break;
563 UINT cmd = p[i++];
564 UINT param = p[i++];
565 if (ch < m_nChannels)
566 {
567 if (cmd)
568 {
569 m[ch].command = cmd;
570 m[ch].param = param;
571 S3MConvert(&m[ch], TRUE);
572 lastvalue[ch].command = m[ch].command;
573 lastvalue[ch].param = m[ch].param;
574 }
575 }
576 }
577 }
578 }
579 for (UINT ncu=0; ncu<MAX_BASECHANNELS; ncu++)
580 {
581 if (ncu>=m_nChannels)
582 {
583 ChnSettings[ncu].nVolume = 64;
584 ChnSettings[ncu].dwFlags &= ~CHN_MUTE;
585 }
586 }
587 m_nMinPeriod = 8;
588 m_nMaxPeriod = 0xF000;
589 return TRUE;
590 }
591
592
593 #ifndef MODPLUG_NO_FILESAVE
594 //#define SAVEITTIMESTAMP
595 #pragma warning(disable:4100)
596
597 BOOL CSoundFile::SaveIT(LPCSTR lpszFileName, UINT nPacking)
598 //---------------------------------------------------------
599 {
600 DWORD dwPatNamLen, dwChnNamLen;
601 ITFILEHEADER header, writeheader;
602 ITINSTRUMENT iti, writeiti;
603 ITSAMPLESTRUCT itss;
604 BYTE smpcount[MAX_SAMPLES];
605 DWORD inspos[MAX_INSTRUMENTS];
606 DWORD patpos[MAX_PATTERNS];
607 DWORD smppos[MAX_SAMPLES];
608 DWORD dwPos = 0, dwHdrPos = 0, dwExtra = 2;
609 WORD patinfo[4];
610 BYTE chnmask[64];
611 BYTE buf[512];
612 MODCOMMAND lastvalue[64];
613 FILE *f;
614
615
616 if ((!lpszFileName) || ((f = fopen(lpszFileName, "wb")) == NULL)) return FALSE;
617 memset(inspos, 0, sizeof(inspos));
618 memset(patpos, 0, sizeof(patpos));
619 memset(smppos, 0, sizeof(smppos));
620 // Writing Header
621 memset(&header, 0, sizeof(header));
622 dwPatNamLen = 0;
623 dwChnNamLen = 0;
624 header.id = 0x4D504D49; // IMPM
625 lstrcpyn((char *)header.songname, m_szNames[0], 27);
626 header.reserved1 = 0x1004;
627 header.ordnum = 0;
628 while ((header.ordnum < MAX_ORDERS) && (Order[header.ordnum] < 0xFF)) header.ordnum++;
629 if (header.ordnum < MAX_ORDERS) Order[header.ordnum++] = 0xFF;
630 header.insnum = m_nInstruments;
631 header.smpnum = m_nSamples;
632 header.patnum = MAX_PATTERNS;
633 while ((header.patnum > 0) && (!Patterns[header.patnum-1])) header.patnum--;
634 header.cwtv = 0x217;
635 header.cmwt = 0x200;
636 header.flags = 0x0001;
637 header.special = 0x0006;
638 if (m_nInstruments) header.flags |= 0x04;
639 if (m_dwSongFlags & SONG_LINEARSLIDES) header.flags |= 0x08;
640 if (m_dwSongFlags & SONG_ITOLDEFFECTS) header.flags |= 0x10;
641 if (m_dwSongFlags & SONG_ITCOMPATMODE) header.flags |= 0x20;
642 if (m_dwSongFlags & SONG_EXFILTERRANGE) header.flags |= 0x1000;
643 header.globalvol = m_nDefaultGlobalVolume >> 1;
644 header.mv = m_nSongPreAmp;
645 // clip song pre-amp values (between 0x20 and 0x7f)
646 if (header.mv < 0x20) header.mv = 0x20;
647 if (header.mv > 0x7F) header.mv = 0x7F;
648 header.speed = m_nDefaultSpeed;
649 header.tempo = m_nDefaultTempo;
650 header.sep = m_nStereoSeparation;
651 dwHdrPos = sizeof(header) + header.ordnum;
652 // Channel Pan and Volume
653 memset(header.chnpan, 0xFF, 64);
654 memset(header.chnvol, 64, 64);
655 for (UINT ich=0; ich<m_nChannels; ich++)
656 {
657 header.chnpan[ich] = ChnSettings[ich].nPan >> 2;
658 if (ChnSettings[ich].dwFlags & CHN_SURROUND) header.chnpan[ich] = 100;
659 header.chnvol[ich] = ChnSettings[ich].nVolume;
660 if (ChnSettings[ich].dwFlags & CHN_MUTE) header.chnpan[ich] |= 0x80;
661 if (ChnSettings[ich].szName[0])
662 {
663 dwChnNamLen = (ich+1) * MAX_CHANNELNAME;
664 }
665 }
666 if (dwChnNamLen) dwExtra += dwChnNamLen + 8;
667 #ifdef SAVEITTIMESTAMP
668 dwExtra += 8; // Time Stamp
669 #endif
670 if (m_dwSongFlags & SONG_EMBEDMIDICFG)
671 {
672 header.flags |= 0x80;
673 header.special |= 0x08;
674 dwExtra += sizeof(MODMIDICFG);
675 }
676 // Pattern Names
677 if ((m_nPatternNames) && (m_lpszPatternNames))
678 {
679 dwPatNamLen = m_nPatternNames * MAX_PATTERNNAME;
680 while ((dwPatNamLen >= MAX_PATTERNNAME) && (!m_lpszPatternNames[dwPatNamLen-MAX_PATTERNNAME])) dwPatNamLen -= MAX_PATTERNNAME;
681 if (dwPatNamLen < MAX_PATTERNNAME) dwPatNamLen = 0;
682 if (dwPatNamLen) dwExtra += dwPatNamLen + 8;
683 }
684 // Mix Plugins
685 dwExtra += SaveMixPlugins(NULL, TRUE);
686 // Comments
687 if (m_lpszSongComments)
688 {
689 header.special |= 1;
690 header.msglength = strlen(m_lpszSongComments)+1;
691 header.msgoffset = dwHdrPos + dwExtra + header.insnum*4 + header.patnum*4 + header.smpnum*4;
692 }
693 // Write file header
694 memcpy(writeheader, header, sizeof(header));
695
696 // Byteswap header information
697 writeheader.id = bswapLE32(writeheader.id);
698 writeheader.reserved1 = bswapLE16(writeheader.reserved1);
699 writeheader.ordnum = bswapLE16(writeheader.ordnum);
700 writeheader.insnum = bswapLE16(writeheader.insnum);
701 writeheader.smpnum = bswapLE16(writeheader.smpnum);
702 writeheader.patnum = bswapLE16(writeheader.patnum);
703 writeheader.cwtv = bswapLE16(writeheader.cwtv);
704 writeheader.cmwt = bswapLE16(writeheader.cmwt);
705 writeheader.flags = bswapLE16(writeheader.flags);
706 writeheader.special = bswapLE16(writeheader.special);
707 writeheader.msglength = bswapLE16(writeheader.msglength);
708 writeheader.msgoffset = bswapLE32(writeheader.msgoffset);
709 writeheader.reserved2 = bswapLE32(writeheader.reserved2);
710
711 fwrite(&writeheader, 1, sizeof(writeheader), f);
712
713 fwrite(Order, 1, header.ordnum, f);
714 if (header.insnum) fwrite(inspos, 4, header.insnum, f);
715 if (header.smpnum) fwrite(smppos, 4, header.smpnum, f);
716 if (header.patnum) fwrite(patpos, 4, header.patnum, f);
717 // Writing editor history information
718 {
719 #ifdef SAVEITTIMESTAMP
720 SYSTEMTIME systime;
721 FILETIME filetime;
722 WORD timestamp[4];
723 WORD nInfoEx = 1;
724 memset(timestamp, 0, sizeof(timestamp));
725 fwrite(&nInfoEx, 1, 2, f);
726 GetSystemTime(&systime);
727 SystemTimeToFileTime(&systime, &filetime);
728 FileTimeToDosDateTime(&filetime, &timestamp[0], &timestamp[1]);
729 fwrite(timestamp, 1, 8, f);
730 #else
731 WORD nInfoEx = 0;
732 fwrite(&nInfoEx, 1, 2, f);
733 #endif
734 }
735 // Writing midi cfg
736 if (header.flags & 0x80)
737 {
738 fwrite(&m_MidiCfg, 1, sizeof(MODMIDICFG), f);
739 }
740 // Writing pattern names
741 if (dwPatNamLen)
742 {
743 DWORD d = bswapLE32(0x4d414e50);
744 UINT len= bswapLE32(dwPatNamLen);
745 fwrite(&d, 1, 4, f);
746 write(&len, 1, 4, f);
747 fwrite(m_lpszPatternNames, 1, dwPatNamLen, f);
748 }
749 // Writing channel Names
750 if (dwChnNamLen)
751 {
752 DWORD d = bswapLE32(0x4d414e43);
753 UINT len= bswapLE32(dwChnNamLen);
754 fwrite(&d, 1, 4, f);
755 fwrite(&len, 1, 4, f);
756 UINT nChnNames = dwChnNamLen / MAX_CHANNELNAME;
757 for (UINT inam=0; inam<nChnNames; inam++)
758 {
759 fwrite(ChnSettings[inam].szName, 1, MAX_CHANNELNAME, f);
760 }
761 }
762 // Writing mix plugins info
763 SaveMixPlugins(f, FALSE);
764 // Writing song message
765 dwPos = dwHdrPos + dwExtra + (header.insnum + header.smpnum + header.patnum) * 4;
766 if (header.special & 1)
767 {
768 dwPos += strlen(m_lpszSongComments) + 1;
769 fwrite(m_lpszSongComments, 1, strlen(m_lpszSongComments)+1, f);
770 }
771 // Writing instruments
772 for (UINT nins=1; nins<=header.insnum; nins++)
773 {
774 memset(&iti, 0, sizeof(iti));
775 iti.id = 0x49504D49; // "IMPI"
776 iti.trkvers = 0x211;
777 if (Headers[nins])
778 {
779 INSTRUMENTHEADER *penv = Headers[nins];
780 memset(smpcount, 0, sizeof(smpcount));
781 memcpy(iti.filename, penv->filename, 12);
782 memcpy(iti.name, penv->name, 26);
783 iti.mbank = penv->wMidiBank;
784 iti.mpr = penv->nMidiProgram;
785 iti.mch = penv->nMidiChannel;
786 iti.nna = penv->nNNA;
787 iti.dct = penv->nDCT;
788 iti.dca = penv->nDNA;
789 iti.fadeout = penv->nFadeOut >> 5;
790 iti.pps = penv->nPPS;
791 iti.ppc = penv->nPPC;
792 iti.gbv = (BYTE)(penv->nGlobalVol << 1);
793 iti.dfp = (BYTE)penv->nPan >> 2;
794 if (!(penv->dwFlags & ENV_SETPANNING)) iti.dfp |= 0x80;
795 iti.rv = penv->nVolSwing;
796 iti.rp = penv->nPanSwing;
797 iti.ifc = penv->nIFC;
798 iti.ifr = penv->nIFR;
799 iti.nos = 0;
800 for (UINT i=0; i<120; i++) if (penv->Keyboard[i] < MAX_SAMPLES)
801 {
802 UINT smp = penv->Keyboard[i];
803 if ((smp) && (!smpcount[smp]))
804 {
805 smpcount[smp] = 1;
806 iti.nos++;
807 }
808 iti.keyboard[i*2] = penv->NoteMap[i] - 1;
809 iti.keyboard[i*2+1] = smp;
810 }
811 // Writing Volume envelope
812 if (penv->dwFlags & ENV_VOLUME) iti.volenv.flags |= 0x01;
813 if (penv->dwFlags & ENV_VOLLOOP) iti.volenv.flags |= 0x02;
814 if (penv->dwFlags & ENV_VOLSUSTAIN) iti.volenv.flags |= 0x04;
815 if (penv->dwFlags & ENV_VOLCARRY) iti.volenv.flags |= 0x08;
816 iti.volenv.num = (BYTE)penv->nVolEnv;
817 iti.volenv.lpb = (BYTE)penv->nVolLoopStart;
818 iti.volenv.lpe = (BYTE)penv->nVolLoopEnd;
819 iti.volenv.slb = penv->nVolSustainBegin;
820 iti.volenv.sle = penv->nVolSustainEnd;
821 // Writing Panning envelope
822 if (penv->dwFlags & ENV_PANNING) iti.panenv.flags |= 0x01;
823 if (penv->dwFlags & ENV_PANLOOP) iti.panenv.flags |= 0x02;
824 if (penv->dwFlags & ENV_PANSUSTAIN) iti.panenv.flags |= 0x04;
825 if (penv->dwFlags & ENV_PANCARRY) iti.panenv.flags |= 0x08;
826 iti.panenv.num = (BYTE)penv->nPanEnv;
827 iti.panenv.lpb = (BYTE)penv->nPanLoopStart;
828 iti.panenv.lpe = (BYTE)penv->nPanLoopEnd;
829 iti.panenv.slb = penv->nPanSustainBegin;
830 iti.panenv.sle = penv->nPanSustainEnd;
831 // Writing Pitch Envelope
832 if (penv->dwFlags & ENV_PITCH) iti.pitchenv.flags |= 0x01;
833 if (penv->dwFlags & ENV_PITCHLOOP) iti.pitchenv.flags |= 0x02;
834 if (penv->dwFlags & ENV_PITCHSUSTAIN) iti.pitchenv.flags |= 0x04;
835 if (penv->dwFlags & ENV_PITCHCARRY) iti.pitchenv.flags |= 0x08;
836 if (penv->dwFlags & ENV_FILTER) iti.pitchenv.flags |= 0x80;
837 iti.pitchenv.num = (BYTE)penv->nPitchEnv;
838 iti.pitchenv.lpb = (BYTE)penv->nPitchLoopStart;
839 iti.pitchenv.lpe = (BYTE)penv->nPitchLoopEnd;
840 iti.pitchenv.slb = (BYTE)penv->nPitchSustainBegin;
841 iti.pitchenv.sle = (BYTE)penv->nPitchSustainEnd;
842 // Writing Envelopes data
843 for (UINT ev=0; ev<25; ev++)
844 {
845 iti.volenv.data[ev*3] = penv->VolEnv[ev];
846 iti.volenv.data[ev*3+1] = penv->VolPoints[ev] & 0xFF;
847 iti.volenv.data[ev*3+2] = penv->VolPoints[ev] >> 8;
848 iti.panenv.data[ev*3] = penv->PanEnv[ev] - 32;
849 iti.panenv.data[ev*3+1] = penv->PanPoints[ev] & 0xFF;
850 iti.panenv.data[ev*3+2] = penv->PanPoints[ev] >> 8;
851 iti.pitchenv.data[ev*3] = penv->PitchEnv[ev] - 32;
852 iti.pitchenv.data[ev*3+1] = penv->PitchPoints[ev] & 0xFF;
853 iti.pitchenv.data[ev*3+2] = penv->PitchPoints[ev] >> 8;
854 }
855 } else
856 // Save Empty Instrument
857 {
858 for (UINT i=0; i<120; i++) iti.keyboard[i*2] = i;
859 iti.ppc = 5*12;
860 iti.gbv = 128;
861 iti.dfp = 0x20;
862 iti.ifc = 0xFF;
863 }
864 if (!iti.nos) iti.trkvers = 0;
865 // Writing instrument
866 inspos[nins-1] = dwPos;
867 dwPos += sizeof(ITINSTRUMENT);
868
869 memcpy(&writeiti, &iti, sizeof(ITINSTRUMENT));
870
871 writeiti.fadeout = bswapLE16(writeiti.fadeout);
872 writeiti.id = bswapLE32(writeiti.id);
873 writeiti.trkvers = bswapLE16(writeiti.trkvers);
874 writeiti.mbank = bswapLE16(writeiti.mbank);
875
876 fwrite(&writeiti, 1, sizeof(ITINSTRUMENT), f);
877 }
878 // Writing sample headers
879 memset(&itss, 0, sizeof(itss));
880 for (UINT hsmp=0; hsmp<header.smpnum; hsmp++)
881 {
882 smppos[hsmp] = dwPos;
883 dwPos += sizeof(ITSAMPLESTRUCT);
884 fwrite(&itss, 1, sizeof(ITSAMPLESTRUCT), f);
885 }
886 // Writing Patterns
887 for (UINT npat=0; npat<header.patnum; npat++)
888 {
889 DWORD dwPatPos = dwPos;
890 UINT len;
891 if (!Patterns[npat]) continue;
892 patpos[npat] = dwPos;
893 patinfo[0] = 0;
894 patinfo[1] = bswapLE16(PatternSize[npat]);
895 patinfo[2] = 0;
896 patinfo[3] = 0;
897 // Check for empty pattern
898 if (PatternSize[npat] == 64)
899 {
900 MODCOMMAND *pzc = Patterns[npat];
901 UINT nz = PatternSize[npat] * m_nChannels;
902 for (UINT iz=0; iz<nz; iz++)
903 {
904 if ((pzc[iz].note) || (pzc[iz].instr)
905 || (pzc[iz].volcmd) || (pzc[iz].command)) break;
906 }
907 if (iz == nz)
908 {
909 patpos[npat] = 0;
910 continue;
911 }
912 }
913 fwrite(patinfo, 8, 1, f);
914 dwPos += 8;
915 memset(chnmask, 0xFF, sizeof(chnmask));
916 memset(lastvalue, 0, sizeof(lastvalue));
917 MODCOMMAND *m = Patterns[npat];
918 for (UINT row=0; row<PatternSize[npat]; row++)
919 {
920 len = 0;
921 for (UINT ch=0; ch<m_nChannels; ch++, m++)
922 {
923 BYTE b = 0;
924 UINT command = m->command;
925 UINT param = m->param;
926 UINT vol = 0xFF;
927 UINT note = m->note;
928 if (note) b |= 1;
929 if ((note) && (note < 0x80)) note--; // 0xfe->0x80 --Toad
930 if (m->instr) b |= 2;
931 if (m->volcmd)
932 {
933 UINT volcmd = m->volcmd;
934 switch(volcmd)
935 {
936 case VOLCMD_VOLUME: vol = m->vol; if (vol > 64) vol = 64; break;
937 case VOLCMD_PANNING: vol = m->vol + 128; if (vol > 192) vol = 192; break;
938 case VOLCMD_VOLSLIDEUP: vol = 85 + ConvertVolParam(m->vol); break;
939 case VOLCMD_VOLSLIDEDOWN: vol = 95 + ConvertVolParam(m->vol); break;
940 case VOLCMD_FINEVOLUP: vol = 65 + ConvertVolParam(m->vol); break;
941 case VOLCMD_FINEVOLDOWN: vol = 75 + ConvertVolParam(m->vol); break;
942 case VOLCMD_VIBRATOSPEED: vol = 203 + ConvertVolParam(m->vol); break;
943 case VOLCMD_VIBRATO: vol = 203; break;
944 case VOLCMD_TONEPORTAMENTO: vol = 193 + ConvertVolParam(m->vol); break;
945 case VOLCMD_PORTADOWN: vol = 105 + ConvertVolParam(m->vol); break;
946 case VOLCMD_PORTAUP: vol = 115 + ConvertVolParam(m->vol); break;
947 default: vol = 0xFF;
948 }
949 }
950 if (vol != 0xFF) b |= 4;
951 if (command)
952 {
953 S3MSaveConvert(&command, &param, TRUE);
954 if (command) b |= 8;
955 }
956 // Packing information
957 if (b)
958 {
959 // Same note ?
960 if (b & 1)
961 {
962 if ((note == lastvalue[ch].note) && (lastvalue[ch].volcmd & 1))
963 {
964 b &= ~1;
965 b |= 0x10;
966 } else
967 {
968 lastvalue[ch].note = note;
969 lastvalue[ch].volcmd |= 1;
970 }
971 }
972 // Same instrument ?
973 if (b & 2)
974 {
975 if ((m->instr == lastvalue[ch].instr) && (lastvalue[ch].volcmd & 2))
976 {
977 b &= ~2;
978 b |= 0x20;
979 } else
980 {
981 lastvalue[ch].instr = m->instr;
982 lastvalue[ch].volcmd |= 2;
983 }
984 }
985 // Same volume column byte ?
986 if (b & 4)
987 {
988 if ((vol == lastvalue[ch].vol) && (lastvalue[ch].volcmd & 4))
989 {
990 b &= ~4;
991 b |= 0x40;
992 } else
993 {
994 lastvalue[ch].vol = vol;
995 lastvalue[ch].volcmd |= 4;
996 }
997 }
998 // Same command / param ?
999 if (b & 8)
1000 {
1001 if ((command == lastvalue[ch].command) && (param == lastvalue[ch].param) && (lastvalue[ch].volcmd & 8))
1002 {
1003 b &= ~8;
1004 b |= 0x80;
1005 } else
1006 {
1007 lastvalue[ch].command = command;
1008 lastvalue[ch].param = param;
1009 lastvalue[ch].volcmd |= 8;
1010 }
1011 }
1012 if (b != chnmask[ch])
1013 {
1014 chnmask[ch] = b;
1015 buf[len++] = (ch+1) | 0x80;
1016 buf[len++] = b;
1017 } else
1018 {
1019 buf[len++] = ch+1;
1020 }
1021 if (b & 1) buf[len++] = note;
1022 if (b & 2) buf[len++] = m->instr;
1023 if (b & 4) buf[len++] = vol;
1024 if (b & 8)
1025 {
1026 buf[len++] = command;
1027 buf[len++] = param;
1028 }
1029 }
1030 }
1031 buf[len++] = 0;
1032 dwPos += len;
1033 patinfo[0] += len;
1034 fwrite(buf, 1, len, f);
1035 }
1036 fseek(f, dwPatPos, SEEK_SET);
1037 patinfo[0] = bswapLE16(patinfo[0]); // byteswap -- Toad
1038 fwrite(patinfo, 8, 1, f);
1039 fseek(f, dwPos, SEEK_SET);
1040 }
1041 // Writing Sample Data
1042 for (UINT nsmp=1; nsmp<=header.smpnum; nsmp++)
1043 {
1044 MODINSTRUMENT *psmp = &Ins[nsmp];
1045 memset(&itss, 0, sizeof(itss));
1046 memcpy(itss.filename, psmp->name, 12);
1047 memcpy(itss.name, m_szNames[nsmp], 26);
1048 itss.id = 0x53504D49;
1049 itss.gvl = (BYTE)psmp->nGlobalVol;
1050 if (m_nInstruments)
1051 {
1052 for (UINT iu=1; iu<=m_nInstruments; iu++) if (Headers[iu])
1053 {
1054 INSTRUMENTHEADER *penv = Headers[iu];
1055 for (UINT ju=0; ju<128; ju++) if (penv->Keyboard[ju] == nsmp)
1056 {
1057 itss.flags = 0x01;
1058 break;
1059 }
1060 }
1061 } else
1062 {
1063 itss.flags = 0x01;
1064 }
1065 if (psmp->uFlags & CHN_LOOP) itss.flags |= 0x10;
1066 if (psmp->uFlags & CHN_SUSTAINLOOP) itss.flags |= 0x20;
1067 if (psmp->uFlags & CHN_PINGPONGLOOP) itss.flags |= 0x40;
1068 if (psmp->uFlags & CHN_PINGPONGSUSTAIN) itss.flags |= 0x80;
1069 itss.C5Speed = psmp->nC4Speed;
1070 if (!itss.C5Speed) // if no C5Speed assume it is XM Sample
1071 {
1072 UINT period;
1073
1074 /**
1075 * C5 note => number 61, but in XM samples:
1076 * RealNote = Note + RelativeTone
1077 */
1078 period = GetPeriodFromNote(61+psmp->RelativeTone, psmp->nFineTune, 0);
1079
1080 if (period)
1081 itss.C5Speed = GetFreqFromPeriod(period, 0, 0);
1082 /**
1083 * If it didn`t work, it may not be a XM file;
1084 * so put the default C5Speed, 8363Hz.
1085 */
1086 if (!itss.C5Speed) itss.C5Speed = 8363;
1087 }
1088
1089 itss.length = psmp->nLength;
1090 itss.loopbegin = psmp->nLoopStart;
1091 itss.loopend = psmp->nLoopEnd;
1092 itss.susloopbegin = psmp->nSustainStart;
1093 itss.susloopend = psmp->nSustainEnd;
1094 itss.vol = psmp->nVolume >> 2;
1095 itss.dfp = psmp->nPan >> 2;
1096 itss.vit = autovibxm2it[psmp->nVibType & 7];
1097 itss.vis = psmp->nVibRate;
1098 itss.vid = psmp->nVibDepth;
1099 itss.vir = (psmp->nVibSweep < 64) ? psmp->nVibSweep * 4 : 255;
1100 if (psmp->uFlags & CHN_PANNING) itss.dfp |= 0x80;
1101 if ((psmp->pSample) && (psmp->nLength)) itss.cvt = 0x01;
1102 UINT flags = RS_PCM8S;
1103 #ifndef NO_PACKING
1104 if (nPacking)
1105 {
1106 if ((!(psmp->uFlags & (CHN_16BIT|CHN_STEREO)))
1107 && (CanPackSample((char *)psmp->pSample, psmp->nLength, nPacking)))
1108 {
1109 flags = RS_ADPCM4;
1110 itss.cvt = 0xFF;
1111 }
1112 } else
1113 #endif // NO_PACKING
1114 {
1115 if (psmp->uFlags & CHN_STEREO)
1116 {
1117 flags = RS_STPCM8S;
1118 itss.flags |= 0x04;
1119 }
1120 if (psmp->uFlags & CHN_16BIT)
1121 {
1122 itss.flags |= 0x02;
1123 flags = (psmp->uFlags & CHN_STEREO) ? RS_STPCM16S : RS_PCM16S;
1124 }
1125 }
1126 itss.samplepointer = dwPos;
1127 fseek(f, smppos[nsmp-1], SEEK_SET);
1128
1129 itss.id = bswapLE32(itss.id);
1130 itss.length = bswapLE32(itss.length);
1131 itss.loopbegin = bswapLE32(itss.loopbegin);
1132 itss.loopend = bswapLE32(itss.loopend);
1133 itss.C5Speed = bswapLE32(itss.C5Speed);
1134 itss.susloopbegin = bswapLE32(itss.susloopbegin);
1135 itss.susloopend = bswapLE32(itss.susloopend);
1136 itss.samplepointer = bswapLE32(itss.samplepointer);
1137
1138 fwrite(&itss, 1, sizeof(ITSAMPLESTRUCT), f);
1139 fseek(f, dwPos, SEEK_SET);
1140 if ((psmp->pSample) && (psmp->nLength))
1141 {
1142 dwPos += WriteSample(f, psmp, flags);
1143 }
1144 }
1145 // Updating offsets
1146 fseek(f, dwHdrPos, SEEK_SET);
1147
1148 /* <Toad> Now we can byteswap them ;-) */
1149 UINT WW;
1150 UINT WX;
1151 WX = (UINT)header.insnum;
1152 WX <<= 2;
1153 for (WW=0; WW < (WX>>2); WW++)
1154 inspos[WW] = bswapLE32(inspos[WW]);
1155
1156 WX = (UINT)header.smpnum;
1157 WX <<= 2;
1158 for (WW=0; WW < (WX>>2); WW++)
1159 smppos[WW] = bswapLE32(smppos[WW]);
1160
1161 WX=(UINT)header.patnum;
1162 WX <<= 2;
1163 for (WW=0; WW < (WX>>2); WW++)
1164 patpos[WW] = bswapLE32(patpos[WW]);
1165
1166 if (header.insnum) fwrite(inspos, 4, header.insnum, f);
1167 if (header.smpnum) fwrite(smppos, 4, header.smpnum, f);
1168 if (header.patnum) fwrite(patpos, 4, header.patnum, f);
1169 fclose(f);
1170 return TRUE;
1171 }
1172
1173 //#pragma warning(default:4100)
1174 #endif // MODPLUG_NO_FILESAVE
1175
1176 //////////////////////////////////////////////////////////////////////////////
1177 // IT 2.14 compression
1178
1179 DWORD ITReadBits(DWORD &bitbuf, UINT &bitnum, LPBYTE &ibuf, CHAR n)
1180 //-----------------------------------------------------------------
1181 {
1182 DWORD retval = 0;
1183 UINT i = n;
1184
1185 if (n > 0)
1186 {
1187 do
1188 {
1189 if (!bitnum)
1190 {
1191 bitbuf = *ibuf++;
1192 bitnum = 8;
1193 }
1194 retval >>= 1;
1195 retval |= bitbuf << 31;
1196 bitbuf >>= 1;
1197 bitnum--;
1198 i--;
1199 } while (i);
1200 i = n;
1201 }
1202 return (retval >> (32-i));
1203 }
1204
1205 #define IT215_SUPPORT
1206 void ITUnpack8Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215)
1207 //-------------------------------------------------------------------------------------------
1208 {
1209 signed char *pDst = pSample;
1210 LPBYTE pSrc = lpMemFile;
1211 DWORD wHdr = 0;
1212 DWORD wCount = 0;
1213 DWORD bitbuf = 0;
1214 UINT bitnum = 0;
1215 BYTE bLeft = 0, bTemp = 0, bTemp2 = 0;
1216
1217 while (dwLen)
1218 {
1219 if (!wCount)
1220 {
1221 wCount = 0x8000;
1222 wHdr = bswapLE16(*((LPWORD)pSrc));
1223 pSrc += 2;
1224 bLeft = 9;
1225 bTemp = bTemp2 = 0;
1226 bitbuf = bitnum = 0;
1227 }
1228 DWORD d = wCount;
1229 if (d > dwLen) d = dwLen;
1230 // Unpacking
1231 DWORD dwPos = 0;
1232 do
1233 {
1234 WORD wBits = (WORD)ITReadBits(bitbuf, bitnum, pSrc, bLeft);
1235 if (bLeft < 7)
1236 {
1237 DWORD i = 1 << (bLeft-1);
1238 DWORD j = wBits & 0xFFFF;
1239 if (i != j) goto UnpackByte;
1240 wBits = (WORD)(ITReadBits(bitbuf, bitnum, pSrc, 3) + 1) & 0xFF;
1241 bLeft = ((BYTE)wBits < bLeft) ? (BYTE)wBits : (BYTE)((wBits+1) & 0xFF);
1242 goto Next;
1243 }
1244 if (bLeft < 9)
1245 {
1246 WORD i = (0xFF >> (9 - bLeft)) + 4;
1247 WORD j = i - 8;
1248 if ((wBits <= j) || (wBits > i)) goto UnpackByte;
1249 wBits -= j;
1250 bLeft = ((BYTE)(wBits & 0xFF) < bLeft) ? (BYTE)(wBits & 0xFF) : (BYTE)((wBits+1) & 0xFF);
1251 goto Next;
1252 }
1253 if (bLeft >= 10) goto SkipByte;
1254 if (wBits >= 256)
1255 {
1256 bLeft = (BYTE)(wBits + 1) & 0xFF;
1257 goto Next;
1258 }
1259 UnpackByte:
1260 if (bLeft < 8)
1261 {
1262 BYTE shift = 8 - bLeft;
1263 signed char c = (signed char)(wBits << shift);
1264 c >>= shift;
1265 wBits = (WORD)c;
1266 }
1267 wBits += bTemp;
1268 bTemp = (BYTE)wBits;
1269 bTemp2 += bTemp;
1270 #ifdef IT215_SUPPORT
1271 pDst[dwPos] = (b215) ? bTemp2 : bTemp;
1272 #else
1273 pDst[dwPos] = bTemp;
1274 #endif
1275 SkipByte:
1276 dwPos++;
1277 Next:
1278 if (pSrc >= lpMemFile+dwMemLength+1) return;
1279 } while (dwPos < d);
1280 // Move On
1281 wCount -= d;
1282 dwLen -= d;
1283 pDst += d;
1284 }
1285 }
1286
1287
1288 void ITUnpack16Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215)
1289 //--------------------------------------------------------------------------------------------
1290 {
1291 signed short *pDst = (signed short *)pSample;
1292 LPBYTE pSrc = lpMemFile;
1293 DWORD wHdr = 0;
1294 DWORD wCount = 0;
1295 DWORD bitbuf = 0;
1296 UINT bitnum = 0;
1297 BYTE bLeft = 0;
1298 signed short wTemp = 0, wTemp2 = 0;
1299
1300 while (dwLen)
1301 {
1302 if (!wCount)
1303 {
1304 wCount = 0x4000;
1305 wHdr = bswapLE16(*((LPWORD)pSrc));
1306 pSrc += 2;
1307 bLeft = 17;
1308 wTemp = wTemp2 = 0;
1309 bitbuf = bitnum = 0;
1310 }
1311 DWORD d = wCount;
1312 if (d > dwLen) d = dwLen;
1313 // Unpacking
1314 DWORD dwPos = 0;
1315 do
1316 {
1317 DWORD dwBits = ITReadBits(bitbuf, bitnum, pSrc, bLeft);
1318 if (bLeft < 7)
1319 {
1320 DWORD i = 1 << (bLeft-1);
1321 DWORD j = dwBits;
1322 if (i != j) goto UnpackByte;
1323 dwBits = ITReadBits(bitbuf, bitnum, pSrc, 4) + 1;
1324 bLeft = ((BYTE)(dwBits & 0xFF) < bLeft) ? (BYTE)(dwBits & 0xFF) : (BYTE)((dwBits+1) & 0xFF);
1325 goto Next;
1326 }
1327 if (bLeft < 17)
1328 {
1329 DWORD i = (0xFFFF >> (17 - bLeft)) + 8;
1330 DWORD j = (i - 16) & 0xFFFF;
1331 if ((dwBits <= j) || (dwBits > (i & 0xFFFF))) goto UnpackByte;
1332 dwBits -= j;
1333 bLeft = ((BYTE)(dwBits & 0xFF) < bLeft) ? (BYTE)(dwBits & 0xFF) : (BYTE)((dwBits+1) & 0xFF);
1334 goto Next;
1335 }
1336 if (bLeft >= 18) goto SkipByte;
1337 if (dwBits >= 0x10000)
1338 {
1339 bLeft = (BYTE)(dwBits + 1) & 0xFF;
1340 goto Next;
1341 }
1342 UnpackByte:
1343 if (bLeft < 16)
1344 {
1345 BYTE shift = 16 - bLeft;
1346 signed short c = (signed short)(dwBits << shift);
1347 c >>= shift;
1348 dwBits = (DWORD)c;
1349 }
1350 dwBits += wTemp;
1351 wTemp = (signed short)dwBits;
1352 wTemp2 += wTemp;
1353 #ifdef IT215_SUPPORT
1354 pDst[dwPos] = (b215) ? wTemp2 : wTemp;
1355 #else
1356 pDst[dwPos] = wTemp;
1357 #endif
1358 SkipByte:
1359 dwPos++;
1360 Next:
1361 if (pSrc >= lpMemFile+dwMemLength+1) return;
1362 } while (dwPos < d);
1363 // Move On
1364 wCount -= d;
1365 dwLen -= d;
1366 pDst += d;
1367 if (pSrc >= lpMemFile+dwMemLength) break;
1368 }
1369 }
1370
1371
1372 UINT CSoundFile::SaveMixPlugins(FILE *f, BOOL bUpdate)
1373 //----------------------------------------------------
1374 {
1375 DWORD chinfo[64];
1376 CHAR s[32];
1377 DWORD nPluginSize, writeSwapDWORD;
1378 SNDMIXPLUGININFO writePluginInfo;
1379 UINT nTotalSize = 0;
1380 UINT nChInfo = 0;
1381
1382 for (UINT i=0; i<MAX_MIXPLUGINS; i++)
1383 {
1384 PSNDMIXPLUGIN p = &m_MixPlugins[i];
1385 if ((p->Info.dwPluginId1) || (p->Info.dwPluginId2))
1386 {
1387 nPluginSize = sizeof(SNDMIXPLUGININFO)+4; // plugininfo+4 (datalen)
1388 if ((p->pMixPlugin) && (bUpdate))
1389 {
1390 p->pMixPlugin->SaveAllParameters();
1391 }
1392 if (p->pPluginData)
1393 {
1394 nPluginSize += p->nPluginDataSize;
1395 }
1396 if (f)
1397 {
1398 s[0] = 'F';
1399 s[1] = 'X';
1400 s[2] = '0' + (i/10);
1401 s[3] = '0' + (i%10);
1402 fwrite(s, 1, 4, f);
1403 writeSwapDWORD = bswapLE32(nPluginSize);
1404 fwrite(&writeSwapDWORD, 1, 4, f);
1405
1406 // Copy Information To Be Written for ByteSwapping
1407 memcpy(&writePluginInfo, &p->Info, sizeof(SNDMIXPLUGININFO));
1408 writePluginInfo.dwPluginId1 = bswapLE32(p->Info.dwPluginId1);
1409 writePluginInfo.dwPluginId2 = bswapLE32(p->Info.dwPluginId2);
1410 writePluginInfo.dwInputRouting = bswapLE32(p->Info.dwInputRouting);
1411 writePluginInfo.dwOutputRouting = bswapLE32(p->Info.dwOutputRouting);
1412 for (UINT j=0; j<4; j++)
1413 {
1414 writePluginInfo.dwReserved[j] = bswapLE32(p->Info.dwReserved[j]);
1415 }
1416
1417 fwrite(&writePluginInfo, 1, sizeof(SNDMIXPLUGININFO), f);
1418 writeSwapDWORD = bswapLE32(m_MixPlugins[i].nPluginDataSize);
1419 fwrite(&writeSwapDWORD, 1, 4, f);
1420 if (m_MixPlugins[i].pPluginData)
1421 {
1422 fwrite(m_MixPlugins[i].pPluginData, 1, m_MixPlugins[i].nPluginDataSize, f);
1423 }
1424 }
1425 nTotalSize += nPluginSize + 8;
1426 }
1427 }
1428 for (UINT j=0; j<m_nChannels; j++)
1429 {
1430 if (j < 64)
1431 {
1432 if ((chinfo[j] = ChnSettings[j].nMixPlugin) != 0)
1433 {
1434 nChInfo = j+1;
1435 chinfo[j] = bswapLE32(chinfo[j]); // inplace BS
1436 }
1437 }
1438 }
1439 if (nChInfo)
1440 {
1441 if (f)
1442 {
1443 nPluginSize = bswapLE32(0x58464843);
1444 fwrite(&nPluginSize, 1, 4, f);
1445 nPluginSize = nChInfo*4;
1446 writeSwapDWORD = bswapLE32(nPluginSize);
1447 fwrite(&writeSwapDWORD, 1, 4, f);
1448 fwrite(chinfo, 1, nPluginSize, f);
1449 }
1450 nTotalSize += nChInfo*4 + 8;
1451 }
1452 return nTotalSize;
1453 }
1454
1455
1456 UINT CSoundFile::LoadMixPlugins(const void *pData, UINT nLen)
1457 //-----------------------------------------------------------
1458 {
1459 const BYTE *p = (const BYTE *)pData;
1460 UINT nPos = 0;
1461
1462 while (nPos+8 < nLen)
1463 {
1464 DWORD nPluginSize;
1465 UINT nPlugin;
1466
1467 nPluginSize = bswapLE32(*(DWORD *)(p+nPos+4));
1468 if (nPluginSize > nLen-nPos-8) break;;
1469 if ((bswapLE32(*(DWORD *)(p+nPos))) == 0x58464843)
1470 {
1471 for (UINT ch=0; ch<64; ch++) if (ch*4 < nPluginSize)
1472 {
1473 ChnSettings[ch].nMixPlugin = bswapLE32(*(DWORD *)(p+nPos+8+ch*4));
1474 }
1475 } else
1476 {
1477 if ((p[nPos] != 'F') || (p[nPos+1] != 'X')
1478 || (p[nPos+2] < '0') || (p[nPos+3] < '0'))
1479 {
1480 break;
1481 }
1482 nPlugin = (p[nPos+2]-'0')*10 + (p[nPos+3]-'0');
1483 if ((nPlugin < MAX_MIXPLUGINS) && (nPluginSize >= sizeof(SNDMIXPLUGININFO)+4))
1484 {
1485 DWORD dwExtra = bswapLE32(*(DWORD *)(p+nPos+8+sizeof(SNDMIXPLUGININFO)));
1486 m_MixPlugins[nPlugin].Info = *(const SNDMIXPLUGININFO *)(p+nPos+8);
1487 m_MixPlugins[nPlugin].Info.dwPluginId1 = bswapLE32(m_MixPlugins[nPlugin].Info.dwPluginId1);
1488 m_MixPlugins[nPlugin].Info.dwPluginId2 = bswapLE32(m_MixPlugins[nPlugin].Info.dwPluginId2);
1489 m_MixPlugins[nPlugin].Info.dwInputRouting = bswapLE32(m_MixPlugins[nPlugin].Info.dwInputRouting);
1490 m_MixPlugins[nPlugin].Info.dwOutputRouting = bswapLE32(m_MixPlugins[nPlugin].Info.dwOutputRouting);
1491 for (UINT j=0; j<4; j++)
1492 {
1493 m_MixPlugins[nPlugin].Info.dwReserved[j] = bswapLE32(m_MixPlugins[nPlugin].Info.dwReserved[j]);
1494 }
1495 if ((dwExtra) && (dwExtra <= nPluginSize-sizeof(SNDMIXPLUGININFO)-4))
1496 {
1497 m_MixPlugins[nPlugin].nPluginDataSize = 0;
1498 m_MixPlugins[nPlugin].pPluginData = new signed char [dwExtra];
1499 if (m_MixPlugins[nPlugin].pPluginData)
1500 {
1501 m_MixPlugins[nPlugin].nPluginDataSize = dwExtra;
1502 memcpy(m_MixPlugins[nPlugin].pPluginData, p+nPos+8+sizeof(SNDMIXPLUGININFO)+4, dwExtra);
1503 }
1504 }
1505 }
1506 }
1507 nPos += nPluginSize + 8;
1508 }
1509 return nPos;
1510 }
1511