openbve-fcmb-train-plugin/src/Managers/SoundManager.cs
2024-11-03 18:01:18 +01:00

107 lines
4.6 KiB
C#

using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
internal static class SoundManager
{
/// <summary>An array storing the sound handles.</summary>
private static SoundHandle[] Sounds;
/// <summary>The callback function to play sounds.</summary>
private static PlaySoundDelegate PlaySoundDelegate;
/// <summary>The callback function to play sounds in a specific car.</summary>
private static PlayCarSoundDelegate PlayCarSoundDelegate;
/// <summary>The callback function to play sounds in specific cars.</summary>
private static PlayMultipleCarSoundDelegate PlayMultipleCarSoundDelegate;
/// <summary>Initializes the sound manager.</summary>
/// <param name="numSounds">The number of sounds supported by the plugin.</param>
/// <param name="soundDelegate">The delegate to play sounds.</param>
/// <param name="carSoundDelegate">The delegate to play sounds in a specific car.</param>
/// <param name="multipleCarSoundDelegate">The delegate to play sounds in specific cars.</param>
internal static void Initialize(int numSounds, PlaySoundDelegate soundDelegate, PlayCarSoundDelegate carSoundDelegate, PlayMultipleCarSoundDelegate multipleCarSoundDelegate)
{
Sounds = new SoundHandle[numSounds];
PlaySoundDelegate = soundDelegate;
PlayCarSoundDelegate = carSoundDelegate;
PlayMultipleCarSoundDelegate = multipleCarSoundDelegate;
}
/// <summary>Plays a sound.</summary>
/// <param name="soundIndex">The index of the sound to be played.</param>
/// <param name="volume">The volume of the sound to be played.</param>
/// <param name="pitch">The pitch of the sound to be played.</param>
/// <param name="looped">Whether the sound to be played is looped.</param>
internal static void Play(int soundIndex, double volume, double pitch, bool looped)
{
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0)
{
if (Playing(soundIndex))
{
// The sound is still playing, update volume and pitch
Sounds[soundIndex].Volume = volume;
Sounds[soundIndex].Pitch = pitch;
}
else
{
// The sound is not playing, play it now
Sounds[soundIndex] = PlaySoundDelegate(soundIndex, volume, pitch, looped);
}
}
}
/// <summary>Plays a sound.</summary>
/// <param name="soundIndex">The index of the sound to be played.</param>
/// <param name="volume">The volume of the sound to be played.</param>
/// <param name="pitch">The pitch of the sound to be played.</param>
/// <param name="looped">Whether the sound to be played is looped.</param>
/// <param name="car">The index of the car where the sound will be played.</param>
internal static void Play(int soundIndex, double volume, double pitch, bool looped, int car)
{
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0)
{
if (Playing(soundIndex))
{
// The sound is still playing, update volume and pitch
Sounds[soundIndex].Volume = volume;
Sounds[soundIndex].Pitch = pitch;
}
else
{
// The sound is not playing, play it now
Sounds[soundIndex] = PlayCarSoundDelegate(soundIndex, volume, pitch, looped, car);
}
}
}
/// <summary>Stops a sound.</summary>
/// <param name="soundIndex">The index of the sound to be stopped.</param>
internal static void Stop(int soundIndex)
{
if (Playing(soundIndex))
{
// Stop sound
Sounds[soundIndex].Stop();
}
}
/// <summary>Whether a sound is playing.</summary>
/// <param name="soundIndex">The index of the sound to check.</param>
internal static bool Playing(int soundIndex)
{
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0 && Sounds[soundIndex] != null)
{
if (Sounds[soundIndex].Playing)
{
return true;
}
}
return false;
}
}
}