From 7f3bee15f0d6d0c9856cca05598fa32369da18d0 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Tue, 14 Jun 2022 16:58:28 +0300 Subject: [PATCH] Merge from V2: Fixed sound duration for missing sounds. Issue #140. --- SpaceCadetPinball/TPinballTable.cpp | 2 ++ SpaceCadetPinball/loader.cpp | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/SpaceCadetPinball/TPinballTable.cpp b/SpaceCadetPinball/TPinballTable.cpp index 7d51f9f..7806a94 100644 --- a/SpaceCadetPinball/TPinballTable.cpp +++ b/SpaceCadetPinball/TPinballTable.cpp @@ -465,6 +465,8 @@ int TPinballTable::Message(int code, float value) Sound::Idle(); LightGroup->Message(28, 0.2f); auto time = loader::play_sound(SoundIndex1); + if (time < 0) + time = 5.0f; LightShowTimer = timer::set(time, this, LightShow_timeout); } diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp index 8761171..e3fb0f1 100644 --- a/SpaceCadetPinball/loader.cpp +++ b/SpaceCadetPinball/loader.cpp @@ -94,8 +94,7 @@ void loader::loadfrom(datFileStruct* 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, nullptr}; sound_count++; } } @@ -111,6 +110,7 @@ void loader::unload() if (sound_list[index].PtrToSmth) memory::free(sound_list[index].PtrToSmth); + sound_list[index] = {}; sound_count = 1; } @@ -151,11 +151,17 @@ int loader::get_sound_id(int groupIndex) sprintf_s(fileName2, pb::FullTiltMode ? "SOUND\\%s" : "%s", fileName); pinball::make_path_name(filePath, fileName2); + float duration = -1; HFILE hFile = _lopen(filePath, 0); - _lread(hFile, &wavHeader, sizeof wavHeader); - _lclose(hFile); - auto sampleCount = wavHeader.data_size / (wavHeader.channels * (wavHeader.bits_per_sample / 8.0)); - sound_list[soundIndex].Duration = static_cast(sampleCount / wavHeader.sample_rate); + if (hFile != HFILE_ERROR) + { + _lread(hFile, &wavHeader, sizeof wavHeader); + _lclose(hFile); + auto sampleCount = wavHeader.data_size / (wavHeader.channels * (wavHeader.bits_per_sample / 8.0)); + duration = static_cast(sampleCount / wavHeader.sample_rate); + } + + sound_list[soundIndex].Duration = duration; sound_list[soundIndex].WavePtr = Sound::LoadWaveFile(filePath); } }