/// <summary> /// Stops a said sound from the music pack. /// </summary> /// <param name="packName"></param> /// <param name="soundName"></param> 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); } }
/********* ** Private methods *********/ /// <summary>Load in the music files from the pack's respective Directory/Songs folder. Typically Content/Music/Wav/FolderName/Songs</summary> 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)}"); } }