Improved sndfile loader implementation

Former-commit-id: d198c2195b50f1f8f1adce1f81e1c06542ada757
This commit is contained in:
Lynix 2014-04-20 13:21:35 +02:00
parent fc970c503a
commit 60cd7c8375
1 changed files with 11 additions and 11 deletions

View File

@ -71,7 +71,6 @@ namespace
{ {
public: public:
sndfileStream() : sndfileStream() :
m_file(nullptr),
m_handle(nullptr) m_handle(nullptr)
{ {
} }
@ -80,9 +79,6 @@ namespace
{ {
if (m_handle) if (m_handle)
sf_close(m_handle); sf_close(m_handle);
if (m_file)
delete m_file;
} }
nzUInt32 GetDuration() const nzUInt32 GetDuration() const
@ -110,14 +106,13 @@ namespace
bool Open(const NzString& filePath, bool forceMono) bool Open(const NzString& filePath, bool forceMono)
{ {
m_file = new NzFile(filePath); if (!m_file.Open(filePath, NzFile::ReadOnly))
if (!m_file->Open(NzFile::ReadOnly))
{ {
NazaraError("Failed to open file " + filePath); NazaraError("Failed to open stream from file: " + NzError::GetLastError());
return false; return false;
} }
return Open(*m_file, forceMono); return Open(m_file, forceMono);
} }
bool Open(NzInputStream& stream, bool forceMono) bool Open(NzInputStream& stream, bool forceMono)
@ -132,12 +127,15 @@ namespace
return false; return false;
} }
m_format = NzAudio::GetAudioFormat(infos.channels); NzCallOnExit onExit([this]
if (m_format == nzAudioFormat_Unknown)
{ {
sf_close(m_handle); sf_close(m_handle);
m_handle = nullptr; m_handle = nullptr;
});
m_format = NzAudio::GetAudioFormat(infos.channels);
if (m_format == nzAudioFormat_Unknown)
{
NazaraError("Channel count not handled"); NazaraError("Channel count not handled");
return false; return false;
} }
@ -161,6 +159,8 @@ namespace
else else
m_mixToMono = false; m_mixToMono = false;
onExit.Reset();
return true; return true;
} }
@ -186,7 +186,7 @@ namespace
private: private:
std::vector<nzInt16> m_mixBuffer; std::vector<nzInt16> m_mixBuffer;
nzAudioFormat m_format; nzAudioFormat m_format;
NzFile* m_file; NzFile m_file;
SNDFILE* m_handle; SNDFILE* m_handle;
bool m_mixToMono; bool m_mixToMono;
unsigned int m_duration; unsigned int m_duration;