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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
|
/*
* ModEdit.cpp
* -----------
* Purpose: Song (pattern, samples, instruments) editing functions
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Mptrack.h"
#include "Mainfrm.h"
#include "Moddoc.h"
#include "Clipboard.h"
#include "dlg_misc.h"
#include "Dlsbank.h"
#include "../soundlib/modsmp_ctrl.h"
#include "../soundlib/mod_specifications.h"
#include "../soundlib/tuning.h"
#include "../soundlib/OPL.h"
#include "../common/misc_util.h"
#include "../common/mptStringBuffer.h"
#include "../common/mptFileIO.h"
#include <sstream>
// Plugin cloning
#include "../soundlib/plugins/PluginManager.h"
#include "../soundlib/plugins/PlugInterface.h"
#include "VstPresets.h"
#include "../common/FileReader.h"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
OPENMPT_NAMESPACE_BEGIN
// Change the number of channels.
// Return true on success.
bool CModDoc::ChangeNumChannels(CHANNELINDEX nNewChannels, const bool showCancelInRemoveDlg)
{
const CHANNELINDEX maxChans = m_SndFile.GetModSpecifications().channelsMax;
if(nNewChannels > maxChans)
{
CString error;
error.Format(_T("Error: Max number of channels for this file type is %u"), maxChans);
Reporting::Warning(error);
return false;
}
if(nNewChannels == GetNumChannels())
return false;
if(nNewChannels < GetNumChannels())
{
// Remove channels
CHANNELINDEX chnsToRemove = 0, maxRemoveCount = 0;
//nNewChannels = 0 means user can choose how many channels to remove
if(nNewChannels > 0)
{
chnsToRemove = GetNumChannels() - nNewChannels;
maxRemoveCount = chnsToRemove;
} else
{
chnsToRemove = 0;
maxRemoveCount = GetNumChannels();
}
CRemoveChannelsDlg rem(m_SndFile, chnsToRemove, showCancelInRemoveDlg);
CheckUsedChannels(rem.m_bKeepMask, maxRemoveCount);
if(rem.DoModal() != IDOK)
return false;
// Removing selected channels
return RemoveChannels(rem.m_bKeepMask, true);
} else
{
// Increasing number of channels
BeginWaitCursor();
std::vector<CHANNELINDEX> channels(nNewChannels, CHANNELINDEX_INVALID);
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
channels[chn] = chn;
}
bool success = (ReArrangeChannels(channels) == nNewChannels);
if(success)
{
SetModified();
UpdateAllViews(nullptr, UpdateHint().ModType());
}
EndWaitCursor();
return success;
}
}
// To remove all channels whose index corresponds to false in the keepMask vector.
// Return true on success.
bool CModDoc::RemoveChannels(const std::vector<bool> &keepMask, bool verbose)
{
CHANNELINDEX nRemainingChannels = 0;
//First calculating how many channels are to be left
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
if(keepMask[chn])
nRemainingChannels++;
}
if(nRemainingChannels == GetNumChannels() || nRemainingChannels < m_SndFile.GetModSpecifications().channelsMin)
{
if(verbose)
{
CString str;
if(nRemainingChannels == GetNumChannels())
str = _T("No channels chosen to be removed.");
else
str = _T("No removal done - channel number is already at minimum.");
Reporting::Information(str, _T("Remove Channels"));
}
return false;
}
BeginWaitCursor();
// Create new channel order, with only channels from m_bChnMask left.
std::vector<CHANNELINDEX> channels(nRemainingChannels, 0);
CHANNELINDEX i = 0;
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
if(keepMask[chn])
{
channels[i++] = chn;
}
}
const bool success = (ReArrangeChannels(channels) == nRemainingChannels);
if(success)
{
SetModified();
UpdateAllViews(nullptr, UpdateHint().ModType());
}
EndWaitCursor();
return success;
}
// Base code for adding, removing, moving and duplicating channels. Returns new number of channels on success, CHANNELINDEX_INVALID otherwise.
// The new channel vector can contain CHANNELINDEX_INVALID for adding new (empty) channels.
CHANNELINDEX CModDoc::ReArrangeChannels(const std::vector<CHANNELINDEX> &newOrder, const bool createUndoPoint)
{
//newOrder[i] tells which current channel should be placed to i:th position in
//the new order, or if i is not an index of current channels, then new channel is
//added to position i. If index of some current channel is missing from the
//newOrder-vector, then the channel gets removed.
const CHANNELINDEX newNumChannels = static_cast<CHANNELINDEX>(newOrder.size()), oldNumChannels = GetNumChannels();
auto &specs = m_SndFile.GetModSpecifications();
if(newNumChannels > specs.channelsMax || newNumChannels < specs.channelsMin)
{
CString str;
str.Format(_T("Can't apply change: Number of channels should be between %u and %u."), specs.channelsMin, specs.channelsMax);
Reporting::Error(str, _T("Rearrange Channels"));
return CHANNELINDEX_INVALID;
}
if(createUndoPoint)
{
PrepareUndoForAllPatterns(true, "Rearrange Channels");
}
CriticalSection cs;
if(oldNumChannels == newNumChannels)
{
// Optimization with no pattern re-allocation
std::vector<ModCommand> oldRow(oldNumChannels);
for(auto &pat : m_SndFile.Patterns)
{
auto m = pat.begin();
for(ROWINDEX row = 0; row < pat.GetNumRows(); row++)
{
oldRow.assign(m, m + oldNumChannels);
for(CHANNELINDEX chn = 0; chn < newNumChannels; chn++, m++)
{
if(newOrder[chn] < oldNumChannels) // Case: getting old channel to the new channel order.
*m = oldRow[newOrder[chn]];
else
*m = ModCommand::Empty();
}
}
}
} else
{
// Create all patterns first so that we can exit cleanly in case of OOM
std::vector<std::vector<ModCommand>> newPatterns;
try
{
newPatterns.resize(m_SndFile.Patterns.Size());
for(PATTERNINDEX i = 0; i < m_SndFile.Patterns.Size(); i++)
{
newPatterns[i].resize(m_SndFile.Patterns[i].GetNumRows() * newNumChannels);
}
} catch(mpt::out_of_memory e)
{
mpt::delete_out_of_memory(e);
Reporting::Error("Out of memory!", "Rearrange Channels");
return oldNumChannels;
}
m_SndFile.m_nChannels = newNumChannels;
for(PATTERNINDEX i = 0; i < m_SndFile.Patterns.Size(); i++)
{
CPattern &pat = m_SndFile.Patterns[i];
if(pat.IsValid())
{
auto mNew = newPatterns[i].begin(), mOld = pat.begin();
for(ROWINDEX row = 0; row < pat.GetNumRows(); row++, mOld += oldNumChannels)
{
for(CHANNELINDEX chn = 0; chn < newNumChannels; chn++, mNew++)
{
if(newOrder[chn] < oldNumChannels) // Case: getting old channel to the new channel order.
*mNew = mOld[newOrder[chn]];
}
}
pat.SetData(std::move(newPatterns[i]));
}
}
}
// Reverse mapping: One of the new indices of an old channel
std::vector<CHANNELINDEX> newIndex(oldNumChannels, CHANNELINDEX_INVALID);
for(CHANNELINDEX chn = 0; chn < newNumChannels; chn++)
{
if(newOrder[chn] < oldNumChannels)
{
newIndex[newOrder[chn]] = chn;
}
}
// Reassign NNA channels (note: if we increase the number of channels, the lowest-indexed NNA channels will still be lost)
const auto muteFlag = CSoundFile::GetChannelMuteFlag();
for(CHANNELINDEX chn = oldNumChannels; chn < MAX_CHANNELS; chn++)
{
auto &channel = m_SndFile.m_PlayState.Chn[chn];
CHANNELINDEX masterChn = channel.nMasterChn, newMasterChn;
if(masterChn > 0 && masterChn <= newIndex.size() && (newMasterChn = newIndex[masterChn - 1]) != CHANNELINDEX_INVALID)
channel.nMasterChn = newMasterChn + 1;
else
channel.Reset(ModChannel::resetTotal, m_SndFile, chn, muteFlag);
}
std::vector<ModChannel> chns(std::begin(m_SndFile.m_PlayState.Chn), std::begin(m_SndFile.m_PlayState.Chn) + oldNumChannels);
std::vector<ModChannelSettings> settings(std::begin(m_SndFile.ChnSettings), std::begin(m_SndFile.ChnSettings) + oldNumChannels);
std::vector<RecordGroup> recordStates(oldNumChannels);
auto chnMutePendings = m_SndFile.m_bChannelMuteTogglePending;
for(CHANNELINDEX chn = 0; chn < oldNumChannels; chn++)
{
recordStates[chn] = GetChannelRecordGroup(chn);
}
ReinitRecordState();
for(CHANNELINDEX chn = 0; chn < newNumChannels; chn++)
{
CHANNELINDEX srcChn = newOrder[chn];
if(srcChn < oldNumChannels)
{
m_SndFile.ChnSettings[chn] = settings[srcChn];
m_SndFile.m_PlayState.Chn[chn] = chns[srcChn];
SetChannelRecordGroup(chn, recordStates[srcChn]);
m_SndFile.m_bChannelMuteTogglePending[chn] = chnMutePendings[srcChn];
if(m_SndFile.m_opl)
m_SndFile.m_opl->MoveChannel(srcChn, chn);
} else
{
m_SndFile.InitChannel(chn);
SetDefaultChannelColors(chn);
}
}
// Reset MOD panning (won't affect other module formats)
m_SndFile.SetupMODPanning();
m_SndFile.InitAmigaResampler();
return newNumChannels;
}
// Base code for adding, removing, moving and duplicating samples. Returns new number of samples on success, SAMPLEINDEX_INVALID otherwise.
// The new sample vector can contain SAMPLEINDEX_INVALID for adding new (empty) samples.
// newOrder indices are zero-based, i.e. newOrder[0] will define the contents of the first sample slot.
SAMPLEINDEX CModDoc::ReArrangeSamples(const std::vector<SAMPLEINDEX> &newOrder)
{
if(newOrder.size() > m_SndFile.GetModSpecifications().samplesMax)
{
return SAMPLEINDEX_INVALID;
}
CriticalSection cs;
const SAMPLEINDEX oldNumSamples = m_SndFile.GetNumSamples(), newNumSamples = static_cast<SAMPLEINDEX>(newOrder.size());
std::vector<int> sampleCount(oldNumSamples + 1, 0);
std::vector<ModSample> sampleHeaders(oldNumSamples + 1);
std::vector<SAMPLEINDEX> newIndex(oldNumSamples + 1, 0); // One of the new indexes for the old sample
std::vector<std::string> sampleNames(oldNumSamples + 1);
std::vector<mpt::PathString> samplePaths(oldNumSamples + 1);
for(SAMPLEINDEX i = 0; i < newNumSamples; i++)
{
const SAMPLEINDEX origSlot = newOrder[i];
if(origSlot > 0 && origSlot <= oldNumSamples)
{
sampleCount[origSlot]++;
sampleHeaders[origSlot] = m_SndFile.GetSample(origSlot);
if(!newIndex[origSlot])
newIndex[origSlot] = i + 1;
}
}
// First, delete all samples that will be removed anyway.
for(SAMPLEINDEX i = 1; i < sampleCount.size(); i++)
{
if(sampleCount[i] == 0)
{
m_SndFile.DestroySample(i);
GetSampleUndo().ClearUndo(i);
}
sampleNames[i] = m_SndFile.m_szNames[i];
samplePaths[i] = m_SndFile.GetSamplePath(i);
}
// Remove sample data references from now unused slots.
for(SAMPLEINDEX i = newNumSamples + 1; i <= oldNumSamples; i++)
{
m_SndFile.GetSample(i).pData.pSample = nullptr;
m_SndFile.GetSample(i).nLength = 0;
m_SndFile.m_szNames[i] = "";
}
// Now, create new sample list.
m_SndFile.m_nSamples = std::max(m_SndFile.m_nSamples, newNumSamples); // Avoid assertions when using GetSample()...
for(SAMPLEINDEX i = 0; i < newNumSamples; i++)
{
const SAMPLEINDEX origSlot = newOrder[i];
ModSample &target = m_SndFile.GetSample(i + 1);
if(origSlot > 0 && origSlot <= oldNumSamples)
{
// Copy an original sample.
target = sampleHeaders[origSlot];
if(--sampleCount[origSlot] > 0 && sampleHeaders[origSlot].HasSampleData())
{
// This sample slot is referenced multiple times, so we have to copy the actual sample.
if(target.CopyWaveform(sampleHeaders[origSlot]))
{
target.PrecomputeLoops(m_SndFile, false);
} else
{
Reporting::Error("Cannot duplicate sample - out of memory!");
}
}
m_SndFile.m_szNames[i + 1] = sampleNames[origSlot];
m_SndFile.SetSamplePath(i + 1, samplePaths[origSlot]);
} else
{
// Invalid sample reference.
target.pData.pSample = nullptr;
target.Initialize(m_SndFile.GetType());
m_SndFile.m_szNames[i + 1] = "";
m_SndFile.ResetSamplePath(i + 1);
}
}
GetSampleUndo().RearrangeSamples(newIndex);
const auto muteFlag = CSoundFile::GetChannelMuteFlag();
for(CHANNELINDEX c = 0; c < std::size(m_SndFile.m_PlayState.Chn); c++)
{
ModChannel &chn = m_SndFile.m_PlayState.Chn[c];
for(SAMPLEINDEX i = 1; i <= oldNumSamples; i++)
{
if(chn.pModSample == &m_SndFile.GetSample(i))
{
chn.pModSample = &m_SndFile.GetSample(newIndex[i]);
if(i == 0 || i > newNumSamples)
{
chn.Reset(ModChannel::resetTotal, m_SndFile, c, muteFlag);
}
break;
}
}
}
m_SndFile.m_nSamples = newNumSamples;
if(m_SndFile.GetNumInstruments())
{
// Instrument mode: Update sample maps.
for(INSTRUMENTINDEX i = 0; i <= m_SndFile.GetNumInstruments(); i++)
{
ModInstrument *ins = m_SndFile.Instruments[i];
if(ins == nullptr)
{
continue;
}
GetInstrumentUndo().RearrangeSamples(i, newIndex);
for(auto &sample : ins->Keyboard)
{
if(sample < newIndex.size())
sample = newIndex[sample];
else
sample = 0;
}
}
} else
{
PrepareUndoForAllPatterns(false, "Rearrange Samples");
m_SndFile.Patterns.ForEachModCommand([&newIndex] (ModCommand &m)
{
if(!m.IsPcNote() && m.instr < newIndex.size())
{
m.instr = static_cast<ModCommand::INSTR>(newIndex[m.instr]);
}
});
}
return GetNumSamples();
}
// Base code for adding, removing, moving and duplicating instruments. Returns new number of instruments on success, INSTRUMENTINDEX_INVALID otherwise.
// The new instrument vector can contain INSTRUMENTINDEX_INVALID for adding new (empty) instruments.
// newOrder indices are zero-based, i.e. newOrder[0] will define the contents of the first instrument slot.
INSTRUMENTINDEX CModDoc::ReArrangeInstruments(const std::vector<INSTRUMENTINDEX> &newOrder, deleteInstrumentSamples removeSamples)
{
if(newOrder.size() > m_SndFile.GetModSpecifications().instrumentsMax || GetNumInstruments() == 0)
{
return INSTRUMENTINDEX_INVALID;
}
CriticalSection cs;
const INSTRUMENTINDEX oldNumInstruments = m_SndFile.GetNumInstruments(), newNumInstruments = static_cast<INSTRUMENTINDEX>(newOrder.size());
std::vector<ModInstrument> instrumentHeaders(oldNumInstruments + 1);
std::vector<INSTRUMENTINDEX> newIndex(oldNumInstruments + 1, 0); // One of the new indexes for the old instrument
for(INSTRUMENTINDEX i = 0; i < newNumInstruments; i++)
{
const INSTRUMENTINDEX origSlot = newOrder[i];
if(origSlot > 0 && origSlot <= oldNumInstruments)
{
if(m_SndFile.Instruments[origSlot] != nullptr)
instrumentHeaders[origSlot] = *m_SndFile.Instruments[origSlot];
newIndex[origSlot] = i + 1;
}
}
// Delete unused instruments first.
for(INSTRUMENTINDEX i = 1; i <= oldNumInstruments; i++)
{
if(newIndex[i] == 0)
{
m_SndFile.DestroyInstrument(i, removeSamples);
}
}
m_SndFile.m_nInstruments = newNumInstruments;
// Now, create new instrument list.
for(INSTRUMENTINDEX i = 0; i < newNumInstruments; i++)
{
ModInstrument *ins = m_SndFile.AllocateInstrument(i + 1);
if(ins == nullptr)
{
continue;
}
const INSTRUMENTINDEX origSlot = newOrder[i];
if(origSlot > 0 && origSlot <= oldNumInstruments)
{
// Copy an original instrument.
*ins = instrumentHeaders[origSlot];
}
}
// Free unused instruments
for(INSTRUMENTINDEX i = newNumInstruments + 1; i <= oldNumInstruments; i++)
{
m_SndFile.DestroyInstrument(i, doNoDeleteAssociatedSamples);
}
PrepareUndoForAllPatterns(false, "Rearrange Instruments");
GetInstrumentUndo().RearrangeInstruments(newIndex);
m_SndFile.Patterns.ForEachModCommand([&newIndex] (ModCommand &m)
{
if(!m.IsPcNote() && m.instr < newIndex.size())
{
m.instr = static_cast<ModCommand::INSTR>(newIndex[m.instr]);
}
});
return GetNumInstruments();
}
SEQUENCEINDEX CModDoc::ReArrangeSequences(const std::vector<SEQUENCEINDEX> &newOrder)
{
CriticalSection cs;
return m_SndFile.Order.Rearrange(newOrder) ? m_SndFile.Order.GetNumSequences() : SEQUENCEINDEX_INVALID;
}
bool CModDoc::ConvertInstrumentsToSamples()
{
if(!m_SndFile.GetNumInstruments())
return false;
GetInstrumentUndo().ClearUndo();
m_SndFile.Patterns.ForEachModCommand([&] (ModCommand &m)
{
if(m.instr && !m.IsPcNote())
{
ModCommand::INSTR instr = m.instr, newinstr = 0;
ModCommand::NOTE note = m.note, newnote = note;
if(ModCommand::IsNote(note))
note = note - NOTE_MIN;
else
note = NOTE_MIDDLEC - NOTE_MIN;
if((instr < MAX_INSTRUMENTS) && (m_SndFile.Instruments[instr]))
{
const ModInstrument *pIns = m_SndFile.Instruments[instr];
newinstr = static_cast<ModCommand::INSTR>(pIns->Keyboard[note]);
newnote = pIns->NoteMap[note];
if(pIns->Keyboard[note] > Util::MaxValueOfType(m.instr))
newinstr = 0;
}
m.instr = newinstr;
if(m.IsNote())
{
m.note = newnote;
}
}
});
return true;
}
bool CModDoc::ConvertSamplesToInstruments()
{
const INSTRUMENTINDEX instrumentMax = m_SndFile.GetModSpecifications().instrumentsMax;
if(GetNumInstruments() > 0 || instrumentMax == 0)
return false;
// If there is no actual sample data, don't bother creating any instruments
bool anySamples = false;
for(SAMPLEINDEX smp = 1; smp <= m_SndFile.m_nSamples; smp++)
{
if(m_SndFile.GetSample(smp).HasSampleData())
{
anySamples = true;
break;
}
}
if(!anySamples)
return true;
m_SndFile.m_nInstruments = std::min(m_SndFile.GetNumSamples(), instrumentMax);
for(SAMPLEINDEX smp = 1; smp <= m_SndFile.m_nInstruments; smp++)
{
const bool muted = IsSampleMuted(smp);
MuteSample(smp, false);
ModInstrument *instrument = m_SndFile.AllocateInstrument(smp, smp);
if(instrument == nullptr)
{
ErrorBox(IDS_ERR_OUTOFMEMORY, CMainFrame::GetMainFrame());
return false;
}
InitializeInstrument(instrument);
instrument->name = m_SndFile.m_szNames[smp];
MuteInstrument(smp, muted);
}
return true;
}
PLUGINDEX CModDoc::RemovePlugs(const std::vector<bool> &keepMask)
{
// Remove all plugins whose keepMask[plugindex] is false.
PLUGINDEX nRemoved = 0;
const PLUGINDEX maxPlug = std::min(MAX_MIXPLUGINS, static_cast<PLUGINDEX>(keepMask.size()));
CriticalSection cs;
for(PLUGINDEX nPlug = 0; nPlug < maxPlug; nPlug++)
{
SNDMIXPLUGIN &plug = m_SndFile.m_MixPlugins[nPlug];
if(keepMask[nPlug])
{
continue;
}
if(plug.pMixPlugin || plug.IsValidPlugin())
{
nRemoved++;
}
plug.Destroy();
plug = {};
for(PLUGINDEX srcPlugSlot = 0; srcPlugSlot < nPlug; srcPlugSlot++)
{
SNDMIXPLUGIN &srcPlug = GetSoundFile().m_MixPlugins[srcPlugSlot];
if(srcPlug.GetOutputPlugin() == nPlug)
{
srcPlug.SetOutputToMaster();
UpdateAllViews(nullptr, PluginHint(static_cast<PLUGINDEX>(srcPlugSlot + 1)).Info());
}
}
UpdateAllViews(nullptr, PluginHint(static_cast<PLUGINDEX>(nPlug + 1)).Info().Names());
}
if(nRemoved && m_SndFile.GetModSpecifications().supportsPlugins)
SetModified();
return nRemoved;
}
bool CModDoc::RemovePlugin(PLUGINDEX plugin)
{
if(plugin >= MAX_MIXPLUGINS)
return false;
std::vector<bool> keepMask(MAX_MIXPLUGINS, true);
keepMask[plugin] = false;
return RemovePlugs(keepMask) == 1;
}
// Clone a plugin slot (source does not necessarily have to be from the current module)
void CModDoc::ClonePlugin(SNDMIXPLUGIN &target, const SNDMIXPLUGIN &source)
{
IMixPlugin *srcVstPlug = source.pMixPlugin;
target.Destroy();
target = source;
// Don't want this plugin to be accidentally erased again...
target.pMixPlugin = nullptr;
if(target.editorX != int32_min)
{
// Move target editor a bit to visually distinguish it from the original editor
int addPixels = Util::ScalePixels(16, CMainFrame::GetMainFrame()->m_hWnd);
target.editorX += addPixels;
target.editorY += addPixels;
}
#ifndef NO_PLUGINS
if(theApp.GetPluginManager()->CreateMixPlugin(target, GetSoundFile()))
{
IMixPlugin *newVstPlug = target.pMixPlugin;
newVstPlug->SetCurrentProgram(srcVstPlug->GetCurrentProgram());
std::ostringstream f(std::ios::out | std::ios::binary);
if(VSTPresets::SaveFile(f, *srcVstPlug, false))
{
const std::string data = f.str();
FileReader file(mpt::as_span(data));
VSTPresets::LoadFile(file, *newVstPlug);
}
}
#endif // !NO_PLUGINS
}
PATTERNINDEX CModDoc::InsertPattern(ROWINDEX rows, ORDERINDEX ord)
{
if(ord != ORDERINDEX_INVALID && m_SndFile.Order().GetLengthTailTrimmed() >= m_SndFile.GetModSpecifications().ordersMax)
return PATTERNINDEX_INVALID;
PATTERNINDEX pat = m_SndFile.Patterns.InsertAny(rows, true);
if(pat != PATTERNINDEX_INVALID)
{
if(ord != ORDERINDEX_INVALID)
{
m_SndFile.Order().insert(ord, 1, pat);
}
SetModified();
}
return pat;
}
SAMPLEINDEX CModDoc::InsertSample()
{
SAMPLEINDEX i = m_SndFile.GetNextFreeSample();
if((i > std::numeric_limits<ModCommand::INSTR>::max() && !m_SndFile.GetNumInstruments()) || i == SAMPLEINDEX_INVALID)
{
ErrorBox(IDS_ERR_TOOMANYSMP, CMainFrame::GetMainFrame());
return SAMPLEINDEX_INVALID;
}
const bool newSlot = (i > m_SndFile.GetNumSamples());
if(newSlot || !m_SndFile.m_szNames[i][0])
m_SndFile.m_szNames[i] = "untitled";
if(newSlot)
m_SndFile.m_nSamples = i;
m_SndFile.GetSample(i).Initialize(m_SndFile.GetType());
m_SndFile.ResetSamplePath(i);
SetModified();
return i;
}
// Insert a new instrument assigned to sample nSample or duplicate instrument "duplicateSource".
// If "sample" is invalid, an appropriate sample slot is selected. 0 means "no sample".
INSTRUMENTINDEX CModDoc::InsertInstrument(SAMPLEINDEX sample, INSTRUMENTINDEX duplicateSource, bool silent)
{
if(m_SndFile.GetModSpecifications().instrumentsMax == 0)
return INSTRUMENTINDEX_INVALID;
ModInstrument *pDup = nullptr;
if(duplicateSource > 0 && duplicateSource <= m_SndFile.m_nInstruments)
{
pDup = m_SndFile.Instruments[duplicateSource];
}
if(!m_SndFile.GetNumInstruments() && (m_SndFile.GetNumSamples() > 1 || m_SndFile.GetSample(1).HasSampleData()))
{
bool doConvert = true;
if(!silent)
{
ConfirmAnswer result = Reporting::Confirm("Convert existing samples to instruments first?", true);
if(result == cnfCancel)
{
return INSTRUMENTINDEX_INVALID;
}
doConvert = (result == cnfYes);
}
if(doConvert)
{
if(!ConvertSamplesToInstruments())
{
return INSTRUMENTINDEX_INVALID;
}
}
}
const INSTRUMENTINDEX newins = m_SndFile.GetNextFreeInstrument();
if(newins == INSTRUMENTINDEX_INVALID)
{
if(!silent)
{
ErrorBox(IDS_ERR_TOOMANYINS, CMainFrame::GetMainFrame());
}
return INSTRUMENTINDEX_INVALID;
} else if(newins > m_SndFile.GetNumInstruments())
{
m_SndFile.m_nInstruments = newins;
}
// Determine which sample slot to use
SAMPLEINDEX newsmp = 0;
if(sample < m_SndFile.GetModSpecifications().samplesMax)
{
// Use specified slot
newsmp = sample;
} else if(!pDup)
{
newsmp = m_SndFile.GetNextFreeSample(newins);
if(newsmp > m_SndFile.GetNumSamples())
{
// Add a new sample
const SAMPLEINDEX inssmp = InsertSample();
if(inssmp != SAMPLEINDEX_INVALID)
newsmp = inssmp;
}
}
CriticalSection cs;
ModInstrument *pIns = m_SndFile.AllocateInstrument(newins, newsmp);
if(pIns == nullptr)
{
if(!silent)
{
cs.Leave();
ErrorBox(IDS_ERR_OUTOFMEMORY, CMainFrame::GetMainFrame());
}
return INSTRUMENTINDEX_INVALID;
}
InitializeInstrument(pIns);
if(pDup)
{
*pIns = *pDup;
}
SetModified();
return newins;
}
// Load default instrument values for inserting new instrument during editing
void CModDoc::InitializeInstrument(ModInstrument *pIns)
{
pIns->pluginVolumeHandling = TrackerSettings::Instance().DefaultPlugVolumeHandling;
}
// Try to set up a new instrument that is linked to a given plugin
INSTRUMENTINDEX CModDoc::InsertInstrumentForPlugin(PLUGINDEX plug)
{
#ifndef NO_PLUGINS
const bool first = (GetNumInstruments() == 0);
INSTRUMENTINDEX instr = InsertInstrument(0, INSTRUMENTINDEX_INVALID, true);
if(instr == INSTRUMENTINDEX_INVALID)
return INSTRUMENTINDEX_INVALID;
ModInstrument &ins = *m_SndFile.Instruments[instr];
ins.name = mpt::ToCharset(m_SndFile.GetCharsetInternal(), MPT_UFORMAT("{}: {}")(plug + 1, m_SndFile.m_MixPlugins[plug].GetName()));
ins.filename = mpt::ToCharset(m_SndFile.GetCharsetInternal(), m_SndFile.m_MixPlugins[plug].GetLibraryName());
ins.nMixPlug = plug + 1;
ins.nMidiChannel = 1;
InstrumentHint hint = InstrumentHint(instr).Info().Envelope().Names();
if(first)
hint.ModType();
UpdateAllViews(nullptr, hint);
if(m_SndFile.GetModSpecifications().supportsPlugins)
{
SetModified();
}
return instr;
#else
return INSTRUMENTINDEX_INVALID;
#endif
}
INSTRUMENTINDEX CModDoc::HasInstrumentForPlugin(PLUGINDEX plug) const
{
for(INSTRUMENTINDEX i = 1; i <= GetNumInstruments(); i++)
{
if(m_SndFile.Instruments[i] != nullptr && m_SndFile.Instruments[i]->nMixPlug == plug + 1)
{
return i;
}
}
return INSTRUMENTINDEX_INVALID;
}
bool CModDoc::RemoveOrder(SEQUENCEINDEX nSeq, ORDERINDEX nOrd)
{
if(nSeq >= m_SndFile.Order.GetNumSequences() || nOrd >= m_SndFile.Order(nSeq).size())
return false;
CriticalSection cs;
m_SndFile.Order(nSeq).Remove(nOrd, nOrd);
SetModified();
return true;
}
bool CModDoc::RemovePattern(PATTERNINDEX nPat)
{
if(m_SndFile.Patterns.IsValidPat(nPat))
{
CriticalSection cs;
GetPatternUndo().PrepareUndo(nPat, 0, 0, GetNumChannels(), m_SndFile.Patterns[nPat].GetNumRows(), "Remove Pattern");
m_SndFile.Patterns.Remove(nPat);
SetModified();
return true;
}
return false;
}
bool CModDoc::RemoveSample(SAMPLEINDEX nSmp)
{
if((nSmp) && (nSmp <= m_SndFile.GetNumSamples()))
{
CriticalSection cs;
m_SndFile.DestroySample(nSmp);
m_SndFile.m_szNames[nSmp] = "";
while((m_SndFile.GetNumSamples() > 1)
&& (!m_SndFile.m_szNames[m_SndFile.GetNumSamples()][0])
&& (!m_SndFile.GetSample(m_SndFile.GetNumSamples()).HasSampleData()))
{
m_SndFile.m_nSamples--;
}
SetModified();
return true;
}
return false;
}
bool CModDoc::RemoveInstrument(INSTRUMENTINDEX nIns)
{
if((nIns) && (nIns <= m_SndFile.GetNumInstruments()) && (m_SndFile.Instruments[nIns]))
{
ConfirmAnswer result = cnfNo;
if(!m_SndFile.Instruments[nIns]->GetSamples().empty())
result = Reporting::Confirm("Remove samples associated with an instrument if they are unused?", "Removing instrument", true);
if(result == cnfCancel)
{
return false;
}
if(m_SndFile.DestroyInstrument(nIns, (result == cnfYes) ? deleteAssociatedSamples : doNoDeleteAssociatedSamples))
{
CriticalSection cs;
if(nIns == m_SndFile.m_nInstruments)
m_SndFile.m_nInstruments--;
bool instrumentsLeft = std::find_if(std::begin(m_SndFile.Instruments), std::end(m_SndFile.Instruments), [](ModInstrument *ins) { return ins != nullptr; }) != std::end(m_SndFile.Instruments);
if(!instrumentsLeft)
m_SndFile.m_nInstruments = 0;
SetModified();
return true;
}
}
return false;
}
bool CModDoc::MoveOrder(ORDERINDEX sourceOrd, ORDERINDEX destOrd, bool update, bool copy, SEQUENCEINDEX sourceSeq, SEQUENCEINDEX destSeq)
{
if(sourceSeq == SEQUENCEINDEX_INVALID)
sourceSeq = m_SndFile.Order.GetCurrentSequenceIndex();
if(destSeq == SEQUENCEINDEX_INVALID)
destSeq = m_SndFile.Order.GetCurrentSequenceIndex();
if(std::max(sourceSeq, destSeq) >= m_SndFile.Order.GetNumSequences())
return false;
const ORDERINDEX maxOrders = m_SndFile.GetModSpecifications().ordersMax;
if(destOrd > maxOrders)
return false;
if(destOrd == maxOrders && (sourceSeq != destSeq || copy))
return false;
auto &sourceSequence = m_SndFile.Order(sourceSeq);
const PATTERNINDEX sourcePat = sourceOrd < sourceSequence.size() ? sourceSequence[sourceOrd] : sourceSequence.GetInvalidPatIndex();
// Delete source
if(!copy)
{
sourceSequence.Remove(sourceOrd, sourceOrd);
if(sourceOrd < destOrd && sourceSeq == destSeq)
destOrd--;
}
// Insert at dest
m_SndFile.Order(destSeq).insert(destOrd, 1, sourcePat);
if(update)
{
UpdateAllViews(nullptr, SequenceHint().Data());
}
return true;
}
BOOL CModDoc::ExpandPattern(PATTERNINDEX nPattern)
{
ROWINDEX numRows;
if(!m_SndFile.Patterns.IsValidPat(nPattern)
|| (numRows = m_SndFile.Patterns[nPattern].GetNumRows()) > m_SndFile.GetModSpecifications().patternRowsMax / 2)
{
return false;
}
BeginWaitCursor();
CriticalSection cs;
GetPatternUndo().PrepareUndo(nPattern, 0, 0, GetNumChannels(), numRows, "Expand Pattern");
bool success = m_SndFile.Patterns[nPattern].Expand();
cs.Leave();
EndWaitCursor();
if(success)
{
SetModified();
UpdateAllViews(NULL, PatternHint(nPattern).Data(), NULL);
} else
{
GetPatternUndo().RemoveLastUndoStep();
}
return success;
}
BOOL CModDoc::ShrinkPattern(PATTERNINDEX nPattern)
{
ROWINDEX numRows;
if(!m_SndFile.Patterns.IsValidPat(nPattern)
|| (numRows = m_SndFile.Patterns[nPattern].GetNumRows()) < m_SndFile.GetModSpecifications().patternRowsMin * 2)
{
return false;
}
BeginWaitCursor();
CriticalSection cs;
GetPatternUndo().PrepareUndo(nPattern, 0, 0, GetNumChannels(), numRows, "Shrink Pattern");
bool success = m_SndFile.Patterns[nPattern].Shrink();
cs.Leave();
EndWaitCursor();
if(success)
{
SetModified();
UpdateAllViews(NULL, PatternHint(nPattern).Data(), NULL);
} else
{
GetPatternUndo().RemoveLastUndoStep();
}
return success;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Copy/Paste envelope
static constexpr const char pszEnvHdr[] = "ModPlug Tracker Envelope\r\n";
static constexpr const char pszEnvFmt[] = "%d,%d,%d,%d,%d,%d,%d,%d\r\n";
static bool EnvelopeToString(CStringA &s, const InstrumentEnvelope &env)
{
// We don't want to copy empty envelopes
if(env.empty())
{
return false;
}
s.Preallocate(2048);
s = pszEnvHdr;
s.AppendFormat(pszEnvFmt, env.size(), env.nSustainStart, env.nSustainEnd, env.nLoopStart, env.nLoopEnd, env.dwFlags[ENV_SUSTAIN] ? 1 : 0, env.dwFlags[ENV_LOOP] ? 1 : 0, env.dwFlags[ENV_CARRY] ? 1 : 0);
for(auto &p : env)
{
s.AppendFormat("%d,%d\r\n", p.tick, p.value);
}
// Writing release node
s.AppendFormat("%u\r\n", env.nReleaseNode);
return true;
}
static bool StringToEnvelope(const std::string_view &s, InstrumentEnvelope &env, const CModSpecifications &specs)
{
uint32 susBegin = 0, susEnd = 0, loopBegin = 0, loopEnd = 0, bSus = 0, bLoop = 0, bCarry = 0, nPoints = 0, releaseNode = ENV_RELEASE_NODE_UNSET;
size_t length = s.size(), pos = std::size(pszEnvHdr) - 1;
if(length <= pos || mpt::CompareNoCaseAscii(s.data(), pszEnvHdr, pos - 2))
{
return false;
}
sscanf(&s[pos], pszEnvFmt, &nPoints, &susBegin, &susEnd, &loopBegin, &loopEnd, &bSus, &bLoop, &bCarry);
while(pos < length && s[pos] != '\r' && s[pos] != '\n')
pos++;
nPoints = std::min(nPoints, static_cast<uint32>(specs.envelopePointsMax));
if(susEnd >= nPoints)
susEnd = 0;
if(susBegin > susEnd)
susBegin = susEnd;
if(loopEnd >= nPoints)
loopEnd = 0;
if(loopBegin > loopEnd)
loopBegin = loopEnd;
try
{
env.resize(nPoints);
} catch(mpt::out_of_memory e)
{
mpt::delete_out_of_memory(e);
return false;
}
env.nSustainStart = static_cast<decltype(env.nSustainStart)>(susBegin);
env.nSustainEnd = static_cast<decltype(env.nSustainEnd)>(susEnd);
env.nLoopStart = static_cast<decltype(env.nLoopStart)>(loopBegin);
env.nLoopEnd = static_cast<decltype(env.nLoopEnd)>(loopEnd);
env.nReleaseNode = static_cast<decltype(env.nReleaseNode)>(releaseNode);
env.dwFlags.set(ENV_LOOP, bLoop != 0);
env.dwFlags.set(ENV_SUSTAIN, bSus != 0);
env.dwFlags.set(ENV_CARRY, bCarry != 0);
env.dwFlags.set(ENV_ENABLED, nPoints > 0);
int oldn = 0;
for(auto &p : env)
{
while(pos < length && (s[pos] < '0' || s[pos] > '9'))
pos++;
if(pos >= length)
break;
int n1 = atoi(&s[pos]);
while(pos < length && s[pos] != ',')
pos++;
while(pos < length && (s[pos] < '0' || s[pos] > '9'))
pos++;
if(pos >= length)
break;
int n2 = atoi(&s[pos]);
if(n1 < oldn)
n1 = oldn + 1;
Limit(n2, ENVELOPE_MIN, ENVELOPE_MAX);
p.tick = (uint16)n1;
p.value = (uint8)n2;
oldn = n1;
while(pos < length && s[pos] != '\r' && s[pos] != '\n')
pos++;
if(pos >= length)
break;
}
env.Sanitize();
// Read release node information.
env.nReleaseNode = ENV_RELEASE_NODE_UNSET;
if(pos < length)
{
auto r = static_cast<decltype(env.nReleaseNode)>(atoi(&s[pos]));
if(r == 0 || r >= nPoints || !specs.hasReleaseNode)
r = ENV_RELEASE_NODE_UNSET;
env.nReleaseNode = r;
}
return true;
}
bool CModDoc::CopyEnvelope(INSTRUMENTINDEX nIns, EnvelopeType nEnv)
{
CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
if((nIns < 1) || (nIns > m_SndFile.m_nInstruments) || (!m_SndFile.Instruments[nIns]) || (!pMainFrm))
return false;
BeginWaitCursor();
const ModInstrument *pIns = m_SndFile.Instruments[nIns];
if(pIns == nullptr)
return false;
CStringA s;
EnvelopeToString(s, pIns->GetEnvelope(nEnv));
int memSize = s.GetLength() + 1;
Clipboard clipboard(CF_TEXT, memSize);
if(auto p = clipboard.As<char>())
{
memcpy(p, s.GetString(), memSize);
}
EndWaitCursor();
return true;
}
bool CModDoc::SaveEnvelope(INSTRUMENTINDEX ins, EnvelopeType env, const mpt::PathString &fileName)
{
if(ins < 1 || ins > m_SndFile.m_nInstruments || !m_SndFile.Instruments[ins])
return false;
BeginWaitCursor();
const ModInstrument *pIns = m_SndFile.Instruments[ins];
if(pIns == nullptr)
return false;
CStringA s;
EnvelopeToString(s, pIns->GetEnvelope(env));
bool ok = false;
try
{
mpt::SafeOutputFile sf(fileName, std::ios::binary, mpt::FlushModeFromBool(TrackerSettings::Instance().MiscFlushFileBuffersOnSave));
mpt::ofstream &f = sf;
f.exceptions(f.exceptions() | std::ios::badbit | std::ios::failbit);
if(f)
ok = mpt::IO::WriteRaw(f, s.GetString(), s.GetLength());
else
ok = false;
} catch(const std::exception &)
{
ok = false;
}
EndWaitCursor();
return ok;
}
bool CModDoc::PasteEnvelope(INSTRUMENTINDEX ins, EnvelopeType env)
{
CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
if(ins < 1 || ins > m_SndFile.m_nInstruments || !m_SndFile.Instruments[ins] || !pMainFrm)
return false;
BeginWaitCursor();
Clipboard clipboard(CF_TEXT);
auto data = clipboard.GetString();
if(!data.length())
{
EndWaitCursor();
return false;
}
bool result = StringToEnvelope(data, m_SndFile.Instruments[ins]->GetEnvelope(env), m_SndFile.GetModSpecifications());
EndWaitCursor();
return result;
}
bool CModDoc::LoadEnvelope(INSTRUMENTINDEX nIns, EnvelopeType nEnv, const mpt::PathString &fileName)
{
InputFile f(fileName, TrackerSettings::Instance().MiscCacheCompleteFileBeforeLoading);
if(nIns < 1 || nIns > m_SndFile.m_nInstruments || !m_SndFile.Instruments[nIns] || !f.IsValid())
return false;
BeginWaitCursor();
FileReader file = GetFileReader(f);
std::string data;
file.ReadNullString(data, 1 << 16);
bool result = StringToEnvelope(data, m_SndFile.Instruments[nIns]->GetEnvelope(nEnv), m_SndFile.GetModSpecifications());
EndWaitCursor();
return result;
}
// Check which channels contain note data. maxRemoveCount specified how many empty channels are reported at max.
void CModDoc::CheckUsedChannels(std::vector<bool> &usedMask, CHANNELINDEX maxRemoveCount) const
{
// Checking for unused channels
CHANNELINDEX chn = GetNumChannels();
usedMask.assign(chn, true);
while(chn-- > 0)
{
if(IsChannelUnused(chn))
{
usedMask[chn] = false;
// Found enough empty channels yet?
if((--maxRemoveCount) == 0)
break;
}
}
}
// Check if a given channel contains note data or global effects.
bool CModDoc::IsChannelUnused(CHANNELINDEX nChn) const
{
const CHANNELINDEX nChannels = GetNumChannels();
if(nChn >= nChannels)
{
return true;
}
for(auto &pat : m_SndFile.Patterns)
{
if(pat.IsValid())
{
const ModCommand *p = pat.GetpModCommand(0, nChn);
for(ROWINDEX row = pat.GetNumRows(); row > 0; row--, p += nChannels)
{
if(p->IsNote() || p->IsInstrPlug() || p->IsGlobalCommand())
return false;
}
}
}
return true;
}
bool CModDoc::IsSampleUsed(SAMPLEINDEX sample, bool searchInMutedChannels) const
{
if(!sample || sample > GetNumSamples())
return false;
if(GetNumInstruments())
{
for(INSTRUMENTINDEX i = 1; i <= GetNumInstruments(); i++)
{
if(m_SndFile.IsSampleReferencedByInstrument(sample, i))
return true;
}
} else
{
for(const auto &pattern : m_SndFile.Patterns)
{
if(!pattern.IsValid())
continue;
auto m = pattern.cbegin();
for(ROWINDEX row = 0; row < pattern.GetNumRows(); row++)
{
for(CHANNELINDEX chn = 0; chn < pattern.GetNumChannels(); chn++, m++)
{
if(searchInMutedChannels || !m_SndFile.ChnSettings[chn].dwFlags[CHN_MUTE])
{
if(m->instr == sample && !m->IsPcNote())
return true;
}
}
}
}
}
return false;
}
bool CModDoc::IsInstrumentUsed(INSTRUMENTINDEX instr, bool searchInMutedChannels) const
{
if(instr < 1 || instr > GetNumInstruments())
return false;
for(const auto &pattern : m_SndFile.Patterns)
{
if(!pattern.IsValid())
continue;
auto m = pattern.cbegin();
for(ROWINDEX row = 0; row < pattern.GetNumRows(); row++)
{
for(CHANNELINDEX chn = 0; chn < pattern.GetNumChannels(); chn++, m++)
{
if(searchInMutedChannels || !m_SndFile.ChnSettings[chn].dwFlags[CHN_MUTE])
{
if(m->instr == instr && !m->IsPcNote())
return true;
}
}
}
}
return false;
}
// Convert module's default global volume to a pattern command.
bool CModDoc::GlobalVolumeToPattern()
{
bool result = false;
if(m_SndFile.GetModSpecifications().HasCommand(CMD_GLOBALVOLUME))
{
for(PATTERNINDEX pat : m_SndFile.Order())
{
if(m_SndFile.Patterns[pat].WriteEffect(EffectWriter(CMD_GLOBALVOLUME, mpt::saturate_cast<ModCommand::PARAM>(m_SndFile.m_nDefaultGlobalVolume * 64 / MAX_GLOBAL_VOLUME)).RetryNextRow()))
{
result = true;
break;
}
}
}
m_SndFile.m_nDefaultGlobalVolume = MAX_GLOBAL_VOLUME;
return result;
}
SAMPLEINDEX CModDoc::GetSampleIndex(const ModCommand &m, ModCommand::INSTR lastInstr) const
{
if(m.IsPcNote())
return 0;
return m_SndFile.GetSampleIndex(m.note, m.instr > 0 ? m.instr : lastInstr);
}
int CModDoc::GetInstrumentGroupSize(INSTRUMENTINDEX instr) const
{
const ModInstrument *ins;
if(instr > 0 && instr <= GetNumInstruments()
&& (ins = m_SndFile.Instruments[instr]) != nullptr && ins->pTuning != nullptr
&& ins->pTuning->GetGroupSize() != 0)
{
return ins->pTuning->GetGroupSize();
}
return 12;
}
int CModDoc::GetBaseNote(INSTRUMENTINDEX instr) const
{
// This may look a bit strange (using -12 and -4 instead of just -5 in the second part) but this is to keep custom tunings centered around middle-C on the keyboard.
return NOTE_MIDDLEC - 12 + (CMainFrame::GetMainFrame()->GetBaseOctave() - 4) * GetInstrumentGroupSize(instr);
}
ModCommand::NOTE CModDoc::GetNoteWithBaseOctave(int noteOffset, INSTRUMENTINDEX instr) const
{
return static_cast<ModCommand::NOTE>(Clamp(GetBaseNote(instr) + noteOffset, NOTE_MIN, NOTE_MAX));
}
OPENMPT_NAMESPACE_END
|