diff --git a/src/Nazara/Audio/Loaders/sndfile/Loader.cpp b/src/Nazara/Audio/Loaders/sndfile/Loader.cpp index a2c36edfc..5f5e40a67 100644 --- a/src/Nazara/Audio/Loaders/sndfile/Loader.cpp +++ b/src/Nazara/Audio/Loaders/sndfile/Loader.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include #include @@ -166,14 +168,14 @@ namespace { if (m_mixToMono) { - std::unique_ptr samples(new nzInt16[m_format*sampleCount]); - unsigned int readSampleCount = sf_read_short(m_handle, samples.get(), m_format*sampleCount); - NzMixToMono(samples.get(), reinterpret_cast(buffer), m_format, sampleCount); + m_mixBuffer.resize(m_format*sampleCount); + unsigned int readSampleCount = sf_read_short(m_handle, m_mixBuffer.data(), m_format*sampleCount); + NzMixToMono(m_mixBuffer.data(), static_cast(buffer), m_format, sampleCount); return readSampleCount / m_format; } else - return sf_read_short(m_handle, reinterpret_cast(buffer), sampleCount); + return sf_read_short(m_handle, static_cast(buffer), sampleCount); } void Seek(nzUInt32 offset) @@ -182,6 +184,7 @@ namespace } private: + std::vector m_mixBuffer; nzAudioFormat m_format; NzFile* m_file; SNDFILE* m_handle; @@ -293,12 +296,12 @@ namespace return false; } + NzCallOnExit onExit([file] { sf_close(file); }); + nzAudioFormat format = NzAudio::GetAudioFormat(info.channels); if (format == nzAudioFormat_Unknown) { NazaraError("Channel count not handled"); - sf_close(file); - return false; } @@ -313,8 +316,6 @@ namespace if (sf_read_short(file, samples.get(), sampleCount) != sampleCount) { - sf_close(file); - NazaraError("Failed to read samples"); return false; } @@ -331,8 +332,6 @@ namespace if (!soundBuffer->Create(format, static_cast(sampleCount), info.samplerate, samples.get())) { - sf_close(file); - NazaraError("Failed to create sound buffer"); return false; }