@@ -91,17 +91,16 @@ struct ADXLoop{
91
91
LoopEndSample = ReadUnsignedIntBE (data+12 );
92
92
LoopEndByte = ReadUnsignedIntBE (data+16 );
93
93
}
94
- void writeLoop (unsigned char * data, unsigned int AlignmentSamples, smplloop Loop, unsigned int BlockSize, unsigned int Index, unsigned int Channels, unsigned int HeaderSize){
95
- unsigned int DataBlockSize = BlockSize - 2 ;
94
+ void writeLoop (unsigned char * data, unsigned int AlignmentSamples, smplloop Loop, unsigned int BlockSize, unsigned int Index, unsigned int Channels, unsigned int HeaderSize, unsigned int SamplesPerFrame){
96
95
unsigned int start = Loop.Start + AlignmentSamples;
97
- unsigned int end = Loop.End + AlignmentSamples;
98
- start = ((start - ( start % (DataBlockSize * Channels))) / (DataBlockSize * Channels)) * BlockSize * Channels + HeaderSize + (start % (DataBlockSize * Channels) ) * Channels;
99
- end = ((end - ( end % (DataBlockSize * Channels))) / (DataBlockSize * Channels)) * BlockSize * Channels + HeaderSize + (end % (DataBlockSize * Channels) ) * Channels;
96
+ unsigned int end = Loop.End + AlignmentSamples;
97
+ start = HeaderSize + (( start / SamplesPerFrame) * BlockSize) * Channels;
98
+ end = HeaderSize + GetNextMultiple ((( end / SamplesPerFrame) * BlockSize + (end % SamplesPerFrame) / BlockSize), BlockSize ) * Channels;
100
99
WriteShortBE (data+0 , Index);
101
100
WriteShortBE (data+2 , 1 ); /* Enable all loops. */
102
101
WriteIntBE (data+4 , Loop.Start + AlignmentSamples);
103
- WriteIntBE (data+8 , Loop. End + AlignmentSamples );
104
- WriteIntBE (data+12 , start );
102
+ WriteIntBE (data+8 , start );
103
+ WriteIntBE (data+12 , Loop. End + AlignmentSamples );
105
104
WriteIntBE (data+16 , end);
106
105
}
107
106
};
@@ -129,19 +128,16 @@ struct Loop{
129
128
Loops[i].loadLoop (data+offset);
130
129
return 0 ;
131
130
}
132
- void writeLoops (unsigned char * data, unsigned int NumberOfLoops, unsigned int ChannelCount, smpl &PCMsmpl, unsigned int BlockSize, unsigned int HeaderSize){
131
+ void writeLoops (unsigned char * data, unsigned int NumberOfLoops, unsigned int ChannelCount, smpl &PCMsmpl, unsigned int BlockSize, unsigned int SamplesPerFrame, unsigned int HeaderSize){
133
132
LoopCount = NumberOfLoops;
134
133
unsigned int start = PCMsmpl.Loops [0 ].Start ;
135
134
unsigned int SamplesInFrame = (BlockSize - 2 ) * 2 ;
136
- if (start % SamplesInFrame == 0 )
137
- AlignmentSamples = start;
138
- else
139
- AlignmentSamples = start + (SamplesInFrame - start % SamplesInFrame) - start;
135
+ AlignmentSamples = GetNextMultiple (start, ChannelCount == 1 ? SamplesInFrame * 2 : SamplesInFrame);
140
136
WriteShortBE (data+0 , AlignmentSamples);
141
137
WriteShortBE (data+2 , LoopCount);
142
138
Loops = new ADXLoop[LoopCount]; /* From what I've seen, ADX doesn't support more than one loop, but this code is already generic as is, so I will allow more customizability. */
143
139
for (unsigned int i = 0 , offset = 4 ; i < LoopCount; i++, offset+=sizeof (ADXLoop)){
144
- Loops[i].writeLoop (data+offset, AlignmentSamples, PCMsmpl.Loops [i], BlockSize, i, ChannelCount, HeaderSize);
140
+ Loops[i].writeLoop (data+offset, AlignmentSamples, PCMsmpl.Loops [i], BlockSize, i, ChannelCount, HeaderSize, SamplesPerFrame );
145
141
}
146
142
}
147
143
};
@@ -363,6 +359,8 @@ struct ADX{
363
359
void writeHeader (unsigned char *& AdxData, unsigned int HeaderSize, PCM &PCMMain, unsigned int BitDepth, unsigned int BlockSize, unsigned int EncodingMode, unsigned short HighpassFrequency, unsigned int SamplesPerChannel, unsigned int AdxVersion){
364
360
Header.writeHeader (AdxData, HeaderSize-4 , EncodingMode, BlockSize, BitDepth, PCMMain.wav .chunks .WAVEfmt .Channels , PCMMain.wav .chunks .WAVEfmt .SampleRate , SamplesPerChannel, EncodingMode == 2 ? 0 : HighpassFrequency, AdxVersion, 0 );
365
361
unsigned int BaseOffset = sizeof (ADXHeader);
362
+ unsigned int FrameSizeBytes = (BlockSize - 2 ) * 8 ;
363
+ unsigned int SamplesPerFrame = FrameSizeBytes / BitDepth;
366
364
367
365
if (AdxVersion == 0x04 || AdxVersion == 0x05 ){
368
366
WriteIntBE (AdxData+BaseOffset, 0 ); /* Padding */
@@ -372,7 +370,7 @@ struct ADX{
372
370
}
373
371
374
372
if (Looping){
375
- LoopPoints.writeLoops (AdxData+BaseOffset, PCMMain.wav .chunks .WAVEsmpl .NumberofSampleLoops , PCMMain.wav .chunks .WAVEfmt .Channels , PCMMain.wav .chunks .WAVEsmpl , BlockSize, HeaderSize);
373
+ LoopPoints.writeLoops (AdxData+BaseOffset, PCMMain.wav .chunks .WAVEsmpl .NumberofSampleLoops , PCMMain.wav .chunks .WAVEfmt .Channels , PCMMain.wav .chunks .WAVEsmpl , BlockSize, SamplesPerFrame, HeaderSize);
376
374
BaseOffset += 4 + (sizeof (ADXLoop) * PCMMain.wav .chunks .WAVEsmpl .NumberofSampleLoops );
377
375
}
378
376
@@ -451,8 +449,7 @@ struct ADX{
451
449
452
450
if (SamplesPerChannel % SamplesPerBlock != 0 ){
453
451
unsigned int SamplesNeeded = GetNextMultiple (SamplesPerChannel, DataBlockSize) * ChannelCount;
454
- SamplesPerChannel = SamplesNeeded / ChannelCount;
455
- Frames = SamplesPerChannel / SamplesPerBlock;
452
+ Frames = (SamplesNeeded / ChannelCount) / SamplesPerBlock;
456
453
PCMData = new short [SamplesNeeded];
457
454
own = true ;
458
455
memcpy (PCMData, PCMMain.Get_PCM16 (), SampleCount * sizeof (short ));
@@ -461,7 +458,6 @@ struct ADX{
461
458
PCMData = PCMMain.Get_PCM16 ();
462
459
Frames = SamplesPerChannel / SamplesPerBlock;
463
460
}
464
-
465
461
Coefficients = new int [2 ];
466
462
if (EncodingMode == 2 ){
467
463
Coefficients[0 ] = (int )StaticCoefficients[Filter * 2 + 0 ];
0 commit comments