Load UI audio effects on a background thread

This commit is contained in:
Henrik Rydgård 2024-10-11 11:31:17 +02:00
parent 049dd8bb79
commit c966067bd0
3 changed files with 31 additions and 9 deletions

View file

@ -564,8 +564,32 @@ void SoundEffectMixer::LoadDefaultSample(UI::UISound sound) {
samples_[(size_t)sound] = std::unique_ptr<Sample>(sample);
}
void SoundEffectMixer::LoadSamples() {
class SampleLoadTask : public Task {
public:
SampleLoadTask(SoundEffectMixer *mixer) : mixer_(mixer) {}
TaskType Type() const override { return TaskType::IO_BLOCKING; }
TaskPriority Priority() const override {
return TaskPriority::NORMAL;
}
virtual void Run() {
mixer_->LoadSamplesOnThread();
}
private:
SoundEffectMixer *mixer_;
};
void SoundEffectMixer::Init() {
samples_.resize((size_t)UI::UISound::COUNT);
UI::SetSoundCallback([](UI::UISound sound, float volume) {
g_BackgroundAudio.SFX().Play(sound, volume);
});
// Load samples in the background.
g_threadManager.EnqueueTask(new SampleLoadTask(this));
}
void SoundEffectMixer::LoadSamplesOnThread() {
LoadDefaultSample(UI::UISound::BACK);
LoadDefaultSample(UI::UISound::SELECT);
LoadDefaultSample(UI::UISound::CONFIRM);
@ -582,8 +606,4 @@ void SoundEffectMixer::LoadSamples() {
} else {
LoadDefaultSample(UI::UISound::LEADERBOARD_SUBMITTED);
}
UI::SetSoundCallback([](UI::UISound sound, float volume) {
g_BackgroundAudio.SFX().Play(sound, volume);
});
}

View file

@ -27,8 +27,7 @@ struct Sample {
// Mixer for things played on top of everything.
class SoundEffectMixer {
public:
void LoadSamples();
void Init();
void Mix(int16_t *buffer, int sz, int sampleRateHz);
void Play(UI::UISound sfx, float volume);
@ -44,7 +43,11 @@ public:
bool done;
};
// This can be called on a thread.
void LoadSamplesOnThread();
private:
bool samplesLoaded_ = false;
std::mutex mutex_;
std::vector<PlayInstance> queue_;
std::vector<PlayInstance> plays_;

View file

@ -729,8 +729,7 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch
}
#endif
// TODO: Load these in the background instead of synchronously.
g_BackgroundAudio.SFX().LoadSamples();
g_BackgroundAudio.SFX().Init();
if (!boot_filename.empty() && stateToLoad.Valid()) {
SaveState::Load(stateToLoad, -1, [](SaveState::Status status, std::string_view message, void *) {