2014-07-23 15:28:20 +00:00
|
|
|
/*
|
|
|
|
** soundemitter.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2014 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp 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 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "soundemitter.h"
|
|
|
|
|
|
|
|
#include "sharedstate.h"
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include "exception.h"
|
2014-08-25 03:28:46 +00:00
|
|
|
#include "config.h"
|
2014-07-23 15:28:20 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <SDL_sound.h>
|
|
|
|
|
|
|
|
#define SE_CACHE_MEM (10*1024*1024) // 10 MB
|
|
|
|
|
|
|
|
struct SoundBuffer
|
|
|
|
{
|
|
|
|
/* Uniquely identifies this or equal buffer */
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
AL::Buffer::ID alBuffer;
|
|
|
|
|
|
|
|
/* Link into the buffer cache priority list */
|
|
|
|
IntruListLink<SoundBuffer> link;
|
|
|
|
|
|
|
|
/* Buffer byte count */
|
|
|
|
uint32_t bytes;
|
|
|
|
|
|
|
|
/* Reference count */
|
|
|
|
uint8_t refCount;
|
|
|
|
|
|
|
|
SoundBuffer()
|
|
|
|
: link(this),
|
|
|
|
refCount(1)
|
|
|
|
|
|
|
|
{
|
|
|
|
alBuffer = AL::Buffer::gen();
|
|
|
|
}
|
|
|
|
|
|
|
|
static SoundBuffer *ref(SoundBuffer *buffer)
|
|
|
|
{
|
|
|
|
++buffer->refCount;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deref(SoundBuffer *buffer)
|
|
|
|
{
|
|
|
|
if (--buffer->refCount == 0)
|
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SoundBuffer()
|
|
|
|
{
|
|
|
|
AL::Buffer::del(alBuffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Before: [a][b][c][d], After (index=1): [a][c][d][b] */
|
|
|
|
static void
|
2014-08-25 03:28:46 +00:00
|
|
|
arrayPushBack(std::vector<size_t> &array, size_t size, size_t index)
|
2014-07-23 15:28:20 +00:00
|
|
|
{
|
|
|
|
size_t v = array[index];
|
|
|
|
|
|
|
|
for (size_t t = index; t < size-1; ++t)
|
|
|
|
array[t] = array[t+1];
|
|
|
|
|
|
|
|
array[size-1] = v;
|
|
|
|
}
|
|
|
|
|
2014-08-25 03:28:46 +00:00
|
|
|
SoundEmitter::SoundEmitter(const Config &conf)
|
|
|
|
: bufferBytes(0),
|
|
|
|
srcCount(conf.SE.sourceCount),
|
|
|
|
alSrcs(srcCount),
|
|
|
|
atchBufs(srcCount),
|
|
|
|
srcPrio(srcCount)
|
2014-07-23 15:28:20 +00:00
|
|
|
{
|
2014-08-25 03:28:46 +00:00
|
|
|
for (size_t i = 0; i < srcCount; ++i)
|
2014-07-23 15:28:20 +00:00
|
|
|
{
|
|
|
|
alSrcs[i] = AL::Source::gen();
|
|
|
|
atchBufs[i] = 0;
|
|
|
|
srcPrio[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundEmitter::~SoundEmitter()
|
|
|
|
{
|
2014-08-25 03:28:46 +00:00
|
|
|
for (size_t i = 0; i < srcCount; ++i)
|
2014-07-23 15:28:20 +00:00
|
|
|
{
|
|
|
|
AL::Source::stop(alSrcs[i]);
|
|
|
|
AL::Source::del(alSrcs[i]);
|
|
|
|
|
|
|
|
if (atchBufs[i])
|
|
|
|
SoundBuffer::deref(atchBufs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferHash::const_iterator iter;
|
|
|
|
for (iter = bufferHash.cbegin(); iter != bufferHash.cend(); ++iter)
|
|
|
|
SoundBuffer::deref(iter->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundEmitter::play(const std::string &filename,
|
|
|
|
int volume,
|
|
|
|
int pitch)
|
|
|
|
{
|
|
|
|
float _volume = clamp<int>(volume, 0, 100) / 100.f;
|
|
|
|
float _pitch = clamp<int>(pitch, 50, 150) / 100.f;
|
|
|
|
|
|
|
|
SoundBuffer *buffer = allocateBuffer(filename);
|
|
|
|
|
|
|
|
/* Try to find first free source */
|
|
|
|
size_t i;
|
2014-08-25 03:28:46 +00:00
|
|
|
for (i = 0; i < srcCount; ++i)
|
2014-07-23 15:28:20 +00:00
|
|
|
if (AL::Source::getState(alSrcs[srcPrio[i]]) != AL_PLAYING)
|
|
|
|
break;
|
|
|
|
|
2014-08-24 07:19:54 +00:00
|
|
|
/* If we didn't find any, try to find the lowest priority source
|
|
|
|
* with the same buffer to overtake */
|
2014-08-25 03:28:46 +00:00
|
|
|
if (i == srcCount)
|
|
|
|
for (size_t j = 0; j < srcCount; ++j)
|
2014-08-24 07:19:54 +00:00
|
|
|
if (atchBufs[srcPrio[j]] == buffer)
|
|
|
|
i = j;
|
|
|
|
|
2014-07-23 15:28:20 +00:00
|
|
|
/* If we didn't find any, overtake the one with lowest priority */
|
2014-08-25 03:28:46 +00:00
|
|
|
if (i == srcCount)
|
2014-07-23 15:28:20 +00:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
size_t srcIndex = srcPrio[i];
|
2014-08-25 03:35:46 +00:00
|
|
|
|
|
|
|
/* Only detach/reattach if it's actually a different buffer */
|
|
|
|
bool switchBuffer = (atchBufs[srcIndex] != buffer);
|
|
|
|
|
|
|
|
/* Push the used source to the back of the priority list */
|
2014-08-25 03:28:46 +00:00
|
|
|
arrayPushBack(srcPrio, srcCount, i);
|
2014-07-23 15:28:20 +00:00
|
|
|
|
|
|
|
AL::Source::ID src = alSrcs[srcIndex];
|
|
|
|
AL::Source::stop(src);
|
2014-08-25 03:35:46 +00:00
|
|
|
|
|
|
|
if (switchBuffer)
|
|
|
|
AL::Source::detachBuffer(src);
|
2014-07-23 15:28:20 +00:00
|
|
|
|
|
|
|
SoundBuffer *old = atchBufs[srcIndex];
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
SoundBuffer::deref(old);
|
|
|
|
|
|
|
|
atchBufs[srcIndex] = SoundBuffer::ref(buffer);
|
|
|
|
|
2014-08-25 03:35:46 +00:00
|
|
|
if (switchBuffer)
|
|
|
|
AL::Source::attachBuffer(src, buffer->alBuffer);
|
2014-07-23 15:28:20 +00:00
|
|
|
|
2014-12-23 19:12:47 +00:00
|
|
|
AL::Source::setVolume(src, _volume * GLOBAL_VOLUME);
|
2014-07-23 15:28:20 +00:00
|
|
|
AL::Source::setPitch(src, _pitch);
|
|
|
|
|
|
|
|
AL::Source::play(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundEmitter::stop()
|
|
|
|
{
|
2014-08-25 03:28:46 +00:00
|
|
|
for (size_t i = 0; i < srcCount; i++)
|
2014-07-23 15:28:20 +00:00
|
|
|
AL::Source::stop(alSrcs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename)
|
|
|
|
{
|
|
|
|
SoundBuffer *buffer = bufferHash.value(filename, 0);
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
/* Buffer still in cashe.
|
|
|
|
* Move to front of priority list */
|
|
|
|
buffers.remove(buffer->link);
|
|
|
|
buffers.append(buffer->link);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Buffer not in cashe, needs to be loaded */
|
|
|
|
SDL_RWops dataSource;
|
2015-02-17 00:42:11 +00:00
|
|
|
char ext[8];
|
2014-07-23 15:28:20 +00:00
|
|
|
|
|
|
|
shState->fileSystem().openRead(dataSource, filename.c_str(),
|
2015-02-17 00:42:11 +00:00
|
|
|
false, ext, sizeof(ext));
|
2014-07-23 15:28:20 +00:00
|
|
|
|
2015-02-17 00:42:11 +00:00
|
|
|
Sound_Sample *sampleHandle = Sound_NewSample(&dataSource, ext, 0, STREAM_BUF_SIZE);
|
2014-07-23 15:28:20 +00:00
|
|
|
|
|
|
|
if (!sampleHandle)
|
|
|
|
{
|
|
|
|
SDL_RWclose(&dataSource);
|
2014-08-22 21:50:08 +00:00
|
|
|
throw Exception(Exception::SDLError, "%s.%s: %s",
|
|
|
|
filename.c_str(), extension, Sound_GetError());
|
2014-07-23 15:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t decBytes = Sound_DecodeAll(sampleHandle);
|
|
|
|
uint8_t sampleSize = formatSampleSize(sampleHandle->actual.format);
|
|
|
|
uint32_t sampleCount = decBytes / sampleSize;
|
|
|
|
|
|
|
|
buffer = new SoundBuffer;
|
|
|
|
buffer->key = filename;
|
|
|
|
buffer->bytes = sampleSize * sampleCount;
|
|
|
|
|
|
|
|
ALenum alFormat = chooseALFormat(sampleSize, sampleHandle->actual.channels);
|
|
|
|
|
|
|
|
AL::Buffer::uploadData(buffer->alBuffer, alFormat, sampleHandle->buffer,
|
|
|
|
buffer->bytes, sampleHandle->actual.rate);
|
|
|
|
|
|
|
|
Sound_FreeSample(sampleHandle);
|
|
|
|
|
|
|
|
uint32_t wouldBeBytes = bufferBytes + buffer->bytes;
|
|
|
|
|
|
|
|
/* If memory limit is reached, delete lowest priority buffer
|
|
|
|
* until there is room or no buffers left */
|
|
|
|
while (wouldBeBytes > SE_CACHE_MEM && !buffers.isEmpty())
|
|
|
|
{
|
|
|
|
SoundBuffer *last = buffers.tail();
|
2014-09-04 23:06:08 +00:00
|
|
|
bufferHash.remove(last->key);
|
2014-07-23 15:28:20 +00:00
|
|
|
buffers.remove(last->link);
|
|
|
|
|
|
|
|
wouldBeBytes -= last->bytes;
|
|
|
|
|
|
|
|
SoundBuffer::deref(last);
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferHash.insert(filename, buffer);
|
|
|
|
buffers.prepend(buffer->link);
|
|
|
|
|
|
|
|
bufferBytes = wouldBeBytes;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|