Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/LiveSplit.Sound/UI/Components/EventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace LiveSplit.UI.Components;
public enum EventType
{
Split,
SplitAheadGaining,
SplitAheadLosing,
SplitBehindGaining,
SplitBehindLosing,
BestSegment,
UndoSplit,
SkipSplit,
PersonalBest,
NotAPersonalBest,
Reset,
Pause,
Resume,
StartTimer,
}

public static class EventTypeEx
{
public static string GetName(this EventType type)
{
return type switch
{
EventType.Split => "Split:",
EventType.SplitAheadGaining => "Split (Ahead, Gaining Time):",
EventType.SplitAheadLosing => "Split (Ahead, Losing Time):",
EventType.SplitBehindGaining => "Split (Behind, Gaining Time):",
EventType.SplitBehindLosing => "Split (Behind, Losing Time):",
EventType.BestSegment => "Split (Best Segment):",
EventType.UndoSplit => "Undo Split:",
EventType.SkipSplit => "Skip Split:",
EventType.PersonalBest => "Personal Best:",
EventType.NotAPersonalBest => "Not a Personal Best:",
EventType.Reset => "Reset:",
EventType.Pause => "Pause:",
EventType.Resume => "Resume:",
EventType.StartTimer => "Start Timer:",
_ => "no name",
};
}
}
144 changes: 71 additions & 73 deletions src/LiveSplit.Sound/UI/Components/SoundComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -75,7 +74,7 @@ public override void SetSettings(XmlNode settings)

private void State_OnStart(object sender, EventArgs e)
{
PlaySound(Settings.StartTimer, Settings.StartTimerVolume);
PlaySound(EventType.StartTimer);
}

private void State_OnSplit(object sender, EventArgs e)
Expand All @@ -84,129 +83,128 @@ private void State_OnSplit(object sender, EventArgs e)
{
if (State.Run.Last().PersonalBestSplitTime[State.CurrentTimingMethod] == null || State.Run.Last().SplitTime[State.CurrentTimingMethod] < State.Run.Last().PersonalBestSplitTime[State.CurrentTimingMethod])
{
PlaySound(Settings.PersonalBest, Settings.PersonalBestVolume);
PlaySound(EventType.PersonalBest);
}
else
{
PlaySound(Settings.NotAPersonalBest, Settings.NotAPersonalBestVolume);
PlaySound(EventType.NotAPersonalBest);
}

return;
}
else
{
IList<string> paths = [];
int volume = Settings.SplitVolume;

int splitIndex = State.CurrentSplitIndex - 1;
TimeSpan? timeDifference = State.Run[splitIndex].SplitTime[State.CurrentTimingMethod] - State.Run[splitIndex].Comparisons[State.CurrentComparison][State.CurrentTimingMethod];
EventType type = EventType.Split;

int splitIndex = State.CurrentSplitIndex - 1;
TimeSpan? timeDifference = State.Run[splitIndex].SplitTime[State.CurrentTimingMethod] - State.Run[splitIndex].Comparisons[State.CurrentComparison][State.CurrentTimingMethod];

if (timeDifference != null)
if (timeDifference != null)
{
if (timeDifference < TimeSpan.Zero)
{
if (timeDifference < TimeSpan.Zero)
{
paths = Settings.SplitAheadGaining;
volume = Settings.SplitAheadGainingVolume;

if (LiveSplitStateHelper.GetPreviousSegmentDelta(State, splitIndex, State.CurrentComparison, State.CurrentTimingMethod) > TimeSpan.Zero)
{
paths = Settings.SplitAheadLosing;
volume = Settings.SplitAheadLosingVolume;
}
}
else
type = EventType.SplitAheadGaining;

if (LiveSplitStateHelper.GetPreviousSegmentDelta(State, splitIndex, State.CurrentComparison, State.CurrentTimingMethod) > TimeSpan.Zero)
{
paths = Settings.SplitBehindLosing;
volume = Settings.SplitBehindLosingVolume;

if (LiveSplitStateHelper.GetPreviousSegmentDelta(State, splitIndex, State.CurrentComparison, State.CurrentTimingMethod) < TimeSpan.Zero)
{
paths = Settings.SplitBehindGaining;
volume = Settings.SplitBehindGainingVolume;
}
type = EventType.SplitAheadLosing;
}
}

//Check for best segment
TimeSpan? curSegment = LiveSplitStateHelper.GetPreviousSegmentTime(State, splitIndex, State.CurrentTimingMethod);

if (curSegment != null)
else
{
if (State.Run[splitIndex].BestSegmentTime[State.CurrentTimingMethod] == null || curSegment < State.Run[splitIndex].BestSegmentTime[State.CurrentTimingMethod])
type = EventType.SplitBehindLosing;

if (LiveSplitStateHelper.GetPreviousSegmentDelta(State, splitIndex, State.CurrentComparison, State.CurrentTimingMethod) < TimeSpan.Zero)
{
paths = Settings.BestSegment;
volume = Settings.BestSegmentVolume;
type = EventType.SplitBehindGaining;
}
}
}

//Check for best segment
TimeSpan? curSegment = LiveSplitStateHelper.GetPreviousSegmentTime(State, splitIndex, State.CurrentTimingMethod);

if (paths.Count == 0)
if (curSegment != null)
{
if (State.Run[splitIndex].BestSegmentTime[State.CurrentTimingMethod] == null || curSegment < State.Run[splitIndex].BestSegmentTime[State.CurrentTimingMethod])
{
paths = Settings.Split;
type = EventType.BestSegment;
}
}

PlaySound(paths, volume);
if (Settings.SoundDataDictionary[type].FilePaths.Count == 0)
{
type = EventType.Split;
}

PlaySound(type);
}

