-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_it.cpp
1516 lines (1460 loc) · 44.6 KB
/
load_it.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This source code is public domain.
*
* Authors: Olivier Lapicque <[email protected]>,
* Adam Goode <[email protected]> (Endian and char fixes for PPC)
* Marco Trillo <[email protected]> (Endian fixes for SaveIT, XM->IT Sample Converter)
*
*/
#include "stdafx.h"
#include "sndfile.h"
#include "it_defs.h"
#ifdef _MSC_VER
#pragma warning(disable:4244)
#endif
BYTE autovibit2xm[8] =
{ 0, 3, 1, 4, 2, 0, 0, 0 };
BYTE autovibxm2it[8] =
{ 0, 2, 4, 1, 3, 0, 0, 0 };
//////////////////////////////////////////////////////////
// Impulse Tracker IT file support
// for conversion of XM samples
extern WORD XMPeriodTable[96+8];
extern UINT XMLinearTable[768];
static inline UINT ConvertVolParam(UINT value)
//--------------------------------------------
{
return (value > 9) ? 9 : value;
}
BOOL CSoundFile::ITInstrToMPT(const void *p, INSTRUMENTHEADER *penv, UINT trkvers)
//--------------------------------------------------------------------------------
{
if (trkvers < 0x0200)
{
const ITOLDINSTRUMENT *pis = (const ITOLDINSTRUMENT *)p;
memcpy(penv->name, pis->name, 26);
memcpy(penv->filename, pis->filename, 12);
penv->nFadeOut = bswapLE16(pis->fadeout) << 6;
penv->nGlobalVol = 64;
for (UINT j=0; j<NOTE_MAX; j++)
{
UINT note = pis->keyboard[j*2];
UINT ins = pis->keyboard[j*2+1];
if (ins < MAX_SAMPLES) penv->Keyboard[j] = ins;
if (note < 128) penv->NoteMap[j] = note+1;
else if (note >= 0xFE) penv->NoteMap[j] = note;
}
if (pis->flags & 0x01) penv->dwFlags |= ENV_VOLUME;
if (pis->flags & 0x02) penv->dwFlags |= ENV_VOLLOOP;
if (pis->flags & 0x04) penv->dwFlags |= ENV_VOLSUSTAIN;
penv->nVolLoopStart = pis->vls;
penv->nVolLoopEnd = pis->vle;
penv->nVolSustainBegin = pis->sls;
penv->nVolSustainEnd = pis->sle;
penv->nVolEnv = 25;
for (UINT ev=0; ev<25; ev++)
{
if ((penv->VolPoints[ev] = pis->nodes[ev*2]) == 0xFF)
{
penv->nVolEnv = ev;
break;
}
penv->VolEnv[ev] = pis->nodes[ev*2+1];
}
penv->nNNA = pis->nna;
penv->nDCT = pis->dnc;
penv->nPan = 0x80;
} else
{
const ITINSTRUMENT *pis = (const ITINSTRUMENT *)p;
memcpy(penv->name, pis->name, 26);
memcpy(penv->filename, pis->filename, 12);
penv->nMidiProgram = pis->mpr;
penv->nMidiChannel = pis->mch;
penv->wMidiBank = bswapLE16(pis->mbank);
penv->nFadeOut = bswapLE16(pis->fadeout) << 5;
penv->nGlobalVol = pis->gbv >> 1;
if (penv->nGlobalVol > 64) penv->nGlobalVol = 64;
for (UINT j=0; j<NOTE_MAX; j++)
{
UINT note = pis->keyboard[j*2];
UINT ins = pis->keyboard[j*2+1];
if (ins < MAX_SAMPLES) penv->Keyboard[j] = ins;
if (note < 128) penv->NoteMap[j] = note+1;
else if (note >= 0xFE) penv->NoteMap[j] = note;
}
// Volume Envelope
if (pis->volenv.flags & 1) penv->dwFlags |= ENV_VOLUME;
if (pis->volenv.flags & 2) penv->dwFlags |= ENV_VOLLOOP;
if (pis->volenv.flags & 4) penv->dwFlags |= ENV_VOLSUSTAIN;
if (pis->volenv.flags & 8) penv->dwFlags |= ENV_VOLCARRY;
penv->nVolEnv = pis->volenv.num;
if (penv->nVolEnv > 25) penv->nVolEnv = 25;
penv->nVolLoopStart = pis->volenv.lpb;
penv->nVolLoopEnd = pis->volenv.lpe;
penv->nVolSustainBegin = pis->volenv.slb;
penv->nVolSustainEnd = pis->volenv.sle;
// Panning Envelope
if (pis->panenv.flags & 1) penv->dwFlags |= ENV_PANNING;
if (pis->panenv.flags & 2) penv->dwFlags |= ENV_PANLOOP;
if (pis->panenv.flags & 4) penv->dwFlags |= ENV_PANSUSTAIN;
if (pis->panenv.flags & 8) penv->dwFlags |= ENV_PANCARRY;
penv->nPanEnv = pis->panenv.num;
if (penv->nPanEnv > 25) penv->nPanEnv = 25;
penv->nPanLoopStart = pis->panenv.lpb;
penv->nPanLoopEnd = pis->panenv.lpe;
penv->nPanSustainBegin = pis->panenv.slb;
penv->nPanSustainEnd = pis->panenv.sle;
// Pitch Envelope
if (pis->pitchenv.flags & 1) penv->dwFlags |= ENV_PITCH;
if (pis->pitchenv.flags & 2) penv->dwFlags |= ENV_PITCHLOOP;
if (pis->pitchenv.flags & 4) penv->dwFlags |= ENV_PITCHSUSTAIN;
if (pis->pitchenv.flags & 8) penv->dwFlags |= ENV_PITCHCARRY;
if (pis->pitchenv.flags & 0x80) penv->dwFlags |= ENV_FILTER;
penv->nPitchEnv = pis->pitchenv.num;
if (penv->nPitchEnv > 25) penv->nPitchEnv = 25;
penv->nPitchLoopStart = pis->pitchenv.lpb;
penv->nPitchLoopEnd = pis->pitchenv.lpe;
penv->nPitchSustainBegin = pis->pitchenv.slb;
penv->nPitchSustainEnd = pis->pitchenv.sle;
// Envelopes Data
for (UINT ev=0; ev<25; ev++)
{
penv->VolEnv[ev] = pis->volenv.data[ev*3];
penv->VolPoints[ev] = (pis->volenv.data[ev*3+2] << 8) | (pis->volenv.data[ev*3+1]);
penv->PanEnv[ev] = pis->panenv.data[ev*3] + 32;
penv->PanPoints[ev] = (pis->panenv.data[ev*3+2] << 8) | (pis->panenv.data[ev*3+1]);
penv->PitchEnv[ev] = pis->pitchenv.data[ev*3] + 32;
penv->PitchPoints[ev] = (pis->pitchenv.data[ev*3+2] << 8) | (pis->pitchenv.data[ev*3+1]);
}
penv->nNNA = pis->nna;
penv->nDCT = pis->dct;
penv->nDNA = pis->dca;
penv->nPPS = pis->pps;
penv->nPPC = pis->ppc;
penv->nIFC = pis->ifc;
penv->nIFR = pis->ifr;
penv->nVolSwing = pis->rv;
penv->nPanSwing = pis->rp;
penv->nPan = (pis->dfp & 0x7F) << 2;
if (penv->nPan > 256) penv->nPan = 128;
if (pis->dfp < 0x80) penv->dwFlags |= ENV_SETPANNING;
}
if ((penv->nVolLoopStart >= 25) || (penv->nVolLoopEnd >= 25)) penv->dwFlags &= ~ENV_VOLLOOP;
if ((penv->nVolSustainBegin >= 25) || (penv->nVolSustainEnd >= 25)) penv->dwFlags &= ~ENV_VOLSUSTAIN;
return TRUE;
}
BOOL CSoundFile::ReadIT(const BYTE *lpStream, DWORD dwMemLength)
//--------------------------------------------------------------
{
DWORD dwMemPos = sizeof(ITFILEHEADER);
DWORD inspos[MAX_INSTRUMENTS];
DWORD smppos[MAX_SAMPLES];
DWORD patpos[MAX_PATTERNS];
BYTE chnmask[64], channels_used[64];
MODCOMMAND lastvalue[64];
if ((!lpStream) || (dwMemLength < sizeof(ITFILEHEADER))) return FALSE;
ITFILEHEADER pifh = *(ITFILEHEADER *)lpStream;
pifh.id = bswapLE32(pifh.id);
pifh.reserved1 = bswapLE16(pifh.reserved1);
pifh.ordnum = bswapLE16(pifh.ordnum);
pifh.insnum = bswapLE16(pifh.insnum);
pifh.smpnum = bswapLE16(pifh.smpnum);
pifh.patnum = bswapLE16(pifh.patnum);
pifh.cwtv = bswapLE16(pifh.cwtv);
pifh.cmwt = bswapLE16(pifh.cmwt);
pifh.flags = bswapLE16(pifh.flags);
pifh.special = bswapLE16(pifh.special);
pifh.msglength = bswapLE16(pifh.msglength);
pifh.msgoffset = bswapLE32(pifh.msgoffset);
pifh.reserved2 = bswapLE32(pifh.reserved2);
if ((pifh.id != 0x4D504D49) || (pifh.insnum >= MAX_INSTRUMENTS)
|| (!pifh.smpnum) || (pifh.smpnum >= MAX_INSTRUMENTS) || (!pifh.ordnum)) return FALSE;
if (dwMemPos + pifh.ordnum + pifh.insnum*4
+ pifh.smpnum*4 + pifh.patnum*4 > dwMemLength) return FALSE;
m_nType = MOD_TYPE_IT;
if (pifh.flags & 0x08) m_dwSongFlags |= SONG_LINEARSLIDES;
if (pifh.flags & 0x10) m_dwSongFlags |= SONG_ITOLDEFFECTS;
if (pifh.flags & 0x20) m_dwSongFlags |= SONG_ITCOMPATMODE;
if (pifh.flags & 0x80) m_dwSongFlags |= SONG_EMBEDMIDICFG;
if (pifh.flags & 0x1000) m_dwSongFlags |= SONG_EXFILTERRANGE;
memcpy(m_szNames[0], pifh.songname, 26);
m_szNames[0][26] = 0;
// Global Volume
if (pifh.globalvol)
{
m_nDefaultGlobalVolume = pifh.globalvol << 1;
if (!m_nDefaultGlobalVolume) m_nDefaultGlobalVolume = 256;
if (m_nDefaultGlobalVolume > 256) m_nDefaultGlobalVolume = 256;
}
if (pifh.speed) m_nDefaultSpeed = pifh.speed;
if (pifh.tempo) m_nDefaultTempo = pifh.tempo;
m_nSongPreAmp = pifh.mv & 0x7F;
// Reading Channels Pan Positions
for (int ipan=0; ipan<64; ipan++) if (pifh.chnpan[ipan] != 0xFF)
{
ChnSettings[ipan].nVolume = pifh.chnvol[ipan];
ChnSettings[ipan].nPan = 128;
if (pifh.chnpan[ipan] & 0x80) ChnSettings[ipan].dwFlags |= CHN_MUTE;
UINT n = pifh.chnpan[ipan] & 0x7F;
if (n <= 64) ChnSettings[ipan].nPan = n << 2;
if (n == 100) ChnSettings[ipan].dwFlags |= CHN_SURROUND;
}
if (m_nChannels < 4) m_nChannels = 4;
// Reading Song Message
if ((pifh.special & 0x01) && (pifh.msglength) && (pifh.msglength <= dwMemLength) && (pifh.msgoffset < dwMemLength - pifh.msglength))
{
m_lpszSongComments = new char[pifh.msglength+1];
if (m_lpszSongComments)
{
memcpy(m_lpszSongComments, lpStream+pifh.msgoffset, pifh.msglength);
m_lpszSongComments[pifh.msglength] = 0;
}
}
// Reading orders
UINT nordsize = pifh.ordnum;
if (nordsize > MAX_ORDERS) nordsize = MAX_ORDERS;
memcpy(Order, lpStream+dwMemPos, nordsize);
dwMemPos += pifh.ordnum;
// Reading Instrument Offsets
memset(inspos, 0, sizeof(inspos));
UINT inspossize = pifh.insnum;
if (inspossize > MAX_INSTRUMENTS) inspossize = MAX_INSTRUMENTS;
inspossize <<= 2;
memcpy(inspos, lpStream+dwMemPos, inspossize);
for (UINT j=0; j < (inspossize>>2); j++)
{
inspos[j] = bswapLE32(inspos[j]);
}
dwMemPos += pifh.insnum * 4;
// Reading Samples Offsets
memset(smppos, 0, sizeof(smppos));
UINT smppossize = pifh.smpnum;
if (smppossize > MAX_SAMPLES) smppossize = MAX_SAMPLES;
smppossize <<= 2;
memcpy(smppos, lpStream+dwMemPos, smppossize);
for (UINT j=0; j < (smppossize>>2); j++)
{
smppos[j] = bswapLE32(smppos[j]);
}
dwMemPos += pifh.smpnum * 4;
// Reading Patterns Offsets
memset(patpos, 0, sizeof(patpos));
UINT patpossize = pifh.patnum;
if (patpossize > MAX_PATTERNS) patpossize = MAX_PATTERNS;
patpossize <<= 2;
memcpy(patpos, lpStream+dwMemPos, patpossize);
for (UINT j=0; j < (patpossize>>2); j++)
{
patpos[j] = bswapLE32(patpos[j]);
}
dwMemPos += pifh.patnum * 4;
// Reading IT Extra Info
if (dwMemPos + 2 < dwMemLength)
{
UINT nflt = bswapLE16(*((WORD *)(lpStream + dwMemPos)));
dwMemPos += 2;
if (dwMemPos + nflt * 8 < dwMemLength) dwMemPos += nflt * 8;
}
// Reading Midi Output & Macros
if (m_dwSongFlags & SONG_EMBEDMIDICFG)
{
if (dwMemPos + sizeof(MODMIDICFG) < dwMemLength)
{
memcpy(&m_MidiCfg, lpStream+dwMemPos, sizeof(MODMIDICFG));
dwMemPos += sizeof(MODMIDICFG);
}
}
// Read pattern names: "PNAM"
if ((dwMemPos + 8 < dwMemLength) && (bswapLE32(*((DWORD *)(lpStream+dwMemPos))) == 0x4d414e50))
{
UINT len = bswapLE32(*((DWORD *)(lpStream+dwMemPos+4)));
dwMemPos += 8;
if ((dwMemPos + len <= dwMemLength) && (len <= MAX_PATTERNS*MAX_PATTERNNAME) && (len >= MAX_PATTERNNAME))
{
m_lpszPatternNames = new char[len];
if (m_lpszPatternNames)
{
m_nPatternNames = len / MAX_PATTERNNAME;
memcpy(m_lpszPatternNames, lpStream+dwMemPos, len);
}
dwMemPos += len;
}
}
// 4-channels minimum
m_nChannels = 4;
// Read channel names: "CNAM"
if ((dwMemPos + 8 < dwMemLength) && (bswapLE32(*((DWORD *)(lpStream+dwMemPos))) == 0x4d414e43))
{
UINT len = bswapLE32(*((DWORD *)(lpStream+dwMemPos+4)));
dwMemPos += 8;
if ((dwMemPos + len <= dwMemLength) && (len <= 64*MAX_CHANNELNAME))
{
UINT n = len / MAX_CHANNELNAME;
if (n > m_nChannels) m_nChannels = n;
for (UINT i=0; i<n; i++)
{
memcpy(ChnSettings[i].szName, (lpStream+dwMemPos+i*MAX_CHANNELNAME), MAX_CHANNELNAME);
ChnSettings[i].szName[MAX_CHANNELNAME-1] = 0;
}
dwMemPos += len;
}
}
// Read mix plugins information
if (dwMemPos + 8 < dwMemLength)
{
dwMemPos += LoadMixPlugins(lpStream+dwMemPos, dwMemLength-dwMemPos);
}
// Checking for unused channels
UINT npatterns = pifh.patnum;
if (npatterns > MAX_PATTERNS) npatterns = MAX_PATTERNS;
for (UINT patchk=0; patchk<npatterns; patchk++)
{
memset(chnmask, 0, sizeof(chnmask));
if ((!patpos[patchk]) || ((DWORD)patpos[patchk] >= dwMemLength - 4)) continue;
UINT len = bswapLE16(*((WORD *)(lpStream+patpos[patchk])));
UINT rows = bswapLE16(*((WORD *)(lpStream+patpos[patchk]+2)));
if ((rows < 4) || (rows > 256)) continue;
if (8+len > dwMemLength || patpos[patchk] > dwMemLength - (8+len)) continue;
UINT i = 0;
const BYTE *p = lpStream+patpos[patchk]+8;
UINT nrow = 0;
while (nrow<rows)
{
if (i >= len) break;
BYTE b = p[i++];
if (!b)
{
nrow++;
continue;
}
UINT ch = b & 0x7F;
if (ch) ch = (ch - 1) & 0x3F;
if (b & 0x80)
{
if (i >= len) break;
chnmask[ch] = p[i++];
}
// Channel used
if (chnmask[ch] & 0x0F)
{
if ((ch >= m_nChannels) && (ch < 64)) m_nChannels = ch+1;
}
// Note
if (chnmask[ch] & 1) i++;
// Instrument
if (chnmask[ch] & 2) i++;
// Volume
if (chnmask[ch] & 4) i++;
// Effect
if (chnmask[ch] & 8) i += 2;
if (i >= len) break;
}
}
// Reading Instruments
m_nInstruments = 0;
if (pifh.flags & 0x04) m_nInstruments = pifh.insnum;
if (m_nInstruments >= MAX_INSTRUMENTS) m_nInstruments = MAX_INSTRUMENTS-1;
for (UINT nins=0; nins<m_nInstruments; nins++)
{
if ((inspos[nins] > 0) && (inspos[nins] < dwMemLength - sizeof(ITOLDINSTRUMENT)))
{
INSTRUMENTHEADER *penv = new INSTRUMENTHEADER;
if (!penv) continue;
Headers[nins+1] = penv;
memset(penv, 0, sizeof(INSTRUMENTHEADER));
ITInstrToMPT(lpStream + inspos[nins], penv, pifh.cmwt);
}
}
// Reading Samples
m_nSamples = pifh.smpnum;
if (m_nSamples >= MAX_SAMPLES) m_nSamples = MAX_SAMPLES-1;
for (UINT nsmp=0; nsmp<pifh.smpnum; nsmp++) if ((smppos[nsmp]) && (smppos[nsmp] <= dwMemLength - sizeof(ITSAMPLESTRUCT)))
{
ITSAMPLESTRUCT pis = *(ITSAMPLESTRUCT *)(lpStream+smppos[nsmp]);
pis.id = bswapLE32(pis.id);
pis.length = bswapLE32(pis.length);
pis.loopbegin = bswapLE32(pis.loopbegin);
pis.loopend = bswapLE32(pis.loopend);
pis.C5Speed = bswapLE32(pis.C5Speed);
pis.susloopbegin = bswapLE32(pis.susloopbegin);
pis.susloopend = bswapLE32(pis.susloopend);
pis.samplepointer = bswapLE32(pis.samplepointer);
if (pis.id == 0x53504D49)
{
MODINSTRUMENT *pins = &Ins[nsmp+1];
memcpy(pins->name, pis.filename, 12);
pins->uFlags = 0;
pins->nLength = 0;
pins->nLoopStart = pis.loopbegin;
pins->nLoopEnd = pis.loopend;
pins->nSustainStart = pis.susloopbegin;
pins->nSustainEnd = pis.susloopend;
pins->nC4Speed = pis.C5Speed;
if (!pins->nC4Speed) pins->nC4Speed = 8363;
if (pis.C5Speed < 256) pins->nC4Speed = 256;
pins->nVolume = pis.vol << 2;
if (pins->nVolume > 256) pins->nVolume = 256;
pins->nGlobalVol = pis.gvl;
if (pins->nGlobalVol > 64) pins->nGlobalVol = 64;
if (pis.flags & 0x10) pins->uFlags |= CHN_LOOP;
if (pis.flags & 0x20) pins->uFlags |= CHN_SUSTAINLOOP;
if (pis.flags & 0x40) pins->uFlags |= CHN_PINGPONGLOOP;
if (pis.flags & 0x80) pins->uFlags |= CHN_PINGPONGSUSTAIN;
pins->nPan = (pis.dfp & 0x7F) << 2;
if (pins->nPan > 256) pins->nPan = 256;
if (pis.dfp & 0x80) pins->uFlags |= CHN_PANNING;
pins->nVibType = autovibit2xm[pis.vit & 7];
pins->nVibRate = pis.vis;
pins->nVibDepth = pis.vid & 0x7F;
pins->nVibSweep = (pis.vir + 3) / 4;
if ((pis.samplepointer) && (pis.samplepointer < dwMemLength) && (pis.length))
{
pins->nLength = pis.length;
if (pins->nLength > MAX_SAMPLE_LENGTH) pins->nLength = MAX_SAMPLE_LENGTH;
UINT flags = (pis.cvt & 1) ? RS_PCM8S : RS_PCM8U;
if (pis.flags & 2)
{
flags += 5;
if (pis.flags & 4) flags |= RSF_STEREO;
pins->uFlags |= CHN_16BIT;
// IT 2.14 16-bit packed sample ?
if (pis.flags & 8) flags = ((pifh.cmwt >= 0x215) && (pis.cvt & 4)) ? RS_IT21516 : RS_IT21416;
} else
{
if (pis.flags & 4) flags |= RSF_STEREO;
if (pis.cvt == 0xFF) flags = RS_ADPCM4; else
// IT 2.14 8-bit packed sample ?
if (pis.flags & 8) flags = ((pifh.cmwt >= 0x215) && (pis.cvt & 4)) ? RS_IT2158 : RS_IT2148;
}
ReadSample(&Ins[nsmp+1], flags, (LPSTR)(lpStream+pis.samplepointer), dwMemLength - pis.samplepointer);
}
}
memcpy(m_szNames[nsmp+1], pis.name, 26);
}
// Reading Patterns
for (UINT npat=0; npat<npatterns; npat++)
{
if ((!patpos[npat]) || ((DWORD)patpos[npat] >= dwMemLength - 4))
{
PatternSize[npat] = 64;
Patterns[npat] = AllocatePattern(64, m_nChannels);
continue;
}
UINT len = bswapLE16(*((WORD *)(lpStream+patpos[npat])));
UINT rows = bswapLE16(*((WORD *)(lpStream+patpos[npat]+2)));
if ((rows < 4) || (rows > 256)) continue;
if (8+len > dwMemLength || patpos[npat] > dwMemLength - (8+len)) continue;
PatternSize[npat] = rows;
if ((Patterns[npat] = AllocatePattern(rows, m_nChannels)) == NULL) continue;
memset(lastvalue, 0, sizeof(lastvalue));
memset(chnmask, 0, sizeof(chnmask));
MODCOMMAND *m = Patterns[npat];
UINT i = 0;
const BYTE *p = lpStream+patpos[npat]+8;
UINT nrow = 0;
while (nrow<rows)
{
if (i >= len) break;
BYTE b = p[i++];
if (!b)
{
nrow++;
m+=m_nChannels;
continue;
}
UINT ch = b & 0x7F;
if (ch) ch = (ch - 1) & 0x3F;
if (b & 0x80)
{
if (i >= len) break;
chnmask[ch] = p[i++];
}
if ((chnmask[ch] & 0x10) && (ch < m_nChannels))
{
m[ch].note = lastvalue[ch].note;
}
if ((chnmask[ch] & 0x20) && (ch < m_nChannels))
{
m[ch].instr = lastvalue[ch].instr;
}
if ((chnmask[ch] & 0x40) && (ch < m_nChannels))
{
m[ch].volcmd = lastvalue[ch].volcmd;
m[ch].vol = lastvalue[ch].vol;
}
if ((chnmask[ch] & 0x80) && (ch < m_nChannels))
{
m[ch].command = lastvalue[ch].command;
m[ch].param = lastvalue[ch].param;
}
if (chnmask[ch] & 1) // Note
{
if (i >= len) break;
UINT note = p[i++];
if (ch < m_nChannels)
{
if (note < 0x80) note++;
m[ch].note = note;
lastvalue[ch].note = note;
channels_used[ch] = TRUE;
}
}
if (chnmask[ch] & 2)
{
if (i >= len) break;
UINT instr = p[i++];
if (ch < m_nChannels)
{
m[ch].instr = instr;
lastvalue[ch].instr = instr;
}
}
if (chnmask[ch] & 4)
{
if (i >= len) break;
UINT vol = p[i++];
if (ch < m_nChannels)
{
// 0-64: Set Volume
if (vol <= 64) { m[ch].volcmd = VOLCMD_VOLUME; m[ch].vol = vol; } else
// 128-192: Set Panning
if ((vol >= 128) && (vol <= 192)) { m[ch].volcmd = VOLCMD_PANNING; m[ch].vol = vol - 128; } else
// 65-74: Fine Volume Up
if (vol < 75) { m[ch].volcmd = VOLCMD_FINEVOLUP; m[ch].vol = vol - 65; } else
// 75-84: Fine Volume Down
if (vol < 85) { m[ch].volcmd = VOLCMD_FINEVOLDOWN; m[ch].vol = vol - 75; } else
// 85-94: Volume Slide Up
if (vol < 95) { m[ch].volcmd = VOLCMD_VOLSLIDEUP; m[ch].vol = vol - 85; } else
// 95-104: Volume Slide Down
if (vol < 105) { m[ch].volcmd = VOLCMD_VOLSLIDEDOWN; m[ch].vol = vol - 95; } else
// 105-114: Pitch Slide Up
if (vol < 115) { m[ch].volcmd = VOLCMD_PORTADOWN; m[ch].vol = vol - 105; } else
// 115-124: Pitch Slide Down
if (vol < 125) { m[ch].volcmd = VOLCMD_PORTAUP; m[ch].vol = vol - 115; } else
// 193-202: Portamento To
if ((vol >= 193) && (vol <= 202)) { m[ch].volcmd = VOLCMD_TONEPORTAMENTO; m[ch].vol = vol - 193; } else
// 203-212: Vibrato
if ((vol >= 203) && (vol <= 212)) { m[ch].volcmd = VOLCMD_VIBRATOSPEED; m[ch].vol = vol - 203; }
lastvalue[ch].volcmd = m[ch].volcmd;
lastvalue[ch].vol = m[ch].vol;
}
}
// Reading command/param
if (chnmask[ch] & 8)
{
if (i > len - 2) break;
UINT cmd = p[i++];
UINT param = p[i++];
if (ch < m_nChannels)
{
if (cmd)
{
m[ch].command = cmd;
m[ch].param = param;
S3MConvert(&m[ch], TRUE);
lastvalue[ch].command = m[ch].command;
lastvalue[ch].param = m[ch].param;
}
}
}
}
}
for (UINT ncu=0; ncu<MAX_BASECHANNELS; ncu++)
{
if (ncu>=m_nChannels)
{
ChnSettings[ncu].nVolume = 64;
ChnSettings[ncu].dwFlags &= ~CHN_MUTE;
}
}
m_nMinPeriod = 8;
m_nMaxPeriod = 0xF000;
return TRUE;
}
#ifndef MODPLUG_NO_FILESAVE
//#define SAVEITTIMESTAMP
#ifdef _MSC_VER
#pragma warning(disable:4100)
#endif
BOOL CSoundFile::SaveIT(LPCSTR lpszFileName, UINT nPacking)
//---------------------------------------------------------
{
DWORD dwPatNamLen, dwChnNamLen;
ITFILEHEADER header, writeheader;
ITINSTRUMENT iti, writeiti;
ITSAMPLESTRUCT itss;
BYTE smpcount[MAX_SAMPLES];
DWORD inspos[MAX_INSTRUMENTS];
DWORD patpos[MAX_PATTERNS];
DWORD smppos[MAX_SAMPLES];
DWORD dwPos = 0, dwHdrPos = 0, dwExtra = 2;
WORD patinfo[4];
BYTE chnmask[64];
BYTE buf[512];
MODCOMMAND lastvalue[64];
FILE *f;
if ((!lpszFileName) || ((f = fopen(lpszFileName, "wb")) == NULL)) return FALSE;
memset(inspos, 0, sizeof(inspos));
memset(patpos, 0, sizeof(patpos));
memset(smppos, 0, sizeof(smppos));
// Writing Header
memset(&header, 0, sizeof(header));
dwPatNamLen = 0;
dwChnNamLen = 0;
header.id = 0x4D504D49; // IMPM
lstrcpyn((char *)header.songname, m_szNames[0], 27);
header.reserved1 = 0x1004;
header.ordnum = 0;
while ((header.ordnum < MAX_ORDERS) && (Order[header.ordnum] < 0xFF)) header.ordnum++;
if (header.ordnum < MAX_ORDERS) Order[header.ordnum++] = 0xFF;
header.insnum = m_nInstruments;
header.smpnum = m_nSamples;
header.patnum = MAX_PATTERNS;
while ((header.patnum > 0) && (!Patterns[header.patnum-1])) header.patnum--;
header.cwtv = 0x217;
header.cmwt = 0x200;
header.flags = 0x0001;
header.special = 0x0006;
if (m_nInstruments) header.flags |= 0x04;
if (m_dwSongFlags & SONG_LINEARSLIDES) header.flags |= 0x08;
if (m_dwSongFlags & SONG_ITOLDEFFECTS) header.flags |= 0x10;
if (m_dwSongFlags & SONG_ITCOMPATMODE) header.flags |= 0x20;
if (m_dwSongFlags & SONG_EXFILTERRANGE) header.flags |= 0x1000;
header.globalvol = m_nDefaultGlobalVolume >> 1;
header.mv = m_nSongPreAmp;
// clip song pre-amp values (between 0x20 and 0x7f)
if (header.mv < 0x20) header.mv = 0x20;
if (header.mv > 0x7F) header.mv = 0x7F;
header.speed = m_nDefaultSpeed;
header.tempo = m_nDefaultTempo;
header.sep = m_nStereoSeparation;
dwHdrPos = sizeof(header) + header.ordnum;
// Channel Pan and Volume
memset(header.chnpan, 0xFF, 64);
memset(header.chnvol, 64, 64);
for (UINT ich=0; ich<m_nChannels; ich++)
{
header.chnpan[ich] = ChnSettings[ich].nPan >> 2;
if (ChnSettings[ich].dwFlags & CHN_SURROUND) header.chnpan[ich] = 100;
header.chnvol[ich] = ChnSettings[ich].nVolume;
if (ChnSettings[ich].dwFlags & CHN_MUTE) header.chnpan[ich] |= 0x80;
if (ChnSettings[ich].szName[0])
{
dwChnNamLen = (ich+1) * MAX_CHANNELNAME;
}
}
if (dwChnNamLen) dwExtra += dwChnNamLen + 8;
#ifdef SAVEITTIMESTAMP
dwExtra += 8; // Time Stamp
#endif
if (m_dwSongFlags & SONG_EMBEDMIDICFG)
{
header.flags |= 0x80;
header.special |= 0x08;
dwExtra += sizeof(MODMIDICFG);
}
// Pattern Names
if ((m_nPatternNames) && (m_lpszPatternNames))
{
dwPatNamLen = m_nPatternNames * MAX_PATTERNNAME;
while ((dwPatNamLen >= MAX_PATTERNNAME) && (!m_lpszPatternNames[dwPatNamLen-MAX_PATTERNNAME])) dwPatNamLen -= MAX_PATTERNNAME;
if (dwPatNamLen < MAX_PATTERNNAME) dwPatNamLen = 0;
if (dwPatNamLen) dwExtra += dwPatNamLen + 8;
}
// Mix Plugins
dwExtra += SaveMixPlugins(NULL, TRUE);
// Comments
if (m_lpszSongComments)
{
header.special |= 1;
header.msglength = strlen(m_lpszSongComments)+1;
header.msgoffset = dwHdrPos + dwExtra + header.insnum*4 + header.patnum*4 + header.smpnum*4;
}
// Write file header
memcpy(&writeheader, &header, sizeof(header));
// Byteswap header information
writeheader.id = bswapLE32(writeheader.id);
writeheader.reserved1 = bswapLE16(writeheader.reserved1);
writeheader.ordnum = bswapLE16(writeheader.ordnum);
writeheader.insnum = bswapLE16(writeheader.insnum);
writeheader.smpnum = bswapLE16(writeheader.smpnum);
writeheader.patnum = bswapLE16(writeheader.patnum);
writeheader.cwtv = bswapLE16(writeheader.cwtv);
writeheader.cmwt = bswapLE16(writeheader.cmwt);
writeheader.flags = bswapLE16(writeheader.flags);
writeheader.special = bswapLE16(writeheader.special);
writeheader.msglength = bswapLE16(writeheader.msglength);
writeheader.msgoffset = bswapLE32(writeheader.msgoffset);
writeheader.reserved2 = bswapLE32(writeheader.reserved2);
fwrite(&writeheader, 1, sizeof(writeheader), f);
fwrite(Order, 1, header.ordnum, f);
if (header.insnum) fwrite(inspos, 4, header.insnum, f);
if (header.smpnum) fwrite(smppos, 4, header.smpnum, f);
if (header.patnum) fwrite(patpos, 4, header.patnum, f);
// Writing editor history information
{
#ifdef SAVEITTIMESTAMP
SYSTEMTIME systime;
FILETIME filetime;
WORD timestamp[4];
WORD nInfoEx = 1;
memset(timestamp, 0, sizeof(timestamp));
fwrite(&nInfoEx, 1, 2, f);
GetSystemTime(&systime);
SystemTimeToFileTime(&systime, &filetime);
FileTimeToDosDateTime(&filetime, ×tamp[0], ×tamp[1]);
fwrite(timestamp, 1, 8, f);
#else
WORD nInfoEx = 0;
fwrite(&nInfoEx, 1, 2, f);
#endif
}
// Writing midi cfg
if (header.flags & 0x80)
{
fwrite(&m_MidiCfg, 1, sizeof(MODMIDICFG), f);
}
// Writing pattern names
if (dwPatNamLen)
{
DWORD d = bswapLE32(0x4d414e50);
UINT len= bswapLE32(dwPatNamLen);
fwrite(&d, 1, 4, f);
fwrite(&len, 1, 4, f);
fwrite(m_lpszPatternNames, 1, dwPatNamLen, f);
}
// Writing channel Names
if (dwChnNamLen)
{
DWORD d = bswapLE32(0x4d414e43);
UINT len= bswapLE32(dwChnNamLen);
fwrite(&d, 1, 4, f);
fwrite(&len, 1, 4, f);
UINT nChnNames = dwChnNamLen / MAX_CHANNELNAME;
for (UINT inam=0; inam<nChnNames; inam++)
{
fwrite(ChnSettings[inam].szName, 1, MAX_CHANNELNAME, f);
}
}
// Writing mix plugins info
SaveMixPlugins(f, FALSE);
// Writing song message
dwPos = dwHdrPos + dwExtra + (header.insnum + header.smpnum + header.patnum) * 4;
if (header.special & 1)
{
dwPos += strlen(m_lpszSongComments) + 1;
fwrite(m_lpszSongComments, 1, strlen(m_lpszSongComments)+1, f);
}
// Writing instruments
for (UINT nins=1; nins<=header.insnum; nins++)
{
memset(&iti, 0, sizeof(iti));
iti.id = 0x49504D49; // "IMPI"
iti.trkvers = 0x211;
if (Headers[nins])
{
INSTRUMENTHEADER *penv = Headers[nins];
memset(smpcount, 0, sizeof(smpcount));
memcpy(iti.filename, penv->filename, 12);
memcpy(iti.name, penv->name, 26);
iti.mbank = penv->wMidiBank;
iti.mpr = penv->nMidiProgram;
iti.mch = penv->nMidiChannel;
iti.nna = penv->nNNA;
iti.dct = penv->nDCT;
iti.dca = penv->nDNA;
iti.fadeout = penv->nFadeOut >> 5;
iti.pps = penv->nPPS;
iti.ppc = penv->nPPC;
iti.gbv = (BYTE)(penv->nGlobalVol << 1);
iti.dfp = (BYTE)penv->nPan >> 2;
if (!(penv->dwFlags & ENV_SETPANNING)) iti.dfp |= 0x80;
iti.rv = penv->nVolSwing;
iti.rp = penv->nPanSwing;
iti.ifc = penv->nIFC;
iti.ifr = penv->nIFR;
iti.nos = 0;
for (UINT i=0; i<NOTE_MAX; i++) if (penv->Keyboard[i] < MAX_SAMPLES)
{
UINT smp = penv->Keyboard[i];
if ((smp) && (!smpcount[smp]))
{
smpcount[smp] = 1;
iti.nos++;
}
iti.keyboard[i*2] = penv->NoteMap[i] - 1;
iti.keyboard[i*2+1] = smp;
}
// Writing Volume envelope
if (penv->dwFlags & ENV_VOLUME) iti.volenv.flags |= 0x01;
if (penv->dwFlags & ENV_VOLLOOP) iti.volenv.flags |= 0x02;
if (penv->dwFlags & ENV_VOLSUSTAIN) iti.volenv.flags |= 0x04;
if (penv->dwFlags & ENV_VOLCARRY) iti.volenv.flags |= 0x08;
iti.volenv.num = (BYTE)penv->nVolEnv;
iti.volenv.lpb = (BYTE)penv->nVolLoopStart;
iti.volenv.lpe = (BYTE)penv->nVolLoopEnd;
iti.volenv.slb = penv->nVolSustainBegin;
iti.volenv.sle = penv->nVolSustainEnd;
// Writing Panning envelope
if (penv->dwFlags & ENV_PANNING) iti.panenv.flags |= 0x01;
if (penv->dwFlags & ENV_PANLOOP) iti.panenv.flags |= 0x02;
if (penv->dwFlags & ENV_PANSUSTAIN) iti.panenv.flags |= 0x04;
if (penv->dwFlags & ENV_PANCARRY) iti.panenv.flags |= 0x08;
iti.panenv.num = (BYTE)penv->nPanEnv;
iti.panenv.lpb = (BYTE)penv->nPanLoopStart;
iti.panenv.lpe = (BYTE)penv->nPanLoopEnd;
iti.panenv.slb = penv->nPanSustainBegin;
iti.panenv.sle = penv->nPanSustainEnd;
// Writing Pitch Envelope
if (penv->dwFlags & ENV_PITCH) iti.pitchenv.flags |= 0x01;
if (penv->dwFlags & ENV_PITCHLOOP) iti.pitchenv.flags |= 0x02;
if (penv->dwFlags & ENV_PITCHSUSTAIN) iti.pitchenv.flags |= 0x04;
if (penv->dwFlags & ENV_PITCHCARRY) iti.pitchenv.flags |= 0x08;
if (penv->dwFlags & ENV_FILTER) iti.pitchenv.flags |= 0x80;
iti.pitchenv.num = (BYTE)penv->nPitchEnv;
iti.pitchenv.lpb = (BYTE)penv->nPitchLoopStart;
iti.pitchenv.lpe = (BYTE)penv->nPitchLoopEnd;
iti.pitchenv.slb = (BYTE)penv->nPitchSustainBegin;
iti.pitchenv.sle = (BYTE)penv->nPitchSustainEnd;
// Writing Envelopes data
for (UINT ev=0; ev<25; ev++)
{
iti.volenv.data[ev*3] = penv->VolEnv[ev];
iti.volenv.data[ev*3+1] = penv->VolPoints[ev] & 0xFF;
iti.volenv.data[ev*3+2] = penv->VolPoints[ev] >> 8;
iti.panenv.data[ev*3] = penv->PanEnv[ev] - 32;
iti.panenv.data[ev*3+1] = penv->PanPoints[ev] & 0xFF;
iti.panenv.data[ev*3+2] = penv->PanPoints[ev] >> 8;
iti.pitchenv.data[ev*3] = penv->PitchEnv[ev] - 32;
iti.pitchenv.data[ev*3+1] = penv->PitchPoints[ev] & 0xFF;
iti.pitchenv.data[ev*3+2] = penv->PitchPoints[ev] >> 8;
}
} else
// Save Empty Instrument
{
for (UINT i=0; i<NOTE_MAX; i++) iti.keyboard[i*2] = i;
iti.ppc = 5*12;
iti.gbv = 128;
iti.dfp = 0x20;
iti.ifc = 0xFF;
}
if (!iti.nos) iti.trkvers = 0;
// Writing instrument
inspos[nins-1] = dwPos;
dwPos += sizeof(ITINSTRUMENT);
memcpy(&writeiti, &iti, sizeof(ITINSTRUMENT));
writeiti.fadeout = bswapLE16(writeiti.fadeout);
writeiti.id = bswapLE32(writeiti.id);
writeiti.trkvers = bswapLE16(writeiti.trkvers);
writeiti.mbank = bswapLE16(writeiti.mbank);
fwrite(&writeiti, 1, sizeof(ITINSTRUMENT), f);
}
// Writing sample headers
memset(&itss, 0, sizeof(itss));
for (UINT hsmp=0; hsmp<header.smpnum; hsmp++)
{
smppos[hsmp] = dwPos;
dwPos += sizeof(ITSAMPLESTRUCT);
fwrite(&itss, 1, sizeof(ITSAMPLESTRUCT), f);
}
// Writing Patterns
for (UINT npat=0; npat<header.patnum; npat++)
{
DWORD dwPatPos = dwPos;
UINT len;
if (!Patterns[npat]) continue;
patpos[npat] = dwPos;
patinfo[0] = 0;
patinfo[1] = bswapLE16(PatternSize[npat]);
patinfo[2] = 0;
patinfo[3] = 0;
// Check for empty pattern
if (PatternSize[npat] == 64)
{
MODCOMMAND *pzc = Patterns[npat];
UINT iz, nz = PatternSize[npat] * m_nChannels;
for (iz=0; iz<nz; iz++)
{
if ((pzc[iz].note) || (pzc[iz].instr)
|| (pzc[iz].volcmd) || (pzc[iz].command)) break;
}
if (iz == nz)
{
patpos[npat] = 0;
continue;
}
}
fwrite(patinfo, 8, 1, f);
dwPos += 8;
memset(chnmask, 0xFF, sizeof(chnmask));
memset(lastvalue, 0, sizeof(lastvalue));
MODCOMMAND *m = Patterns[npat];
for (UINT row=0; row<PatternSize[npat]; row++)
{
len = 0;
for (UINT ch=0; ch<m_nChannels; ch++, m++)
{
BYTE b = 0;
UINT command = m->command;
UINT param = m->param;
UINT vol = 0xFF;
UINT note = m->note;
if (note) b |= 1;
if ((note) && (note < 0x80)) note--; // 0xfe->0x80 --Toad
if (m->instr) b |= 2;
if (m->volcmd)
{
UINT volcmd = m->volcmd;
switch(volcmd)
{
case VOLCMD_VOLUME: vol = m->vol; if (vol > 64) vol = 64; break;
case VOLCMD_PANNING: vol = m->vol + 128; if (vol > 192) vol = 192; break;
case VOLCMD_VOLSLIDEUP: vol = 85 + ConvertVolParam(m->vol); break;
case VOLCMD_VOLSLIDEDOWN: vol = 95 + ConvertVolParam(m->vol); break;
case VOLCMD_FINEVOLUP: vol = 65 + ConvertVolParam(m->vol); break;
case VOLCMD_FINEVOLDOWN: vol = 75 + ConvertVolParam(m->vol); break;
case VOLCMD_VIBRATOSPEED: vol = 203 + ConvertVolParam(m->vol); break;
case VOLCMD_VIBRATO: vol = 203; break;
case VOLCMD_TONEPORTAMENTO: vol = 193 + ConvertVolParam(m->vol); break;
case VOLCMD_PORTADOWN: vol = 105 + ConvertVolParam(m->vol); break;
case VOLCMD_PORTAUP: vol = 115 + ConvertVolParam(m->vol); break;
default: vol = 0xFF;
}
}
if (vol != 0xFF) b |= 4;
if (command)
{
S3MSaveConvert(&command, ¶m, TRUE);
if (command) b |= 8;
}
// Packing information
if (b)
{
// Same note ?
if (b & 1)
{
if ((note == lastvalue[ch].note) && (lastvalue[ch].volcmd & 1))
{
b &= ~1;
b |= 0x10;
} else
{
lastvalue[ch].note = note;
lastvalue[ch].volcmd |= 1;
}
}
// Same instrument ?
if (b & 2)
{
if ((m->instr == lastvalue[ch].instr) && (lastvalue[ch].volcmd & 2))
{
b &= ~2;
b |= 0x20;
} else
{
lastvalue[ch].instr = m->instr;
lastvalue[ch].volcmd |= 2;
}
}
// Same volume column byte ?
if (b & 4)
{
if ((vol == lastvalue[ch].vol) && (lastvalue[ch].volcmd & 4))
{
b &= ~4;
b |= 0x40;
} else
{
lastvalue[ch].vol = vol;
lastvalue[ch].volcmd |= 4;
}
}