Skip to content

Commit

Permalink
Merge pull request #1016 from Ghabry/cache_audio
Browse files Browse the repository at this point in the history
Audio SE performance improvements
  • Loading branch information
fdelapena committed Sep 1, 2016
2 parents bcb09a0 + 8c2a519 commit 5f4ad40
Show file tree
Hide file tree
Showing 10 changed files with 518 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Expand Up @@ -20,6 +20,8 @@ libeasyrpg_player_la_SOURCES = \
src/audio_decoder.h \
src/audio_resampler.cpp \
src/audio_resampler.h \
src/audio_secache.cpp \
src/audio_secache.h \
src/background.cpp \
src/background.h \
src/baseui.cpp \
Expand Down
2 changes: 2 additions & 0 deletions builds/vs2015/PlayerLib.vcxproj
Expand Up @@ -435,6 +435,7 @@
<ClCompile Include="..\..\src\audio_decoder.cpp" />
<ClCompile Include="..\..\src\audio_resampler.cpp" />
<ClCompile Include="..\..\src\audio_sdl.cpp" />
<ClCompile Include="..\..\src\audio_secache.cpp" />
<ClCompile Include="..\..\src\background.cpp" />
<ClCompile Include="..\..\src\baseui.cpp" />
<ClCompile Include="..\..\src\battle_animation.cpp" />
Expand Down Expand Up @@ -583,6 +584,7 @@
<ClInclude Include="..\..\src\audio_decoder.h" />
<ClInclude Include="..\..\src\audio_resampler.h" />
<ClInclude Include="..\..\src\audio_sdl.h" />
<ClInclude Include="..\..\src\audio_secache.h" />
<ClInclude Include="..\..\src\background.h" />
<ClInclude Include="..\..\src\baseui.h" />
<ClInclude Include="..\..\src\battle_animation.h" />
Expand Down
6 changes: 6 additions & 0 deletions builds/vs2015/PlayerLib.vcxproj.filters
Expand Up @@ -62,6 +62,9 @@
<ClCompile Include="..\..\src\audio_sdl.cpp">
<Filter>Source Files\Backend\Audio</Filter>
</ClCompile>
<ClCompile Include="..\..\src\audio_secache.cpp">
<Filter>Source Files\Backend\Audio</Filter>
</ClCompile>
<ClCompile Include="..\..\src\midisequencer.cpp">
<Filter>Source Files\Backend\Audio</Filter>
</ClCompile>
Expand Down Expand Up @@ -502,6 +505,9 @@
<ClInclude Include="..\..\src\audio_sdl.h">
<Filter>Source Files\Backend\Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\src\audio_secache.h">
<Filter>Source Files\Backend\Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\src\midiprogram.h">
<Filter>Source Files\Backend\Audio</Filter>
</ClInclude>
Expand Down
2 changes: 2 additions & 0 deletions src/audio_resampler.h
Expand Up @@ -19,7 +19,9 @@
#define EASYRPG_AUDIO_RESAMPLER_H

// Headers
// Don't remove the system.h include, prevents heap corruption for automake (preprocessor defines)
#include "audio_decoder.h"
#include "system.h"
#include <string>
#include <memory>

Expand Down
43 changes: 38 additions & 5 deletions src/audio_sdl.cpp
Expand Up @@ -23,6 +23,7 @@

#ifdef HAVE_SDL_MIXER

#include "audio_secache.h"
#include "baseui.h"
#include "audio_sdl.h"
#include "filefinder.h"
Expand Down Expand Up @@ -573,19 +574,51 @@ void SdlAudio::BGS_Volume(int volume) {
Mix_Volume(BGS_CHANNEL_NUM, volume * MIX_MAX_VOLUME / 100);
}

void SdlAudio::SE_Play(std::string const& file, int volume, int /* pitch */) {
std::shared_ptr<Mix_Chunk> sound(Mix_LoadWAV(file.c_str()), &Mix_FreeChunk);
void SdlAudio::SE_Play(std::string const& file, int volume, int pitch) {
std::unique_ptr<AudioSeCache> cache = AudioSeCache::Create(file);
std::shared_ptr<Mix_Chunk> sound;
AudioSeRef se_ref = nullptr;

if (cache) {
int audio_rate;
Uint16 sdl_format;
int audio_channels;
if (!Mix_QuerySpec(&audio_rate, &sdl_format, &audio_channels)) {
Output::Warning("Couldn't query mixer spec.\n%s", Mix_GetError());
return;
}
AudioDecoder::Format audio_format = sdl_format_to_format(sdl_format);

// When this fails the resampler is probably not compiled in and output will be garbage, just use SDL
if (cache->SetFormat(audio_rate, audio_format, audio_channels)) {
cache->SetPitch(pitch);

se_ref = cache->Decode();

sound.reset(Mix_QuickLoad_RAW(se_ref->buffer.data(), se_ref->buffer.size()), &Mix_FreeChunk);

if (!sound) {
Output::Warning("Couldn't load %s SE.\n%s", file.c_str(), Mix_GetError());
}
}
}

if (!sound) {
Output::Warning("Couldn't load %s SE.\n%s", file.c_str(), Mix_GetError());
return;
sound.reset(Mix_LoadWAV(file.c_str()), &Mix_FreeChunk);
if (!sound) {
Output::Warning("Couldn't load %s SE.\n%s", file.c_str(), Mix_GetError());
return;
}
}

int channel = Mix_PlayChannel(-1, sound.get(), 0);
Mix_Volume(channel, volume * MIX_MAX_VOLUME / 100);
if (channel == -1) {
Output::Warning("Couldn't play %s SE.\n%s", file.c_str(), Mix_GetError());
return;
}
sounds[channel] = sound;
sounds[channel].first = sound;
sounds[channel].second = se_ref;
}

void SdlAudio::SE_Stop() {
Expand Down
3 changes: 2 additions & 1 deletion src/audio_sdl.h
Expand Up @@ -20,6 +20,7 @@

#include "audio.h"
#include "audio_decoder.h"
#include "audio_secache.h"

#include <map>

Expand Down Expand Up @@ -66,7 +67,7 @@ struct SdlAudio : public AudioInterface {
bool bgs_stop = true;
bool played_once = false;

typedef std::map<int, std::shared_ptr<Mix_Chunk> > sounds_type;
typedef std::map<int, std::pair<std::shared_ptr<Mix_Chunk>, AudioSeRef>> sounds_type;
sounds_type sounds;

std::unique_ptr<AudioDecoder> audio_decoder;
Expand Down

0 comments on commit 5f4ad40

Please sign in to comment.