From e16e3658f13c4c6b8beeef99b6749aedc2ba188a Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 20 Apr 2014 13:12:02 +0200 Subject: [PATCH] Improved sndfile Loader When streaming and mixing to mono, the samples buffer is kept between the calls Made SoundBuffer loader exception-safe Fixed sf_close not being called Former-commit-id: cef1d114809dc1fde9a84d2d96dd7afc7550c32e --- src/Nazara/Audio/Loaders/sndfile/Loader.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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; }