using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
internal static class SoundManager
{
/// An array storing the sound handles.
private static SoundHandle[] Sounds;
/// The callback function to play sounds.
private static PlaySoundDelegate PlaySoundDelegate;
/// The callback function to play sounds in a specific car.
private static PlayCarSoundDelegate PlayCarSoundDelegate;
/// The callback function to play sounds in specific cars.
private static PlayMultipleCarSoundDelegate PlayMultipleCarSoundDelegate;
/// Initializes the sound manager.
/// The number of sounds supported by the plugin.
/// The delegate to play sounds.
/// The delegate to play sounds in a specific car.
/// The delegate to play sounds in specific cars.
internal static void Initialize(int numSounds, PlaySoundDelegate soundDelegate, PlayCarSoundDelegate carSoundDelegate, PlayMultipleCarSoundDelegate multipleCarSoundDelegate)
{
Sounds = new SoundHandle[numSounds];
PlaySoundDelegate = soundDelegate;
PlayCarSoundDelegate = carSoundDelegate;
PlayMultipleCarSoundDelegate = multipleCarSoundDelegate;
}
/// Plays a sound.
/// The index of the sound to be played.
/// The volume of the sound to be played.
/// The pitch of the sound to be played.
/// Whether the sound to be played is looped.
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);
}
}
}
/// Plays a sound.
/// The index of the sound to be played.
/// The volume of the sound to be played.
/// The pitch of the sound to be played.
/// Whether the sound to be played is looped.
/// The index of the car where the sound will be played.
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);
}
}
}
/// Stops a sound.
/// The index of the sound to be stopped.
internal static void Stop(int soundIndex)
{
if (Playing(soundIndex))
{
// Stop sound
Sounds[soundIndex].Stop();
}
}
/// Whether a sound is playing.
/// The index of the sound to check.
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;
}
}
}