SoundEmitter: Make SE source count configurable

Default is still 6.
This commit is contained in:
Jonas Kulla 2014-08-25 05:28:46 +02:00
parent 5ee62ba0fd
commit b1ebc655f9
8 changed files with 48 additions and 22 deletions

View File

@ -173,6 +173,14 @@
# midi.reverb=false
# Number of OpenAL sources to allocate for SE playback.
# If there are a lot of sounds playing at the same time
# and audibly cutting each other off, try increasing
# this number. Maximum: 64.
#
# SE.sourceCount=6
# Give a hint on which language the game title as
# specified in the Game.ini is, useful if the encoding
# is being falsely detected. Relevant only if mkxp was

View File

@ -60,10 +60,11 @@ struct AudioPrivate
MeWatchState state;
} meWatch;
AudioPrivate()
AudioPrivate(const Config &conf)
: bgm(ALStream::Looped, "bgm"),
bgs(ALStream::Looped, "bgs"),
me(ALStream::NotLooped, "me")
me(ALStream::NotLooped, "me"),
se(conf)
{
meWatch.active = false;
meWatch.termReq = false;
@ -238,8 +239,8 @@ struct AudioPrivate
}
};
Audio::Audio()
: p(new AudioPrivate)
Audio::Audio(const Config &conf)
: p(new AudioPrivate(conf))
{}

View File

@ -33,6 +33,7 @@
* quite make out their meaning yet) */
struct AudioPrivate;
struct Config;
class Audio
{
@ -75,7 +76,7 @@ public:
#endif
private:
Audio();
Audio(const Config &conf);
~Audio();
friend struct SharedStatePrivate;

View File

@ -141,6 +141,7 @@ Config::Config()
{
midi.chorus = false;
midi.reverb = false;
SE.sourceCount = 6;
}
void Config::read(int argc, char *argv[])
@ -165,6 +166,7 @@ void Config::read(int argc, char *argv[])
PO_DESC(midi.soundFont, std::string) \
PO_DESC(midi.chorus, bool) \
PO_DESC(midi.reverb, bool) \
PO_DESC(SE.sourceCount, int) \
PO_DESC(customScript, std::string) \
PO_DESC(pathCache, bool) \
PO_DESC(useScriptNames, bool)
@ -221,6 +223,8 @@ void Config::read(int argc, char *argv[])
#undef PO_DESC
#undef PO_DESC_ALL
SE.sourceCount = clamp(SE.sourceCount, 1, 64);
}
static std::string baseName(const std::string &path)

View File

@ -66,6 +66,11 @@ struct Config
bool reverb;
} midi;
struct
{
int sourceCount;
} SE;
bool useScriptNames;
std::string customScript;

View File

@ -107,6 +107,7 @@ struct SharedStatePrivate
midiState(threadData->config),
#endif
graphics(threadData),
audio(threadData->config),
fontState(threadData->config),
stampCounter(0)
{

View File

@ -24,6 +24,7 @@
#include "sharedstate.h"
#include "filesystem.h"
#include "exception.h"
#include "config.h"
#include "util.h"
#include <SDL_sound.h>
@ -76,7 +77,7 @@ private:
/* Before: [a][b][c][d], After (index=1): [a][c][d][b] */
static void
arrayPushBack(size_t array[], size_t size, size_t index)
arrayPushBack(std::vector<size_t> &array, size_t size, size_t index)
{
size_t v = array[index];
@ -86,10 +87,14 @@ arrayPushBack(size_t array[], size_t size, size_t index)
array[size-1] = v;
}
SoundEmitter::SoundEmitter()
: bufferBytes(0)
SoundEmitter::SoundEmitter(const Config &conf)
: bufferBytes(0),
srcCount(conf.SE.sourceCount),
alSrcs(srcCount),
atchBufs(srcCount),
srcPrio(srcCount)
{
for (int i = 0; i < SE_SOURCES; ++i)
for (size_t i = 0; i < srcCount; ++i)
{
alSrcs[i] = AL::Source::gen();
atchBufs[i] = 0;
@ -99,7 +104,7 @@ SoundEmitter::SoundEmitter()
SoundEmitter::~SoundEmitter()
{
for (int i = 0; i < SE_SOURCES; ++i)
for (size_t i = 0; i < srcCount; ++i)
{
AL::Source::stop(alSrcs[i]);
AL::Source::del(alSrcs[i]);
@ -124,24 +129,24 @@ void SoundEmitter::play(const std::string &filename,
/* Try to find first free source */
size_t i;
for (i = 0; i < SE_SOURCES; ++i)
for (i = 0; i < srcCount; ++i)
if (AL::Source::getState(alSrcs[srcPrio[i]]) != AL_PLAYING)
break;
/* If we didn't find any, try to find the lowest priority source
* with the same buffer to overtake */
if (i == SE_SOURCES)
for (size_t j = 0; j < SE_SOURCES; ++j)
if (i == srcCount)
for (size_t j = 0; j < srcCount; ++j)
if (atchBufs[srcPrio[j]] == buffer)
i = j;
/* If we didn't find any, overtake the one with lowest priority */
if (i == SE_SOURCES)
if (i == srcCount)
i = 0;
/* Push the used source to the back of the priority list */
size_t srcIndex = srcPrio[i];
arrayPushBack(srcPrio, SE_SOURCES, i);
arrayPushBack(srcPrio, srcCount, i);
AL::Source::ID src = alSrcs[srcIndex];
AL::Source::stop(src);
@ -164,7 +169,7 @@ void SoundEmitter::play(const std::string &filename,
void SoundEmitter::stop()
{
for (int i = 0; i < SE_SOURCES; i++)
for (size_t i = 0; i < srcCount; i++)
AL::Source::stop(alSrcs[i]);
}

View File

@ -27,10 +27,10 @@
#include "boost-hash.h"
#include <string>
#define SE_SOURCES 6
#include <vector>
struct SoundBuffer;
struct Config;
struct SoundEmitter
{
@ -42,13 +42,14 @@ struct SoundEmitter
/* Byte count sum of all cached / playing buffers */
uint32_t bufferBytes;
AL::Source::ID alSrcs[SE_SOURCES];
SoundBuffer *atchBufs[SE_SOURCES];
const size_t srcCount;
std::vector<AL::Source::ID> alSrcs;
std::vector<SoundBuffer*> atchBufs;
/* Indices of sources, sorted by priority (lowest first) */
size_t srcPrio[SE_SOURCES];
std::vector<size_t> srcPrio;
SoundEmitter();
SoundEmitter(const Config &conf);
~SoundEmitter();
void play(const std::string &filename,