Skip to content

Commit

Permalink
Merged revision(s) 20385-20406 from trunk/OpenMPT:
Browse files Browse the repository at this point in the history
[Mod] Compressor DMO: Similar to Chorus / Flanger, avoid chance of reading from the wrong buffer offset at extremely high sample rates. In this particular case, the mix rate needs to be above 500 kHz before the overflow would occur.
........
[Fix] MED: Some samples were playing too low (e.g. in mix94.mmd1, see https://www.un4seen.com/forum/?topic=15448.msg142556#msg142556)
........


git-svn-id: https://source.openmpt.org/svn/openmpt/branches/OpenMPT-1.30@20408 56274372-70c3-4bfc-bfc3-4c3a0b034d27
  • Loading branch information
sagamusix committed Mar 17, 2024
1 parent 7d1e409 commit 72913ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
23 changes: 11 additions & 12 deletions soundlib/Load_med.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ bool CSoundFile::ReadMED(FileReader &file, ModLoadingFlags loadFlags)
instr.AssignSample(0);
}

MMD0Sample sampleHeader;
sampleHeaderChunk.ReadStruct(sampleHeader);

uint8 numSamples = 1;
static constexpr uint8 SamplesPerType[] = {1, 5, 3, 2, 4, 6, 7};
if(!isSynth && maskedType < std::size(SamplesPerType))
Expand Down Expand Up @@ -836,10 +839,10 @@ bool CSoundFile::ReadMED(FileReader &file, ModLoadingFlags loadFlags)
// TODO: Move octaves so that they align better (C-4 = lowest, we don't have access to the highest four octaves)
for(int octave = 4; octave < 10; octave++)
{
for(int note = 0; note < 12; note++)
for(int note = 0, i = 12 * octave; note < 12; note++, i++)
{
instr.Keyboard[12 * octave + note] = smp + OctSampleMap[numSamples - 2][octave - 4];
instr.NoteMap[12 * octave + note] += OctTransposeMap[numSamples - 2][octave - 4];
instr.Keyboard[i] = smp + OctSampleMap[numSamples - 2][octave - 4];
instr.NoteMap[i] = static_cast<uint8>(instr.NoteMap[i] + OctTransposeMap[numSamples - 2][octave - 4]);
}
}
} else if(maskedType == MMDInstrHeader::EXTSAMPLE)
Expand All @@ -848,18 +851,14 @@ bool CSoundFile::ReadMED(FileReader &file, ModLoadingFlags loadFlags)
instr.Transpose(-24);
} else if(!isSynth && hardwareMixSamples)
{
for(int octave = 7; octave < 10; octave++)
for(auto &note : instr.NoteMap)
{
for(int note = 0; note < 12; note++)
{
instr.NoteMap[12 * octave + note] -= static_cast<uint8>((octave - 6) * 12);
}
int realNote = note + sampleHeader.sampleTranspose;
if(realNote >= NOTE_MIDDLEC + 24)
note -= static_cast<uint8>(mpt::align_down(realNote - NOTE_MIDDLEC - 12, 12));
}
}

MMD0Sample sampleHeader;
sampleHeaderChunk.ReadStruct(sampleHeader);

// midiChannel = 0xFF == midi instrument but with invalid channel, midiChannel = 0x00 == sample-based instrument?
if(sampleHeader.midiChannel > 0 && sampleHeader.midiChannel <= 16)
{
Expand Down Expand Up @@ -1137,12 +1136,12 @@ bool CSoundFile::ReadMED(FileReader &file, ModLoadingFlags loadFlags)
if(!order.empty())
order.push_back(order.GetIgnoreIndex());

const size_t orderStart = order.size();
size_t readOrders = playSeq.length;
if(!file.CanRead(readOrders))
LimitMax(readOrders, file.BytesLeft());
LimitMax(readOrders, ORDERINDEX_MAX);

size_t orderStart = order.size();
order.reserve(orderStart + readOrders);
for(size_t ord = 0; ord < readOrders; ord++)
{
Expand Down
6 changes: 3 additions & 3 deletions soundlib/plugins/dmo/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ void Compressor::Process(float *pOutL, float *pOutR, uint32 numFrames)
}
compGainPow >>= (31 - compGainInt);

int32 readOffset = m_predelay + m_bufPos * 4096 + m_bufSize - 1;
int32 readOffset = m_predelay + m_bufSize - 1;
readOffset /= 4096;
readOffset %= m_bufSize;
readOffset = (readOffset + m_bufPos) % m_bufSize;

float outGain = (compGainPow * (1.0f / 2147483648.0f)) * m_gain;
*(out[0])++ = m_buffer[readOffset * 2] * outGain;
*(out[1])++ = m_buffer[readOffset * 2 + 1] * outGain;
Expand Down

0 comments on commit 72913ae

Please sign in to comment.