Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework.Audio;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace RPGamesEngine.src {
- class SoundEngine {
- public static Dictionary<string, SoundEffect> sfx = new Dictionary<string, SoundEffect>();
- public static Dictionary<string, SoundEffect> music = new Dictionary<string, SoundEffect>();
- public static Dictionary<string, SoundEffectInstance> musicInstances = new Dictionary<string, SoundEffectInstance>();
- public static Dictionary<string, float> fadingMusic = new Dictionary<string, float>();
- private static float sfxVolume = 1;
- private static float musicVolume = 1;
- public static void addSoundEffect(string name, string path) {
- name = name.ToLower();
- FileStream fs = new FileStream(path, FileMode.Open);
- SoundEffect newSound = SoundEffect.FromStream(fs);
- sfx.Add(name, newSound);
- fs.Dispose();
- }
- public static void addMusic(string name, string path) {
- name = name.ToLower();
- FileStream fs = new FileStream(path, FileMode.Open);
- SoundEffect newMusic = SoundEffect.FromStream(fs);
- music.Add(name, newMusic);
- fs.Dispose();
- }
- public static void playSound(string name, float volume = 1) {
- name = name.ToLower();
- SoundEffect effect = sfx[name];
- SoundEffectInstance effectInstance = effect.CreateInstance();
- effectInstance.Volume = volume * sfxVolume;
- effectInstance.Play();
- }
- public static void playMusic(string name, float volume = 1, bool startFromBeginning = true, bool loop = true, float fade = 0F) {
- name = name.ToLower();
- // prevent crash from music that doesn't exist
- if (!music.ContainsKey(name)) {
- Console.Out.WriteLine("Music not Found: " + name);
- return;
- }
- // either make new effect instance or grab pre - existing one for music
- SoundEffectInstance effectInstance;
- if (musicInstances.ContainsKey(name)) {
- // grab music from pre - existing instances
- effectInstance = musicInstances[name];
- // apply settings
- effectInstance.Volume = volume * musicVolume;
- effectInstance.IsLooped = loop;
- // continue pre - existing music unless otherwise specified
- if (!startFromBeginning) effectInstance.Resume();
- else { effectInstance.Play(); }
- }
- else {
- // add music to pre - existing instances
- SoundEffect effect = music[name];
- effectInstance = effect.CreateInstance();
- musicInstances.Add(name, effectInstance);
- // apply settings and play from beginning
- effectInstance.Volume = volume * musicVolume;
- effectInstance.IsLooped = loop;
- effectInstance.Play();
- }
- // process fade capabilities
- if (fade != 0F) {
- startMusicFade(name, fade);
- }
- }
- // start or change the fade of a song
- public static void startMusicFade(string name, float rate = 1.0F) {
- name = name.ToLower();
- // prevent crash from music that doesn't exist
- if (!music.ContainsKey(name)) {
- Console.Out.WriteLine("Music not Found: " + name);
- return;
- }
- // prevent crash from music instance that doesn't exist
- if (!musicInstances.ContainsKey(name)) {
- Console.Out.WriteLine("Music instance not Found: " + name);
- return;
- }
- if (rate > 0 && musicVolume == 0) {
- musicInstances[name].Volume = 0;
- musicInstances[name].Resume();
- return;
- }
- float actualRate = rate / 60F; // convert the rate (amount per second) to the rate per tick
- if (fadingMusic.ContainsKey(name)) fadingMusic.Remove(name);
- fadingMusic.Add(name, actualRate);
- }
- public static void continueMusicFade() {
- // loop throgh each music track being faded
- List<String> toRemove = new List<String>();
- foreach (KeyValuePair<string, float> entry in fadingMusic) {
- float newVolume = musicInstances[entry.Key].Volume + entry.Value * musicVolume;
- // stop fading if rade is finished
- if (newVolume < 0F) {
- musicInstances[entry.Key].Volume = 0F;
- toRemove.Add(entry.Key);
- // pause music that is at 0% volume
- musicInstances[entry.Key].Pause();
- }
- else if (newVolume > 1F * musicVolume) {
- musicInstances[entry.Key].Volume = 1F * musicVolume;
- toRemove.Add(entry.Key);
- }
- // normal change of volume
- else {
- musicInstances[entry.Key].Volume = newVolume;
- }
- }
- // finish removal of completed fades
- foreach (string name in toRemove) { fadingMusic.Remove(name); }
- }
- public static void updateSFXVolume(float newVolume) {
- if (newVolume < 0 || newVolume > 1) return;
- sfxVolume = newVolume;
- }
- public static void updateMusicVolume(float newVolume) {
- if (newVolume < 0 || newVolume > 1) return;
- foreach (KeyValuePair<string, SoundEffectInstance> entry in musicInstances) {
- if (fadingMusic.Keys.Contains(entry.Key)) continue;
- musicInstances[entry.Key].Volume = newVolume;
- }
- musicVolume = newVolume;
- }
- // getters
- public static float getSFXVolume() { return sfxVolume; }
- public static float getMusicVolume() { return musicVolume; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement