using Microsoft.Xna.Framework.Audio; using StardewModdingAPI; using StardewValley; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleSoundManager.Framework { public class SoundManager { public Dictionary sounds; public Dictionary musicBanks; public List currentlyPlayingSounds = new List(); /// /// Constructor for this class. /// public SoundManager() { this.sounds = new Dictionary(); this.musicBanks = new Dictionary(); currentlyPlayingSounds = new List(); } /// /// Constructor for wav files. /// /// /// public void loadWavFile(string soundName,string pathToWav) { WavSound wav = new WavSound(soundName,pathToWav); this.sounds.Add(soundName,wav); } /// /// Constructor for wav files. /// /// /// /// public void loadWavFile(IModHelper helper,string soundName,string pathToWav) { WavSound wav = new WavSound(helper ,soundName,pathToWav); this.sounds.Add(soundName,wav); } /// /// Constructor for wav files. /// /// /// /// public void loadWavFile(IModHelper helper,string songName,List pathToWav) { WavSound wav = new WavSound(helper,songName,pathToWav); this.sounds.Add(songName,wav); } /// /// Constructor for XACT files. /// /// /// /// public void loadXACTFile(WaveBank waveBank, ISoundBank soundBank, string songName) { XACTSound xactSound = new XACTSound(waveBank, soundBank, songName); this.sounds.Add(songName, xactSound); } /// /// Constructor for XACT files based on already added music packs. /// /// /// public void loadXACTFile(string pairName, string songName) { XACTMusicPair musicPair = getMusicPack(pairName); if (pairName == null) { return; } loadXACTFile(musicPair.waveBank, musicPair.soundBank, songName); } /// /// Creates a music pack pair that holds .xwb and .xsb music files. /// /// The mod's helper that will handle the path of the files. /// The name of this music pack pair. /// The relative path to the .xwb file /// The relative path to the .xsb file public void loadXACTMusicBank(IModHelper helper,string pairName,string wavName, string soundName) { this.musicBanks.Add(pairName,new XACTMusicPair(helper, wavName, soundName)); } /// /// Gets the music pack pair from the sound pool. /// /// /// public XACTMusicPair getMusicPack(string name) { foreach(var pack in this.musicBanks) { if (name == pack.Key) return pack.Value; } return null; } /// /// Gets a clone of the loaded sound. /// /// /// public Sound getSoundClone(string name) { foreach(var sound in this.sounds) { if (sound.Key == name) return sound.Value.clone(); } return null; } /// /// Play the sound with the given name. /// /// public void playSound(string soundName) { foreach(var sound in this.sounds) { if (sound.Key == soundName) { var s=getSoundClone(soundName); s.play(); this.currentlyPlayingSounds.Add(s); break; } } } /// /// Stop the sound that is playing. /// /// public void stopSound(string soundName) { List removalList = new List(); foreach (var sound in this.currentlyPlayingSounds) { if (sound.getSoundName() == soundName) { sound.stop(); removalList.Add(sound); } } foreach(var v in removalList) { this.currentlyPlayingSounds.Remove(v); } } /// /// Pause the sound with this name? /// /// public void pauseSound(string soundName) { List removalList = new List(); foreach (var sound in this.currentlyPlayingSounds) { if (sound.getSoundName() == soundName) { sound.pause(); removalList.Add(sound); } } foreach (var v in removalList) { this.currentlyPlayingSounds.Remove(v); } } public void update() { List removalList = new List(); foreach(Sound song in this.currentlyPlayingSounds) { if (song.isStopped()) { removalList.Add(song); } } foreach(var v in removalList) { this.currentlyPlayingSounds.Remove(v); } } public void stopAllSounds() { foreach(var v in this.currentlyPlayingSounds) { v.stop(); } } } }