From: <sag...@us...> - 2011-04-13 20:38:35
|
Revision: 854 http://modplug.svn.sourceforge.net/modplug/?rev=854&view=rev Author: saga-games Date: 2011-04-13 20:38:28 +0000 (Wed, 13 Apr 2011) Log Message: ----------- [Fix] XM Saving: In compatible mode, the number of samples per instrument is now limited to 16. [Ref] Eliminating more duplicate code... Modified Paths: -------------- trunk/OpenMPT/mptrack/CleanupSong.cpp trunk/OpenMPT/soundlib/Load_xm.cpp trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/Sndfile.h Modified: trunk/OpenMPT/mptrack/CleanupSong.cpp =================================================================== --- trunk/OpenMPT/mptrack/CleanupSong.cpp 2011-04-13 20:00:31 UTC (rev 853) +++ trunk/OpenMPT/mptrack/CleanupSong.cpp 2011-04-13 20:38:28 UTC (rev 854) @@ -528,10 +528,9 @@ if(pSndFile == nullptr) return false; CHAR s[512]; - vector<bool> bIns; - int nExt = 0; + vector<bool> samplesUsed; int nRemoved = 0; - bIns.resize(pSndFile->GetNumSamples() + 1, false); + samplesUsed.resize(pSndFile->GetNumSamples() + 1, false); BeginWaitCursor(); for (SAMPLEINDEX nSmp = pSndFile->m_nSamples; nSmp >= 1; nSmp--) if (pSndFile->Samples[nSmp].pSample) @@ -546,49 +545,12 @@ nRemoved++; } } - if (pSndFile->m_nInstruments) - { - for (PATTERNINDEX nPat = 0; nPat < pSndFile->GetNumPatterns(); nPat++) - { - MODCOMMAND *p = pSndFile->Patterns[nPat]; - if (p) - { - UINT jmax = pSndFile->Patterns[nPat].GetNumRows() * pSndFile->GetNumChannels(); - for (UINT j=0; j<jmax; j++, p++) - { - if ((p->note) && (p->note <= NOTE_MAX) && (!p->IsPcNote())) - { - if ((p->instr) && (p->instr < MAX_INSTRUMENTS)) - { - MODINSTRUMENT *pIns = pSndFile->Instruments[p->instr]; - if (pIns) - { - UINT n = pIns->Keyboard[p->note-1]; - if (n <= pSndFile->GetNumSamples()) bIns[n] = true; - } - } else - { - for (UINT k=1; k<=pSndFile->m_nInstruments; k++) - { - MODINSTRUMENT *pIns = pSndFile->Instruments[k]; - if (pIns) - { - UINT n = pIns->Keyboard[p->note-1]; - if (n <= pSndFile->GetNumSamples()) bIns[n] = true; - } - } - } - } - } - } - } - for (SAMPLEINDEX ichk = 1; ichk <= pSndFile->GetNumSamples(); ichk++) - { - if ((!bIns[ichk]) && (pSndFile->Samples[ichk].pSample)) nExt++; - } - } + + SAMPLEINDEX nExt = pSndFile->DetectUnusedSamples(samplesUsed); + EndWaitCursor(); - if (nExt && !((pSndFile->GetType() == MOD_TYPE_IT) && (pSndFile->m_dwSongFlags & SONG_ITPROJECT))) + + if (nExt && !((pSndFile->GetType() == MOD_TYPE_IT) && (pSndFile->m_dwSongFlags & SONG_ITPROJECT))) { //We don't remove an instrument's unused samples in an ITP. wsprintf(s, "OpenMPT detected %d sample%s referenced by an instrument,\n" "but not used in the song. Do you want to remove them?", nExt, (nExt == 1) ? "" : "s"); @@ -596,7 +558,7 @@ { for (SAMPLEINDEX nSmp = 1; nSmp <= pSndFile->GetNumSamples(); nSmp++) { - if ((!bIns[nSmp]) && (pSndFile->Samples[nSmp].pSample)) + if ((!samplesUsed[nSmp]) && (pSndFile->Samples[nSmp].pSample)) { m_pModDoc->GetSampleUndo()->PrepareUndo(nSmp, sundo_delete); BEGIN_CRITICAL(); Modified: trunk/OpenMPT/soundlib/Load_xm.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_xm.cpp 2011-04-13 20:00:31 UTC (rev 853) +++ trunk/OpenMPT/soundlib/Load_xm.cpp 2011-04-13 20:38:28 UTC (rev 854) @@ -175,7 +175,7 @@ p->command = src[j++]; p->param = src[j++]; } - if (p->note == 97) p->note = 0xFF; else + if (p->note == 97) p->note = NOTE_KEYOFF; else if ((p->note) && (p->note < 97)) p->note += 12; if (p->command | p->param) pSndFile->ConvertModCommand(p); if (p->instr == 0xff) p->instr = 0; @@ -241,8 +241,6 @@ XMSAMPLEHEADER xmsh; XMSAMPLESTRUCT xmss; DWORD dwMemPos; - BYTE samples_used[(MAX_SAMPLES + 7) / 8]; // for removing unused samples - UINT unused_samples; // dito bool bMadeWithModPlug = false, bProbablyMadeWithModPlug = false, bProbablyMPT109 = false, bIsFT2 = false; @@ -300,8 +298,8 @@ if(dwMemPos == 0) return true; } - memset(samples_used, 0, sizeof(samples_used)); - unused_samples = 0; + vector<bool> samples_used; // for removing unused samples + SAMPLEINDEX unused_samples = 0; // dito // Reading instruments for (INSTRUMENTINDEX iIns = 1; iIns <= m_nInstruments; iIns++) @@ -316,7 +314,7 @@ DWORD ihsize = LittleEndian(*((DWORD *)(lpStream + dwMemPos))); if (dwMemPos + ihsize > dwMemLength) return true; - memset(&pih, 0, sizeof(pih)); + MemsetZero(pih); memcpy(&pih, lpStream + dwMemPos, min(sizeof(pih), ihsize)); if ((Instruments[iIns] = new MODINSTRUMENT) == nullptr) continue; @@ -357,6 +355,7 @@ bIsFT2 = true; // definitely not MPT. (or any other tracker) } + if (LittleEndian(pih.size)) dwMemPos += LittleEndian(pih.size); else @@ -400,14 +399,14 @@ if (!unused_samples) { unused_samples = DetectUnusedSamples(samples_used); - if (!unused_samples) unused_samples = 0xFFFF; + if (!unused_samples) unused_samples = SAMPLEINDEX_INVALID; } - if ((unused_samples) && (unused_samples != 0xFFFF)) + if ((unused_samples) && (unused_samples != SAMPLEINDEX_INVALID)) { - for (UINT iext=m_nSamples; iext>=1; iext--) if (0 == (samples_used[iext>>3] & (1<<(iext&7)))) + for (UINT iext=m_nSamples; iext>=1; iext--) if (!samples_used[iext]) { unused_samples--; - samples_used[iext>>3] |= (1<<(iext&7)); + samples_used[iext] = true; DestroySample(iext); n = iext; for (UINT mapchk=0; mapchk<nmap; mapchk++) @@ -422,7 +421,7 @@ if (pks->Keyboard[ks] == n) pks->Keyboard[ks] = 0; } } - memset(&Samples[n], 0, sizeof(Samples[0])); + MemsetZero(Samples[n]); break; } } @@ -836,7 +835,7 @@ UINT param = ModSaveCommand(p, true, bCompatibilityExport); UINT command = param >> 8; param &= 0xFF; - if (note >= 0xFE) note = 97; else + if (note >= NOTE_MIN_SPECIAL) note = 97; else if ((note <= 12) || (note > 96+12)) note = 0; else note -= 12; UINT vol = 0; @@ -993,7 +992,7 @@ smptable[xmih.samples++] = sample; //record in instrument's sample table } - if (xmih.samples >= 32) break; + if ((xmih.samples >= 32) || (xmih.samples >= 16 && bCompatibilityExport)) break; xmsh.snum[j] = k; //record sample table offset in instrument's note map } } Modified: trunk/OpenMPT/soundlib/Sndfile.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.cpp 2011-04-13 20:00:31 UTC (rev 853) +++ trunk/OpenMPT/soundlib/Sndfile.cpp 2011-04-13 20:38:28 UTC (rev 854) @@ -2550,54 +2550,58 @@ } -UINT CSoundFile::DetectUnusedSamples(BYTE *pbIns) -//----------------------------------------------- +SAMPLEINDEX CSoundFile::DetectUnusedSamples(vector<bool> &sampleUsed) +//------------------------------------------------------------------- { - UINT nExt = 0; + sampleUsed.assign(GetNumSamples() + 1, false); - if (!pbIns) return 0; - if (m_nInstruments) + if(GetNumInstruments() == 0) { - memset(pbIns, 0, (MAX_SAMPLES+7)/8); - for (UINT ipat=0; ipat<Patterns.Size(); ipat++) + return 0; + } + SAMPLEINDEX nExt = 0; + + for (PATTERNINDEX nPat = 0; nPat < GetNumPatterns(); nPat++) + { + const MODCOMMAND *p = Patterns[nPat]; + if(p == nullptr) { - MODCOMMAND *p = Patterns[ipat]; - if (p) + continue; + } + + UINT jmax = Patterns[nPat].GetNumRows() * GetNumChannels(); + for (UINT j=0; j<jmax; j++, p++) + { + if ((p->note) && (p->note <= NOTE_MAX) && (!p->IsPcNote())) { - UINT jmax = Patterns[ipat].GetNumRows() * m_nChannels; - for (UINT j=0; j<jmax; j++, p++) + if ((p->instr) && (p->instr < MAX_INSTRUMENTS)) { - if ((p->note) && (p->note <= NOTE_MAX)) + MODINSTRUMENT *pIns = Instruments[p->instr]; + if (pIns) { - if ((p->instr) && (p->instr < MAX_INSTRUMENTS)) + SAMPLEINDEX n = pIns->Keyboard[p->note-1]; + if (n <= GetNumSamples()) sampleUsed[n] = true; + } + } else + { + for (INSTRUMENTINDEX k = GetNumInstruments(); k >= 1; k--) + { + MODINSTRUMENT *pIns = Instruments[k]; + if (pIns) { - MODINSTRUMENT *pIns = Instruments[p->instr]; - if (pIns) - { - UINT n = pIns->Keyboard[p->note-1]; - if (n < MAX_SAMPLES) pbIns[n>>3] |= 1<<(n&7); - } - } else - { - for (UINT k=1; k<=m_nInstruments; k++) - { - MODINSTRUMENT *pIns = Instruments[k]; - if (pIns) - { - UINT n = pIns->Keyboard[p->note-1]; - if (n < MAX_SAMPLES) pbIns[n>>3] |= 1<<(n&7); - } - } + SAMPLEINDEX n = pIns->Keyboard[p->note-1]; + if (n <= GetNumSamples()) sampleUsed[n] = true; } } } } } - for (UINT ichk=1; ichk<=m_nSamples; ichk++) - { - if ((0 == (pbIns[ichk>>3]&(1<<(ichk&7)))) && (Samples[ichk].pSample)) nExt++; - } } + for (SAMPLEINDEX ichk = GetNumSamples(); ichk >= 1; ichk--) + { + if ((!sampleUsed[ichk]) && (Samples[ichk].pSample)) nExt++; + } + return nExt; } Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2011-04-13 20:00:31 UTC (rev 853) +++ trunk/OpenMPT/soundlib/Sndfile.h 2011-04-13 20:38:28 UTC (rev 854) @@ -951,7 +951,7 @@ bool IsSampleUsed(SAMPLEINDEX nSample); bool IsInstrumentUsed(INSTRUMENTINDEX nInstr); bool RemoveInstrumentSamples(INSTRUMENTINDEX nInstr); - UINT DetectUnusedSamples(BYTE *); // bitmask + SAMPLEINDEX DetectUnusedSamples(vector<bool> &sampleUsed); bool RemoveSelectedSamples(bool *pbIns); void AdjustSampleLoop(MODSAMPLE *pSmp); // Samples file I/O This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |