From: <sag...@us...> - 2013-08-12 23:31:56
|
Revision: 2602 http://sourceforge.net/p/modplug/code/2602 Author: saga-games Date: 2013-08-12 23:31:49 +0000 (Mon, 12 Aug 2013) Log Message: ----------- [Ref] Unify usage of CMappedFile and move class implementation to its own files. Modified Paths: -------------- trunk/OpenMPT/mptrack/Ctrl_ins.cpp trunk/OpenMPT/mptrack/Ctrl_smp.cpp trunk/OpenMPT/mptrack/MainFrm.cpp trunk/OpenMPT/mptrack/Moddoc.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/Mptrack.h trunk/OpenMPT/mptrack/View_tre.cpp trunk/OpenMPT/mptrack/Vstplug.cpp trunk/OpenMPT/mptrack/mptrack_08.vcproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters trunk/OpenMPT/soundlib/Dlsbank.cpp trunk/OpenMPT/soundlib/Load_itp.cpp trunk/OpenMPT/soundlib/SampleFormats.cpp trunk/OpenMPT/soundlib/Sndfile.h Added Paths: ----------- trunk/OpenMPT/mptrack/MemoryMappedFile.cpp trunk/OpenMPT/mptrack/MemoryMappedFile.h Modified: trunk/OpenMPT/mptrack/Ctrl_ins.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -22,6 +22,7 @@ #include "../common/misc_util.h" #include "../common/StringFixer.h" #include "SelectPluginDialog.h" +#include "MemoryMappedFile.h" #pragma warning(disable:4244) //conversion from 'type1' to 'type2', possible loss of data @@ -1395,8 +1396,6 @@ { CMappedFile f; BOOL bFirst, bOk; - DWORD len; - LPBYTE lpFile; BeginWaitCursor(); if ((!lpszFileName) || (!f.Open(lpszFileName))) @@ -1405,11 +1404,11 @@ return FALSE; } bFirst = FALSE; - len = f.GetLength(); - if (len > CTrackApp::gMemStatus.dwTotalPhys) len = CTrackApp::gMemStatus.dwTotalPhys; - lpFile = f.Lock(len); + + FileReader file = f.GetFile(); + bOk = FALSE; - if (lpFile) + if (file.IsValid()) { if (!m_sndFile.GetNumInstruments()) { @@ -1419,7 +1418,7 @@ m_modDoc.SetModified(); } if (!m_nInstrument) m_nInstrument = 1; - if (m_sndFile.ReadInstrumentFromFile(m_nInstrument, lpFile, len, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad)) + if (m_sndFile.ReadInstrumentFromFile(m_nInstrument, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad)) { m_modDoc.UpdateAllViews(NULL, HINT_SAMPLEINFO | HINT_MODTYPE, NULL); // -> CODE#0023 Modified: trunk/OpenMPT/mptrack/Ctrl_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -27,6 +27,7 @@ #include "modsmp_ctrl.h" #include "Autotune.h" #include "../common/StringFixer.h" +#include "MemoryMappedFile.h" #include "../soundlib/FileReader.h" #include <Shlwapi.h> @@ -743,13 +744,12 @@ EndWaitCursor(); return false; } - const size_t len = f.GetLength(); - const void *lpFile = f.Lock(len); - if (!lpFile) goto OpenError; + + FileReader file = f.GetFile(); + if(file.IsValid()) goto OpenError; { m_modDoc.GetSampleUndo().PrepareUndo(m_nSample, sundo_replace); - FileReader file(lpFile, len); bOk = m_sndFile.ReadSampleFromFile(m_nSample, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); } @@ -771,7 +771,7 @@ ModSample &sample = m_sndFile.GetSample(m_nSample); m_sndFile.DestroySampleThreadsafe(m_nSample); - sample.nLength = len; + sample.nLength = file.GetLength(); SampleIO sampleIO = dlg.GetSampleFormat(); @@ -791,8 +791,8 @@ sample.nLength /= 2; } - FileReader chunk(lpFile, len); - if(sampleIO.ReadSample(sample, chunk)) + file.Rewind(); + if(sampleIO.ReadSample(sample, file)) { bOk = true; Modified: trunk/OpenMPT/mptrack/MainFrm.cpp =================================================================== --- trunk/OpenMPT/mptrack/MainFrm.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/MainFrm.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -36,6 +36,7 @@ #include "SelectPluginDialog.h" #include "ExceptionHandler.h" #include "PatternClipboard.h" +#include "MemoryMappedFile.h" #include "soundlib/FileReader.h" #include "../common/Profiler.h" @@ -1617,26 +1618,21 @@ if(f.Open(lpszFileName)) { - DWORD dwLen = f.GetLength(); - if(dwLen) + FileReader file = f.GetFile(); + if(file.IsValid()) { - LPBYTE p = f.Lock(); - if(p) + InitPreview(); + m_WaveFile.m_SongFlags.set(SONG_PAUSED); + // Avoid hanging audio while reading file - we have removed all sample and instrument references before, + // so it's safe to replace the sample / instrument now. + cs.Leave(); + ok = m_WaveFile.ReadInstrumentFromFile(1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); + cs.Enter(); + if(!ok) { - InitPreview(); - m_WaveFile.m_SongFlags.set(SONG_PAUSED); - // Avoid hanging audio while reading file - we have removed all sample and instrument references before, - // so it's safe to replace the sample / instrument now. - cs.Leave(); - ok = m_WaveFile.ReadInstrumentFromFile(1, p, dwLen, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); - cs.Enter(); - if(!ok) - { - // Try reading as sample if reading as instrument fails - FileReader file(p, dwLen); - ok = m_WaveFile.ReadSampleFromFile(1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); - m_WaveFile.AllocateInstrument(1, 1); - } + // Try reading as sample if reading as instrument fails + ok = m_WaveFile.ReadSampleFromFile(1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); + m_WaveFile.AllocateInstrument(1, 1); } } } Added: trunk/OpenMPT/mptrack/MemoryMappedFile.cpp =================================================================== --- trunk/OpenMPT/mptrack/MemoryMappedFile.cpp (rev 0) +++ trunk/OpenMPT/mptrack/MemoryMappedFile.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -0,0 +1,109 @@ +/* + * MemoryMappedFile.cpp + * -------------------- + * Purpose: Wrapper class for memory-mapped files + * Notes : (currently none) + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#include "stdafx.h" +#include "MemoryMappedFile.h" + + +CMappedFile::~CMappedFile() +//------------------------- +{ + Close(); +} + + +bool CMappedFile::Open(LPCSTR lpszFileName) +//----------------------------------------- +{ + return m_File.Open(lpszFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite) != FALSE; +} + + +void CMappedFile::Close() +//----------------------- +{ + if(m_pData) Unlock(); + m_File.Close(); +} + + +size_t CMappedFile::GetLength() +//----------------------------- +{ + return mpt::saturate_cast<size_t>(m_File.GetLength()); +} + + +const void *CMappedFile::Lock() +//----------------------------- +{ + size_t length = GetLength(); + if(!length) return nullptr; + + void *lpStream; + + HANDLE hmf = CreateFileMapping( + m_File.m_hFile, + NULL, + PAGE_READONLY, + 0, 0, + NULL); + + // Try memory-mapping first + if(hmf) + { + lpStream = MapViewOfFile( + hmf, + FILE_MAP_READ, + 0, 0, + 0); + if(lpStream) + { + m_hFMap = hmf; + m_pData = lpStream; + return lpStream; + } + CloseHandle(hmf); + hmf = nullptr; + } + + // Fallback if memory-mapping fails for some weird reason + if((lpStream = malloc(length)) == nullptr) return nullptr; + m_File.Read(lpStream, length); + m_pData = lpStream; + return lpStream; +} + + +FileReader CMappedFile::GetFile() +//------------------------------- +{ + return FileReader(Lock(), GetLength()); +} + + +void CMappedFile::Unlock() +//------------------------ +{ + if(m_hFMap) + { + if(m_pData) + { + UnmapViewOfFile(m_pData); + m_pData = nullptr; + } + CloseHandle(m_hFMap); + m_hFMap = nullptr; + } else if(m_pData) + { + free(m_pData); + m_pData = nullptr; + } +} \ No newline at end of file Added: trunk/OpenMPT/mptrack/MemoryMappedFile.h =================================================================== --- trunk/OpenMPT/mptrack/MemoryMappedFile.h (rev 0) +++ trunk/OpenMPT/mptrack/MemoryMappedFile.h 2013-08-12 23:31:49 UTC (rev 2602) @@ -0,0 +1,38 @@ +/* + * MemoryMappedFile.h + * ------------------ + * Purpose: Header file for memory-mapped file wrapper + * Notes : (currently none) + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#pragma once + +#include "../soundlib/FileReader.h" + +////////////////////////////////////////////////////////////////// +// File Mapping Class + +//=============== +class CMappedFile +//=============== +{ +protected: + CFile m_File; + HANDLE m_hFMap; + void *m_pData; + +public: + CMappedFile() : m_hFMap(nullptr), m_pData(nullptr) { } + ~CMappedFile(); + +public: + bool Open(LPCSTR lpszFileName); + void Close(); + size_t GetLength(); + const void *Lock(); + FileReader GetFile(); + void Unlock(); +}; Modified: trunk/OpenMPT/mptrack/Moddoc.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moddoc.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Moddoc.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -24,8 +24,12 @@ #include "modsmp_ctrl.h" #include "CleanupSong.h" #include "../common/StringFixer.h" +#ifdef NO_FILEREADER_STD_ISTREAM +#include "MemoryMappedFile.h" +#else +#include <fstream> +#endif #include "soundlib/FileReader.h" -#include <fstream> #include <shlwapi.h> #ifdef _DEBUG @@ -215,15 +219,10 @@ CMappedFile f; if (f.Open(lpszPathName)) { - DWORD dwLen = f.GetLength(); - if (dwLen) + FileReader file = f.GetFile(); + if(file.IsValid()) { - LPBYTE lpStream = f.Lock(); - if (lpStream) - { - m_SndFile.Create(FileReader(lpStream, dwLen), CSoundFile::loadCompleteModule, this); - f.Unlock(); - } + m_SndFile.Create(file, CSoundFile::loadCompleteModule, this); } } } @@ -333,16 +332,14 @@ { // Load from Instrument or Sample file CHAR szName[_MAX_FNAME], szExt[_MAX_EXT]; - CFile f; + CMappedFile f; - if (f.Open(pszMidiMapName, CFile::modeRead)) + if(f.Open(pszMidiMapName)) { - DWORD len = static_cast<DWORD>(f.GetLength()); - LPBYTE lpFile; - if ((len) && ((lpFile = (LPBYTE)GlobalAllocPtr(GHND, len)) != NULL)) + FileReader file = f.GetFile(); + if(file.IsValid()) { - f.Read(lpFile, len); - m_SndFile.ReadInstrumentFromFile(nIns, lpFile, len, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); + m_SndFile.ReadInstrumentFromFile(nIns, file, false); _splitpath(pszMidiMapName, NULL, NULL, szName, szExt); strncat(szName, szExt, sizeof(szName)); pIns = m_SndFile.Instruments[nIns]; @@ -360,7 +357,6 @@ } } } - f.Close(); } } } Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -1948,108 +1948,6 @@ } -///////////////////////////////////////////////////////////////////////////////////// -// CMappedFile - -CMappedFile::CMappedFile() -//------------------------ -{ - m_hFMap = NULL; - m_lpData = NULL; -} - - -CMappedFile::~CMappedFile() -//------------------------- -{ - Close(); -} - - -BOOL CMappedFile::Open(LPCSTR lpszFileName) -//----------------------------------------- -{ - return m_File.Open(lpszFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite); -} - - -void CMappedFile::Close() -//----------------------- -{ - if (m_lpData) Unlock(); - m_File.Close(); -} - - -DWORD CMappedFile::GetLength() -//---------------------------- -{ - return static_cast<DWORD>(m_File.GetLength()); -} - - -LPBYTE CMappedFile::Lock(DWORD dwMaxLen) -//-------------------------------------- -{ - DWORD dwLen = GetLength(); - LPBYTE lpStream; - - if (!dwLen) return NULL; - if ((dwMaxLen) && (dwLen > dwMaxLen)) dwLen = dwMaxLen; - HANDLE hmf = CreateFileMapping( - (HANDLE)m_File.m_hFile, - NULL, - PAGE_READONLY, - 0, 0, - NULL - ); - if (hmf) - { - lpStream = (LPBYTE)MapViewOfFile( - hmf, - FILE_MAP_READ, - 0, 0, - 0 - ); - if (lpStream) - { - m_hFMap = hmf; - m_lpData = lpStream; - return lpStream; - } - CloseHandle(hmf); - } - // Fallback - if (dwLen > CTrackApp::gMemStatus.dwTotalPhys) return NULL; - if ((lpStream = (LPBYTE)GlobalAllocPtr(GHND, dwLen)) == NULL) return NULL; - m_File.Read(lpStream, dwLen); - m_lpData = lpStream; - return lpStream; -} - - -BOOL CMappedFile::Unlock() -//------------------------ -{ - if (m_hFMap) - { - if (m_lpData) - { - UnmapViewOfFile(m_lpData); - m_lpData = NULL; - } - CloseHandle(m_hFMap); - m_hFMap = NULL; - } - if (m_lpData) - { - GlobalFreePtr(m_lpData); - m_lpData = NULL; - } - return TRUE; -} - - /////////////////////////////////////////////////////////////////////////////////// // // DirectX Plugins Modified: trunk/OpenMPT/mptrack/Mptrack.h =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.h 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Mptrack.h 2013-08-12 23:31:49 UTC (rev 2602) @@ -239,31 +239,6 @@ ////////////////////////////////////////////////////////////////// -// File Mapping Class - -//=============== -class CMappedFile -//=============== -{ -protected: - CFile m_File; - HANDLE m_hFMap; - LPVOID m_lpData; - -public: - CMappedFile(); - virtual ~CMappedFile(); - -public: - BOOL Open(LPCSTR lpszFileName); - void Close(); - DWORD GetLength(); - LPBYTE Lock(DWORD dwMaxLen=0); - BOOL Unlock(); -}; - - -////////////////////////////////////////////////////////////////// // More Bitmap Helpers //#define FASTBMP_XSHIFT 12 // 4K pixels Modified: trunk/OpenMPT/mptrack/View_tre.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_tre.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/View_tre.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -17,6 +17,7 @@ #include "Dlsbank.h" #include "dlg_misc.h" #include "vstplug.h" +#include "MemoryMappedFile.h" #include "../soundlib/FileReader.h" @@ -267,28 +268,24 @@ SetCurrentDirectory(m_szInstrLibPath); if (f.Open(pszSongName)) { - DWORD dwLen = f.GetLength(); - if (dwLen) + FileReader file = f.GetFile(); + if (file.IsValid()) { - LPBYTE lpStream = f.Lock(); - if (lpStream) + if(m_SongFile != nullptr) { - if(m_SongFile != nullptr) - { - m_SongFile->Destroy(); - } else - { - m_SongFile = new (std::nothrow) CSoundFile; - } - if(m_SongFile != nullptr) - { - m_SongFile->Create(FileReader(lpStream, dwLen), CSoundFile::loadNoPatternData, nullptr); - // Destroy some stuff that we're not going to use anyway. - m_SongFile->Patterns.DestroyPatterns(); - m_SongFile->songMessage.clear(); - } - f.Unlock(); + m_SongFile->Destroy(); + } else + { + m_SongFile = new (std::nothrow) CSoundFile; } + if(m_SongFile != nullptr) + { + m_SongFile->Create(file, CSoundFile::loadNoPatternData, nullptr); + // Destroy some stuff that we're not going to use anyway. + m_SongFile->Patterns.DestroyPatterns(); + m_SongFile->songMessage.clear(); + } + f.Unlock(); } } } Modified: trunk/OpenMPT/mptrack/Vstplug.cpp =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/Vstplug.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -25,6 +25,7 @@ #include "../common/version.h" #include "midimappingdialog.h" #include "../common/StringFixer.h" +#include "MemoryMappedFile.h" #include "../soundlib/FileReader.h" #include "../soundlib/plugins/JBridge.h" #include <fstream> @@ -1678,9 +1679,7 @@ const char *errorStr = nullptr; if(f.Open(files.first_file.c_str())) { - size_t len = f.GetLength(); - FileReader file(f.Lock(len), len); - + FileReader file = f.GetFile(); errorStr = VSTPresets::GetErrorMessage(VSTPresets::LoadFile(file, *this)); } else { Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-08-12 23:31:49 UTC (rev 2602) @@ -367,6 +367,10 @@ > </File> <File + RelativePath=".\MemoryMappedFile.cpp" + > + </File> + <File RelativePath="..\soundlib\Message.cpp" > </File> @@ -945,6 +949,10 @@ > </File> <File + RelativePath=".\MemoryMappedFile.h" + > + </File> + <File RelativePath="..\soundlib\MIDIEvents.h" > </File> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-08-12 23:31:49 UTC (rev 2602) @@ -379,6 +379,7 @@ <ClCompile Include="KeyConfigDlg.cpp" /> <ClCompile Include="Mainbar.cpp" /> <ClCompile Include="MainFrm.cpp" /> + <ClCompile Include="MemoryMappedFile.cpp" /> <ClCompile Include="MIDIMacroDialog.cpp" /> <ClCompile Include="MIDIMapping.cpp" /> <ClCompile Include="MIDIMappingDialog.cpp" /> @@ -554,6 +555,7 @@ <ClInclude Include="InputHandler.h" /> <ClInclude Include="Mainbar.h" /> <ClInclude Include="Mainfrm.h" /> + <ClInclude Include="MemoryMappedFile.h" /> <ClInclude Include="MIDIMacroDialog.h" /> <ClInclude Include="MIDIMapping.h" /> <ClInclude Include="mod2midi.h" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-08-12 23:31:49 UTC (rev 2602) @@ -481,6 +481,9 @@ <ClCompile Include="..\soundlib\MixerLoops.cpp"> <Filter>Source Files\soundlib</Filter> </ClCompile> + <ClCompile Include="MemoryMappedFile.cpp"> + <Filter>Source Files\mptrack</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\soundlib\Loaders.h"> @@ -897,6 +900,9 @@ <ClInclude Include="..\soundlib\SampleFormat.h"> <Filter>Header Files\soundlib</Filter> </ClInclude> + <ClInclude Include="MemoryMappedFile.h"> + <Filter>Header Files\mptrack</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <None Include="res\bitmap1.bmp"> Modified: trunk/OpenMPT/soundlib/Dlsbank.cpp =================================================================== --- trunk/OpenMPT/soundlib/Dlsbank.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/soundlib/Dlsbank.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -13,6 +13,7 @@ #include "Sndfile.h" #ifdef MODPLUG_TRACKER #include "../mptrack/mptrack.h" +#include "../mptrack/MemoryMappedFile.h" #endif #include "Dlsbank.h" #include "Wav.h" @@ -25,10 +26,7 @@ //#define DLSBANK_LOG //#define DLSINSTR_LOG -//#define ASM_DLSUNITCONVERSION -#ifndef ASM_DLSUNITCONVERSION #include <math.h> -#endif #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 @@ -1156,7 +1154,7 @@ CMappedFile MapFile; if (!MapFile.Open(lpszFileName)) return FALSE; dwMemLength = MapFile.GetLength(); - if (dwMemLength >= 256) lpMemFile = MapFile.Lock(); + if (dwMemLength >= 256) lpMemFile = (const BYTE *)MapFile.Lock(); if (!lpMemFile) { return FALSE; Modified: trunk/OpenMPT/soundlib/Load_itp.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_itp.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/soundlib/Load_itp.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -17,6 +17,7 @@ #ifdef MODPLUG_TRACKER #include "../mptrack/mptrack.h" #include "../mptrack/TrackerSettings.h" +#include "../mptrack/MemoryMappedFile.h" #endif #include "../common/version.h" #include "Loaders.h" @@ -220,16 +221,13 @@ // Load instruments CMappedFile f; - for(INSTRUMENTINDEX ins = 0; ins < GetNumInstruments(); ins++) { if(m_szInstrumentPath[ins].empty() || !f.Open(m_szInstrumentPath[ins].c_str())) continue; - size = f.GetLength(); - LPBYTE lpFile = f.Lock(size); - if(!lpFile) { f.Close(); continue; } - - ReadInstrumentFromFile(ins + 1, lpFile, size, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); + FileReader file = f.GetFile(); + if(file.IsValid()) + ReadInstrumentFromFile(ins + 1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); f.Close(); } Modified: trunk/OpenMPT/soundlib/SampleFormats.cpp =================================================================== --- trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-08-12 23:31:49 UTC (rev 2602) @@ -88,16 +88,16 @@ } -bool CSoundFile::ReadInstrumentFromFile(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength, bool mayNormalize) -//---------------------------------------------------------------------------------------------------------------------------- +bool CSoundFile::ReadInstrumentFromFile(INSTRUMENTINDEX nInstr, FileReader &file, bool mayNormalize) +//-------------------------------------------------------------------------------------------------- { - FileReader file(lpMemFile, dwFileLength); if ((!nInstr) || (nInstr >= MAX_INSTRUMENTS)) return false; - if ((!ReadXIInstrument(nInstr, file)) - && (!ReadPATInstrument(nInstr, lpMemFile, dwFileLength)) - && (!ReadITIInstrument(nInstr, file)) - // Generic read - && (!ReadSampleAsInstrument(nInstr, file, mayNormalize))) return false; + file.Rewind(); + if(!ReadPATInstrument(nInstr, (const LPBYTE)file.GetRawData(), file.GetLength()) + && !ReadXIInstrument(nInstr, file) + && !ReadITIInstrument(nInstr, file) + // Generic read + && !ReadSampleAsInstrument(nInstr, file, mayNormalize)) return false; if(nInstr > GetNumInstruments()) m_nInstruments = nInstr; return true; @@ -711,8 +711,8 @@ // PAT Instrument -bool CSoundFile::ReadPATInstrument(INSTRUMENTINDEX nInstr, LPBYTE lpStream, DWORD dwMemLength) -//-------------------------------------------------------------------------------------------- +bool CSoundFile::ReadPATInstrument(INSTRUMENTINDEX nInstr, const LPBYTE lpStream, DWORD dwMemLength) +//-------------------------------------------------------------------------------------------------- { GF1PATCHFILEHEADER *phdr = (GF1PATCHFILEHEADER *)lpStream; GF1INSTRUMENT *pih = (GF1INSTRUMENT *)(lpStream+sizeof(GF1PATCHFILEHEADER)); Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2013-08-11 21:27:57 UTC (rev 2601) +++ trunk/OpenMPT/soundlib/Sndfile.h 2013-08-12 23:31:49 UTC (rev 2602) @@ -740,7 +740,7 @@ #endif // Instrument file I/O - bool ReadInstrumentFromFile(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength, bool mayNormalize=false); + bool ReadInstrumentFromFile(INSTRUMENTINDEX nInstr, FileReader &file, bool mayNormalize=false); bool ReadXIInstrument(INSTRUMENTINDEX nInstr, FileReader &file); bool ReadITIInstrument(INSTRUMENTINDEX nInstr, FileReader &file); bool ReadPATInstrument(INSTRUMENTINDEX nInstr, const LPBYTE lpMemFile, DWORD dwFileLength); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |