From 134eaa1583b180ceceff40b669ae965dc1a51412 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Wed, 24 Jan 2024 01:17:23 +0100 Subject: [PATCH] sync subtitle --- client/CMusicHandler.cpp | 28 ++++++++++++++++++++++++++ client/CMusicHandler.h | 1 + client/mainmenu/CPrologEpilogVideo.cpp | 26 +++++++++++++++++------- client/mainmenu/CPrologEpilogVideo.h | 2 ++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/client/CMusicHandler.cpp b/client/CMusicHandler.cpp index b6101c7cb..2d9c64fb0 100644 --- a/client/CMusicHandler.cpp +++ b/client/CMusicHandler.cpp @@ -183,6 +183,34 @@ void CSoundHandler::ambientStopSound(const AudioPath & soundId) setChannelVolume(ambientChannels[soundId], volume); } +double CSoundHandler::getSoundDuration(const AudioPath & sound) { + if (!initialized || sound.empty()) + return 0.0; + + auto data = CResourceHandler::get()->load(sound.addPrefix("SOUNDS/"))->readAll(); + + SDL_AudioSpec spec; + uint32_t audioLen; + uint8_t *audioBuf; + double seconds = 0.0; + + if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), (int)data.second), 1, &spec, &audioBuf, &audioLen) != NULL) { + SDL_FreeWAV(audioBuf); + uint32_t sampleSize = SDL_AUDIO_BITSIZE(spec.format) / 8; + uint32_t sampleCount = audioLen / sampleSize; + uint32_t sampleLen = 0; + if(spec.channels) { + sampleLen = sampleCount / spec.channels; + } else { + sampleLen = sampleCount; + } + seconds = (double)sampleLen / (double)spec.freq; + } else + return 0.0; + + return seconds; +} + // Plays a sound, and return its channel so we can fade it out later int CSoundHandler::playSound(soundBase::soundID soundID, int repeats) { diff --git a/client/CMusicHandler.h b/client/CMusicHandler.h index f0b653e7c..514a16834 100644 --- a/client/CMusicHandler.h +++ b/client/CMusicHandler.h @@ -77,6 +77,7 @@ public: void setChannelVolume(int channel, ui32 percent); // Sounds + double getSoundDuration(const AudioPath & sound); int playSound(soundBase::soundID soundID, int repeats=0); int playSound(const AudioPath & sound, int repeats=0, bool cache=false); int playSound(std::pair, si64> & data, int repeats=0, bool cache=false); diff --git a/client/mainmenu/CPrologEpilogVideo.cpp b/client/mainmenu/CPrologEpilogVideo.cpp index d81bd07e7..98cdb1ad4 100644 --- a/client/mainmenu/CPrologEpilogVideo.cpp +++ b/client/mainmenu/CPrologEpilogVideo.cpp @@ -14,13 +14,15 @@ #include "../CGameInfo.h" #include "../CMusicHandler.h" #include "../CVideoHandler.h" +#include "../gui/WindowHandler.h" #include "../gui/CGuiHandler.h" +#include "../gui/FramerateManager.h" #include "../widgets/TextControls.h" #include "../render/Canvas.h" CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function callback) - : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback) + : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback), elapsedTime(0) { OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; addUsedEvents(LCLICK); @@ -31,32 +33,42 @@ CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::f videoSoundHandle = CCS->soundh->playSound(audioData); CCS->videoh->open(spe.prologVideo); CCS->musich->playMusic(spe.prologMusic, true, true); + voiceDuration = CCS->soundh->getSoundDuration(spe.prologVoice); voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice); auto onVoiceStop = [this]() { voiceStopped = true; + elapsedTime = 0.0; }; CCS->soundh->setCallback(voiceSoundHandle, onVoiceStop); text = std::make_shared(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString()); - text->scrollTextTo(-100); + text->scrollTextTo(-50); } void CPrologEpilogVideo::show(Canvas & to) { + elapsedTime += GH.framerate().getElapsedMilliseconds() / 1000.0; + to.drawColor(pos, Colors::BLACK); //some videos are 800x600 in size while some are 800x400 CCS->videoh->update(pos.x, pos.y + (CCS->videoh->size().y == 400 ? 100 : 0), to.getInternalSurface(), true, false); - //move text every 5 calls/frames; seems to be good enough - ++positionCounter; - if(positionCounter % 5 == 0) + const double speed = (voiceDuration == 0.0) ? 0.1 : (voiceDuration / (text->textSize.y)); + + if(elapsedTime > speed && text->textSize.y - 50 > positionCounter) + { text->scrollTextBy(1); + elapsedTime = 0.0; + ++positionCounter; + } else + { text->showAll(to); // blit text over video, if needed - if(text->textSize.y + 100 < positionCounter / 5 && voiceStopped) - clickPressed(GH.getCursorPosition()); + if(elapsedTime > (voiceDuration == 0.0 ? 6.0 : 3.0) && voiceStopped) + clickPressed(GH.getCursorPosition()); + } } void CPrologEpilogVideo::clickPressed(const Point & cursorPosition) diff --git a/client/mainmenu/CPrologEpilogVideo.h b/client/mainmenu/CPrologEpilogVideo.h index 1666a87c1..ffee32e12 100644 --- a/client/mainmenu/CPrologEpilogVideo.h +++ b/client/mainmenu/CPrologEpilogVideo.h @@ -19,6 +19,8 @@ class CPrologEpilogVideo : public CWindowObject CampaignScenarioPrologEpilog spe; int positionCounter; int voiceSoundHandle; + double voiceDuration; + double elapsedTime; int videoSoundHandle; std::function exitCb;