Skip to content

Commit

Permalink
add karaoke mode via #INSTRUMENTALS tag, key to toggle karaoke mode i…
Browse files Browse the repository at this point in the history
…s K while playing.
  • Loading branch information
dgruss committed Jul 25, 2024
1 parent 89f44e7 commit 14d9f9a
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 24 deletions.
1 change: 1 addition & 0 deletions game/languages/English.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,7 @@ Q = Quit UltraStar Deluxe
SEC_002 = Playback
SPACE = Pause/unpause song
P = Pause/unpause song
K = Toggle karaoke mode (if file is available via the #INSTRUMENTAL tag)
R = Restart song from beginning
V = Toggle video/visualization/background (if available)
W = Toggle webcam on/off (if available)
Expand Down
3 changes: 2 additions & 1 deletion src/base/UMusic.pas
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,11 @@ TAudioOutputDevice = class
function Finished: boolean;
function Length: real;

function Open(const Filename: IPath): boolean; // true if succeed
function Open(const Filename: IPath; const FilenameKaraoke: IPath): boolean; // true if succeed
procedure Close;

procedure Play;
procedure ToggleKaraoke;
procedure Pause;
procedure Stop;

Expand Down
15 changes: 14 additions & 1 deletion src/base/USong.pas
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ TSong = class
Mp3: IPath;
Background: IPath;
Video: IPath;
Karaoke: IPath;

// sorting methods
Genre: UTF8String;
Expand Down Expand Up @@ -264,6 +265,7 @@ constructor TSong.Create();
Self.FileName := PATH_NONE();
Self.Cover := PATH_NONE();
Self.Mp3 := PATH_NONE();
Self.Karaoke := PATH_NONE();
Self.Background:= PATH_NONE();
Self.Video := PATH_NONE();
end;
Expand Down Expand Up @@ -860,7 +862,10 @@ function TSong.ReadTXTHeader(SongFile: TTextFileStream; ReadCustomTags: Boolean)
if (Self.Path.Append(EncFile).IsFile) then
begin
self.Mp3 := EncFile;

if (not Assigned(self.Karaoke)) or (self.Karaoke = PATH_NONE()) then
begin
self.Karaoke := EncFile;
end;
//Add Mp3 Flag to Done
Done := Done or 4;
end
Expand Down Expand Up @@ -919,6 +924,14 @@ function TSong.ReadTXTHeader(SongFile: TTextFileStream; ReadCustomTags: Boolean)
Log.LogError('Can''t find video file in song: ' + FullFileName);
end

// Karaoke Mp3
else if (Identifier = 'INSTRUMENTAL') then
begin
EncFile := DecodeFilename(Value);
if (self.Path.Append(EncFile).IsFile) then
self.Karaoke := EncFile;
end

// Video Gap
else if (Identifier = 'VIDEOGAP') then
begin
Expand Down
67 changes: 60 additions & 7 deletions src/media/UAudioPlaybackBase.pas
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ TAudioPlaybackBase = class(TInterfacedObject, IAudioPlayback)

OutputDeviceList: TAudioOutputDeviceList;
MusicStream: TAudioPlaybackStream;
KaraokeMusicStream: TAudioPlaybackStream;
KaraokeMode: boolean;

IReplayGain: FReplayGain;

Expand All @@ -64,11 +66,12 @@ TAudioPlaybackBase = class(TInterfacedObject, IAudioPlayback)
constructor Create(); virtual;
function GetName: string; virtual; abstract;

function Open(const Filename: IPath): boolean; // true if succeed
function Open(const Filename: IPath; const FilenameKaraoke: IPath): boolean; // true if succeed
procedure Close;

procedure Play;
procedure Pause;
procedure ToggleKaraoke;
procedure Stop;
procedure FadeIn(Time: real; TargetVolume: single);
procedure Fade(Time: real; TargetVolume: single);
Expand Down Expand Up @@ -135,21 +138,24 @@ implementation
constructor TAudioPlaybackBase.Create();
begin
inherited;
KaraokeMode := false;
end;

{ TAudioPlaybackBase }

function TAudioPlaybackBase.FinalizePlayback: boolean;
begin
FreeAndNil(MusicStream);
FreeAndNil(KaraokeMusicStream);
ClearOutputDeviceList();
Result := true;
end;

function TAudioPlaybackBase.Open(const Filename: IPath): boolean;
function TAudioPlaybackBase.Open(const Filename: IPath; const FilenameKaraoke: IPath): boolean;
begin
// free old MusicStream
MusicStream.Free;
KaraokeMusicStream.Free;

MusicStream := OpenStream(Filename);
if not assigned(MusicStream) then
Expand All @@ -158,14 +164,27 @@ function TAudioPlaybackBase.Open(const Filename: IPath): boolean;
Exit;
end;

if assigned(IReplayGain) and IReplayGain.CanEnable then MusicStream.AddSoundFX(IReplayGain.Create());
if assigned(FilenameKaraoke) then
KaraokeMusicStream := OpenStream(FilenameKaraoke);

if assigned(IReplayGain) and IReplayGain.CanEnable then begin
MusicStream.AddSoundFX(IReplayGain.Create());
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.AddSoundFX(IReplayGain.Create());
end;

KaraokeMode := false;
if assigned(KaraokeMusicStream) then begin
KaraokeMusicStream.Volume := 0;
end;

Result := true;
end;

procedure TAudioPlaybackBase.Close;
begin
FreeAndNil(MusicStream);
FreeAndNil(KaraokeMusicStream);
end;

function TAudioPlaybackBase.OpenDecodeStream(const Filename: IPath): TAudioDecodeStream;
Expand Down Expand Up @@ -251,18 +270,38 @@ procedure TAudioPlaybackBase.Play;
begin
if assigned(MusicStream) then
MusicStream.Play();
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.Play();
end;

procedure TAudioPlaybackBase.Pause;
begin
if assigned(MusicStream) then
MusicStream.Pause();
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.Pause();
end;

procedure TAudioPlaybackBase.ToggleKaraoke;
begin
KaraokeMode := not KaraokeMode;
if KaraokeMode and assigned(KaraokeMusicStream) and assigned(MusicStream) then begin
KaraokeMusicStream.Volume := MusicStream.Volume;
MusicStream.Volume := 0;
end;
if (not KaraokeMode) and assigned(KaraokeMusicStream) and assigned(MusicStream) then begin
MusicStream.Volume := KaraokeMusicStream.Volume;
KaraokeMusicStream.Volume := 0;
end;
end;

procedure TAudioPlaybackBase.Stop;
begin
if assigned(MusicStream) then
MusicStream.Stop();
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.Stop();
KaraokeMode := false;
end;

function TAudioPlaybackBase.Length: real;
Expand All @@ -289,12 +328,16 @@ procedure TAudioPlaybackBase.SetPosition(Time: real);
begin
if assigned(MusicStream) then
MusicStream.Position := Time;
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.Position := Time;
end;

procedure TAudioPlaybackBase.SetSyncSource(SyncSource: TSyncSource);
begin
if assigned(MusicStream) then
MusicStream.SetSyncSource(SyncSource);
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.SetSyncSource(SyncSource);
end;

procedure TAudioPlaybackBase.Rewind;
Expand All @@ -312,26 +355,34 @@ function TAudioPlaybackBase.Finished: boolean;

procedure TAudioPlaybackBase.SetVolume(Volume: single);
begin
if assigned(MusicStream) then
if (not KaraokeMode) and assigned(MusicStream) then
MusicStream.Volume := Volume;
if KaraokeMode and assigned(KaraokeMusicStream) then
KaraokeMusicStream.Volume := Volume;
end;

procedure TAudioPlaybackBase.FadeIn(Time: real; TargetVolume: single);
begin
if assigned(MusicStream) then
if (not KaraokeMode) and assigned(MusicStream) then
MusicStream.FadeIn(Time, TargetVolume);
if KaraokeMode and assigned(KaraokeMusicStream) then
KaraokeMusicStream.FadeIn(Time, TargetVolume);
end;

procedure TAudioPlaybackBase.Fade(Time: real; TargetVolume: single);
begin
if assigned(MusicStream) then
if (not KaraokeMode) and assigned(MusicStream) then
MusicStream.Fade(Time, TargetVolume);
if KaraokeMode and assigned(KaraokeMusicStream) then
KaraokeMusicStream.Fade(Time, TargetVolume);
end;

procedure TAudioPlaybackBase.SetLoop(Enabled: boolean);
begin
if assigned(MusicStream) then
MusicStream.Loop := Enabled;
if assigned(KaraokeMusicStream) then
KaraokeMusicStream.Loop := Enabled;
end;

// Equalizer
Expand All @@ -347,8 +398,10 @@ procedure TAudioPlaybackBase.GetFFTData(var data: TFFTData);
*}
function TAudioPlaybackBase.GetPCMData(var data: TPCMData): Cardinal;
begin
if assigned(MusicStream) then
if (not KaraokeMode) and assigned(MusicStream) then
Result := MusicStream.GetPCMData(data)
else if KaraokeMode and assigned(KaraokeMusicStream) then
Result := KaraokeMusicStream.GetPCMData(data)
else
Result := 0;
end;
Expand Down
9 changes: 7 additions & 2 deletions src/media/UMedia_dummy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ TAudio_Dummy = class( TInterfacedObject, IAudioPlayback, IAudioInput )
function Init(): boolean;
function Finalize(): boolean;

function Open(const aFileName: IPath): boolean; // true if succeed
function Open(const aFileName: IPath; const aFileNameKaraoke: IPath): boolean; // true if succeed
procedure Close;

procedure Play;
procedure ToggleKaraoke;
procedure Pause;
procedure Stop;

Expand Down Expand Up @@ -187,7 +188,7 @@ function TAudio_Dummy.Finalize(): boolean;
Result := true;
end;

function TAudio_Dummy.Open(const aFileName : IPath): boolean; // true if succeed
function TAudio_Dummy.Open(const aFileName : IPath; const aFileNameKaraoke : IPath): boolean; // true if succeed
begin
Result := false;
end;
Expand All @@ -200,6 +201,10 @@ procedure TAudio_Dummy.Play;
begin
end;

procedure TAudio_Dummy.ToggleKaraoke;
begin
end;

procedure TAudio_Dummy.Pause;
begin
end;
Expand Down
8 changes: 2 additions & 6 deletions src/screens/UScreenCredits.pas
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ procedure TScreenCredits.OnShow;
Credits_X := 580;

{ open credits tune, we play it after initial delay }
AudioPlayback.Open(soundpath.Append('wome-credits-tune.mp3')); // thank you wetue
AudioPlayback.Open(soundpath.Append('wome-credits-tune.mp3'),nil); // thank you wetue

{ reset twinkling stars }
GoldenRec.KillAll;
Expand Down Expand Up @@ -893,7 +893,7 @@ procedure TScreenCredits.DrawOutro;
begin
CTime_hold := 0;
AudioPlayback.Stop;
AudioPlayback.Open(SoundPath.Append('credits-outro-tune.mp3'));
AudioPlayback.Open(SoundPath.Append('credits-outro-tune.mp3'),nil);
AudioPlayback.SetVolume(0.2);
AudioPlayback.SetLoop(true);
AudioPlayback.Play;
Expand Down Expand Up @@ -1268,10 +1268,6 @@ procedure Effect_Twinkle_Down (const Tex: TTexture; Progress: double);
GoldenRec.Spawn(NameX + RandomRange(-NameW div 2, NameW div 2), NameY - NameH/2 + (1 - Progress) * NameH, 1, 16, 0, -1, PerfectLineTwinkle, 5);
end;

{ beat detection algorithm
based on a tutorial from Frédéric Patin on gamedev.net
http://www.gamedev.net/reference/programming/features/beatdetection/default.asp }

{ calculates average value of a history buffer }
function Average(History: TEnergyHistory): single;
var I: integer;
Expand Down
6 changes: 3 additions & 3 deletions src/screens/UScreenEditSub.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ function TScreenEditSub.ParseMouse(MouseButton: Integer; BtnDown: boolean; X, Y:
SelectsS[Interactions[nBut].Num].SelectedOption := SelectsS[Interactions[nBut].Num].SelectedOption -1;
CurrentSong.Mp3 := Path(SelectsS[Interactions[nBut].Num].TextOptT[SelectsS[Interactions[nBut].Num].SelectedOption]);
AudioPlayback.Close;
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3));
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3),nil);
end;

