Skip to content

Commit

Permalink
Fix music and video interation. FadeIn/Out music while play/stop vide…
Browse files Browse the repository at this point in the history
…o file

Reduce music starting FadeIn time from 2000ms to 500ms
  • Loading branch information
reyandme committed May 15, 2020
1 parent 159952f commit 38014ee
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/KM_FormMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ procedure TFormMain.FormShow(Sender: TObject);

if not fStartVideoPlayed and (gGameApp.GameSettings <> nil) and gGameApp.GameSettings.VideoStartup then
begin
gVideoPlayer.AddVideo('Campaigns\The Shattered Kingdom\Logo');
gVideoPlayer.AddVideo('KaM');
gVideoPlayer.AddVideo('Campaigns' + PathDelim + 'The Shattered Kingdom' + PathDelim + 'Logo', vfkStarting);
gVideoPlayer.AddVideo('KaM', vfkStarting);
gVideoPlayer.Play;
fStartVideoPlayed := True;
end;
Expand Down
22 changes: 15 additions & 7 deletions src/KM_Music.pas
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ TKMMusicLib = class
procedure ToggleShuffle(aEnableShuffle: Boolean);
procedure FadeMusic; overload;
procedure FadeMusic(aFadeTime: Integer); overload;
procedure UnfadeMusic(aHandleCrackling: Boolean); overload;
procedure UnfadeMusic(aHandleCrackling: Boolean; aFadeTime: Integer); overload;
procedure UnfadeStartingMusic;
procedure UnfadeMusic; overload;
procedure UnfadeMusic(aFadeTime: Integer; aHandleCrackling: Boolean = False); overload;
procedure PauseMusicToPlayFile(const aFileName: UnicodeString; aVolume: Single);
procedure StopPlayingOtherFile;
function GetTrackTitle: UnicodeString;
Expand All @@ -90,6 +91,7 @@ implementation


const
STARTING_MUSIC_UNFADE_TIME = 500; //Time to unfade game starting music, in ms
FADE_TIME = 2000; //Time that a fade takes to occur in ms


Expand Down Expand Up @@ -323,7 +325,7 @@ procedure TKMMusicLib.PlayMenuTrack;
fMusicGain := 0;
PlayMusicFile(fMusicTracks[0]);
fMusicGain := prevVolume;
UnfadeMusic(True);
UnfadeStartingMusic;
end;


Expand Down Expand Up @@ -478,14 +480,20 @@ procedure TKMMusicLib.FadeMusic(aFadeTime: Integer);
end;


procedure TKMMusicLib.UnfadeMusic(aHandleCrackling: Boolean);
procedure TKMMusicLib.UnfadeStartingMusic;
begin
UnfadeMusic(aHandleCrackling, FADE_TIME);
UnfadeMusic(STARTING_MUSIC_UNFADE_TIME, True);
end;


procedure TKMMusicLib.UnfadeMusic;
begin
UnfadeMusic(FADE_TIME);
end;


// aHandleCrackling flag is used to mitigate initial sound crackling
procedure TKMMusicLib.UnfadeMusic(aHandleCrackling: Boolean; aFadeTime: Integer);
procedure TKMMusicLib.UnfadeMusic(aFadeTime: Integer; aHandleCrackling: Boolean = False);
{$IFDEF USELIBZPLAY}
var
startTime, endTime: TStreamTime;
Expand Down Expand Up @@ -542,7 +550,7 @@ procedure TKMMusicLib.UpdateStateIdle;
if fFadedToPlayOther and (fFadeState = fsFaded) and IsOtherEnded then
begin
fFadedToPlayOther := False;
UnfadeMusic(False);
UnfadeMusic;
end;
end;

Expand Down
6 changes: 3 additions & 3 deletions src/KM_Sound.pas
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TKMSoundPlayer = class
fMusicIsFaded: Boolean;

