From ff612c1bb0b427d3fcc279f4fd546b633de345ed Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Thu, 4 Apr 2019 01:46:26 -0700 Subject: [PATCH] Reworking Simple Sound Manager to be something way better. --- .../Framework/Music/Config.cs | 25 ++ .../Framework/Music/MusicManager.cs | 111 +++++++++ .../Framework/Music/MusicPack.cs | 224 ++++++++++++++++++ .../Framework/Music/SongListNode.cs | 31 +++ .../SimpleSoundManager/Framework/Sound.cs | 27 --- .../Framework/SoundManager.cs | 202 ---------------- .../SimpleSoundManager/Framework/WavSound.cs | 211 ----------------- .../SimpleSoundManager/Framework/XACTSound.cs | 118 --------- .../Framework/XactMusicPair.cs | 26 -- GeneralMods/SimpleSoundManager/ModCore.cs | 62 +++++ .../SimpleSoundManager.csproj | 17 +- .../SimpleSoundManagerMod.cs | 18 -- 12 files changed, 464 insertions(+), 608 deletions(-) create mode 100644 GeneralMods/SimpleSoundManager/Framework/Music/Config.cs create mode 100644 GeneralMods/SimpleSoundManager/Framework/Music/MusicManager.cs create mode 100644 GeneralMods/SimpleSoundManager/Framework/Music/MusicPack.cs create mode 100644 GeneralMods/SimpleSoundManager/Framework/Music/SongListNode.cs delete mode 100644 GeneralMods/SimpleSoundManager/Framework/Sound.cs delete mode 100644 GeneralMods/SimpleSoundManager/Framework/SoundManager.cs delete mode 100644 GeneralMods/SimpleSoundManager/Framework/WavSound.cs delete mode 100644 GeneralMods/SimpleSoundManager/Framework/XACTSound.cs delete mode 100644 GeneralMods/SimpleSoundManager/Framework/XactMusicPair.cs create mode 100644 GeneralMods/SimpleSoundManager/ModCore.cs delete mode 100644 GeneralMods/SimpleSoundManager/SimpleSoundManagerMod.cs diff --git a/GeneralMods/SimpleSoundManager/Framework/Music/Config.cs b/GeneralMods/SimpleSoundManager/Framework/Music/Config.cs new file mode 100644 index 00000000..ed7799a9 --- /dev/null +++ b/GeneralMods/SimpleSoundManager/Framework/Music/Config.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SimpleSoundManager.Framework.Music +{ + /// + /// Config class for the mod. + /// + public class Config + { + public bool EnableDebugLog; + + /// + /// Constructor. + /// + public Config() + { + EnableDebugLog = false; + } + + } +} diff --git a/GeneralMods/SimpleSoundManager/Framework/Music/MusicManager.cs b/GeneralMods/SimpleSoundManager/Framework/Music/MusicManager.cs new file mode 100644 index 00000000..4c83dab4 --- /dev/null +++ b/GeneralMods/SimpleSoundManager/Framework/Music/MusicManager.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Timers; +using Microsoft.Xna.Framework.Audio; +using StardewValley; + +namespace SimpleSoundManager.Framework +{ + /// Manages all music for the mod. + public class MusicManager + { + /********* + ** Fields + *********/ + /// The RNG used to select music packs and songs. + private readonly Random Random = new Random(); + + /// The delay timer between songs. + private readonly Timer Timer = new Timer(); + + + /********* + ** Accessors + *********/ + /// The loaded music packs. + public IDictionary MusicPacks { get; } = new Dictionary(); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public MusicManager() + { + + } + + /// Adds a valid xwb music pack to the list of music packs available. + /// The music pack to add. + /// Whether or not to display the process to the console. Will include information from the pack's metadata. Default:False + /// If displayLogInformation is also true this will display the name of all of the songs in the music pack when it is added in. + public void addMusicPack(MusicPack musicPack, bool displayLogInformation = false, bool displaySongs = false) + { + if (displayLogInformation) + { + if (ModCore.Config.EnableDebugLog) + { + ModCore.ModMonitor.Log("Adding music pack:"); + ModCore.ModMonitor.Log($" Name: {musicPack.Name}"); + ModCore.ModMonitor.Log($" Author: {musicPack.Manifest.Author}"); + ModCore.ModMonitor.Log($" Description: {musicPack.Manifest.Description}"); + ModCore.ModMonitor.Log($" Version Info: {musicPack.Manifest.Version}"); + } + if (displaySongs && ModCore.Config.EnableDebugLog) + { + ModCore.ModMonitor.Log(" Song List:"); + foreach (string songName in musicPack.Sounds.Keys) + ModCore.ModMonitor.Log($" {songName}"); + } + } + + this.MusicPacks.Add(musicPack.Name, musicPack); + } + + /// + /// Plays the specified sound from the music pack. + /// + /// + /// + public void playSound(string packName, string soundName) + { + if (this.MusicPacks.ContainsKey(packName)) + { + this.MusicPacks[packName].PlaySound(soundName); + } + else + { + ModCore.DebugLog("No pack with specified key/name: " + packName); + } + } + + /// + /// Stops a said sound from the music pack. + /// + /// + /// + public void stopSound(string packName,string soundName) + { + if (this.MusicPacks.ContainsKey(packName)) + { + this.MusicPacks[packName].StopSound(); + } + else + { + ModCore.DebugLog("No pack with specified key/name: " + packName); + } + } + + /// + /// Updates all music packs every so often. + /// + public void update() + { + foreach(MusicPack pack in this.MusicPacks.Values) + { + pack.update(); + } + } + } +} diff --git a/GeneralMods/SimpleSoundManager/Framework/Music/MusicPack.cs b/GeneralMods/SimpleSoundManager/Framework/Music/MusicPack.cs new file mode 100644 index 00000000..72393434 --- /dev/null +++ b/GeneralMods/SimpleSoundManager/Framework/Music/MusicPack.cs @@ -0,0 +1,224 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Xna.Framework.Audio; +using Microsoft.Xna.Framework.Graphics; +using NAudio.Vorbis; +using NAudio.Wave; +using StardewModdingAPI; +using StardewValley; + +namespace SimpleSoundManager.Framework +{ + /// A content pack which can provide music and sounds. + public class MusicPack + { + /********* + ** Fields + *********/ + /// The name of the folder which contains the saved player settings. + private readonly string DataFolderName = "data"; + + /// The name of the folder which contains available music. + private readonly string MusicFolderName = "music"; + + + /********* + ** Accessors + *********/ + /// The underlying content pack. + public IContentPack ContentPack { get; } + + /// The current song name being played, if any. + public string lastPlayedSoundName { get; private set; } + + /// The currently sound being played, if any. + public SoundEffectInstance lastPlayedSound { get; private set; } + + public Dictionary> playingSounds; + + /// The manifest info. + public IManifest Manifest => this.ContentPack.Manifest; + + /// The name of the music pack. + public string Name => this.ContentPack.Manifest.Name; + + /// The available sounds. + public Dictionary Sounds { get; } = new Dictionary(); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The underlying content pack. + public MusicPack(IContentPack contentPack) + { + this.ContentPack = contentPack; + this.playingSounds = new Dictionary>(); + this.LoadMusicFiles(); + } + + /// Play a song. + /// The song name to play. + public void PlaySound(string name) + { + // get sound + if (!this.Sounds.TryGetValue(name, out SoundEffectInstance sound)) + { + ModCore.ModMonitor.Log("An error occured where we can't find the song anymore. Weird. Please contact Omegasis with a SMAPI Log and describe when/how the event occured."); + return; + } + + // play sound + this.lastPlayedSoundName = name; + this.lastPlayedSound = sound; + this.lastPlayedSound.Play(); + + if (this.playingSounds.ContainsKey(name)) + { + this.playingSounds[name].Add(sound); + } + else + { + this.playingSounds.Add(name,new List()); + this.playingSounds[name].Add(sound); + } + + } + + /// Stop the currently playing song. + public void StopSound() + { + this.lastPlayedSound?.Stop(true); + this.lastPlayedSoundName = null; + } + + /// + /// Stops all of the currently playing sounds. + /// + public void stopAllSounds() + { + foreach (string soundName in playingSounds.Keys) + { + for (int i = 0; i < playingSounds[soundName].Count; i++) + { + if (playingSounds[soundName][i].State == SoundState.Stopped) + { + playingSounds[soundName][i].Stop(true); + continue; + } + } + } + } + + /// + /// Updates the music pack to clean up all sounds that have been stopped. + /// + public void update() + { + ///Clean up the list. + foreach(string soundName in playingSounds.Keys) + { + for(int i = 0; i < playingSounds[soundName].Count; i++) + { + if(playingSounds[soundName][i].State== SoundState.Stopped) + { + playingSounds[soundName].RemoveAt(i); + i--; + continue; + } + } + } + } + + /// Get whether the content pack is currently playing a sound. + public bool IsPlaying() + { + return this.lastPlayedSound?.State == SoundState.Playing; + } + + /// + /// Checks if there is a sound with said name playing. + /// + /// + /// + public bool IsPlaying(string SoundName) + { + return this.playingSounds[SoundName].FindAll(s => s.State== SoundState.Playing).Count>0; + } + + /********* + ** Private methods + *********/ + + /// Load in the music files from the pack's respective Directory/Songs folder. Typically Content/Music/Wav/FolderName/Songs + private void LoadMusicFiles() + { + DateTime startTime = DateTime.Now; + + DirectoryInfo songFolder = new DirectoryInfo(Path.Combine(this.ContentPack.DirectoryPath, this.MusicFolderName)); + foreach (FileInfo file in songFolder.GetFiles()) + { + // get name + string name = Path.GetFileNameWithoutExtension(file.Name); + if (this.Sounds.ContainsKey(name)) + continue; + + // load data + SoundEffect effect = null; + using (Stream waveFileStream = File.OpenRead(file.FullName)) + { + switch (file.Extension) + { + case ".wav": + effect = SoundEffect.FromStream(waveFileStream); + break; + + case ".mp3": + using (Mp3FileReader reader = new Mp3FileReader(waveFileStream)) + using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) + { + string tempPath = Path.Combine(songFolder.FullName, $"{name}.wav"); + ModCore.ModMonitor.Log($"Converting: {tempPath}"); + + WaveFileWriter.CreateWaveFile(tempPath, pcmStream); + using (Stream tempStream = File.OpenRead(tempPath)) + effect = SoundEffect.FromStream(tempStream); + File.Delete(tempPath); + } + break; + + case ".ogg": + // Credits: https://social.msdn.microsoft.com/Forums/vstudio/en-US/100a97af-2a1c-4b28-b464-d43611b9b5d6/converting-multichannel-ogg-to-stereo-wav-file?forum=csharpgeneral + using (VorbisWaveReader vorbisStream = new VorbisWaveReader(file.FullName)) + { + string tempPath = Path.Combine(songFolder.FullName, $"{name}.wav"); + ModCore.DebugLog($"Converting: {tempPath}"); + + WaveFileWriter.CreateWaveFile(tempPath, vorbisStream.ToWaveProvider16()); + using (Stream tempStream = File.OpenRead(tempPath)) + effect = SoundEffect.FromStream(tempStream); + File.Delete(tempPath); + } + break; + + default: + ModCore.ModMonitor.Log($"Unsupported file extension {file.Extension}.", LogLevel.Warn); + break; + } + } + if (effect == null) + continue; + + // add sound + SoundEffectInstance instance = effect.CreateInstance(); + this.Sounds.Add(name, instance); + } + + // log loading time + if (ModCore.Config.EnableDebugLog) + ModCore.ModMonitor.Log($"Time to load WAV music pack {this.Name}: {startTime.Subtract(DateTime.Now)}"); + } + } +} diff --git a/GeneralMods/SimpleSoundManager/Framework/Music/SongListNode.cs b/GeneralMods/SimpleSoundManager/Framework/Music/SongListNode.cs new file mode 100644 index 00000000..0465af69 --- /dev/null +++ b/GeneralMods/SimpleSoundManager/Framework/Music/SongListNode.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Linq; + +namespace SimpleSoundManager.Framework +{ + /// A class that keeps track of the trigger and the list of songs associated with that trigger. + internal class SongListNode + { + /********* + ** Accessors + *********/ + /// The trigger name for the list of songs. + public string Trigger { get; } + + /// The list of songs associated with a trigger. + public string[] SongList { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The trigger name for the list of songs. + /// The list of songs associated with a trigger. + public SongListNode(string trigger, IEnumerable songList) + { + this.Trigger = trigger; + this.SongList = songList.ToArray(); + } + } +} diff --git a/GeneralMods/SimpleSoundManager/Framework/Sound.cs b/GeneralMods/SimpleSoundManager/Framework/Sound.cs deleted file mode 100644 index 0fb68e22..00000000 --- a/GeneralMods/SimpleSoundManager/Framework/Sound.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace SimpleSoundManager.Framework -{ - /// Interface used for common sound functionality; - public interface Sound - { - /// Handles playing a sound. - void play(); - - void play(float volume); - - /// Handles pausing a song. - void pause(); - - /// Handles stopping a song. - void stop(); - - /// Handles restarting a song. - void restart(); - - /// Handles getting a clone of the song. - Sound clone(); - - string getSoundName(); - - bool isStopped(); - } -} diff --git a/GeneralMods/SimpleSoundManager/Framework/SoundManager.cs b/GeneralMods/SimpleSoundManager/Framework/SoundManager.cs deleted file mode 100644 index 7d19f501..00000000 --- a/GeneralMods/SimpleSoundManager/Framework/SoundManager.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System.Collections.Generic; -using Microsoft.Xna.Framework.Audio; -using StardewModdingAPI; -using StardewValley; - -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(); - this.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 { } - } - - /// Constructor for wav files. - public void loadWavFile(IModHelper helper, string soundName, string relativePath) - { - WavSound wav = new WavSound(helper, soundName, relativePath); - SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName); - try - { - this.sounds.Add(soundName, wav); - } - catch - { - //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 { } - } - - /// 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 = this.getMusicPack(pairName); - if (pairName == null) - return; - this.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 = this.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 = this.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(); - } - } -} diff --git a/GeneralMods/SimpleSoundManager/Framework/WavSound.cs b/GeneralMods/SimpleSoundManager/Framework/WavSound.cs deleted file mode 100644 index d7f40618..00000000 --- a/GeneralMods/SimpleSoundManager/Framework/WavSound.cs +++ /dev/null @@ -1,211 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using Microsoft.Xna.Framework.Audio; -using SimpleSoundManager.Framework; -using StardewModdingAPI; - -namespace SimpleSoundManager -{ - class WavSound : Sound - { - - /// Used to actually play the song. - DynamicSoundEffectInstance dynamicSound; - - /// Used to keep track of where in the song we are. - int position; - - int count; - - /// Used to store the info for the song. - byte[] byteArray; - - public string path; - - public string soundName; - - public bool loop; - - /// Get a raw disk path to the wav file. - public WavSound(string name, string pathToWavFile, bool loop = false) - { - this.path = pathToWavFile; - this.LoadWavFromFileToStream(); - this.soundName = name; - this.loop = loop; - } - - /// A constructor that takes a mod helper and a relative path to a wav file. - public WavSound(IModHelper modHelper, string name, string relativePath, bool loop = false) - { - string path = Path.Combine(modHelper.DirectoryPath, relativePath); - this.path = path; - this.soundName = name; - this.loop = loop; - } - - /// Constructor that is more flexible than typing an absolute path. - /// The mod helper for the mod you wish to use to load the music files from. - /// The list of folders and files that make up a complete path. - public WavSound(IModHelper modHelper, string soundName, List pathPieces, bool loop = false) - { - string dirPath = modHelper.DirectoryPath; - foreach (string str in pathPieces) - dirPath = Path.Combine(dirPath, str); - this.path = dirPath; - this.soundName = soundName; - this.loop = loop; - } - - /// Loads the .wav file from disk and plays it. - public void LoadWavFromFileToStream() - { - // Create a new SpriteBatch, which can be used to draw textures. - - string file = this.path; - Stream waveFileStream = File.OpenRead(file); //TitleContainer.OpenStream(file); - - BinaryReader reader = new BinaryReader(waveFileStream); - - int chunkID = reader.ReadInt32(); - int fileSize = reader.ReadInt32(); - int riffType = reader.ReadInt32(); - int fmtID = reader.ReadInt32(); - int fmtSize = reader.ReadInt32(); - int fmtCode = reader.ReadInt16(); - int channels = reader.ReadInt16(); - int sampleRate = reader.ReadInt32(); - int fmtAvgBPS = reader.ReadInt32(); - int fmtBlockAlign = reader.ReadInt16(); - int bitDepth = reader.ReadInt16(); - - if (fmtSize == 18) - { - // Read any extra values - int fmtExtraSize = reader.ReadInt16(); - reader.ReadBytes(fmtExtraSize); - } - - int dataID = reader.ReadInt32(); - int dataSize = reader.ReadInt32(); - - this.byteArray = reader.ReadBytes(dataSize); - - - this.dynamicSound = new DynamicSoundEffectInstance(sampleRate, (AudioChannels)channels); - this.count = this.byteArray.Length;//dynamicSound.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(1000)); - - this.dynamicSound.BufferNeeded += this.DynamicSound_BufferNeeded; - } - - void DynamicSound_BufferNeeded(object sender, EventArgs e) - { - try - { - this.dynamicSound.SubmitBuffer(this.byteArray, this.position, this.count); - } - catch { } - - this.position += this.count; - if (this.position + this.count > this.byteArray.Length) - { - - if (this.loop) - this.position = 0; - //else - // this.stop(); - } - } - - /// Used to pause the current song. - public void pause() - { - this.dynamicSound?.Pause(); - } - - /// Used to play a song. - public void play() - { - if (this.isPlaying()) - return; - - this.LoadWavFromFileToStream(); - this.dynamicSound.Play(); - } - - /// Used to play a song. - /// How lound the sound is when playing. 0~1.0f - public void play(float volume) - { - if (this.isPlaying()) - return; - - this.LoadWavFromFileToStream(); - this.dynamicSound.Volume = volume; - this.dynamicSound.Play(); - } - - - /// Used to resume the currently playing song. - public void resume() - { - this.dynamicSound?.Resume(); - } - - /// Used to stop the currently playing song. - public void stop() - { - if (this.dynamicSound != null) - { - this.dynamicSound.Stop(true); - this.dynamicSound.BufferNeeded -= this.DynamicSound_BufferNeeded; - this.position = 0; - this.count = 0; - this.byteArray = new byte[0]; - } - } - - /// Used to change from one playing song to another; - public void swap(string pathToNewWavFile) - { - this.stop(); - this.path = pathToNewWavFile; - this.play(); - } - - /// Checks if the song is currently playing. - public bool isPlaying() - { - return this.dynamicSound?.State == SoundState.Playing; - } - - /// Checks if the song is currently paused. - public bool isPaused() - { - return this.dynamicSound?.State == SoundState.Paused; - } - - /// Checks if the song is currently stopped. - public bool isStopped() - { - return this.dynamicSound?.State == SoundState.Stopped; - } - - public Sound clone() - { - return new WavSound(this.getSoundName(), this.path); - } - - public string getSoundName() - { - return this.soundName; - } - - public void restart() - { - this.stop(); - this.play(); - } - } -} diff --git a/GeneralMods/SimpleSoundManager/Framework/XACTSound.cs b/GeneralMods/SimpleSoundManager/Framework/XACTSound.cs deleted file mode 100644 index a9e6379e..00000000 --- a/GeneralMods/SimpleSoundManager/Framework/XACTSound.cs +++ /dev/null @@ -1,118 +0,0 @@ -using Microsoft.Xna.Framework.Audio; -using StardewValley; - -namespace SimpleSoundManager.Framework -{ - public class XACTSound : Sound - { - public WaveBank waveBank; - public ISoundBank soundBank; - public string soundName; - readonly WaveBank vanillaWaveBank; - readonly ISoundBank vanillaSoundBank; - readonly Cue song; - - /// Make a new Sound Manager to play and manage sounds in a modded wave bank. - /// The reference to the wave bank in the mod's asset folder. - /// The reference to the sound bank in the mod's asset folder. - public XACTSound(WaveBank newWaveBank, ISoundBank newSoundBank, string soundName) - { - this.waveBank = newWaveBank; - this.soundBank = newSoundBank; - - this.vanillaSoundBank = Game1.soundBank; - this.vanillaWaveBank = Game1.waveBank; - this.soundName = soundName; - this.song = this.soundBank.GetCue(this.soundName); - } - - /// Play a sound from the mod's wave bank. - /// The name of the sound in the mod's wave bank. This will fail if the sound doesn't exists. This is also case sensitive. - public void play(string soundName) - { - Game1.waveBank = this.waveBank; - Game1.soundBank = this.soundBank; - - if (this.song == null) return; - - this.song.Play(); - - Game1.waveBank = this.vanillaWaveBank; - Game1.soundBank = this.vanillaSoundBank; - } - - /// Pauses the first instance of this sound. - /// - public void pause(string soundName) - { - this.song?.Pause(); - } - - /// Resume the first instance of the sound that has this name. - public void resume(string soundName) - { - this.song?.Resume(); - } - - - /// Stop the first instance of the sound that has this name. - /// - public void stop(string soundName) - { - this.song?.Stop(AudioStopOptions.Immediate); - } - - /// Resumes a paused song. - public void resume() - { - this.resume(this.soundName); - } - - /// Plays this song. - public void play() - { - this.play(this.soundName); - } - - /// Plays this song. - public void play(float volume) - { - this.play(this.soundName); - } - - /// Pauses this song. - public void pause() - { - this.pause(this.soundName); - } - - /// Stops this somg. - public void stop() - { - this.stop(this.soundName); - } - - /// Restarts this song. - public void restart() - { - this.stop(); - this.play(); - } - - /// Gets a clone of this song. - public Sound clone() - { - return new XACTSound(this.waveBank, this.soundBank, this.soundName); - } - - public string getSoundName() - { - return this.soundName; - } - - public bool isStopped() - { - return this.song == null || this.song.IsStopped; - } - } -} diff --git a/GeneralMods/SimpleSoundManager/Framework/XactMusicPair.cs b/GeneralMods/SimpleSoundManager/Framework/XactMusicPair.cs deleted file mode 100644 index 84129dc1..00000000 --- a/GeneralMods/SimpleSoundManager/Framework/XactMusicPair.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.IO; -using Microsoft.Xna.Framework.Audio; -using StardewModdingAPI; -using StardewValley; - -namespace SimpleSoundManager -{ - public class XACTMusicPair - { - public WaveBank waveBank; - public ISoundBank soundBank; - - /// Create a xwb and xsb music pack pair. - /// The mod helper from the mod that will handle loading in the file. - /// A relative path to the .xwb file in the mod helper's mod directory. - /// A relative path to the .xsb file in the mod helper's mod directory. - public XACTMusicPair(IModHelper helper, string wavBankPath, string soundBankPath) - { - wavBankPath = Path.Combine(helper.DirectoryPath, wavBankPath); - soundBankPath = Path.Combine(helper.DirectoryPath, soundBankPath); - - this.waveBank = new WaveBank(Game1.audioEngine, wavBankPath); - this.soundBank = new SoundBankWrapper(new SoundBank(Game1.audioEngine, soundBankPath)); - } - } -} diff --git a/GeneralMods/SimpleSoundManager/ModCore.cs b/GeneralMods/SimpleSoundManager/ModCore.cs new file mode 100644 index 00000000..f9cee672 --- /dev/null +++ b/GeneralMods/SimpleSoundManager/ModCore.cs @@ -0,0 +1,62 @@ +using SimpleSoundManager.Framework; +using SimpleSoundManager.Framework.Music; +using StardewModdingAPI; + +namespace SimpleSoundManager +{ + public class ModCore : Mod + { + internal static IModHelper ModHelper; + internal static IMonitor ModMonitor; + internal static Config Config; + public static MusicManager MusicManager; + + /// The mod entry point, called after the mod is first loaded. + /// Provides simplified APIs for writing mods. + public override void Entry(IModHelper helper) + { + ModHelper = helper; + ModMonitor = this.Monitor; + Config = helper.ReadConfig(); + this.loadContentPacks(); + this.Helper.Events.GameLoop.OneSecondUpdateTicked += this.GameLoop_OneSecondUpdateTicked; + } + + /// + /// Update all music packs every second. + /// + /// + /// + private void GameLoop_OneSecondUpdateTicked(object sender, StardewModdingAPI.Events.OneSecondUpdateTickedEventArgs e) + { + foreach(MusicPack pack in MusicManager.MusicPacks.Values) + { + pack.update(); + } + } + + /// + /// Loads all content packs for SimpleSoundManager + /// + private void loadContentPacks() + { + MusicManager = new MusicManager(); + foreach (IContentPack contentPack in this.Helper.ContentPacks.GetOwned()) + { + this.Monitor.Log($"Reading content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}"); + MusicPack musicPack = new MusicPack(contentPack); + MusicManager.addMusicPack(musicPack, true, true); + } + } + + /// + /// Easy way to display debug logs when allowing for a check to see if they are enabled. + /// + /// The message to display. + public static void DebugLog(string s) + { + if (Config.EnableDebugLog) + ModMonitor.Log(s); + } + } +} diff --git a/GeneralMods/SimpleSoundManager/SimpleSoundManager.csproj b/GeneralMods/SimpleSoundManager/SimpleSoundManager.csproj index 4e840a0a..39ae7e70 100644 --- a/GeneralMods/SimpleSoundManager/SimpleSoundManager.csproj +++ b/GeneralMods/SimpleSoundManager/SimpleSoundManager.csproj @@ -66,6 +66,12 @@ MinimumRecommendedRules.ruleset + + 1.8.5 + + + 1.0.0 + @@ -73,12 +79,11 @@ - - - - - - + + + + + diff --git a/GeneralMods/SimpleSoundManager/SimpleSoundManagerMod.cs b/GeneralMods/SimpleSoundManager/SimpleSoundManagerMod.cs deleted file mode 100644 index 1a583b3b..00000000 --- a/GeneralMods/SimpleSoundManager/SimpleSoundManagerMod.cs +++ /dev/null @@ -1,18 +0,0 @@ -using StardewModdingAPI; - -namespace SimpleSoundManager -{ - public class SimpleSoundManagerMod : Mod - { - internal static IModHelper ModHelper; - internal static IMonitor ModMonitor; - - /// The mod entry point, called after the mod is first loaded. - /// Provides simplified APIs for writing mods. - public override void Entry(IModHelper helper) - { - ModHelper = helper; - ModMonitor = this.Monitor; - } - } -}