if ((Mp3SlideId = Interactions[nBut].Num) and (Action = maRight) and (SelectsS[Interactions[nBut].Num].SelectedOption < Length(SelectsS[Interactions[nBut].Num].TextOptT)-1)) then
Expand All @@ -2801,7 +2801,7 @@ function TScreenEditSub.ParseMouse(MouseButton: Integer; BtnDown: boolean; X, Y:
SelectsS[Interactions[nBut].Num].SelectedOption := SelectsS[Interactions[nBut].Num].SelectedOption +1;
CurrentSong.Mp3 := Path(SelectsS[Interactions[nBut].Num].TextOptT[SelectsS[Interactions[nBut].Num].SelectedOption]);
AudioPlayback.Close();
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3));
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3),nil);
end;

if (((VolumeAudioSlideId = Interactions[nBut].Num) or (VolumeMidiSlideId = Interactions[nBut].Num) or (VolumeClickSlideId = Interactions[nBut].Num))
Expand Down Expand Up @@ -4933,7 +4933,7 @@ procedure TScreenEditSub.OnShow;
Tracks[TrackIndex].Lines[0].Notes[0].Color := P1_INVERTED;
end;

AudioPlayBack.Open(CurrentSong.Path.Append(CurrentSong.Mp3));
AudioPlayBack.Open(CurrentSong.Path.Append(CurrentSong.Mp3),nil);
//Set Down Music Volume for Better hearability of Midi Sounds
//Music.SetVolume(0.4);

Expand Down
2 changes: 1 addition & 1 deletion src/screens/UScreenJukebox.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,7 @@ procedure TScreenJukebox.Play();
var
I: integer;
begin
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3));
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3),nil);
AudioPlayback.SetVolume(1.0);

//AudioPlayback.Position := CurrentSong.Start;
Expand Down
2 changes: 1 addition & 1 deletion src/screens/UScreenScore.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ procedure TScreenSCore.StartPreview;
begin
AudioPlayback.Close;

if AudioPlayback.Open(CatSongs.Song[select].Path.Append(CatSongs.Song[select].Mp3)) then
if AudioPlayback.Open(CatSongs.Song[select].Path.Append(CatSongs.Song[select].Mp3),nil) then
begin
if (CatSongs.Song[select].PreviewStart > 0) then
AudioPlayback.Position := CatSongs.Song[select].PreviewStart
Expand Down
2 changes: 1 addition & 1 deletion src/screens/UScreenSong.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3776,7 +3776,7 @@ procedure TScreenSong.StartMusicPreview();
Exit;

PlayMidi := false;
if AudioPlayback.Open(Song.Path.Append(Song.Mp3)) then
if AudioPlayback.Open(Song.Path.Append(Song.Mp3),nil) then
begin
PreviewOpened := Interaction;

Expand Down
7 changes: 6 additions & 1 deletion src/screens/controllers/UScreenSingController.pas
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ function TScreenSingController.ParseInput(PressedKey: Cardinal; CharCode: UCS4Ch
Pause;
end;

SDLK_K:
begin
AudioPlayback.ToggleKaraoke;
end;

SDLK_RIGHT:
begin
if (SDL_ModState = KMOD_LCTRL) then // seek 5 seconds forward
Expand Down Expand Up @@ -857,7 +862,7 @@ procedure TScreenSingController.onShowFinish;
PlayMidi := false;
MidiFadeIn := false;

AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3));
AudioPlayback.Open(CurrentSong.Path.Append(CurrentSong.Mp3),CurrentSong.Path.Append(CurrentSong.Karaoke));
if ScreenSong.Mode = smMedley then
AudioPlayback.SetVolume(0.1)
else
Expand Down

0 comments on commit 14d9f9a

Please sign in to comment.