From: <man...@us...> - 2013-06-30 17:48:09
|
Revision: 2450 http://sourceforge.net/p/modplug/code/2450 Author: manxorist Date: 2013-06-30 17:47:59 +0000 (Sun, 30 Jun 2013) Log Message: ----------- [Fix] Make ReadSampleAsInstrument() work on big-endian. [Ref] Convert ReadSampleAsInstrument() to use FileReader. Modified Paths: -------------- trunk/OpenMPT/mptrack/Ctrl_smp.cpp trunk/OpenMPT/mptrack/MainFrm.cpp trunk/OpenMPT/mptrack/View_smp.cpp trunk/OpenMPT/soundlib/Load_umx.cpp trunk/OpenMPT/soundlib/SampleFormats.cpp trunk/OpenMPT/soundlib/Sndfile.h Modified: trunk/OpenMPT/mptrack/Ctrl_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-06-30 17:47:59 UTC (rev 2450) @@ -753,7 +753,8 @@ { m_modDoc.GetSampleUndo().PrepareUndo(m_nSample, sundo_replace); - bOk = m_sndFile.ReadSampleFromFile(m_nSample, lpFile, len); + FileReader file(lpFile, len); + bOk = m_sndFile.ReadSampleFromFile(m_nSample, file); } if (!bOk) Modified: trunk/OpenMPT/mptrack/MainFrm.cpp =================================================================== --- trunk/OpenMPT/mptrack/MainFrm.cpp 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/mptrack/MainFrm.cpp 2013-06-30 17:47:59 UTC (rev 2450) @@ -1580,7 +1580,8 @@ if(!ok) { // Try reading as sample if reading as instrument fails - ok = m_WaveFile.ReadSampleFromFile(1, p, dwLen); + FileReader file(p, dwLen); + ok = m_WaveFile.ReadSampleFromFile(1, file); m_WaveFile.AllocateInstrument(1, 1); } } Modified: trunk/OpenMPT/mptrack/View_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_smp.cpp 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/mptrack/View_smp.cpp 2013-06-30 17:47:59 UTC (rev 2450) @@ -23,6 +23,7 @@ #include "SampleEditorDialogs.h" #include "modsmp_ctrl.h" #include "Wav.h" +#include "../soundlib/FileReader.h" #define new DEBUG_NEW @@ -2000,7 +2001,8 @@ memcpy(s, pSndFile->m_szNames[m_nSample], 32); memcpy(s2, sample.filename, 22); - pSndFile->ReadSampleFromFile(m_nSample, p, dwMemSize); + FileReader file(p, dwMemSize); + pSndFile->ReadSampleFromFile(m_nSample, file); if (!pSndFile->m_szNames[m_nSample][0]) { memcpy(pSndFile->m_szNames[m_nSample], s, 32); Modified: trunk/OpenMPT/soundlib/Load_umx.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_umx.cpp 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/soundlib/Load_umx.cpp 2013-06-30 17:47:59 UTC (rev 2450) @@ -291,7 +291,7 @@ } else if(isSound && GetNumSamples() < MAX_SAMPLES - 1) { // Read as sample - if(ReadSampleFromFile(GetNumSamples() + 1, (LPBYTE)fileChunk.GetRawData(), fileChunk.GetLength())) + if(ReadSampleFromFile(GetNumSamples() + 1, fileChunk)) { m_nSamples++; if(static_cast<size_t>(objName) < names.size()) Modified: trunk/OpenMPT/soundlib/SampleFormats.cpp =================================================================== --- trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-06-30 17:47:59 UTC (rev 2450) @@ -27,28 +27,52 @@ #ifndef NO_MP3_SAMPLES // Check for valid MPEG header -static bool IsMPEG(const void *data) -//---------------------------------- +static bool IsMPEG(const uint8 (&header)[3]) +//------------------------------------------ { - const uint8 *header = static_cast<const uint8 *>(data); return header[0] == 0xFF && (header[1] & 0xE0) == 0xE0 && (header[1] & 0x18) != 0x08 && (header[1] & 0x06) != 0x00 && (header[2] & 0xF0) != 0xF0; } +static bool IsMPEG(FileReader file) +//--------------------------------- +{ + uint8 header[3]; + if(!file.CanRead(3)) + return false; + file.ReadArrayLE(header); + file.SkipBack(3); + return IsMPEG(header); +} + +static bool IsID3(FileReader file) +//-------------------------------- +{ + char header[3]; + if(!file.CanRead(3)) + return false; + file.ReadArrayLE(header); + file.SkipBack(3); + return header[0] == 'I' && header[1] == 'D' && header[2] == '3'; +} #endif // NO_MP3_SAMPLES -bool CSoundFile::ReadSampleFromFile(SAMPLEINDEX nSample, const LPBYTE lpMemFile, DWORD dwFileLength) -//-------------------------------------------------------------------------------------------------- +bool CSoundFile::ReadSampleFromFile(SAMPLEINDEX nSample, FileReader &file) +//------------------------------------------------------------------------ { - FileReader file(lpMemFile, dwFileLength); + file.Rewind(); + + const BYTE * const lpMemFile = reinterpret_cast<const BYTE*>(file.GetRawData()); + const DWORD dwFileLength = file.GetLength(); + if(!nSample || nSample >= MAX_SAMPLES) return false; if(!ReadWAVSample(nSample, file) && !ReadXISample(nSample, file) && !ReadITISample(nSample, file) && !ReadAIFFSample(nSample, file) && !ReadITSSample(nSample, file) - && !ReadPATSample(nSample, lpMemFile, dwFileLength) - && !Read8SVXSample(nSample, lpMemFile, dwFileLength) - && !ReadS3ISample(nSample, lpMemFile, dwFileLength) + && !ReadPATSample(nSample, const_cast<BYTE*>(lpMemFile), dwFileLength) + && !Read8SVXSample(nSample, const_cast<BYTE*>(lpMemFile), dwFileLength) + && !ReadS3ISample(nSample, const_cast<BYTE*>(lpMemFile), dwFileLength) && !ReadFLACSample(nSample, file) && !ReadMP3Sample(nSample, file)) { @@ -72,18 +96,23 @@ && (!ReadPATInstrument(nInstr, lpMemFile, dwFileLength)) && (!ReadITIInstrument(nInstr, file)) // Generic read - && (!ReadSampleAsInstrument(nInstr, lpMemFile, dwFileLength))) return false; + && (!ReadSampleAsInstrument(nInstr, file))) return false; if(nInstr > GetNumInstruments()) m_nInstruments = nInstr; return true; } -bool CSoundFile::ReadSampleAsInstrument(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength) -//--------------------------------------------------------------------------------------------------------- +bool CSoundFile::ReadSampleAsInstrument(INSTRUMENTINDEX nInstr, FileReader &file) +//------------------------------------------------------------------------------- { - const uint32 *psig = reinterpret_cast<const uint32 *>(lpMemFile); - if((!lpMemFile) || (dwFileLength < 80)) return false; + file.Rewind(); + + if(!file.CanRead(80)) + return false; + uint32 psig[20]; + file.ReadArrayLE(psig); + file.SkipBack(80); if((psig[0] == LittleEndian(0x46464952) && psig[2] == LittleEndian(0x45564157)) // RIFF....WAVE signature || (psig[0] == LittleEndian(0x5453494C) && psig[2] == LittleEndian(0x65766177)) // LIST....wave || psig[76/4] == LittleEndian(0x53524353) // S3I signature @@ -95,8 +124,8 @@ || psig[0] == LittleEndian('CaLf') // FLAC signature #endif // NO_FLAC #ifndef NO_MP3_SAMPLES - || IsMPEG(lpMemFile) // MPEG signature - || (lpMemFile[0] == 'I' && lpMemFile[1] == 'D' && lpMemFile[2] == '3') // MP3 signature + || IsMPEG(file) // MPEG signature + || IsID3(file) // MP3 signature #endif // NO_MP3_SAMPLES ) { @@ -122,7 +151,7 @@ DestroyInstrument(nInstr, deleteAssociatedSamples); Instruments[nInstr] = pIns; - ReadSampleFromFile(nSample, lpMemFile, dwFileLength); + ReadSampleFromFile(nSample, file); return true; } return false; Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2013-06-30 17:39:01 UTC (rev 2449) +++ trunk/OpenMPT/soundlib/Sndfile.h 2013-06-30 17:47:59 UTC (rev 2450) @@ -783,7 +783,7 @@ static void AdjustSampleLoop(ModSample &sample); // Samples file I/O - bool ReadSampleFromFile(SAMPLEINDEX nSample, const LPBYTE lpMemFile, DWORD dwFileLength); + bool ReadSampleFromFile(SAMPLEINDEX nSample, FileReader &file); bool ReadWAVSample(SAMPLEINDEX nSample, FileReader &file, FileReader *wsmpChunk = nullptr); bool ReadPATSample(SAMPLEINDEX nSample, const LPBYTE lpMemFile, DWORD dwFileLength); bool ReadS3ISample(SAMPLEINDEX nSample, const LPBYTE lpMemFile, DWORD dwFileLength); @@ -805,7 +805,7 @@ bool ReadXIInstrument(INSTRUMENTINDEX nInstr, FileReader &file); bool ReadITIInstrument(INSTRUMENTINDEX nInstr, FileReader &file); bool ReadPATInstrument(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength); - bool ReadSampleAsInstrument(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength); + bool ReadSampleAsInstrument(INSTRUMENTINDEX nInstr, FileReader &file); #ifndef MODPLUG_NO_FILESAVE bool SaveXIInstrument(INSTRUMENTINDEX nInstr, const LPCSTR lpszFileName) const; bool SaveITIInstrument(INSTRUMENTINDEX nInstr, const LPCSTR lpszFileName, bool compress) const; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |