Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.27 KB | None | 0 0
  1. using Microsoft.Xna.Framework.Audio;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace RPGamesEngine.src {
  10.     class SoundEngine {
  11.  
  12.         public static Dictionary<string, SoundEffect> sfx = new Dictionary<string, SoundEffect>();
  13.  
  14.         public static Dictionary<string, SoundEffect> music = new Dictionary<string, SoundEffect>();
  15.         public static Dictionary<string, SoundEffectInstance> musicInstances = new Dictionary<string, SoundEffectInstance>();
  16.         public static Dictionary<string, float> fadingMusic = new Dictionary<string, float>();
  17.  
  18.         private static float sfxVolume = 1;
  19.         private static float musicVolume = 1;
  20.  
  21.         public static void addSoundEffect(string name, string path) {
  22.             name = name.ToLower();
  23.  
  24.             FileStream fs = new FileStream(path, FileMode.Open);
  25.             SoundEffect newSound = SoundEffect.FromStream(fs);
  26.             sfx.Add(name, newSound);
  27.             fs.Dispose();
  28.         }
  29.  
  30.         public static void addMusic(string name, string path) {
  31.             name = name.ToLower();
  32.  
  33.             FileStream fs = new FileStream(path, FileMode.Open);
  34.             SoundEffect newMusic = SoundEffect.FromStream(fs);
  35.             music.Add(name, newMusic);
  36.             fs.Dispose();
  37.         }
  38.  
  39.         public static void playSound(string name, float volume = 1) {
  40.             name = name.ToLower();
  41.  
  42.             SoundEffect effect = sfx[name];
  43.             SoundEffectInstance effectInstance = effect.CreateInstance();
  44.             effectInstance.Volume = volume * sfxVolume;
  45.             effectInstance.Play();
  46.         }
  47.  
  48.         public static void playMusic(string name, float volume = 1, bool startFromBeginning = true, bool loop = true, float fade = 0F) {
  49.             name = name.ToLower();
  50.            
  51.             // prevent crash from music that doesn't exist
  52.             if (!music.ContainsKey(name)) {
  53.                 Console.Out.WriteLine("Music not Found: " + name);
  54.                 return;
  55.             }
  56.  
  57.             // either make new effect instance or grab pre - existing one for music
  58.             SoundEffectInstance effectInstance;
  59.             if (musicInstances.ContainsKey(name)) {
  60.                 // grab music from pre - existing instances
  61.                 effectInstance = musicInstances[name];
  62.  
  63.                 // apply settings
  64.                 effectInstance.Volume = volume * musicVolume;
  65.                 effectInstance.IsLooped = loop;
  66.  
  67.                 // continue pre - existing music unless otherwise specified
  68.                 if (!startFromBeginning) effectInstance.Resume();
  69.                 else { effectInstance.Play(); }
  70.             }
  71.             else {
  72.                 // add music to pre - existing instances
  73.                 SoundEffect effect = music[name];
  74.                 effectInstance = effect.CreateInstance();
  75.                 musicInstances.Add(name, effectInstance);
  76.  
  77.                 // apply settings and play from beginning
  78.                 effectInstance.Volume = volume * musicVolume;
  79.                 effectInstance.IsLooped = loop;
  80.                 effectInstance.Play();
  81.             }
  82.  
  83.             // process fade capabilities
  84.             if (fade != 0F) {
  85.                 startMusicFade(name, fade);
  86.             }
  87.         }
  88.  
  89.         // start or change the fade of a song
  90.         public static void startMusicFade(string name, float rate = 1.0F) {
  91.             name = name.ToLower();
  92.  
  93.             // prevent crash from music that doesn't exist
  94.             if (!music.ContainsKey(name)) {
  95.                 Console.Out.WriteLine("Music not Found: " + name);
  96.                 return;
  97.             }
  98.             // prevent crash from music instance that doesn't exist
  99.             if (!musicInstances.ContainsKey(name)) {
  100.                 Console.Out.WriteLine("Music instance not Found: " + name);
  101.                 return;
  102.             }
  103.  
  104.             if (rate > 0 && musicVolume == 0) {
  105.                 musicInstances[name].Volume = 0;
  106.                 musicInstances[name].Resume();
  107.                 return;
  108.             }
  109.  
  110.             float actualRate = rate / 60F; // convert the rate (amount per second) to the rate per tick
  111.  
  112.             if (fadingMusic.ContainsKey(name)) fadingMusic.Remove(name);
  113.             fadingMusic.Add(name, actualRate);
  114.         }
  115.  
  116.         public static void continueMusicFade() {
  117.  
  118.             // loop throgh each music track being faded
  119.             List<String> toRemove = new List<String>();
  120.            
  121.             foreach (KeyValuePair<string, float> entry in fadingMusic) {
  122.                 float newVolume = musicInstances[entry.Key].Volume + entry.Value * musicVolume;
  123.                
  124.                 // stop fading if rade is finished
  125.                 if (newVolume < 0F) {
  126.                     musicInstances[entry.Key].Volume = 0F;
  127.                     toRemove.Add(entry.Key);
  128.  
  129.                     // pause music that is at 0% volume
  130.                     musicInstances[entry.Key].Pause();
  131.                 }
  132.                 else if (newVolume > 1F * musicVolume) {
  133.                     musicInstances[entry.Key].Volume = 1F * musicVolume;
  134.                     toRemove.Add(entry.Key);
  135.                 }
  136.                 // normal change of volume
  137.                 else {
  138.                     musicInstances[entry.Key].Volume = newVolume;
  139.                 }
  140.  
  141.             }
  142.  
  143.             // finish removal of completed fades
  144.             foreach (string name in toRemove) { fadingMusic.Remove(name); }
  145.         }
  146.  
  147.         public static void updateSFXVolume(float newVolume) {
  148.             if (newVolume < 0 || newVolume > 1) return;
  149.  
  150.             sfxVolume = newVolume;
  151.         }
  152.         public static void updateMusicVolume(float newVolume) {
  153.             if (newVolume < 0 || newVolume > 1) return;
  154.  
  155.             foreach (KeyValuePair<string, SoundEffectInstance> entry in musicInstances) {
  156.                 if (fadingMusic.Keys.Contains(entry.Key)) continue;
  157.                 musicInstances[entry.Key].Volume = newVolume;
  158.             }
  159.             musicVolume = newVolume;
  160.         }
  161.  
  162.         // getters
  163.         public static float getSFXVolume() { return sfxVolume; }
  164.         public static float getMusicVolume() { return musicVolume; }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement