| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
|
| 24 |
// Platform specific includes |
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
// 3rd party library includes |
| 28 |
|
| 29 |
// FIFE includes |
| 30 |
// These includes are split up in two parts, separated by one empty line |
| 31 |
// First block: files included from the FIFE root src directory |
| 32 |
// Second block: files included from the same folder |
| 33 |
#include "util/base/exception.h" |
| 34 |
#include "util/log/logger.h" |
| 35 |
#include "loaders/native/audio/ogg_loader.h" |
| 36 |
|
| 37 |
#include "soundclip.h" |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
static Logger _log(LM_AUDIO); |
| 41 |
|
| 42 |
SoundClip::SoundClip(IResourceLoader* loader) : |
| 43 |
IResource(createUniqueClipName(), loader), |
| 44 |
m_isStream(false), |
| 45 |
m_decoder(NULL), |
| 46 |
m_deleteDecoder(false) { |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
SoundClip::SoundClip(const std::string& name, IResourceLoader* loader) : |
| 51 |
IResource(name, loader), |
| 52 |
m_isStream(false), |
| 53 |
m_decoder(NULL), |
| 54 |
m_deleteDecoder(false) { |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
SoundClip::~SoundClip() { |
| 59 |
free(); |
| 60 |
|
| 61 |
// delete decoder |
| 62 |
if (m_deleteDecoder && m_decoder != NULL) { |
| 63 |
delete m_decoder; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void SoundClip::load(){ |
| 68 |
if (m_loader){ |
| 69 |
m_loader->load(this); |
| 70 |
} |
| 71 |
else { //no loader specified so find one to use |
| 72 |
if(m_name.find(".ogg", m_name.size() - 4) != std::string::npos) { |
| 73 |
OggLoader loader; |
| 74 |
loader.load(this); |
| 75 |
} else { |
| 76 |
FL_WARN(_log, LMsg() << "No audio-decoder available for file \"" << m_name << "\"!"); |
| 77 |
throw InvalidFormat("Error: Ogg loader can't load files without ogg extension"); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
assert(m_decoder); //should be set by now |
| 82 |
|
| 83 |
m_isStream = m_decoder->needsStreaming(); |
| 84 |
|
| 85 |
if (!m_isStream) { |
| 86 |
// only for non-streaming buffers |
| 87 |
SoundBufferEntry* ptr = new SoundBufferEntry(); |
| 88 |
|
| 89 |
// iterate the bufs and fill them with data |
| 90 |
for (int32_t i = 0; i < BUFFER_NUM; i++) { |
| 91 |
|
| 92 |
if (m_decoder->decode(BUFFER_LEN)) { |
| 93 |
// EOF or error |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
// generate buffer and fill it with data |
| 98 |
alGenBuffers(1, &ptr->buffers[i]); |
| 99 |
|
| 100 |
alBufferData(ptr->buffers[i], m_decoder->getALFormat(), m_decoder->getBuffer(), |
| 101 |
m_decoder->getBufferSize(), m_decoder->getSampleRate()); |
| 102 |
|
| 103 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error copying data to buffers") |
| 104 |
|
| 105 |
ptr->usedbufs++; |
| 106 |
} |
| 107 |
|
| 108 |
m_decoder->releaseBuffer(); |
| 109 |
|
| 110 |
// push the buffer information to the vector |
| 111 |
m_buffervec.push_back(ptr); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
m_state = IResource::RES_LOADED; |
| 116 |
} |
| 117 |
|
| 118 |
void SoundClip::free(){ |
| 119 |
if (m_state == IResource::RES_LOADED) { |
| 120 |
if (m_isStream) { |
| 121 |
// erase all elements from the list |
| 122 |
std::vector<SoundBufferEntry*>::iterator it; |
| 123 |
for (it = m_buffervec.begin(); it != m_buffervec.end(); ++it) { |
| 124 |
if ((*it) && (*it)->buffers[0] != 0) { |
| 125 |
alDeleteBuffers(BUFFER_NUM, (*it)->buffers); |
| 126 |
} |
| 127 |
delete (*it); |
| 128 |
} |
| 129 |
} else { |
| 130 |
// for non-streaming soundclips |
| 131 |
SoundBufferEntry* ptr = m_buffervec.at(0); |
| 132 |
for(uint32_t i = 0; i < ptr->usedbufs; i++) { |
| 133 |
alDeleteBuffers(1, &ptr->buffers[i]); |
| 134 |
} |
| 135 |
delete ptr; |
| 136 |
} |
| 137 |
m_buffervec.clear(); |
| 138 |
} |
| 139 |
m_state = IResource::RES_NOT_LOADED; |
| 140 |
} |
| 141 |
|
| 142 |
bool SoundClip::isStream() const { |
| 143 |
return m_isStream; |
| 144 |
} |
| 145 |
|
| 146 |
uint32_t SoundClip::countBuffers() const { |
| 147 |
return m_buffervec.at(0)->usedbufs; |
| 148 |
} |
| 149 |
|
| 150 |
ALuint* SoundClip::getBuffers(uint32_t streamid) const { |
| 151 |
return m_buffervec.at(streamid)->buffers; |
| 152 |
} |
| 153 |
|
| 154 |
uint32_t SoundClip::beginStreaming() { |
| 155 |
SoundBufferEntry* ptr = NULL; |
| 156 |
uint32_t id = 0; |
| 157 |
for (uint32_t i = 0; i < m_buffervec.size(); i++) { |
| 158 |
if (m_buffervec.at(i) == NULL) { |
| 159 |
ptr = new SoundBufferEntry(); |
| 160 |
m_buffervec.at(i) = ptr; |
| 161 |
id = i; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
// create new sound buffer entry |
| 166 |
if (!ptr) { |
| 167 |
ptr = new SoundBufferEntry(); |
| 168 |
m_buffervec.push_back(ptr); |
| 169 |
id = m_buffervec.size() -1 ; |
| 170 |
} |
| 171 |
|
| 172 |
ptr->usedbufs=0; |
| 173 |
ptr->deccursor = 0; |
| 174 |
alGenBuffers(BUFFER_NUM, ptr->buffers); |
| 175 |
|
| 176 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error creating streaming-buffers") |
| 177 |
|
| 178 |
return id; |
| 179 |
} |
| 180 |
|
| 181 |
bool SoundClip::setStreamPos(uint32_t streamid, SoundPositionType type, float value) { |
| 182 |
uint64_t pos = 0; |
| 183 |
// convert position to bytes |
| 184 |
switch (type) { |
| 185 |
case SD_BYTE_POS: |
| 186 |
pos = static_cast<uint64_t>(value); |
| 187 |
break; |
| 188 |
case SD_TIME_POS: |
| 189 |
value /= static_cast<float>(m_decoder->getSampleRate()); |
| 190 |
case SD_SAMPLE_POS: |
| 191 |
pos = static_cast<uint64_t>((m_decoder->getBitResolution() / 8) * (m_decoder->isStereo() ? 2 : 1) * value); |
| 192 |
break; |
| 193 |
} |
| 194 |
|
| 195 |
if (pos > m_decoder->getDecodedLength()) { |
| 196 |
// EOF! |
| 197 |
m_buffervec.at(streamid)->deccursor = m_decoder->getDecodedLength(); |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
// set cursor position |
| 202 |
m_buffervec.at(streamid)->deccursor = pos; |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
float SoundClip::getStreamPos(uint32_t streamid, SoundPositionType type) const{ |
| 207 |
uint64_t pos = m_buffervec.at(streamid)->deccursor; |
| 208 |
switch(type) { |
| 209 |
case SD_BYTE_POS: |
| 210 |
return pos; |
| 211 |
case SD_SAMPLE_POS: |
| 212 |
return pos / (m_decoder->getBitResolution() / 8 * (m_decoder->isStereo() ? 2 : 1)); |
| 213 |
case SD_TIME_POS: |
| 214 |
return pos / (m_decoder->getBitResolution() / 8 * (m_decoder->isStereo() ? 2 : 1) * m_decoder->getSampleRate()); |
| 215 |
} |
| 216 |
return 0.0f; |
| 217 |
} |
| 218 |
|
| 219 |
void SoundClip::acquireStream(uint32_t streamid) { |
| 220 |
SoundBufferEntry* ptr = m_buffervec.at(streamid); |
| 221 |
for (int32_t i = 0; i < BUFFER_NUM; i++) { |
| 222 |
if (getStream(streamid, ptr->buffers[i])) { |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
bool SoundClip::getStream(uint32_t streamid, ALuint buffer) { |
| 229 |
SoundBufferEntry* ptr = m_buffervec.at(streamid); |
| 230 |
|
| 231 |
if (ptr->deccursor >= m_decoder->getDecodedLength()) { |
| 232 |
// EOF! |
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
// set cursor of decoder |
| 237 |
if (!m_decoder->setCursor(ptr->deccursor)) { |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
// Error while decoding file? |
| 242 |
if (m_decoder->decode(BUFFER_LEN)) { |
| 243 |
throw Exception("error while reading from audio file"); |
| 244 |
} |
| 245 |
|
| 246 |
// fill the buffer with data |
| 247 |
alBufferData(buffer, m_decoder->getALFormat(), |
| 248 |
m_decoder->getBuffer(), m_decoder->getBufferSize(), m_decoder->getSampleRate()); |
| 249 |
|
| 250 |
// update cursor |
| 251 |
ptr->deccursor += m_decoder->getBufferSize(); |
| 252 |
|
| 253 |
m_decoder->releaseBuffer(); |
| 254 |
|
| 255 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error catching stream") |
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
void SoundClip::quitStreaming(uint32_t streamid) { |
| 261 |
// release the buffers |
| 262 |
SoundBufferEntry* ptr = m_buffervec.at(streamid); |
| 263 |
alDeleteBuffers(BUFFER_NUM, ptr->buffers); |
| 264 |
ptr->buffers[0] = 0; |
| 265 |
} |
| 266 |
|
| 267 |
void SoundClip::endStreaming(uint32_t streamid) { |
| 268 |
SoundBufferEntry** ptr = &m_buffervec.at(streamid); |
| 269 |
delete *ptr; |
| 270 |
*ptr = NULL; |
| 271 |
} |
| 272 |
|
| 273 |
void SoundClip::adobtDecoder(SoundDecoder* decoder) { |
| 274 |
m_decoder = decoder; |
| 275 |
m_deleteDecoder = true; |
| 276 |
} |
| 277 |
|
| 278 |
void SoundClip::setDecoder(SoundDecoder* decoder) { |
| 279 |
m_decoder = decoder; |
| 280 |
m_deleteDecoder = false; |
| 281 |
} |
| 282 |
|
| 283 |
SoundDecoder* SoundClip::getDecoder() const { |
| 284 |
return m_decoder; |
| 285 |
} |
| 286 |
|
| 287 |
size_t SoundClip::getSize() { |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
std::string SoundClip::createUniqueClipName() { |
| 292 |
// automated counting for name generation, in case the user doesn't provide a name |
| 293 |
static uint32_t uniqueNumber = 0; |
| 294 |
static std::string baseName = "soundclip"; |
| 295 |
|
| 296 |
std::ostringstream oss; |
| 297 |
oss << uniqueNumber << "_" << baseName; |
| 298 |
|
| 299 |
const std::string name = oss.str(); |
| 300 |
++uniqueNumber; |
| 301 |
|
| 302 |
return name; |
| 303 |
} |
| 304 |
} |