private void State_OnSkipSplit(object sender, EventArgs e)
{
PlaySound(Settings.SkipSplit, Settings.SkipSplitVolume);
PlaySound(EventType.SkipSplit);
}

private void State_OnUndoSplit(object sender, EventArgs e)
{
PlaySound(Settings.UndoSplit, Settings.UndoSplitVolume);
PlaySound(EventType.UndoSplit);
}

private void State_OnPause(object sender, EventArgs e)
{
PlaySound(Settings.Pause, Settings.PauseVolume);
PlaySound(EventType.Pause);
}

private void State_OnResume(object sender, EventArgs e)
{
PlaySound(Settings.Resume, Settings.ResumeVolume);
PlaySound(EventType.Resume);
}

private void State_OnReset(object sender, TimerPhase e)
{
if (e != TimerPhase.Ended)
{
PlaySound(Settings.Reset, Settings.ResetVolume);
PlaySound(EventType.Reset);
}
}

private void PlaySound(IList<string> paths, int volume)
private void PlaySound(EventType type)
{
Player.Stop();

if (Activated)
if (!Activated)
{
string[] existingPaths = [.. paths.Where(File.Exists)];
if (existingPaths.Length == 0)
{
return;
}
return;
}

int index = Rnd.Next(0, existingPaths.Length);
string path = existingPaths[index];
Task.Factory.StartNew(() =>
string[] existingPaths = [.. Settings.SoundDataDictionary[type].FilePaths.Where(File.Exists)];
if (existingPaths.Length == 0)
{
return;
}

int index = Rnd.Next(0, existingPaths.Length);
string path = existingPaths[index];

int volume = Settings.SoundDataDictionary[type].Volume;

Task.Factory.StartNew(() =>
{
try
{
try
var audioFileReader = new AudioFileReader(path)
{
var audioFileReader = new AudioFileReader(path)
{
Volume = volume / 100f * (Settings.GeneralVolume / 100f)
};

Player.DeviceNumber = Settings.OutputDevice;
Player.Init(audioFileReader);
Player.Play();
}
catch (Exception e)
{
Log.Error(e);
}
});
}
Volume = volume / 100f * (Settings.GeneralVolume / 100f)
};

Player.DeviceNumber = Settings.OutputDevice;
Player.Init(audioFileReader);
Player.Play();
}
catch (Exception e)
{
Log.Error(e);
}
});
}

public int GetSettingsHashCode()
Expand Down
14 changes: 14 additions & 0 deletions src/LiveSplit.Sound/UI/Components/SoundData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace LiveSplit.UI.Components;
public class SoundData
{
public IList<string> FilePaths { get; set; }
public int Volume { get; set; }

public SoundData(IList<string> filePaths, int volume)
{
FilePaths = filePaths;
Volume = volume;
}
}
107 changes: 107 additions & 0 deletions src/LiveSplit.Sound/UI/Components/SoundFileSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading