Przeglądaj źródła

Merge pull request #3553 from Laserlicht/sync_subtitle

sync subtitle with audio
Ivan Savenko 1 rok temu
rodzic
commit
dc2f8cb81d

+ 24 - 0
client/CMusicHandler.cpp

@@ -183,6 +183,30 @@ void CSoundHandler::ambientStopSound(const AudioPath & soundId)
 	setChannelVolume(ambientChannels[soundId], volume);
 }
 
+uint32_t CSoundHandler::getSoundDurationMilliseconds(const AudioPath & sound)
+{
+	if (!initialized || sound.empty())
+		return 0;
+
+	auto data = CResourceHandler::get()->load(sound.addPrefix("SOUNDS/"))->readAll();
+
+	SDL_AudioSpec spec;
+	uint32_t audioLen;
+	uint8_t *audioBuf;
+	uint32_t miliseconds = 0;
+
+	if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), (int)data.second), 1, &spec, &audioBuf, &audioLen) != nullptr)
+	{
+		SDL_FreeWAV(audioBuf);
+		uint32_t sampleSize = SDL_AUDIO_BITSIZE(spec.format) / 8;
+		uint32_t sampleCount = audioLen / sampleSize;
+		uint32_t sampleLen = sampleCount / spec.channels;
+		miliseconds = 1000 * sampleLen / spec.freq;
+	}
+
+	return miliseconds ;
+}
+
 // Plays a sound, and return its channel so we can fade it out later
 int CSoundHandler::playSound(soundBase::soundID soundID, int repeats)
 {

+ 1 - 0
client/CMusicHandler.h

@@ -77,6 +77,7 @@ public:
 	void setChannelVolume(int channel, ui32 percent);
 
 	// Sounds
+	uint32_t getSoundDurationMilliseconds(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<std::unique_ptr<ui8 []>, si64> & data, int repeats=0, bool cache=false);

+ 24 - 12
client/mainmenu/CPrologEpilogVideo.cpp

@@ -14,16 +14,18 @@
 #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<void()> 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), elapsedTimeMilliseconds(0)
 {
 	OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
-	addUsedEvents(LCLICK);
+	addUsedEvents(LCLICK | TIME);
 	pos = center(Rect(0, 0, 800, 600));
 	updateShadow();
 
@@ -31,15 +33,33 @@ CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::f
 	videoSoundHandle = CCS->soundh->playSound(audioData);
 	CCS->videoh->open(spe.prologVideo);
 	CCS->musich->playMusic(spe.prologMusic, true, true);
+	voiceDurationMilliseconds = CCS->soundh->getSoundDurationMilliseconds(spe.prologVoice);
 	voiceSoundHandle = CCS->soundh->playSound(spe.prologVoice);
 	auto onVoiceStop = [this]()
 	{
 		voiceStopped = true;
+		elapsedTimeMilliseconds = 0;
 	};
 	CCS->soundh->setCallback(voiceSoundHandle, onVoiceStop);
 
 	text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
-	text->scrollTextTo(-100);
+	text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
+}
+
+void CPrologEpilogVideo::tick(uint32_t msPassed)
+{
+	elapsedTimeMilliseconds += msPassed;
+
+	const uint32_t speed = (voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
+
+	if(elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
+	{
+		text->scrollTextBy(1);
+		elapsedTimeMilliseconds -= speed;
+		++positionCounter;
+	}
+	else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
+		clickPressed(GH.getCursorPosition());
 }
 
 void CPrologEpilogVideo::show(Canvas & to)
@@ -48,15 +68,7 @@ void CPrologEpilogVideo::show(Canvas & to)
 	//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)
-		text->scrollTextBy(1);
-	else
-		text->showAll(to); // blit text over video, if needed
-
-	if(text->textSize.y + 100 < positionCounter / 5 && voiceStopped)
-		clickPressed(GH.getCursorPosition());
+	text->showAll(to); // blit text over video, if needed
 }
 
 void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)

+ 3 - 0
client/mainmenu/CPrologEpilogVideo.h

@@ -19,6 +19,8 @@ class CPrologEpilogVideo : public CWindowObject
 	CampaignScenarioPrologEpilog spe;
 	int positionCounter;
 	int voiceSoundHandle;
+	uint32_t voiceDurationMilliseconds;
+	uint32_t elapsedTimeMilliseconds;
 	int videoSoundHandle;
 	std::function<void()> exitCb;
 
@@ -29,6 +31,7 @@ class CPrologEpilogVideo : public CWindowObject
 public:
 	CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback);
 
+	void tick(uint32_t msPassed) override;
 	void clickPressed(const Point & cursorPosition) override;
 	void show(Canvas & to) override;
 };