fOnFadeMusic: TEvent;
fOnUnfadeMusic: TBooleanEvent;
fOnUnfadeMusic: TEvent;
procedure CheckOpenALError;
function IsSoundPlaying(aIndex: Integer): Boolean;

Expand All @@ -64,7 +64,7 @@ TKMSoundPlayer = class
function ActiveCount: Byte;

property OnRequestFade: TEvent write fOnFadeMusic;
property OnRequestUnfade: TBooleanEvent write fOnUnfadeMusic;
property OnRequestUnfade: TEvent write fOnUnfadeMusic;
procedure AbortAllFadeSounds;
procedure AbortAllScriptSounds;
procedure AbortAllLongSounds;
Expand Down Expand Up @@ -816,7 +816,7 @@ procedure TKMSoundPlayer.UpdateStateIdle;
//If we reached the end without exiting then we need to resume the music
fMusicIsFaded := False;
if Assigned(fOnUnfadeMusic) then
fOnUnfadeMusic(False);
fOnUnfadeMusic;
end;


Expand Down
59 changes: 40 additions & 19 deletions src/KM_Video.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
SysUtils, SyncObjs, Types, Messages, Classes, Dialogs, KromOGLUtils, KM_VLC
SysUtils, SyncObjs, Types, Messages, Classes, Dialogs, KromOGLUtils, KM_VLC, Generics.Collections
{$IFDEF WDC} , UITypes {$ENDIF}
{$IFDEF FPC} , Controls {$ENDIF}
;
Expand All @@ -19,6 +19,14 @@ interface
type
TKMVideoPlayerCallback = reference to procedure;

TKMVideoFileKind = (vfkNone,
vfkStarting); //Game starting video

TKMVideoFile = record
Path: string;
Kind: TKMVideoFileKind;
end;

TKMVideoPlayer = class
private
{$IFDEF VIDEOS}
Expand All @@ -38,28 +46,28 @@ TKMVideoPlayer = class
FLenght: Int64;
FTime: Int64;

FLastMusicOff: Boolean;

FCallback: TKMVideoPlayerCallback;

FInstance: PVLCInstance;
FMediaPlayer: PVLCMediaPlayer;

FTrackList: TStringList;
FVideoList: TStringList;
FVideoList: TList<TKMVideoFile>;

function TryGetPathFile(const aPath: string; var aFileName: string): Boolean;
procedure SetTrackByLocale;
function GetState: TVLCPlayerState;

procedure StopVideo;

procedure AddVideoToList(aPath: string; aKind: TKMVideoFileKind = vfkNone);
{$ENDIF}
public
constructor Create;
destructor Destroy; override;
procedure AddCampaignVideo(const aCampaignPath, aVideoName: string);
procedure AddMissionVideo(const aMissionFile, aVideoName: string);
procedure AddVideo(const AVideoName: String);
procedure AddVideo(const AVideoName: String; aKind: TKMVideoFileKind = vfkNone);
procedure Play;
procedure Stop;
procedure Pause;
Expand All @@ -86,10 +94,12 @@ TKMVideoPlayer = class
gVideoPlayer: TKMVideoPlayer;

implementation

uses
KM_Render, KM_RenderUI, dglOpenGL, KM_ResLocales, KM_GameApp, KM_Sound;

const
FADE_MUSIC_TIME = 100; // Music fade / unfade time, in ms

{$IFDEF VIDEOS}

function VLCLock(aOpaque: Pointer; var aPlanes: Pointer): Pointer; cdecl;
Expand Down Expand Up @@ -118,7 +128,7 @@ constructor TKMVideoPlayer.Create;
FTexture.V := 1;
FCallback := nil;
FCriticalSection := TCriticalSection.Create;
FVideoList := TStringList.Create;
FVideoList := TList<TKMVideoFile>.Create;
FTrackList := TStringList.Create;

VLCLoadLibrary;
Expand All @@ -141,6 +151,16 @@ destructor TKMVideoPlayer.Destroy;
end;


