@@ -67,7 +67,7 @@ void CInstrument2A03::Store(CDocumentFile *pDocFile)
67
67
{
68
68
CSeqInstrument::Store (pDocFile); // // //
69
69
70
- int Version = 6 ;
70
+ int Version = pDocFile-> GetBlockVersion () ;
71
71
int Octaves = Version >= 2 ? OCTAVE_RANGE : 6 ;
72
72
73
73
if (Version >= 7 ) // // // 050B
@@ -76,7 +76,7 @@ void CInstrument2A03::Store(CDocumentFile *pDocFile)
76
76
for (int j = 0 ; j < NOTE_RANGE; ++j) {
77
77
if (Version >= 7 ) { // // // 050B
78
78
if (!GetSampleIndex (i, j)) continue ;
79
- pDocFile->WriteBlockChar (i * NOTE_RANGE + j );
79
+ pDocFile->WriteBlockChar (MIDI_NOTE (i, j)+ 1 );
80
80
}
81
81
pDocFile->WriteBlockChar (GetSampleIndex (i, j));
82
82
pDocFile->WriteBlockChar (GetSamplePitch (i, j));
@@ -95,11 +95,12 @@ bool CInstrument2A03::Load(CDocumentFile *pDocFile)
95
95
96
96
const auto ReadAssignment = [&] (int Octave, int Note) {
97
97
try {
98
- int Index = CModuleException::AssertRangeFmt<MODULE_ERROR_STRICT>(
98
+ char Sample = CModuleException::AssertRangeFmt<MODULE_ERROR_STRICT>(
99
99
pDocFile->GetBlockChar (), 0 , MAX_DSAMPLES, " DPCM sample assignment index" , " %i" );
100
- if (Index > MAX_DSAMPLES)
101
- Index = 0 ;
102
- SetSampleIndex (Octave, Note, Index);
100
+ if (Sample > MAX_DSAMPLES)
101
+ Sample = 0 ;
102
+
103
+ SetSampleIndex (Octave, Note, Sample);
103
104
char Pitch = pDocFile->GetBlockChar ();
104
105
CModuleException::AssertRangeFmt<MODULE_ERROR_STRICT>(Pitch & 0x7F , 0 , 0xF , " DPCM sample pitch" , " %i" );
105
106
SetSamplePitch (Octave, Note, Pitch & 0x8F );
@@ -122,7 +123,7 @@ bool CInstrument2A03::Load(CDocumentFile *pDocFile)
122
123
for (int i = 0 ; i < Count; ++i) {
123
124
int Note = CModuleException::AssertRangeFmt<MODULE_ERROR_STRICT>(
124
125
pDocFile->GetBlockChar (), 0 , NOTE_COUNT - 1 , " DPCM sample assignment note index" , " %i" );
125
- ReadAssignment (GET_OCTAVE (Note), GET_NOTE (Note) - 1 );
126
+ ReadAssignment (GET_OCTAVE (Note - 1 ), GET_NOTE (Note - 1 ) );
126
127
}
127
128
}
128
129
else
@@ -155,10 +156,10 @@ void CInstrument2A03::SaveFile(CInstrumentFile *pFile)
155
156
memset (UsedSamples, 0 , sizeof (bool ) * MAX_DSAMPLES);
156
157
157
158
int UsedCount = 0 ;
158
- for (int i = 0 ; i < OCTAVE_RANGE; ++i) { // octaves
159
- for (int j = 0 ; j < NOTE_RANGE; ++j) { // notes
159
+ for (int i = 0 ; i < OCTAVE_RANGE; ++i) {
160
+ for (int j = 0 ; j < NOTE_RANGE; ++j) {
160
161
if (unsigned char Sample = GetSampleIndex (i, j)) {
161
- unsigned char Index = i * NOTE_RANGE + j ;
162
+ unsigned char Index = MIDI_NOTE (i, j) + 1 ;
162
163
pFile->WriteChar (Index);
163
164
pFile->WriteChar (Sample);
164
165
pFile->WriteChar (GetSamplePitch (i, j));
@@ -189,28 +190,28 @@ void CInstrument2A03::SaveFile(CInstrumentFile *pFile)
189
190
190
191
bool CInstrument2A03::LoadFile (CInstrumentFile *pFile, int iVersion)
191
192
{
192
- char SampleNames[MAX_DSAMPLES][256 ];
193
-
194
193
if (!CSeqInstrument::LoadFile (pFile, iVersion)) // // //
195
194
return false ;
196
195
197
- unsigned int Count;
198
- pFile->Read (&Count, sizeof (int ));
199
- CModuleException::AssertRangeFmt (Count, 0U , static_cast <unsigned >(NOTE_COUNT), " DPCM assignment count" , " %u" );
196
+ unsigned int Count = CModuleException::AssertRangeFmt (pFile->ReadInt (), 0U , static_cast <unsigned >(NOTE_COUNT), " DPCM assignment count" , " %u" );
200
197
201
198
// DPCM instruments
202
199
for (unsigned int i = 0 ; i < Count; ++i) {
203
- unsigned char InstNote = pFile->ReadChar ();
204
- int Octave = InstNote / NOTE_RANGE;
205
- int Note = InstNote % NOTE_RANGE;
200
+ unsigned char InstNote = CModuleException::AssertRangeFmt (
201
+ pFile->ReadChar (), 0 , NOTE_COUNT - 1 , " DPCM sample assignment note index" , " %i" );
202
+ int Octave = GET_OCTAVE (InstNote - 1 );
203
+ int Note = GET_NOTE (InstNote - 1 );
206
204
try {
207
- unsigned char Sample = CModuleException::AssertRangeFmt (pFile->ReadChar (), 0U , 0x7FU , " DPCM sample assignment index" , " %u" );
205
+ char Sample = CModuleException::AssertRangeFmt (pFile->ReadChar (), 0 , MAX_DSAMPLES , " DPCM sample assignment index" , " %u" );
208
206
if (Sample > MAX_DSAMPLES)
209
207
Sample = 0 ;
210
- unsigned char Pitch = pFile->ReadChar ();
211
- CModuleException::AssertRangeFmt (Pitch & 0x7FU , 0U , 0xFU , " DPCM sample pitch" , " %u" );
212
- SetSamplePitch (Octave, Note, Pitch);
213
208
SetSampleIndex (Octave, Note, Sample);
209
+
210
+ char Pitch = pFile->ReadChar ();
211
+ CModuleException::AssertRangeFmt (Pitch & 0x7F , 0 , 0xF , " DPCM sample pitch" , " %i" );
212
+
213
+ SetSamplePitch (Octave, Note, Pitch);
214
+
214
215
SetSampleDeltaValue (Octave, Note, CModuleException::AssertRangeFmt (
215
216
static_cast <char >(iVersion >= 24 ? pFile->ReadChar () : -1 ), -1 , 0x7F , " DPCM sample delta value" , " %i" ));
216
217
}
@@ -229,20 +230,24 @@ bool CInstrument2A03::LoadFile(CInstrumentFile *pFile, int iVersion)
229
230
230
231
unsigned int SampleCount = pFile->ReadInt ();
231
232
for (unsigned int i = 0 ; i < SampleCount; ++i) {
233
+
232
234
int Index = CModuleException::AssertRangeFmt (
233
235
pFile->ReadInt (), 0U , static_cast <unsigned >(MAX_DSAMPLES - 1 ), " DPCM sample index" , " %u" );
236
+
234
237
int Len = CModuleException::AssertRangeFmt (
235
238
pFile->ReadInt (), 0U , static_cast <unsigned >(CDSample::MAX_NAME_SIZE - 1 ), " DPCM sample name length" , " %u" );
236
- pFile->Read (SampleNames[Index], Len);
237
- SampleNames[Index][Len] = 0 ;
239
+
240
+ char SampleName[256 ]{};
241
+ pFile->Read (SampleName, Len);
242
+
238
243
int Size = pFile->ReadInt ();
239
244
char *SampleData = new char [Size ];
240
245
pFile->Read (SampleData, Size );
241
246
bool Found = false ;
242
247
for (int j = 0 ; j < MAX_DSAMPLES; ++j) if (const CDSample *pSample = m_pInstManager->GetDSample (j)) { // // //
243
248
// Compare size and name to see if identical sample exists
244
249
if (pSample->GetSize () == Size && !memcmp (pSample->GetData (), SampleData, Size ) && // // //
245
- !strcmp (pSample->GetName (), SampleNames[Index] )) {
250
+ !strcmp (pSample->GetName (), SampleName )) {
246
251
Found = true ;
247
252
// Assign sample
248
253
for (int o = 0 ; o < OCTAVE_RANGE; ++o) {
@@ -270,9 +275,9 @@ bool CInstrument2A03::LoadFile(CInstrumentFile *pFile, int iVersion)
270
275
e->Raise ();
271
276
}
272
277
CDSample *pSample = new CDSample (); // // //
273
- pSample->SetName (SampleNames[Index] );
278
+ pSample->SetName (SampleName );
274
279
pSample->SetData (Size , SampleData);
275
- int FreeSample = m_pInstManager->AddDSample (pSample);
280
+ int FreeSample = m_pInstManager->AddDSample (pSample); // not off-by-one
276
281
if (FreeSample == -1 ) {
277
282
SAFE_RELEASE (pSample);
278
283
CModuleException *e = new CModuleException ();
@@ -332,6 +337,7 @@ char CInstrument2A03::GetSampleDeltaValue(int Octave, int Note) const
332
337
333
338
void CInstrument2A03::SetSampleIndex (int Octave, int Note, char Sample)
334
339
{
340
+ // Sample is off by one; 0 means there is no index assigned to a note
335
341
m_cSamples[Octave][Note] = Sample;
336
342
InstrumentChanged ();
337
343
}
0 commit comments