From 72913aed6b402d191efeab685c94d1ec5069226b Mon Sep 17 00:00:00 2001 From: Johannes Schultz Date: Sun, 17 Mar 2024 12:35:23 +0000 Subject: [PATCH] Merged revision(s) 20385-20406 from trunk/OpenMPT: [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 --- soundlib/Load_med.cpp | 23 +++++++++++------------ soundlib/plugins/dmo/Compressor.cpp | 6 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/soundlib/Load_med.cpp b/soundlib/Load_med.cpp index 2c60512c2ab..bdecc847735 100644 --- a/soundlib/Load_med.cpp +++ b/soundlib/Load_med.cpp @@ -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)) @@ -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(instr.NoteMap[i] + OctTransposeMap[numSamples - 2][octave - 4]); } } } else if(maskedType == MMDInstrHeader::EXTSAMPLE) @@ -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 ¬e : instr.NoteMap) { - for(int note = 0; note < 12; note++) - { - instr.NoteMap[12 * octave + note] -= static_cast((octave - 6) * 12); - } + int realNote = note + sampleHeader.sampleTranspose; + if(realNote >= NOTE_MIDDLEC + 24) + note -= static_cast(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) { @@ -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++) { diff --git a/soundlib/plugins/dmo/Compressor.cpp b/soundlib/plugins/dmo/Compressor.cpp index 97d5cbcf05a..72b5822f9fe 100644 --- a/soundlib/plugins/dmo/Compressor.cpp +++ b/soundlib/plugins/dmo/Compressor.cpp @@ -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;