From: <sag...@us...> - 2013-12-16 17:10:57
|
Revision: 3483 http://sourceforge.net/p/modplug/code/3483 Author: saga-games Date: 2013-12-16 17:10:39 +0000 (Mon, 16 Dec 2013) Log Message: ----------- [Ref] MIDI mapping deserialization uses FileReader now. [Ref] Further readability improvements in song extension writing. [Mod] When normalizing loaded samples, prevent sample volume from being 0. [Mod] OpenMPT: Version is now 1.22.07.08 Modified Paths: -------------- trunk/OpenMPT/common/versionNumber.h trunk/OpenMPT/mptrack/MIDIMapping.cpp trunk/OpenMPT/mptrack/MIDIMapping.h trunk/OpenMPT/soundlib/Load_it.cpp trunk/OpenMPT/soundlib/SampleIO.cpp Modified: trunk/OpenMPT/common/versionNumber.h =================================================================== --- trunk/OpenMPT/common/versionNumber.h 2013-12-16 17:07:08 UTC (rev 3482) +++ trunk/OpenMPT/common/versionNumber.h 2013-12-16 17:10:39 UTC (rev 3483) @@ -17,7 +17,7 @@ #define VER_MAJORMAJOR 1 #define VER_MAJOR 22 #define VER_MINOR 07 -#define VER_MINORMINOR 07 +#define VER_MINORMINOR 08 //Version string. For example "1.17.02.28" #define MPT_VERSION_STR VER_STRINGIZE(VER_MAJORMAJOR) "." VER_STRINGIZE(VER_MAJOR) "." VER_STRINGIZE(VER_MINOR) "." VER_STRINGIZE(VER_MINORMINOR) Modified: trunk/OpenMPT/mptrack/MIDIMapping.cpp =================================================================== --- trunk/OpenMPT/mptrack/MIDIMapping.cpp 2013-12-16 17:07:08 UTC (rev 3482) +++ trunk/OpenMPT/mptrack/MIDIMapping.cpp 2013-12-16 17:10:39 UTC (rev 3483) @@ -12,6 +12,7 @@ #include "MIDIMapping.h" #include "../soundlib/MIDIEvents.h" #include "Mainfrm.h" +#include "../soundlib/FileReader.h" std::string CMIDIMappingDirective::ToString() const @@ -64,7 +65,7 @@ } else temp8 |= (2 << 6); - fwrite(&temp8, 1, sizeof(temp8), f); + fwrite(&temp8, 1, sizeof(temp8), f); fwrite(&temp16, 1, sizeof(temp16), f); temp8 = citer->GetPlugIndex(); fwrite(&temp8, 1, sizeof(temp8), f); @@ -73,37 +74,35 @@ } -bool CMIDIMapper::Deserialize(const char *ptr, const size_t size) -//--------------------------------------------------------------- +bool CMIDIMapper::Deserialize(FileReader &file) +//--------------------------------------------- { m_Directives.clear(); - const char* endptr = ptr + size; - while(ptr + 5 <= endptr) + while(file.AreBytesLeft()) { - uint8 i8 = 0; - uint16 i16 = 0; - uint32 i32 = 0; - memcpy(&i8, ptr, 1); ptr++; - BYTE psize = 0; + uint8 i8 = file.ReadUint8(); + uint8 psize = 0; + // Determine size of this event (depends on size of plugin parameter index) switch(i8 >> 6) { - case 0: psize = 5; break; - case 1: psize = 6; break; - case 2: psize = 8; break; - case 3: default: psize = 12; break; + case 0: psize = 4; break; + case 1: psize = 5; break; + case 2: psize = 7; break; + case 3: default: psize = 11; break; } - if(ptr + psize - 1 > endptr) return true; - if(((i8 >> 2) & 7) != 0) {ptr += psize - 1; continue;} //Skipping unrecognised mapping types. + if(!file.CanRead(psize)) return true; + if(((i8 >> 2) & 7) != 0) { file.Skip(psize); continue;} //Skipping unrecognised mapping types. CMIDIMappingDirective s; s.SetActive((i8 & 1) != 0); s.SetCaptureMIDI((i8 & (1 << 1)) != 0); s.SetAllowPatternEdit((i8 & (1 << 5)) != 0); - memcpy(&i16, ptr, 2); ptr += 2; //Channel, event, MIDIbyte1. - memcpy(&i8, ptr, 1); ptr++; //Plugindex - const BYTE remainingbytes = psize - 4; - memcpy(&i32, ptr, MIN(4, remainingbytes)); ptr += remainingbytes; + uint16 i16 = file.ReadUint16LE(); //Channel, event, MIDIbyte1. + i8 = file.ReadUint8(); //Plugindex + uint32 i32; + file.ReadStructPartial(i32, psize - 3); + SwapBytesLE(i32); s.SetChannel(((i16 & 1) != 0) ? 0 : 1 + ((i16 >> 1) & 0xF)); s.SetEvent(static_cast<BYTE>((i16 >> 5) & 0xF)); Modified: trunk/OpenMPT/mptrack/MIDIMapping.h =================================================================== --- trunk/OpenMPT/mptrack/MIDIMapping.h 2013-12-16 17:07:08 UTC (rev 3482) +++ trunk/OpenMPT/mptrack/MIDIMapping.h 2013-12-16 17:10:39 UTC (rev 3483) @@ -72,6 +72,8 @@ }; class CSoundFile; +class FileReader; + inline bool operator<(const CMIDIMappingDirective& a, const CMIDIMappingDirective& b) {return a.GetController() < b.GetController();} inline bool operator<(const CMIDIMappingDirective& d, const BYTE& ctrlVal) {return d.GetController() < ctrlVal;} inline bool operator<(const BYTE& ctrlVal, const CMIDIMappingDirective& d) {return ctrlVal < d.GetController();} @@ -111,7 +113,7 @@ size_t GetSerializationSize() const; void Serialize(FILE* f) const; - bool Deserialize(const char *ptr, const size_t size); //Return false if succesful, true otherwise. + bool Deserialize(FileReader &file); //Return false if succesful, true otherwise. bool AreOrderEqual(const size_t a, const size_t b) {return !(m_Directives[a] < m_Directives[b] || m_Directives[b] < m_Directives[a]);} Modified: trunk/OpenMPT/soundlib/Load_it.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_it.cpp 2013-12-16 17:07:08 UTC (rev 3482) +++ trunk/OpenMPT/soundlib/Load_it.cpp 2013-12-16 17:10:39 UTC (rev 3483) @@ -1895,13 +1895,7 @@ void CSoundFile::SaveExtendedInstrumentProperties(UINT nInstruments, FILE* f) const //--------------------------------------------------------------------------------- { - uint32 code=0; - -/* if(Instruments[1] == NULL) { - return; - }*/ - - code = MULTICHAR4_LE_MSVC('M','P','T','X'); // write extension header code + uint32 code = MULTICHAR4_LE_MSVC('M','P','T','X'); // write extension header code fwrite(&code, 1, sizeof(uint32), f); if (nInstruments == 0) @@ -1934,12 +1928,12 @@ if(GetType() & MOD_TYPE_MPT) { - UINT maxNodes = 0; + uint32 maxNodes = 0; for(INSTRUMENTINDEX nIns = 1; nIns <= m_nInstruments; nIns++) if(Instruments[nIns] != nullptr) { - maxNodes = MAX(maxNodes, Instruments[nIns]->VolEnv.nNodes); - maxNodes = MAX(maxNodes, Instruments[nIns]->PanEnv.nNodes); - maxNodes = MAX(maxNodes, Instruments[nIns]->PitchEnv.nNodes); + maxNodes = std::max(maxNodes, Instruments[nIns]->VolEnv.nNodes); + maxNodes = std::max(maxNodes, Instruments[nIns]->PanEnv.nNodes); + maxNodes = std::max(maxNodes, Instruments[nIns]->PitchEnv.nNodes); } // write full envelope information for MPTM files (more env points) if(maxNodes > 25) @@ -1962,7 +1956,7 @@ } void CSoundFile::WriteInstrumentPropertyForAllInstruments(uint32 code, int16 size, FILE* f, UINT nInstruments) const -//--------------------------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------------------------------------------ { fwrite(&code, 1, sizeof(uint32), f); //write code fwrite(&size, 1, sizeof(int16), f); //write size @@ -1973,9 +1967,8 @@ WriteInstrumentHeaderStructOrField(Instruments[nins], f, code, size); } else { - ModInstrument *emptyInstrument = new ModInstrument(); - WriteInstrumentHeaderStructOrField(emptyInstrument, f, code, size); - delete emptyInstrument; + ModInstrument emptyInstrument; + WriteInstrumentHeaderStructOrField(&emptyInstrument, f, code, size); } } } @@ -1984,46 +1977,38 @@ //-------------------------------------------------------- { //Extra song data - Yet Another Hack. - uint16 size; - uint32 code = MULTICHAR4_LE_MSVC('M','P','T','S'); //Extra song file data + const uint32 code = MULTICHAR4_LE_MSVC('M','P','T','S'); fwrite(&code, 1, sizeof(uint32), f); +#define WRITEMODULARHEADER(c1, c2, c3, c4, fsize) \ + { \ + const uint32 code = MULTICHAR4_LE_MSVC(c1, c2, c3, c4); \ + fwrite(&code, 1, sizeof(code), f); \ + const uint16 size = (fsize); \ + fwrite(&size, 1, sizeof(size), f); \ + } +#define WRITEMODULAR(c1, c2, c3, c4, field) \ + { \ + WRITEMODULARHEADER(c1, c2, c3, c4, sizeof(field)) \ + fwrite(&(field), 1, sizeof(field), f); \ + } + if(m_nDefaultTempo > 255) { - code = MULTICHAR4_LE_MSVC('D','T','.','.'); //write m_nDefaultTempo field code - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nDefaultTempo); //write m_nDefaultTempo field size - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nDefaultTempo, 1, size, f); //write m_nDefaultTempo + WRITEMODULAR('D','T','.','.', m_nDefaultTempo); } - code = MULTICHAR4_LE_MSVC('R','P','B','.'); //write m_nRowsPerBeat - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nDefaultRowsPerBeat); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nDefaultRowsPerBeat, 1, size, f); + WRITEMODULAR('R','P','B','.', m_nDefaultRowsPerBeat); + WRITEMODULAR('R','P','M','.', m_nDefaultRowsPerMeasure); - code = MULTICHAR4_LE_MSVC('R','P','M','.'); //write m_nRowsPerMeasure - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nDefaultRowsPerMeasure); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nDefaultRowsPerMeasure, 1, size, f); - if(GetType() != MOD_TYPE_XM) { - code = MULTICHAR4_LE_MSVC('C','.','.','.'); //write m_nChannels - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nChannels); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nChannels, 1, size, f); + WRITEMODULAR('C','.','.','.', m_nChannels); } if(TypeIsIT_MPT() && GetNumChannels() > 64) //IT header has room only for 64 channels. Save the { //settings that do not fit to the header here as an extension. - code = MULTICHAR4_LE_MSVC('C','h','n','S'); - fwrite(&code, 1, sizeof(uint32), f); - size = (GetNumChannels() - 64) * 2; - fwrite(&size, 1, sizeof(uint16), f); + WRITEMODULARHEADER('C','h','n','S', (GetNumChannels() - 64) * 2); for(CHANNELINDEX chn = 64; chn < GetNumChannels(); chn++) { uint8 panvol[2]; @@ -2035,71 +2020,33 @@ } } - code = MULTICHAR4_LE_MSVC('T','M','.','.'); //write m_nTempoMode - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nTempoMode); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nTempoMode, 1, size, f); + WRITEMODULAR('T','M','.','.', m_nTempoMode); - code = MULTICHAR4_LE_MSVC('P','M','M','.'); //write m_nMixLevels - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nMixLevels); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nMixLevels, 1, size, f); + WRITEMODULAR('P','M','M','.', m_nMixLevels); if(m_dwCreatedWithVersion) { - code = MULTICHAR4_LE_MSVC('C','W','V','.'); //write m_dwCreatedWithVersion - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_dwCreatedWithVersion); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_dwCreatedWithVersion, 1, size, f); + WRITEMODULAR('C','W','V','.', m_dwCreatedWithVersion); } - code = MULTICHAR4_LE_MSVC('L','S','W','V'); //write m_dwLastSavedWithVersion - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_dwLastSavedWithVersion); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_dwLastSavedWithVersion, 1, size, f); + WRITEMODULAR('L','S','W','V', m_dwLastSavedWithVersion); + WRITEMODULAR('S','P','A','.', m_nSamplePreAmp); + WRITEMODULAR('V','S','T','V', m_nVSTiVolume); - code = MULTICHAR4_LE_MSVC('S','P','A','.'); //write m_nSamplePreAmp - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nSamplePreAmp); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nSamplePreAmp, 1, size, f); - - code = MULTICHAR4_LE_MSVC('V','S','T','V'); //write m_nVSTiVolume - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nVSTiVolume); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nVSTiVolume, 1, size, f); - if(GetType() == MOD_TYPE_XM && m_nDefaultGlobalVolume != MAX_GLOBAL_VOLUME) { - code = MULTICHAR4_LE_MSVC('D','G','V','.'); //write m_nDefaultGlobalVolume - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nDefaultGlobalVolume); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nDefaultGlobalVolume, 1, size, f); + WRITEMODULAR('D','G','V','.', m_nDefaultGlobalVolume); } if(GetType() != MOD_TYPE_XM && m_nRestartPos != 0) { - code = MULTICHAR4_LE_MSVC('R','P','.','.'); //write m_nRestartPos - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_nRestartPos); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_nRestartPos, 1, size, f); + WRITEMODULAR('R','P','.','.', m_nRestartPos); } //Additional flags for XM/IT/MPTM if(m_ModFlags) { - code = MULTICHAR4_LE_MSVC('M','S','F','.'); - fwrite(&code, 1, sizeof(uint32), f); - size = sizeof(m_ModFlags); - fwrite(&size, 1, sizeof(uint16), f); - fwrite(&m_ModFlags, 1, size, f); + WRITEMODULAR('M','S','F','.', m_ModFlags); } #ifdef MODPLUG_TRACKER @@ -2110,19 +2057,16 @@ if(objectsize > size_t(int16_max)) { AddToLog("Datafield overflow with MIDI to plugparam mappings; data won't be written."); - } - else + } else { - code = MULTICHAR4_LE_MSVC('M','I','M','A'); - fwrite(&code, 1, sizeof(uint32), f); - size = static_cast<int16>(objectsize); - fwrite(&size, 1, sizeof(uint16), f); + WRITEMODULARHEADER('M','I','M','A', static_cast<uint16>(objectsize)); GetMIDIMapper().Serialize(f); } } #endif - +#undef WRITEMODULAR +#undef WRITEMODULARHEADER return; } @@ -2233,7 +2177,7 @@ case MULTICHAR4_LE_MSVC('R','P','.','.'): if(modtype != MOD_TYPE_XM) ReadField(chunk, size, m_nRestartPos); break; case MULTICHAR4_LE_MSVC('M','S','F','.'): ReadFieldFlagSet(chunk, size, m_ModFlags); break; #ifdef MODPLUG_TRACKER - case MULTICHAR4_LE_MSVC('M','I','M','A'): GetMIDIMapper().Deserialize(chunk.GetRawData(), size); break; + case MULTICHAR4_LE_MSVC('M','I','M','A'): GetMIDIMapper().Deserialize(chunk); break; #endif case MULTICHAR4_LE_MSVC('C','h','n','S'): if(size <= (MAX_BASECHANNELS - 64) * 2 && (size % 2u) == 0) Modified: trunk/OpenMPT/soundlib/SampleIO.cpp =================================================================== --- trunk/OpenMPT/soundlib/SampleIO.cpp 2013-12-16 17:07:08 UTC (rev 3482) +++ trunk/OpenMPT/soundlib/SampleIO.cpp 2013-12-16 17:10:39 UTC (rev 3483) @@ -323,7 +323,7 @@ if(bytesRead) { // Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision. - sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(0), uint32(64))); + sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(1), uint32(64))); } } @@ -343,7 +343,7 @@ if(bytesRead) { // Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision. - sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(0), uint32(64))); + sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(1), uint32(64))); } } @@ -363,7 +363,7 @@ if(bytesRead) { // Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision. - sample.nGlobalVol = Util::Round<uint16>(Clamp(sample.nGlobalVol * srcPeak, 0.0f, 64.0f)); + sample.nGlobalVol = Util::Round<uint16>(Clamp(sample.nGlobalVol * srcPeak, 1.0f, 64.0f)); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |