1 using OpenBveApi.Runtime; 2 3 namespace Plugin { 4 internal class Sounds { 5 6 // --- classes --- 7 8 /// <summary>Represents a looping sound.</summary> 9 internal class Sound { 10 internal readonly int Index; 11 internal SoundHandle Handle; 12 internal bool IsToBePlayed; Sound(int index)13 internal Sound(int index) { 14 this.Index = index; 15 this.Handle = null; 16 } Play()17 internal void Play() { 18 this.IsToBePlayed = true; 19 } 20 } 21 22 23 // --- members --- 24 25 private readonly PlaySoundDelegate PlaySound; 26 27 28 // --- looping sounds --- 29 30 internal readonly Sound AtsBell; 31 32 internal readonly Sound AtsChime; 33 34 internal readonly Sound Eb; 35 36 private readonly Sound[] LoopingSounds; 37 38 39 // --- play once sounds --- 40 41 internal readonly Sound AtsPBell; 42 43 internal readonly Sound AtcBell; 44 45 internal readonly Sound ToAts; 46 47 internal readonly Sound ToAtc; 48 49 private readonly Sound[] PlayOnceSounds; 50 51 52 // --- constructors --- 53 54 /// <summary>Creates a new instance of sounds.</summary> 55 /// <param name="playSound">The delegate to the function to play sounds.</param> Sounds(PlaySoundDelegate playSound)56 internal Sounds(PlaySoundDelegate playSound) { 57 this.PlaySound = playSound; 58 // --- looping --- 59 this.AtsBell = new Sound(0); 60 this.AtsChime = new Sound(1); 61 this.Eb = new Sounds.Sound(5); 62 this.LoopingSounds = new[] { this.AtsBell, this.AtsChime, this.Eb }; 63 // --- play once --- 64 this.AtsPBell = new Sound(2); 65 this.AtcBell = new Sound(2); 66 this.ToAts = new Sound(3); 67 this.ToAtc = new Sound(4); 68 this.PlayOnceSounds = new[] { this.AtsPBell, this.AtcBell, this.ToAts, this.ToAtc }; 69 } 70 71 72 // --- functions --- 73 74 /// <summary>Is called every frame.</summary> 75 /// <param name="data">The data.</param> Elapse(ElapseData data)76 internal void Elapse(ElapseData data) { 77 foreach (Sound sound in this.LoopingSounds) { 78 if (sound.IsToBePlayed) { 79 if (sound.Handle == null || sound.Handle.Stopped) { 80 sound.Handle = PlaySound(sound.Index, 1.0, 1.0, true); 81 } 82 } else { 83 if (sound.Handle != null && sound.Handle.Playing) { 84 sound.Handle.Stop(); 85 } 86 } 87 sound.IsToBePlayed = false; 88 } 89 foreach (Sound sound in this.PlayOnceSounds) { 90 if (sound.IsToBePlayed) { 91 PlaySound(sound.Index, 1.0, 1.0, false); 92 sound.IsToBePlayed = false; 93 } 94 } 95 } 96 97 } 98 }