Skip to content

Commit

Permalink
RetroAchievements audio customization: Fix checks for WAV file format
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Sep 11, 2023
1 parent dbbf8e2 commit e5a0788
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 6 additions & 1 deletion UI/BackgroundAudio.cpp
Expand Up @@ -42,6 +42,11 @@ struct WavData {
free(raw_data);
raw_data = nullptr;
}

bool IsSimpleWAV() const {
bool isBad = raw_bytes_per_frame > sizeof(int16_t) * num_channels;
return !isBad && num_channels > 0 && sample_rate >= 8000 && codec == 0;
}
};

void WavData::Read(RIFFReader &file_) {
Expand Down Expand Up @@ -389,7 +394,7 @@ Sample *Sample::Load(const std::string &path) {

delete[] data;

if (wave.num_channels > 2 || wave.raw_bytes_per_frame > sizeof(int16_t) * wave.num_channels) {
if (!wave.IsSimpleWAV()) {
ERROR_LOG(AUDIO, "Wave format not supported for mixer playback. Must be 8-bit or 16-bit raw mono or stereo. '%s'", path.c_str());
return nullptr;
}
Expand Down
8 changes: 2 additions & 6 deletions UI/RetroAchievementScreens.cpp
Expand Up @@ -39,17 +39,13 @@ AudioFileChooser::AudioFileChooser(std::string *value, const std::string &title,
return UI::EVENT_DONE;
});
Add(new FileChooserChoice(value, title, BrowseFileType::SOUND_EFFECT, new LinearLayoutParams(1.0f)))->OnChange.Add([=](UI::EventParams &e) {
// TODO: Check the file format here.
// Need to forward the event out.
std::string path = e.s;
Sample *sample = Sample::Load(path);
if (sample) {
g_BackgroundAudio.SFX().UpdateSample(sound, sample);
} else {
if (!sample) {
auto au = GetI18NCategory(I18NCat::AUDIO);
g_OSD.Show(OSDType::MESSAGE_WARNING, au->T("Audio file format not supported. Must be 16-bit WAV."));
}
auto au = GetI18NCategory(I18NCat::AUDIO);
g_OSD.Show(OSDType::MESSAGE_ERROR, au->T("Audio file format not supported. Must be WAV."));
value->clear();
}
return UI::EVENT_DONE;
Expand Down

0 comments on commit e5a0788

Please sign in to comment.