diff options
Diffstat (limited to 'src/sound/oalsound/buffer.cpp')
-rw-r--r-- | src/sound/oalsound/buffer.cpp | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/src/sound/oalsound/buffer.cpp b/src/sound/oalsound/buffer.cpp index b27029c..df31b82 100644 --- a/src/sound/oalsound/buffer.cpp +++ b/src/sound/oalsound/buffer.cpp @@ -1,23 +1,29 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsiteŃ.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ #include "sound/oalsound/buffer.h" -#include <cstring> +#include <memory> + +#include "common/resources/resourcemanager.h" + Buffer::Buffer() { @@ -42,19 +48,17 @@ bool Buffer::LoadFromFile(std::string filename, Sound sound) m_sound = sound; GetLogger()->Debug("Loading audio file: %s\n", filename.c_str()); - SF_INFO fileInfo; - memset(&fileInfo, 0, sizeof(SF_INFO)); - SNDFILE *file = sf_open(filename.c_str(), SFM_READ, &fileInfo); + std::unique_ptr<CSNDFile> file = std::unique_ptr<CSNDFile>(CResourceManager::GetSNDFileHandler(filename)); - GetLogger()->Trace(" channels %d\n", fileInfo.channels); - GetLogger()->Trace(" format %d\n", fileInfo.format); - GetLogger()->Trace(" frames %d\n", fileInfo.frames); - GetLogger()->Trace(" samplerate %d\n", fileInfo.samplerate); - GetLogger()->Trace(" sections %d\n", fileInfo.sections); + GetLogger()->Trace(" channels %d\n", file->GetFileInfo().channels); + GetLogger()->Trace(" format %d\n", file->GetFileInfo().format); + GetLogger()->Trace(" frames %d\n", file->GetFileInfo().frames); + GetLogger()->Trace(" samplerate %d\n", file->GetFileInfo().samplerate); + GetLogger()->Trace(" sections %d\n", file->GetFileInfo().sections); - if (!file) + if (!file->IsOpen()) { - GetLogger()->Warn("Could not load file. Reason: %s\n", sf_strerror(file)); + GetLogger()->Warn("Could not load file %s. Reason: %s\n", filename.c_str(), file->GetLastError().c_str()); m_loaded = false; return false; } @@ -64,23 +68,21 @@ bool Buffer::LoadFromFile(std::string filename, Sound sound) { GetLogger()->Warn("Could not create audio buffer\n"); m_loaded = false; - sf_close(file); return false; } // read chunks of 4096 samples std::vector<uint16_t> data; std::array<int16_t, 4096> buffer; - data.reserve(fileInfo.frames); + data.reserve(file->GetFileInfo().frames); size_t read = 0; - while ((read = sf_read_short(file, buffer.data(), buffer.size())) != 0) + while ((read = file->Read(buffer.data(), buffer.size())) != 0) { data.insert(data.end(), buffer.begin(), buffer.begin() + read); } - sf_close(file); - alBufferData(m_buffer, fileInfo.channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, &data.front(), data.size() * sizeof(uint16_t), fileInfo.samplerate); - m_duration = static_cast<float>(fileInfo.frames) / fileInfo.samplerate; + alBufferData(m_buffer, file->GetFileInfo().channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, &data.front(), data.size() * sizeof(uint16_t), file->GetFileInfo().samplerate); + m_duration = static_cast<float>(file->GetFileInfo().frames) / file->GetFileInfo().samplerate; m_loaded = true; return true; } |