From: <sag...@us...> - 2011-04-14 14:23:46
|
Revision: 855 http://modplug.svn.sourceforge.net/modplug/?rev=855&view=rev Author: saga-games Date: 2011-04-14 14:23:39 +0000 (Thu, 14 Apr 2011) Log Message: ----------- [Fix] Song Cleanup: When converting instruments to samples, "Remove samples associated with an instrument" actually destroyed all samples. Removed this checkbox as the sample cleanup options can be used to do the same. [Ref] More code cleanup... Modified Paths: -------------- trunk/OpenMPT/mptrack/CleanupSong.cpp trunk/OpenMPT/soundlib/Sampleio.cpp trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/Sndfile.h trunk/OpenMPT/soundlib/modsmp_ctrl.cpp Modified: trunk/OpenMPT/mptrack/CleanupSong.cpp =================================================================== --- trunk/OpenMPT/mptrack/CleanupSong.cpp 2011-04-13 20:38:28 UTC (rev 854) +++ trunk/OpenMPT/mptrack/CleanupSong.cpp 2011-04-14 14:23:39 UTC (rev 855) @@ -528,23 +528,20 @@ if(pSndFile == nullptr) return false; CHAR s[512]; - vector<bool> samplesUsed; - int nRemoved = 0; - samplesUsed.resize(pSndFile->GetNumSamples() + 1, false); + vector<bool> samplesUsed(pSndFile->GetNumSamples() + 1, true); BeginWaitCursor(); - for (SAMPLEINDEX nSmp = pSndFile->m_nSamples; nSmp >= 1; nSmp--) if (pSndFile->Samples[nSmp].pSample) + for (SAMPLEINDEX nSmp = pSndFile->GetNumSamples(); nSmp >= 1; nSmp--) if (pSndFile->Samples[nSmp].pSample) { if (!pSndFile->IsSampleUsed(nSmp)) { + samplesUsed[nSmp] = false; m_pModDoc->GetSampleUndo()->PrepareUndo(nSmp, sundo_delete); - BEGIN_CRITICAL(); - pSndFile->DestroySample(nSmp); - if ((nSmp == pSndFile->m_nSamples) && (nSmp > 1)) pSndFile->m_nSamples--; - END_CRITICAL(); - nRemoved++; } } + BEGIN_CRITICAL(); + SAMPLEINDEX nRemoved = pSndFile->RemoveSelectedSamples(samplesUsed); + END_CRITICAL(); SAMPLEINDEX nExt = pSndFile->DetectUnusedSamples(samplesUsed); @@ -569,7 +566,7 @@ m_pModDoc->GetSampleUndo()->ClearUndo(nSmp); } } - wsprintf(s, "%d unused sample%s removed\n" ,nRemoved, (nRemoved == 1) ? "" : "s"); + wsprintf(s, "%d unused sample%s removed\n" , nRemoved, (nRemoved == 1) ? "" : "s"); m_pModDoc->AddToLog(s); return true; } @@ -971,19 +968,17 @@ CSoundFile *pSndFile = m_pModDoc->GetSoundFile(); if(pSndFile == nullptr) return false; - if (pSndFile->m_nSamples == 0) return false; - for (SAMPLEINDEX nSmp = 1; nSmp <= pSndFile->m_nSamples; nSmp++) + if (pSndFile->GetNumSamples() == 0) return false; + vector<bool> keepSamples(pSndFile->GetNumSamples() + 1, false); + + for (SAMPLEINDEX nSmp = 1; nSmp <= pSndFile->GetNumSamples(); nSmp++) { m_pModDoc->GetSampleUndo()->PrepareUndo(nSmp, sundo_delete, 0, pSndFile->Samples[nSmp].nLength); - if(pSndFile->Samples[nSmp].pSample) - { - BEGIN_CRITICAL(); - pSndFile->DestroySample(nSmp); - END_CRITICAL(); - } } ctrlSmp::ResetSamples(*pSndFile, ctrlSmp::SmpResetInit); - pSndFile->m_nSamples = 1; + BEGIN_CRITICAL(); + pSndFile->RemoveSelectedSamples(keepSamples); + END_CRITICAL(); return true; } @@ -994,27 +989,20 @@ CSoundFile *pSndFile = m_pModDoc->GetSoundFile(); if(pSndFile == nullptr) return false; - if (pSndFile->m_nInstruments == 0) return false; + if (pSndFile->GetNumInstruments() == 0) return false; - char removeSamples = -1; if(bConfirm) { - if (CMainFrame::GetMainFrame()->MessageBox("Do you want to convert all instruments to samples ?\n", + if (CMainFrame::GetMainFrame()->MessageBox("Do you want to convert all instruments to samples?", "Removing all instruments", MB_YESNO | MB_ICONQUESTION) == IDYES) { m_pModDoc->ConvertInstrumentsToSamples(); } - - if (::MessageBox(NULL, "Remove samples associated with an instrument if they are unused?", "Removing all instruments", MB_YESNO | MB_ICONQUESTION) == IDYES) { - removeSamples = 1; - } } - if(removeSamples == -1) m_pModDoc->GetSampleUndo()->ClearUndo(); - - for (INSTRUMENTINDEX i = 1; i <= pSndFile->m_nInstruments; i++) + for (INSTRUMENTINDEX i = 1; i <= pSndFile->GetNumInstruments(); i++) { - pSndFile->DestroyInstrument(i, removeSamples); + pSndFile->DestroyInstrument(i, -1); } pSndFile->m_nInstruments = 0; Modified: trunk/OpenMPT/soundlib/Sampleio.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sampleio.cpp 2011-04-13 20:38:28 UTC (rev 854) +++ trunk/OpenMPT/soundlib/Sampleio.cpp 2011-04-14 14:23:39 UTC (rev 855) @@ -113,8 +113,10 @@ // -> CODE#0003 // -> DESC="remove instrument's samples" //rewbs: changed message - if(removeSamples > 0 || (removeSamples == 0 && ::MessageBox(NULL, "Remove samples associated with an instrument if they are unused?", "Removing instrument", MB_YESNO | MB_ICONQUESTION) == IDYES)) + if(removeSamples > 0 || (removeSamples == 0 && ::MessageBox(NULL, "Remove samples associated with an instrument if it is unused?", "Removing instrument", MB_YESNO | MB_ICONQUESTION) == IDYES)) + { RemoveInstrumentSamples(nInstr); + } // -! BEHAVIOUR_CHANGE#0003 // -> CODE#0023 @@ -146,8 +148,7 @@ bool CSoundFile::RemoveInstrumentSamples(INSTRUMENTINDEX nInstr) //-------------------------------------------------------------- { - vector<bool> sampleused; - sampleused.resize(GetNumSamples() + 1, false); + vector<bool> sampleused(GetNumSamples() + 1, false); if (Instruments[nInstr]) { @@ -169,7 +170,7 @@ for (SAMPLEINDEX nSmp = 1; nSmp <= GetNumSamples(); nSmp++) if (sampleused[nSmp]) { DestroySample(nSmp); - m_szNames[nSmp][0] = 0; + strcpy(m_szNames[nSmp], ""); } return true; } Modified: trunk/OpenMPT/soundlib/Sndfile.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.cpp 2011-04-13 20:38:28 UTC (rev 854) +++ trunk/OpenMPT/soundlib/Sndfile.cpp 2011-04-14 14:23:39 UTC (rev 855) @@ -2505,16 +2505,19 @@ #ifndef FASTSOUNDLIB -bool CSoundFile::IsSampleUsed(SAMPLEINDEX nSample) -//------------------------------------------------ +// Check whether a sample is used. +// In sample mode, the sample numbers in all patterns are checked. +// In instrument mode, it is only checked if a sample is referenced by an instrument (but not if the sample is actually played anywhere) +bool CSoundFile::IsSampleUsed(SAMPLEINDEX nSample) const +//------------------------------------------------------ { - if ((!nSample) || (nSample > m_nSamples)) return false; - if (m_nInstruments) + if ((!nSample) || (nSample > GetNumSamples())) return false; + if (GetNumInstruments()) { - for (UINT i=1; i<=m_nInstruments; i++) if (Instruments[i]) + for (UINT i = 1; i <= GetNumInstruments(); i++) if (Instruments[i]) { MODINSTRUMENT *pIns = Instruments[i]; - for (UINT j=0; j<128; j++) + for (UINT j = 0; j < CountOf(pIns->Keyboard); j++) { if (pIns->Keyboard[j] == nSample) return true; } @@ -2523,7 +2526,7 @@ { for (UINT i=0; i<Patterns.Size(); i++) if (Patterns[i]) { - MODCOMMAND *m = Patterns[i]; + const MODCOMMAND *m = Patterns[i]; for (UINT j=m_nChannels*Patterns[i].GetNumRows(); j; m++, j--) { if (m->instr == nSample && !m->IsPcNote()) return true; @@ -2534,13 +2537,13 @@ } -bool CSoundFile::IsInstrumentUsed(INSTRUMENTINDEX nInstr) -//------------------------------------------------------- +bool CSoundFile::IsInstrumentUsed(INSTRUMENTINDEX nInstr) const +//------------------------------------------------------------- { - if ((!nInstr) || (nInstr > m_nInstruments) || (!Instruments[nInstr])) return false; + if ((!nInstr) || (nInstr > GetNumInstruments()) || (!Instruments[nInstr])) return false; for (UINT i=0; i<Patterns.Size(); i++) if (Patterns[i]) { - MODCOMMAND *m = Patterns[i]; + const MODCOMMAND *m = Patterns[i]; for (UINT j=m_nChannels*Patterns[i].GetNumRows(); j; m++, j--) { if (m->instr == nInstr && !m->IsPcNote()) return true; @@ -2550,8 +2553,10 @@ } -SAMPLEINDEX CSoundFile::DetectUnusedSamples(vector<bool> &sampleUsed) -//------------------------------------------------------------------- +// Detect samples are are referenced by an instrument, but actually not used in a song. +// Only works in instrument mode. Unused samples are marked as false in the vector. +SAMPLEINDEX CSoundFile::DetectUnusedSamples(vector<bool> &sampleUsed) const +//------------------------------------------------------------------------- { sampleUsed.assign(GetNumSamples() + 1, false); @@ -2606,19 +2611,23 @@ } -bool CSoundFile::RemoveSelectedSamples(bool *pbIns) -//------------------------------------------------- +// Destroy samples where keepSamples index is false. First sample is keepSamples[1]! +SAMPLEINDEX CSoundFile::RemoveSelectedSamples(const vector<bool> &keepSamples) +//---------------------------------------------------------------------------- { - if (!pbIns) return false; - for (SAMPLEINDEX nSmp=1; nSmp<MAX_SAMPLES; nSmp++) + SAMPLEINDEX nRemoved = 0; + for (SAMPLEINDEX nSmp = (SAMPLEINDEX)min(MAX_SAMPLES - 1, keepSamples.size()); nSmp >= 1; nSmp--) { - if ((!pbIns[nSmp]) && (Samples[nSmp].pSample)) + if(!keepSamples[nSmp]) { - DestroySample(nSmp); - if ((nSmp == m_nSamples) && (nSmp > 1)) m_nSamples--; + if(DestroySample(nSmp)) + { + nRemoved++; + } + if((nSmp == GetNumSamples()) && (nSmp > 1)) m_nSamples--; } } - return true; + return nRemoved; } Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2011-04-13 20:38:28 UTC (rev 854) +++ trunk/OpenMPT/soundlib/Sndfile.h 2011-04-14 14:23:39 UTC (rev 855) @@ -948,11 +948,11 @@ //BOOL DestroyInstrument(UINT nInstr); bool DestroyInstrument(INSTRUMENTINDEX nInstr, char removeSamples = 0); // -! BEHAVIOUR_CHANGE#0003 - bool IsSampleUsed(SAMPLEINDEX nSample); - bool IsInstrumentUsed(INSTRUMENTINDEX nInstr); + bool IsSampleUsed(SAMPLEINDEX nSample) const; + bool IsInstrumentUsed(INSTRUMENTINDEX nInstr) const; bool RemoveInstrumentSamples(INSTRUMENTINDEX nInstr); - SAMPLEINDEX DetectUnusedSamples(vector<bool> &sampleUsed); - bool RemoveSelectedSamples(bool *pbIns); + SAMPLEINDEX DetectUnusedSamples(vector<bool> &sampleUsed) const; + SAMPLEINDEX RemoveSelectedSamples(const vector<bool> &keepSamples); void AdjustSampleLoop(MODSAMPLE *pSmp); // Samples file I/O bool ReadSampleFromFile(SAMPLEINDEX nSample, LPBYTE lpMemFile, DWORD dwFileLength); Modified: trunk/OpenMPT/soundlib/modsmp_ctrl.cpp =================================================================== --- trunk/OpenMPT/soundlib/modsmp_ctrl.cpp 2011-04-13 20:38:28 UTC (rev 854) +++ trunk/OpenMPT/soundlib/modsmp_ctrl.cpp 2011-04-14 14:23:39 UTC (rev 855) @@ -226,6 +226,8 @@ switch(resetflag) { case SmpResetInit: + strcpy(rSndFile.m_szNames[i], ""); + strcpy(rSndFile.Samples[i].filename, ""); rSndFile.Samples[i].nC5Speed = 8363; // note: break is left out intentionally. keep this order or c&p the stuff from below if you change anything! case SmpResetCompo: @@ -411,7 +413,7 @@ template <class T> void UnsignSampleImpl(T* pStart, T nOffset, const SmpLength nLength) -//--------------------------------------------------------------------- +//------------------------------------------------------------------ { for(SmpLength i = 0; i < nLength; i++) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |