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 float volume; public List currentlyPlayingSounds = new List(); /// /// Constructor for this class. /// public SoundManager() { this.sounds = new Dictionary(); this.musicBanks = new Dictionary(); currentlyPlayingSounds = new List(); this.volume = 1.0f; } /// /// Constructor for wav files. /// /// /// public void loadWavFile(string soundName,string pathToWav) { WavSound wav = new WavSound(soundName,pathToWav); SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName); try { this.sounds.Add(soundName, wav); } catch(Exception err) { } } /// /// Constructor for wav files. /// /// /// /// public void loadWavFile(IModHelper helper,string soundName,string pathToWav) { WavSound wav = new WavSound(helper ,soundName,pathToWav); SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName); try { this.sounds.Add(soundName, wav); } catch(Exception err) { //Sound already added so no need to worry? } } /// /// Constructor for wav files. /// /// /// /// public void loadWavFile(IModHelper helper,string songName,List pathToWav) { WavSound wav = new WavSound(helper,songName,pathToWav); SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + songName); try { this.sounds.Add(songName, wav); } catch(Exception err) { } } /// /// 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) { SimpleSoundManagerMod.ModMonitor.Log("Trying to play sound: " + soundName); foreach(var sound in this.sounds) { if (sound.Key == soundName) { SimpleSoundManagerMod.ModMonitor.Log("Time to play sound: " + soundName); var s=getSoundClone(soundName); s.play(this.volume); this.currentlyPlayingSounds.Add(s); break; } } } /// /// Play the sound with the given name and volume. /// /// /// public void playSound(string soundName,float volume=1.0f) { SimpleSoundManagerMod.ModMonitor.Log("Trying to play sound: " + soundName); foreach (var sound in this.sounds) { if (sound.Key == soundName) { SimpleSoundManagerMod.ModMonitor.Log("Time to play sound: " + soundName); var s = getSoundClone(soundName); s.play(volume); 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 swapSounds(string newSong) { this.playSound(newSong); } 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(); } } } }