From: <sag...@us...> - 2011-09-03 01:34:48
|
Revision: 1010 http://modplug.svn.sourceforge.net/modplug/?rev=1010&view=rev Author: saga-games Date: 2011-09-03 01:34:39 +0000 (Sat, 03 Sep 2011) Log Message: ----------- [Ref] A bit more cleanup Modified Paths: -------------- trunk/OpenMPT/mptrack/Ctrl_smp.cpp trunk/OpenMPT/mptrack/MPTHacks.cpp trunk/OpenMPT/mptrack/Modedit.cpp trunk/OpenMPT/mptrack/Moptions.cpp trunk/OpenMPT/mptrack/Undo.cpp trunk/OpenMPT/mptrack/View_smp.cpp trunk/OpenMPT/soundlib/IT_DEFS.H trunk/OpenMPT/soundlib/Load_it.cpp trunk/OpenMPT/soundlib/modsmp_ctrl.cpp trunk/OpenMPT/soundlib/modsmp_ctrl.h Modified: trunk/OpenMPT/mptrack/Ctrl_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -919,15 +919,18 @@ CSoundFile *pSndFile = m_pModDoc->GetSoundFile(); SetCurrentSample(smp); - if(bDuplicate && nOldSmp >= 1 && nOldSmp < MAX_SAMPLES) + if(bDuplicate && nOldSmp >= 1 && nOldSmp <= pSndFile->GetNumSamples()) { m_pModDoc->GetSampleUndo()->PrepareUndo(smp, sundo_replace); MemCopy(m_pSndFile->GetSample(smp), m_pSndFile->GetSample(nOldSmp)); strcpy(m_pSndFile->m_szNames[smp], m_pSndFile->m_szNames[nOldSmp]); - // clone sample. - if((m_pSndFile->GetSample(smp).pSample = CSoundFile::AllocateSample(m_pSndFile->GetSample(nOldSmp).GetSampleSizeInBytes())) != nullptr) + // Clone sample. + if(m_pSndFile->GetSample(nOldSmp).pSample != nullptr) { - memcpy(m_pSndFile->GetSample(smp).pSample, m_pSndFile->GetSample(nOldSmp).pSample, m_pSndFile->GetSample(nOldSmp).GetSampleSizeInBytes()); + if((m_pSndFile->GetSample(smp).pSample = CSoundFile::AllocateSample(m_pSndFile->GetSample(nOldSmp).GetSampleSizeInBytes())) != nullptr) + { + memcpy(m_pSndFile->GetSample(smp).pSample, m_pSndFile->GetSample(nOldSmp).pSample, m_pSndFile->GetSample(nOldSmp).GetSampleSizeInBytes()); + } } } @@ -1501,14 +1504,14 @@ } } if (sample.nLoopStart >= dwEnd) sample.nLoopStart += (dwEnd-dwStart); else - if (sample.nLoopStart > dwStart) sample.nLoopStart += (sample.nLoopStart - dwStart); + if (sample.nLoopStart > dwStart) sample.nLoopStart += (sample.nLoopStart - dwStart); if (sample.nLoopEnd >= dwEnd) sample.nLoopEnd += (dwEnd-dwStart); else - if (sample.nLoopEnd > dwStart) sample.nLoopEnd += (sample.nLoopEnd - dwStart); + if (sample.nLoopEnd > dwStart) sample.nLoopEnd += (sample.nLoopEnd - dwStart); if (sample.nSustainStart >= dwEnd) sample.nSustainStart += (dwEnd-dwStart); else - if (sample.nSustainStart > dwStart) sample.nSustainStart += (sample.nSustainStart - dwStart); + if (sample.nSustainStart > dwStart) sample.nSustainStart += (sample.nSustainStart - dwStart); if (sample.nSustainEnd >= dwEnd) sample.nSustainEnd += (dwEnd-dwStart); else - if (sample.nSustainEnd > dwStart) sample.nSustainEnd += (sample.nSustainEnd - dwStart); - + if (sample.nSustainEnd > dwStart) sample.nSustainEnd += (sample.nSustainEnd - dwStart); + CriticalSection cs; for (UINT iFix=0; iFix<MAX_CHANNELS; iFix++) @@ -2215,7 +2218,7 @@ SELECTIONPOINTS selection = GetSelectionPoints(); m_pModDoc->GetSampleUndo()->PrepareUndo(m_nSample, sundo_reverse, selection.nStart, selection.nEnd); - if(ctrlSmp::ReverseSample(&sample, selection.nStart, selection.nEnd, m_pSndFile) == true) + if(ctrlSmp::ReverseSample(sample, selection.nStart, selection.nEnd, m_pSndFile) == true) { m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEDATA, NULL); m_pModDoc->SetModified(); @@ -2236,7 +2239,7 @@ SELECTIONPOINTS selection = GetSelectionPoints(); m_pModDoc->GetSampleUndo()->PrepareUndo(m_nSample, sundo_invert, selection.nStart, selection.nEnd); - if(ctrlSmp::InvertSample(&sample, selection.nStart, selection.nEnd, m_pSndFile) == true) + if(ctrlSmp::InvertSample(sample, selection.nStart, selection.nEnd, m_pSndFile) == true) { m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEDATA, NULL); m_pModDoc->SetModified(); @@ -2262,7 +2265,7 @@ SELECTIONPOINTS selection = GetSelectionPoints(); m_pModDoc->GetSampleUndo()->PrepareUndo(m_nSample, sundo_unsign, selection.nStart, selection.nEnd); - if(ctrlSmp::UnsignSample(&sample, selection.nStart, selection.nEnd, m_pSndFile) == true) + if(ctrlSmp::UnsignSample(sample, selection.nStart, selection.nEnd, m_pSndFile) == true) { m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEDATA, NULL); m_pModDoc->SetModified(); @@ -3163,7 +3166,7 @@ sample.nLoopStart = nFadeLength; } - if(ctrlSmp::XFadeSample(&sample, nFadeLength, m_pSndFile)) + if(ctrlSmp::XFadeSample(sample, nFadeLength, m_pSndFile)) { m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEDATA | HINT_SAMPLEINFO, NULL); m_pModDoc->SetModified(); Modified: trunk/OpenMPT/mptrack/MPTHacks.cpp =================================================================== --- trunk/OpenMPT/mptrack/MPTHacks.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/MPTHacks.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -265,7 +265,7 @@ foundHere = foundHacks = true; if(autofix) { - ctrlSmp::ConvertToMono(&smp, &m_SndFile); + ctrlSmp::ConvertToMono(smp, &m_SndFile); } else { break; Modified: trunk/OpenMPT/mptrack/Modedit.cpp =================================================================== --- trunk/OpenMPT/mptrack/Modedit.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/Modedit.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -462,8 +462,8 @@ return SAMPLEINDEX_INVALID; } if (!m_SndFile.m_szNames[i][0]) strcpy(m_SndFile.m_szNames[i], "untitled"); + if (i > m_SndFile.GetNumSamples()) m_SndFile.m_nSamples = i; InitializeSample(m_SndFile.GetSample(i)); - if (i > m_SndFile.GetNumSamples()) m_SndFile.m_nSamples = i; SetModified(); return i; } @@ -474,9 +474,10 @@ INSTRUMENTINDEX CModDoc::InsertInstrument(SAMPLEINDEX nSample, INSTRUMENTINDEX nDuplicate) //---------------------------------------------------------------------------------------- { + if (m_SndFile.GetModSpecifications().instrumentsMax == 0) return INSTRUMENTINDEX_INVALID; + MODINSTRUMENT *pDup = nullptr; const INSTRUMENTINDEX nInstrumentMax = m_SndFile.GetModSpecifications().instrumentsMax - 1; - if ((m_SndFile.m_nType != MOD_TYPE_XM) && !(m_SndFile.m_nType & (MOD_TYPE_IT | MOD_TYPE_MPT))) return INSTRUMENTINDEX_INVALID; if ((nDuplicate > 0) && (nDuplicate <= m_SndFile.m_nInstruments)) { pDup = m_SndFile.Instruments[nDuplicate]; @@ -494,15 +495,17 @@ m_SndFile.GetSample(smp).uFlags &= ~CHN_MUTE; if (!m_SndFile.Instruments[smp]) { - MODINSTRUMENT *p = new MODINSTRUMENT; - if (!p) + try { + MODINSTRUMENT *p = new MODINSTRUMENT; + InitializeInstrument(p, smp); + m_SndFile.Instruments[smp] = p; + lstrcpyn(p->name, m_SndFile.m_szNames[smp], sizeof(p->name)); + } catch(...) + { ErrorBox(IDS_ERR_OUTOFMEMORY, CMainFrame::GetMainFrame()); return INSTRUMENTINDEX_INVALID; } - InitializeInstrument(p, smp); - m_SndFile.Instruments[smp] = p; - lstrcpyn(p->name, m_SndFile.m_szNames[smp], sizeof(p->name)); } } m_SndFile.m_nInstruments = nInstruments; Modified: trunk/OpenMPT/mptrack/Moptions.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moptions.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/Moptions.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -81,7 +81,7 @@ { CPropertyPage::OnInitDialog(); m_pPreviewDib = LoadDib(MAKEINTRESOURCE(IDB_COLORSETUP)); - memcpy(CustomColors, CMainFrame::GetSettings().rgbCustomColors, sizeof(CustomColors)); + MemCopy(CustomColors, CMainFrame::GetSettings().rgbCustomColors); for (UINT i = 0; i < CountOf(gColorDefs); i++) { m_ComboItem.SetItemData(m_ComboItem.AddString(gColorDefs[i].pszName), i); @@ -129,7 +129,7 @@ CMainFrame::GetSettings().m_nRowHighlightMeasures = GetDlgItemInt(IDC_PRIMARYHILITE); CMainFrame::GetSettings().m_nRowHighlightBeats = GetDlgItemInt(IDC_SECONDARYHILITE); - memcpy(CMainFrame::GetSettings().rgbCustomColors, CustomColors, sizeof(CMainFrame::GetSettings().rgbCustomColors)); + MemCopy(CMainFrame::GetSettings().rgbCustomColors, CustomColors); CMainFrame::UpdateColors(); CMainFrame *pMainFrm = CMainFrame::GetMainFrame(); if (pMainFrm) pMainFrm->PostMessage(WM_MOD_INVALIDATEPATTERNS, HINT_MPTOPTIONS); Modified: trunk/OpenMPT/mptrack/Undo.cpp =================================================================== --- trunk/OpenMPT/mptrack/Undo.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/Undo.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -381,17 +381,17 @@ case sundo_invert: // invert again - ctrlSmp::InvertSample(&sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); + ctrlSmp::InvertSample(sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); break; case sundo_reverse: // reverse again - ctrlSmp::ReverseSample(&sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); + ctrlSmp::ReverseSample(sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); break; case sundo_unsign: // unsign again - ctrlSmp::UnsignSample(&sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); + ctrlSmp::UnsignSample(sample, pUndo->nChangeStart, pUndo->nChangeEnd, pSndFile); break; case sundo_insert: Modified: trunk/OpenMPT/mptrack/View_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_smp.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/mptrack/View_smp.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -2045,7 +2045,7 @@ if ((sample.uFlags & CHN_STEREO) && (sample.pSample) && (sample.nLength)) { pModDoc->GetSampleUndo()->PrepareUndo(m_nSample, sundo_replace); - if(ctrlSmp::ConvertToMono(&sample, pSndFile)) + if(ctrlSmp::ConvertToMono(sample, pSndFile)) { pModDoc->SetModified(); pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEDATA | HINT_SAMPLEINFO, NULL); Modified: trunk/OpenMPT/soundlib/IT_DEFS.H =================================================================== --- trunk/OpenMPT/soundlib/IT_DEFS.H 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/soundlib/IT_DEFS.H 2011-09-03 01:34:39 UTC (rev 1010) @@ -3,7 +3,7 @@ #pragma pack(1) -typedef struct tagITFILEHEADER +struct ITFILEHEADER { DWORD id; // 0x4D504D49 CHAR songname[26]; @@ -28,10 +28,10 @@ DWORD reserved; // ChibiTracker writes "CHBI" here. OpenMPT writes "OMPT" here in some cases, see Load_it.cpp BYTE chnpan[64]; BYTE chnvol[64]; -} ITFILEHEADER; +}; -typedef struct tagITENVELOPE +struct ITENVELOPE { BYTE flags; BYTE num; @@ -41,10 +41,10 @@ BYTE sle; BYTE data[25*3]; BYTE reserved; -} ITENVELOPE; +}; // Old Impulse Instrument Format (cmwt < 0x200) -typedef struct tagITOLDINSTRUMENT +struct ITOLDINSTRUMENT { DWORD id; // IMPI = 0x49504D49 CHAR filename[12]; // DOS file name @@ -66,11 +66,11 @@ BYTE keyboard[240]; BYTE volenv[200]; BYTE nodes[50]; -} ITOLDINSTRUMENT; +}; // Impulse Instrument Format -typedef struct tagITINSTRUMENT +struct ITINSTRUMENT { DWORD id; CHAR filename[12]; @@ -99,19 +99,19 @@ ITENVELOPE panenv; ITENVELOPE pitchenv; BYTE dummy[4]; // was 7, but IT v2.17 saves 554 bytes -} ITINSTRUMENT; +}; // MPT IT Instrument Extension -typedef struct _ITINSTRUMENTEX +struct ITINSTRUMENTEX { ITINSTRUMENT iti; BYTE keyboardhi[120]; -} ITINSTRUMENTEX, *PITINSTRUMENTEX; +}; // IT Sample Format -typedef struct tagITSAMPLESTRUCT +struct ITSAMPLESTRUCT { DWORD id; // 0x53504D49 CHAR filename[12]; @@ -133,16 +133,16 @@ BYTE vid; BYTE vir; BYTE vit; -} ITSAMPLESTRUCT; +}; // IT Header extension: Save history -typedef struct tagITHISTORYSTRUCT +struct ITHISTORYSTRUCT { - uint16 fatdate; // DOS/FAT date when the file was opened / created in the editor. For details, read http://msdn.microsoft.com/en-us/library/ms724247(VS.85).aspx - uint16 fattime; // DOS/FAT time when the file was opened / created in the editor. + uint16 fatdate; // DOS / FAT date when the file was opened / created in the editor. For details, read http://msdn.microsoft.com/en-us/library/ms724247(VS.85).aspx + uint16 fattime; // DOS / FAT time when the file was opened / created in the editor. uint32 runtime; // The time how long the file was open in the editor, in 1/18.2th seconds. (= ticks of the DOS timer) -} ITHISTORYSTRUCT; +}; #pragma pack() @@ -159,6 +159,10 @@ #define IT_OMPT 0x54504D4F // "OMPT" magic bytes for non-standard OpenMPT IT files #define IT_CHBI 0x49424843 // "CHBI" magic bytes in the IT header to identify ChibiTracker +// MPT stuff +#define IT_PNAM 0x4D414E50 // "PNAM" pattern names +#define IT_CNAM 0x4D414E43 // "CNAM" channel names + enum IT_ReaderBitMasks { // pattern row parsing, the channel data is read to obtain Modified: trunk/OpenMPT/soundlib/Load_it.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_it.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/soundlib/Load_it.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -493,7 +493,7 @@ // -> DESC="misc quantity changes" // BYTE chnmask[64], channels_used[64]; // MODCOMMAND lastvalue[64]; - BYTE chnmask[MAX_BASECHANNELS], channels_used[MAX_BASECHANNELS]; + BYTE chnmask[MAX_BASECHANNELS]; MODCOMMAND lastvalue[MAX_BASECHANNELS]; // -! BEHAVIOUR_CHANGE#0006 @@ -825,7 +825,7 @@ // Read pattern names: "PNAM" char *patNames = nullptr; UINT patNamesLen = 0; - if ((dwMemPos + 8 < dwMemLength) && (*((DWORD *)(lpStream+dwMemPos)) == 0x4d414e50)) + if ((dwMemPos + 8 < dwMemLength) && (*((DWORD *)(lpStream+dwMemPos)) == LittleEndian(IT_PNAM))) { patNamesLen = *((DWORD *)(lpStream + dwMemPos + 4)); dwMemPos += 8; @@ -838,7 +838,7 @@ m_nChannels = GetModSpecifications().channelsMin; // Read channel names: "CNAM" - if ((dwMemPos + 8 < dwMemLength) && (*((DWORD *)(lpStream+dwMemPos)) == 0x4d414e43)) + if ((dwMemPos + 8 < dwMemLength) && (*((DWORD *)(lpStream+dwMemPos)) == LittleEndian(IT_CNAM))) { UINT len = *((DWORD *)(lpStream+dwMemPos+4)); dwMemPos += 8; @@ -1151,7 +1151,6 @@ } m[ch].note = note; lastvalue[ch].note = note; - channels_used[ch] = TRUE; } } if (chnmask[ch] & 2) @@ -1411,11 +1410,9 @@ if(header.ordnum < 2) header.ordnum = 2; } - header.insnum = m_nInstruments; - header.smpnum = m_nSamples; - header.patnum = (GetType() == MOD_TYPE_MPT) ? Patterns.Size() : MAX_PATTERNS; - if(Patterns.Size() < header.patnum) Patterns.ResizeArray(header.patnum); - while ((header.patnum > 0) && (!Patterns[header.patnum - 1])) header.patnum--; + header.insnum = min(m_nInstruments, specs.instrumentsMax); + header.smpnum = min(m_nSamples, specs.samplesMax); + header.patnum = min(Patterns.GetNumPatterns(), specs.patternsMax); // Parapointers vector<DWORD> patpos(header.patnum, 0); @@ -1433,7 +1430,7 @@ MptVersion::VersionNum vVersion = MptVersion::num; header.cwtv = LittleEndianW(0x5000 | (WORD)((vVersion >> 16) & 0x0FFF)); // format: txyy (t = tracker ID, x = version major, yy = version minor), e.g. 0x5117 (OpenMPT = 5, 117 = v1.17) header.cmwt = LittleEndianW(0x0214); // Common compatible tracker :) - // hack from schism tracker: + // Hack from schism tracker: for(INSTRUMENTINDEX nIns = 1; nIns <= GetNumInstruments(); nIns++) { if(Instruments[nIns] && Instruments[nIns]->PitchEnv.dwFlags & ENV_FILTER) @@ -1458,7 +1455,7 @@ if (m_dwSongFlags & SONG_ITCOMPATGXX) header.flags |= 0x20; if ((m_dwSongFlags & SONG_EXFILTERRANGE) && !compatExport) header.flags |= 0x1000; header.globalvol = m_nDefaultGlobalVolume >> 1; - header.mv = CLAMP(m_nSamplePreAmp, 0, 128); + header.mv = min(m_nSamplePreAmp, 128); header.speed = m_nDefaultSpeed; header.tempo = min(m_nDefaultTempo, 255); //Limit this one to 255, we save the real one as an extension below. header.sep = 128; // pan separation @@ -1467,7 +1464,7 @@ memset(header.chnpan, 0xA0, 64); memset(header.chnvol, 64, 64); - for (UINT ich=0; ich</*m_nChannels*/64; ich++) //Header only has room for settings for 64 chans... + for (size_t ich = 0; ich < 64; ich++) //Header only has room for settings for 64 chans... { header.chnpan[ich] = ChnSettings[ich].nPan >> 2; if (ChnSettings[ich].dwFlags & CHN_SURROUND) header.chnpan[ich] = 100; @@ -1515,8 +1512,8 @@ if (m_lpszSongComments) { header.special |= 1; - header.msglength = strlen(m_lpszSongComments)+1; - header.msgoffset = dwHdrPos + dwExtra + header.insnum*4 + header.patnum*4 + header.smpnum*4; + header.msglength = strlen(m_lpszSongComments) + 1; + header.msgoffset = dwHdrPos + dwExtra + (header.insnum + header.smpnum + header.patnum) * 4; } // Write file header @@ -1538,7 +1535,7 @@ // Writing pattern names if (numNamedPats && !compatExport) { - DWORD d = 0x4d414e50; + DWORD d = LittleEndian(IT_PNAM); // "PNAM" fwrite(&d, 1, 4, f); d = numNamedPats * MAX_PATTERNNAME; fwrite(&d, 1, 4, f); @@ -1555,7 +1552,7 @@ // Writing channel names if (dwChnNamLen && !compatExport) { - DWORD d = 0x4d414e43; + DWORD d = LittleEndian(IT_CNAM); // "CNAM" fwrite(&d, 1, 4, f); fwrite(&dwChnNamLen, 1, 4, f); UINT nChnNames = dwChnNamLen / MAX_CHANNELNAME; Modified: trunk/OpenMPT/soundlib/modsmp_ctrl.cpp =================================================================== --- trunk/OpenMPT/soundlib/modsmp_ctrl.cpp 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/soundlib/modsmp_ctrl.cpp 2011-09-03 01:34:39 UTC (rev 1010) @@ -11,7 +11,7 @@ namespace ctrlSmp { -void ReplaceSample(MODSAMPLE& smp, const LPSTR pNewSample, const SmpLength nNewLength, CSoundFile* pSndFile) +void ReplaceSample(MODSAMPLE &smp, const LPSTR pNewSample, const SmpLength nNewLength, CSoundFile* pSndFile) //---------------------------------------------------------------------------------------------------------- { LPSTR const pOldSmp = smp.pSample; @@ -36,7 +36,7 @@ } -SmpLength InsertSilence(MODSAMPLE& smp, const SmpLength nSilenceLength, const SmpLength nStartFrom, CSoundFile* pSndFile) +SmpLength InsertSilence(MODSAMPLE &smp, const SmpLength nSilenceLength, const SmpLength nStartFrom, CSoundFile* pSndFile) //----------------------------------------------------------------------------------------------------------------------- { if(nSilenceLength == 0 || nSilenceLength >= MAX_SAMPLE_LENGTH || smp.nLength > MAX_SAMPLE_LENGTH - nSilenceLength) @@ -92,7 +92,7 @@ } -SmpLength ResizeSample(MODSAMPLE& smp, const SmpLength nNewLength, CSoundFile* pSndFile) +SmpLength ResizeSample(MODSAMPLE &smp, const SmpLength nNewLength, CSoundFile* pSndFile) //-------------------------------------------------------------------------------------- { // Invalid sample size @@ -140,7 +140,7 @@ template <class T> -void AdjustEndOfSampleImpl(MODSAMPLE& smp) +void AdjustEndOfSampleImpl(MODSAMPLE &smp) //---------------------------------------- { MODSAMPLE* const pSmp = &smp; @@ -169,46 +169,44 @@ } // unnamed namespace. -bool AdjustEndOfSample(MODSAMPLE& smp, CSoundFile* pSndFile) +bool AdjustEndOfSample(MODSAMPLE &smp, CSoundFile* pSndFile) //---------------------------------------------------------- { - MODSAMPLE* const pSmp = &smp; - - if ((!pSmp->nLength) || (!pSmp->pSample)) + if ((!smp.nLength) || (!smp.pSample)) return false; CriticalSection cs; - if (pSmp->GetElementarySampleSize() == 2) - AdjustEndOfSampleImpl<int16>(*pSmp); - else if(pSmp->GetElementarySampleSize() == 1) - AdjustEndOfSampleImpl<int8>(*pSmp); + if (smp.GetElementarySampleSize() == 2) + AdjustEndOfSampleImpl<int16>(smp); + else if(smp.GetElementarySampleSize() == 1) + AdjustEndOfSampleImpl<int8>(smp); // Update channels with new loop values if(pSndFile != nullptr) { CSoundFile& rSndFile = *pSndFile; - for (UINT i=0; i<MAX_CHANNELS; i++) if ((rSndFile.Chn[i].pModSample == pSmp) && (rSndFile.Chn[i].nLength)) + for (UINT i=0; i<MAX_CHANNELS; i++) if ((rSndFile.Chn[i].pModSample == &smp) && (rSndFile.Chn[i].nLength)) { - if ((pSmp->nLoopStart + 3 < pSmp->nLoopEnd) && (pSmp->nLoopEnd <= pSmp->nLength)) + if ((smp.nLoopStart + 3 < smp.nLoopEnd) && (smp.nLoopEnd <= smp.nLength)) { - rSndFile.Chn[i].nLoopStart = pSmp->nLoopStart; - rSndFile.Chn[i].nLoopEnd = pSmp->nLoopEnd; - rSndFile.Chn[i].nLength = pSmp->nLoopEnd; + rSndFile.Chn[i].nLoopStart = smp.nLoopStart; + rSndFile.Chn[i].nLoopEnd = smp.nLoopEnd; + rSndFile.Chn[i].nLength = smp.nLoopEnd; if (rSndFile.Chn[i].nPos > rSndFile.Chn[i].nLength) { rSndFile.Chn[i].nPos = rSndFile.Chn[i].nLoopStart; rSndFile.Chn[i].dwFlags &= ~CHN_PINGPONGFLAG; } DWORD d = rSndFile.Chn[i].dwFlags & ~(CHN_PINGPONGLOOP|CHN_LOOP); - if (pSmp->uFlags & CHN_LOOP) + if (smp.uFlags & CHN_LOOP) { d |= CHN_LOOP; - if (pSmp->uFlags & CHN_PINGPONGLOOP) d |= CHN_PINGPONGLOOP; + if (smp.uFlags & CHN_PINGPONGLOOP) d |= CHN_PINGPONGLOOP; } rSndFile.Chn[i].dwFlags = d; } else - if (!(pSmp->uFlags & CHN_LOOP)) + if (!(smp.uFlags & CHN_LOOP)) { rSndFile.Chn[i].dwFlags &= ~(CHN_PINGPONGLOOP|CHN_LOOP); } @@ -219,7 +217,7 @@ } -void ResetSamples(CSoundFile& rSndFile, ResetFlag resetflag) +void ResetSamples(CSoundFile &rSndFile, ResetFlag resetflag) //---------------------------------------------------------- { const UINT nSamples = rSndFile.GetNumSamples(); @@ -313,7 +311,7 @@ // Remove DC offset -float RemoveDCOffset(MODSAMPLE& smp, +float RemoveDCOffset(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, const MODTYPE modtype, @@ -323,27 +321,25 @@ if(smp.pSample == nullptr || smp.nLength < 1) return 0; - MODSAMPLE* const pSmp = &smp; - - if (iEnd > pSmp->nLength) iEnd = pSmp->nLength; + if (iEnd > smp.nLength) iEnd = smp.nLength; if (iStart > iEnd) iStart = iEnd; if (iStart == iEnd) { iStart = 0; - iEnd = pSmp->nLength; + iEnd = smp.nLength; } - iStart *= pSmp->GetNumChannels(); - iEnd *= pSmp->GetNumChannels(); + iStart *= smp.GetNumChannels(); + iEnd *= smp.GetNumChannels(); - const double dMaxAmplitude = (pSmp->GetElementarySampleSize() == 2) ? GetMaxAmplitude<int16>() : GetMaxAmplitude<int8>(); + const double dMaxAmplitude = (smp.GetElementarySampleSize() == 2) ? GetMaxAmplitude<int16>() : GetMaxAmplitude<int8>(); // step 1: Calculate offset. OffsetData oData = {0,0,0}; - if(pSmp->GetElementarySampleSize() == 2) - oData = CalculateOffset(reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetElementarySampleSize() == 1) - oData = CalculateOffset(reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart); + if(smp.GetElementarySampleSize() == 2) + oData = CalculateOffset(reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetElementarySampleSize() == 1) + oData = CalculateOffset(reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart); double dMin = oData.dMin, dMax = oData.dMax, dOffset = oData.dOffset; @@ -361,22 +357,22 @@ // step 2: centralize + normalize sample dOffset *= dMaxAmplitude * dAmplify; - if(pSmp->GetElementarySampleSize() == 2) - RemoveOffsetAndNormalize( reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart, dOffset, dAmplify); - else if(pSmp->GetElementarySampleSize() == 1) - RemoveOffsetAndNormalize( reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart, dOffset, dAmplify); + if(smp.GetElementarySampleSize() == 2) + RemoveOffsetAndNormalize( reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart, dOffset, dAmplify); + else if(smp.GetElementarySampleSize() == 1) + RemoveOffsetAndNormalize( reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart, dOffset, dAmplify); // step 3: adjust global vol (if available) - if((modtype & (MOD_TYPE_IT | MOD_TYPE_MPT)) && (iStart == 0) && (iEnd == pSmp->nLength * pSmp->GetNumChannels())) + if((modtype & (MOD_TYPE_IT | MOD_TYPE_MPT)) && (iStart == 0) && (iEnd == smp.nLength * smp.GetNumChannels())) { CriticalSection cs; - pSmp->nGlobalVol = min((WORD)(pSmp->nGlobalVol / dAmplify), 64); + smp.nGlobalVol = min((WORD)(smp.nGlobalVol / dAmplify), 64); for (CHANNELINDEX i = 0; i < MAX_CHANNELS; i++) { - if(pSndFile->Chn[i].pSample == pSmp->pSample) + if(pSndFile->Chn[i].pSample == smp.pSample) { - pSndFile->Chn[i].nGlobalVol = pSmp->nGlobalVol; + pSndFile->Chn[i].nGlobalVol = smp.nGlobalVol; } } } @@ -398,30 +394,30 @@ } // Reverse sample data -bool ReverseSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) -//----------------------------------------------------------------------------------------- +bool ReverseSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) +//---------------------------------------------------------------------------------------- { - if(pSmp->pSample == nullptr) return false; - if(iEnd == 0 || iStart > pSmp->nLength || iEnd > pSmp->nLength) + if(smp.pSample == nullptr) return false; + if(iEnd == 0 || iStart > smp.nLength || iEnd > smp.nLength) { iStart = 0; - iEnd = pSmp->nLength; + iEnd = smp.nLength; } if(iEnd - iStart < 2) return false; - if(pSmp->GetBytesPerSample() == 8) // unused (yet) - ReverseSampleImpl(reinterpret_cast<int64*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetBytesPerSample() == 4) // 16 bit stereo - ReverseSampleImpl(reinterpret_cast<int32*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetBytesPerSample() == 2) // 16 bit mono / 8 bit stereo - ReverseSampleImpl(reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetBytesPerSample() == 1) // 8 bit mono - ReverseSampleImpl(reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart); + if(smp.GetBytesPerSample() == 8) // unused (yet) + ReverseSampleImpl(reinterpret_cast<int64*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetBytesPerSample() == 4) // 16 bit stereo + ReverseSampleImpl(reinterpret_cast<int32*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetBytesPerSample() == 2) // 16 bit mono / 8 bit stereo + ReverseSampleImpl(reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetBytesPerSample() == 1) // 8 bit mono + ReverseSampleImpl(reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart); else return false; - AdjustEndOfSample(*pSmp, pSndFile); + AdjustEndOfSample(smp, pSndFile); return true; } @@ -438,25 +434,25 @@ } // Virtually unsign sample data -bool UnsignSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) -//---------------------------------------------------------------------------------------- +bool UnsignSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) +//--------------------------------------------------------------------------------------- { - if(pSmp->pSample == nullptr) return false; - if(iEnd == 0 || iStart > pSmp->nLength || iEnd > pSmp->nLength) + if(smp.pSample == nullptr) return false; + if(iEnd == 0 || iStart > smp.nLength || iEnd > smp.nLength) { iStart = 0; - iEnd = pSmp->nLength; + iEnd = smp.nLength; } - iStart *= pSmp->GetNumChannels(); - iEnd *= pSmp->GetNumChannels(); - if(pSmp->GetElementarySampleSize() == 2) - UnsignSampleImpl(reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetElementarySampleSize() == 1) - UnsignSampleImpl(reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart); + iStart *= smp.GetNumChannels(); + iEnd *= smp.GetNumChannels(); + if(smp.GetElementarySampleSize() == 2) + UnsignSampleImpl(reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetElementarySampleSize() == 1) + UnsignSampleImpl(reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart); else return false; - AdjustEndOfSample(*pSmp, pSndFile); + AdjustEndOfSample(smp, pSndFile); return true; } @@ -472,25 +468,25 @@ } // Invert sample data (flip by 180 degrees) -bool InvertSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) -//---------------------------------------------------------------------------------------- +bool InvertSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile) +//--------------------------------------------------------------------------------------- { - if(pSmp->pSample == nullptr) return false; - if(iEnd == 0 || iStart > pSmp->nLength || iEnd > pSmp->nLength) + if(smp.pSample == nullptr) return false; + if(iEnd == 0 || iStart > smp.nLength || iEnd > smp.nLength) { iStart = 0; - iEnd = pSmp->nLength; + iEnd = smp.nLength; } - iStart *= pSmp->GetNumChannels(); - iEnd *= pSmp->GetNumChannels(); - if(pSmp->GetElementarySampleSize() == 2) - InvertSampleImpl(reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart); - else if(pSmp->GetElementarySampleSize() == 1) - InvertSampleImpl(reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart); + iStart *= smp.GetNumChannels(); + iEnd *= smp.GetNumChannels(); + if(smp.GetElementarySampleSize() == 2) + InvertSampleImpl(reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart); + else if(smp.GetElementarySampleSize() == 1) + InvertSampleImpl(reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart); else return false; - AdjustEndOfSample(*pSmp, pSndFile); + AdjustEndOfSample(smp, pSndFile); return true; } @@ -507,27 +503,27 @@ } // X-Fade sample data to create smooth loop transitions -bool XFadeSample(MODSAMPLE *pSmp, SmpLength iFadeLength, CSoundFile *pSndFile) -//---------------------------------------------------------------------------- +bool XFadeSample(MODSAMPLE &smp, SmpLength iFadeLength, CSoundFile *pSndFile) +//--------------------------------------------------------------------------- { - if(pSmp->pSample == nullptr) return false; - if(pSmp->nLoopEnd <= pSmp->nLoopStart || pSmp->nLoopEnd > pSmp->nLength) return false; - if(pSmp->nLoopStart < iFadeLength) return false; + if(smp.pSample == nullptr) return false; + if(smp.nLoopEnd <= smp.nLoopStart || smp.nLoopEnd > smp.nLength) return false; + if(smp.nLoopStart < iFadeLength) return false; - SmpLength iStart = pSmp->nLoopStart - iFadeLength; - SmpLength iEnd = pSmp->nLoopEnd - iFadeLength; - iStart *= pSmp->GetNumChannels(); - iEnd *= pSmp->GetNumChannels(); - iFadeLength *= pSmp->GetNumChannels(); + SmpLength iStart = smp.nLoopStart - iFadeLength; + SmpLength iEnd = smp.nLoopEnd - iFadeLength; + iStart *= smp.GetNumChannels(); + iEnd *= smp.GetNumChannels(); + iFadeLength *= smp.GetNumChannels(); - if(pSmp->GetElementarySampleSize() == 2) - XFadeSampleImpl(reinterpret_cast<int16*>(pSmp->pSample) + iStart, iEnd - iStart, iFadeLength); - else if(pSmp->GetElementarySampleSize() == 1) - XFadeSampleImpl(reinterpret_cast<int8*>(pSmp->pSample) + iStart, iEnd - iStart, iFadeLength); + if(smp.GetElementarySampleSize() == 2) + XFadeSampleImpl(reinterpret_cast<int16*>(smp.pSample) + iStart, iEnd - iStart, iFadeLength); + else if(smp.GetElementarySampleSize() == 1) + XFadeSampleImpl(reinterpret_cast<int8*>(smp.pSample) + iStart, iEnd - iStart, iFadeLength); else return false; - AdjustEndOfSample(*pSmp, pSndFile); + AdjustEndOfSample(smp, pSndFile); return true; } @@ -545,30 +541,30 @@ // Convert a multichannel sample to mono (currently only implemented for stereo) -bool ConvertToMono(MODSAMPLE *pSmp, CSoundFile *pSndFile) -//------------------------------------------------------- +bool ConvertToMono(MODSAMPLE &smp, CSoundFile *pSndFile) +//------------------------------------------------------ { - if(pSmp->pSample == nullptr || pSmp->nLength == 0 || pSmp->GetNumChannels() != 2) return false; + if(smp.pSample == nullptr || smp.nLength == 0 || smp.GetNumChannels() != 2) return false; // Note: Sample is overwritten in-place! Unused data is not deallocated! - if(pSmp->GetElementarySampleSize() == 2) - ConvertStereoToMonoImpl(reinterpret_cast<int16*>(pSmp->pSample), pSmp->nLength); - else if(pSmp->GetElementarySampleSize() == 1) - ConvertStereoToMonoImpl(reinterpret_cast<int8*>(pSmp->pSample), pSmp->nLength); + if(smp.GetElementarySampleSize() == 2) + ConvertStereoToMonoImpl(reinterpret_cast<int16*>(smp.pSample), smp.nLength); + else if(smp.GetElementarySampleSize() == 1) + ConvertStereoToMonoImpl(reinterpret_cast<int8*>(smp.pSample), smp.nLength); else return false; CriticalSection cs; - pSmp->uFlags &= ~(CHN_STEREO); + smp.uFlags &= ~(CHN_STEREO); for (CHANNELINDEX i = 0; i < MAX_CHANNELS; i++) { - if(pSndFile->Chn[i].pSample == pSmp->pSample) + if(pSndFile->Chn[i].pSample == smp.pSample) { pSndFile->Chn[i].dwFlags &= ~CHN_STEREO; } } - AdjustEndOfSample(*pSmp, pSndFile); + AdjustEndOfSample(smp, pSndFile); return true; } Modified: trunk/OpenMPT/soundlib/modsmp_ctrl.h =================================================================== --- trunk/OpenMPT/soundlib/modsmp_ctrl.h 2011-09-03 00:55:03 UTC (rev 1009) +++ trunk/OpenMPT/soundlib/modsmp_ctrl.h 2011-09-03 01:34:39 UTC (rev 1010) @@ -22,30 +22,30 @@ // Insert silence to given location. // Note: Is currently implemented only for inserting silence to the beginning and to the end of the sample. // Return: Length of the new sample. -SmpLength InsertSilence(MODSAMPLE& smp, const SmpLength nSilenceLength, const SmpLength nStartFrom, CSoundFile* pSndFile); +SmpLength InsertSilence(MODSAMPLE &smp, const SmpLength nSilenceLength, const SmpLength nStartFrom, CSoundFile* pSndFile); // Change sample size. // Note: If resized sample is bigger, silence will be added to the sample's tail. // Return: Length of the new sample. -SmpLength ResizeSample(MODSAMPLE& smp, const SmpLength nNewLength, CSoundFile* pSndFile); +SmpLength ResizeSample(MODSAMPLE &smp, const SmpLength nNewLength, CSoundFile* pSndFile); // Replaces sample in 'smp' with given sample and frees the old sample. // If valid CSoundFile pointer is given, the sample will be replaced also from the sounds channels. -void ReplaceSample(MODSAMPLE& smp, const LPSTR pNewSample, const SmpLength nNewLength, CSoundFile* pSndFile); +void ReplaceSample(MODSAMPLE &smp, const LPSTR pNewSample, const SmpLength nNewLength, CSoundFile* pSndFile); -bool AdjustEndOfSample(MODSAMPLE& smp, CSoundFile* pSndFile = 0); +bool AdjustEndOfSample(MODSAMPLE &smp, CSoundFile* pSndFile = 0); // Returns the number of bytes allocated(at least) for sample data. // Note: Currently the return value is based on the sample length and the actual // allocation may be more than what this function returns. -inline SmpLength GetSampleCapacity(MODSAMPLE& smp) {return smp.GetSampleSizeInBytes();} +inline SmpLength GetSampleCapacity(MODSAMPLE &smp) {return smp.GetSampleSizeInBytes();} // Resets samples. -void ResetSamples(CSoundFile& rSndFile, ResetFlag resetflag); +void ResetSamples(CSoundFile &rSndFile, ResetFlag resetflag); // Remove DC offset and normalize. // Return: If DC offset was removed, returns original offset value, zero otherwise. -float RemoveDCOffset(MODSAMPLE& smp, +float RemoveDCOffset(MODSAMPLE &smp, SmpLength iStart, // Start position (for partial DC offset removal). SmpLength iEnd, // End position (for partial DC offset removal). const MODTYPE modtype, // Used to determine whether to adjust global or default volume @@ -54,19 +54,19 @@ CSoundFile* const pSndFile); // Passed to AdjustEndOfSample. // Reverse sample data -bool ReverseSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); +bool ReverseSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); // Virtually unsign sample data -bool UnsignSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); +bool UnsignSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); // Invert sample data (flip by 180 degrees) -bool InvertSample(MODSAMPLE *pSmp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); +bool InvertSample(MODSAMPLE &smp, SmpLength iStart, SmpLength iEnd, CSoundFile *pSndFile); // Crossfade sample data to create smooth loops -bool XFadeSample(MODSAMPLE *pSmp, SmpLength iFadeLength, CSoundFile *pSndFile); +bool XFadeSample(MODSAMPLE &smp, SmpLength iFadeLength, CSoundFile *pSndFile); // Convert a sample with any number of channels to mono -bool ConvertToMono(MODSAMPLE *pSmp, CSoundFile *pSndFile); +bool ConvertToMono(MODSAMPLE &smp, CSoundFile *pSndFile); } // Namespace ctrlSmp This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |