From 80bf62cb3cab8712dce16d69333e1b011786595d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 2 Oct 2022 21:19:32 -0700 Subject: [PATCH] Start the VPX audio track on the first frame MoviePlayer::Start is called about a second (potentially) before MoviePlayer::Frame starts getting called to actually play the video, causing the audio to start early and require significant synchronization adjustment. This isn't ideal to constantly check if the audio track needs to play in MoviePlayer::Frame, but it's better than starting prematurely. --- src/common/cutscenes/movieplayer.cpp | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/common/cutscenes/movieplayer.cpp b/src/common/cutscenes/movieplayer.cpp index 3c28b38456..e3f497e2f3 100644 --- a/src/common/cutscenes/movieplayer.cpp +++ b/src/common/cutscenes/movieplayer.cpp @@ -556,23 +556,9 @@ public: { MusicStream = ZMusic_OpenSong(GetMusicReader(reader), MDEV_DEFAULT, nullptr); } - if (MusicStream) + if (!MusicStream) { - bool ok = false; - SoundStreamInfo info{}; - ZMusic_GetStreamInfo(MusicStream, &info); - // if mBufferSize == 0, the music stream is played externally (e.g. - // Windows' MIDI synth), which we can't keep synced. Play anyway? - if (info.mBufferSize > 0 && ZMusic_Start(MusicStream, 0, false)) - { - ok = AudioTrack.Start(info.mSampleRate, abs(info.mNumChannels), - (info.mNumChannels < 0) ? MusicSamples16bit : MusicSamplesFloat, &StreamCallbackC, this); - } - if (!ok) - { - ZMusic_Close(MusicStream); - MusicStream = nullptr; - } + Printf(PRINT_BOLD, "Failed to decode %s\n", fileSystem.GetFileFullName(soundtrack, false)); } } animtex.SetSize(AnimTexture::YUV, width, height); @@ -605,6 +591,25 @@ public: framenum++; if (framenum >= numframes) stop = true; + if (!AudioTrack.GetAudioStream() && MusicStream) + { + bool ok = false; + SoundStreamInfo info{}; + ZMusic_GetStreamInfo(MusicStream, &info); + // if mBufferSize == 0, the music stream is played externally (e.g. + // Windows' MIDI synth), which we can't keep synced. Play anyway? + if (info.mBufferSize > 0 && ZMusic_Start(MusicStream, 0, false)) + { + ok = AudioTrack.Start(info.mSampleRate, abs(info.mNumChannels), + (info.mNumChannels < 0) ? MusicSamples16bit : MusicSamplesFloat, &StreamCallbackC, this); + } + if (!ok) + { + ZMusic_Close(MusicStream); + MusicStream = nullptr; + } + } + bool nostopsound = (flags & NOSOUNDCUTOFF); int soundframe = convdenom ? Scale(framenum, convnumer, convdenom) : framenum; if (soundframe > lastsoundframe)