From 5e42f37fba93477e3323eb4c10026a0d810d4309 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Tue, 14 Jun 2022 11:46:11 +0300 Subject: [PATCH] Fixed sound duration for missing sounds. Issue #140. --- SpaceCadetPinball/TPinballTable.cpp | 2 ++ SpaceCadetPinball/loader.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/SpaceCadetPinball/TPinballTable.cpp b/SpaceCadetPinball/TPinballTable.cpp index 652cf9b..a806920 100644 --- a/SpaceCadetPinball/TPinballTable.cpp +++ b/SpaceCadetPinball/TPinballTable.cpp @@ -447,6 +447,8 @@ int TPinballTable::Message(int code, float value) pinball::MissTextBox->Clear(); LightGroup->Message(28, 0.2f); auto time = loader::play_sound(SoundIndex1, nullptr, "TPinballTable2"); + if (time < 0) + time = 5.0f; LightShowTimer = timer::set(time, this, LightShow_timeout); } diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp index 968b6a3..e123a9b 100644 --- a/SpaceCadetPinball/loader.cpp +++ b/SpaceCadetPinball/loader.cpp @@ -94,8 +94,7 @@ void loader::loadfrom(DatFile* datFile) { if (sound_count < 65) { - sound_list[sound_count].WavePtr = nullptr; - sound_list[sound_count].GroupIndex = groupIndex; + sound_list[sound_count] = {nullptr, groupIndex, 0, 0}; sound_count++; } } @@ -108,8 +107,7 @@ void loader::unload() for (int index = 1; index < sound_count; ++index) { Sound::FreeSound(sound_list[index].WavePtr); - sound_list[index].Loaded = 0; - sound_list[index].WavePtr = nullptr; + sound_list[index] = {}; } sound_count = 1; @@ -158,16 +156,18 @@ int loader::get_sound_id(int groupIndex) fileName.insert(0, "SOUND"); } + float duration = -1; auto filePath = pinball::make_path_name(fileName); auto file = fopenu(filePath.c_str(), "rb"); if (file) { fread(&wavHeader, 1, sizeof wavHeader, file); fclose(file); + auto sampleCount = wavHeader.data_size / (wavHeader.channels * (wavHeader.bits_per_sample / 8.0)); + duration = static_cast(sampleCount / wavHeader.sample_rate); } - auto sampleCount = wavHeader.data_size / (wavHeader.channels * (wavHeader.bits_per_sample / 8.0)); - sound_list[soundIndex].Duration = static_cast(sampleCount / wavHeader.sample_rate); + sound_list[soundIndex].Duration = duration; sound_list[soundIndex].WavePtr = Sound::LoadWaveFile(filePath); } }