procedure TKMVideoPlayer.AddVideoToList(aPath: string; aKind: TKMVideoFileKind = vfkNone);
var
videoFileData: TKMVideoFile;
begin
videoFileData.Path := aPath;
videoFileData.Kind := aKind;
FVideoList.Add(videoFileData);
end;


procedure TKMVideoPlayer.AddCampaignVideo(const aCampaignPath, aVideoName: string);
{$IFDEF VIDEOS}
var
Expand All @@ -155,7 +175,7 @@ procedure TKMVideoPlayer.AddCampaignVideo(const aCampaignPath, aVideoName: strin

if TryGetPathFile(aCampaignPath + aVideoName, Path) or
TryGetPathFile(VIDEOFILE_PATH + aVideoName, Path) then
FVideoList.Add(Path);
AddVideoToList(Path);
{$ENDIF}
end;

Expand All @@ -177,11 +197,11 @@ procedure TKMVideoPlayer.AddMissionVideo(const aMissionFile, aVideoName: string)
if TryGetPathFile(MissionPath + FileName, Path) or
TryGetPathFile(MissionPath + aVideoName, Path) or
TryGetPathFile(VIDEOFILE_PATH + aVideoName, Path) then
FVideoList.Add(Path);
AddVideoToList(Path);
{$ENDIF}
end;

procedure TKMVideoPlayer.AddVideo(const aVideoName: String);
procedure TKMVideoPlayer.AddVideo(const aVideoName: String; aKind: TKMVideoFileKind = vfkNone);
{$IFDEF VIDEOS}
var
Path: string;
Expand All @@ -194,7 +214,7 @@ procedure TKMVideoPlayer.AddVideo(const aVideoName: String);
Exit;
if TryGetPathFile(aVideoName, Path) or
TryGetPathFile(VIDEOFILE_PATH + aVideoName, Path) then
FVideoList.Add(Path);
AddVideoToList(Path, aKind);
{$ENDIF}
end;

Expand Down Expand Up @@ -405,16 +425,14 @@ procedure TKMVideoPlayer.Play;
begin
gSoundPlayer.AbortAllFadeSounds;
gGameApp.MusicLib.StopPlayingOtherFile;
FLastMusicOff := gGameApp.GameSettings.MusicOff;
gGameApp.GameSettings.MusicOff := True;
gGameApp.MusicLib.ToggleMusic(false);
gGameApp.MusicLib.FadeMusic(FADE_MUSIC_TIME);
end;

FTrackList.Clear;
FWidth := 0;
FHeight := 0;

path := FVideoList[FIndex];
path := FVideoList[FIndex].Path;

FInstance := libvlc_new(0, nil);
Media := libvlc_media_new_path(FInstance, PAnsiChar(UTF8Encode((path))));
Expand Down Expand Up @@ -496,21 +514,24 @@ procedure TKMVideoPlayer.StopVideo;
{$ENDIF}

procedure TKMVideoPlayer.Stop;
var
startingVideo: Boolean;
begin
{$IFDEF VIDEOS}
StopVideo;

startingVideo := ( FVideoList[FIndex].Kind = vfkStarting );
Inc(FIndex);
if FIndex >= FVideoList.Count then
begin
FIndex := 0;
FVideoList.Clear;
if Assigned(gGameApp) then
begin
gGameApp.GameSettings.MusicOff := FLastMusicOff;
gGameApp.MusicLib.ToggleMusic(not FLastMusicOff);
if not FLastMusicOff then
gGameApp.MusicLib.ToggleShuffle(FLastMusicOff);
if startingVideo then
gGameApp.MusicLib.UnfadeStartingMusic
else
gGameApp.MusicLib.UnfadeMusic(FADE_MUSIC_TIME);
end;

if Assigned(FCallback) then
Expand Down

0 comments on commit 38014ee

Please sign in to comment.