FMOD Ex-based sound system for game project

We (I and the members of my project team) are creating a game for our Project work lecture. I'm creating sound system :) I went to FMOD website and took a FMOD Ex package. There were some examples for C# :) If you want to create a sound system with C# and FMOD you need to copy at least fmod.cs file from <FMOD Ex installation directory>\api\csharp. But if you did that, also grab fmod_dsp.cs and fmod_errors.cs. They might be useful latter :) And also include in your project <FMOD Ex installation directory>\api\fmodex.dll. My not finished sound system can play sounds in multiple channels :) Right now I am thinking, should I create a universal sound playing and stopping method (something like Play(string song)), but I'll see :) So, the code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.Remoting.Services; namespace NetworkMenuSystem.Sounds { class SoundSystem { #region Fields /// /// Library of the channels /// private Dictionary channels = new Dictionary(); /// /// Library of the sounds /// private Dictionary sounds = new Dictionary(); private FMOD.System system = null; private int channelsplaying = 0; private FMOD.DSP dsp = null; private string _BackgroundSound = string.Empty; private string _JumpSound = string.Empty; private string _CoalizionSound = string.Empty; #endregion #region Properties public string BackgroundSound { get { return _BackgroundSound; } set { _BackgroundSound = value; } } public string JumpSound { get { return _JumpSound; } set { _JumpSound = value; } } public string CoalizionSound { get { return _CoalizionSound; } set { _CoalizionSound = value; } } #endregion #region Initialization /// /// Initializes the sound system /// public void InitSoundSystem() { uint version = 0; FMOD.RESULT result; result = FMOD.Factory.System_Create(ref system); ERRCHECK(result); result = system.getVersion(ref version); ERRCHECK(result); if (version < FMOD.VERSION.number) { Environment.Exit(-1); } result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null); ERRCHECK(result); if (_BackgroundSound != string.Empty) { AddSound(_BackgroundSound, "BackgroundSound"); AddChannel("BackgroundChannel"); } if (_JumpSound != string.Empty) { AddSound(_JumpSound, "JumpSound"); AddChannel("JumpChannel"); } if (_CoalizionSound != string.Empty) { AddSound(_CoalizionSound, "CoalizionSound"); AddChannel("CoalizionChannel"); } } #endregion #region Methods /// /// Adds the sound to the sounds library /// /// Path to the song to be played /// The name, in which the song will be stored in the library private void AddSound(string soundPath, string soundName) { if (soundPath != string.Empty) { FMOD.Sound sound = null; FMOD.RESULT result; result = system.createSound(soundPath, FMOD.MODE.SOFTWARE | FMOD.MODE.CREATECOMPRESSEDSAMPLE, ref sound); ERRCHECK(result); sounds.Add(soundName, sound); } } /// /// Adds the channel to the channels library /// /// The name of the channel private void AddChannel(string channelName) { FMOD.Channel channel = new FMOD.Channel(); channels.Add(channelName, channel); } /// /// Starts playing the sound. If the sound is played at the moment exits . /// /// File name to be played /// ArgumentNullException - file does not exist! public void Play(string fileName, ref FMOD.Channel channel, ref FMOD.Sound sound, FMOD.MODE options) { if (channel != null) { bool isPlaying = false; channel.isPlaying(ref isPlaying); if (isPlaying) return; } FMOD.RESULT result; result = system.createSound(fileName, FMOD.MODE.SOFTWARE | FMOD.MODE.CREATECOMPRESSEDSAMPLE, ref sound); ERRCHECK(result); result = sound.setMode(options); ERRCHECK(result); result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel); ERRCHECK(result); } /// /// Stops playing the sound /// public void Stop(FMOD.Channel channel) { if (channel != null) { channel.stop(); } } /// /// Updates the sound engine /// /// Need to be called in Update(GameTime gameTime) method! public void Update() { if (system != null) { system.getChannelsPlaying(ref channelsplaying); system.update(); } } /// /// Closes the streams /// /// True - dispose, false - otherwise public void Dispose(bool disposing) { if (disposing) { FMOD.RESULT result; /* Shut down */ foreach (FMOD.Sound sound in sounds.Values) { if (sound != null) { result = sound.release(); ERRCHECK(result); } } if (system != null) { result = system.close(); ERRCHECK(result); result = system.release(); ERRCHECK(result); } } } /// /// Plays background sound /// /// KeyNotFoundException public void PlayBackgroundSound() { FMOD.Sound sound = sounds["BackgroundSound"]; FMOD.Channel channel = channels["BackgroundChannel"]; if (sound != null && channel != null) Play(_BackgroundSound, ref channel, ref sound, FMOD.MODE.LOOP_NORMAL); } /// /// Stops playing background sound /// /// KeyNotFoundException public void StopBackgroundSound() { FMOD.Channel backgroundChannel = channels["BackgroundChannel"]; if (backgroundChannel != null) { bool isPlaying = false; backgroundChannel.isPlaying(ref isPlaying); if (isPlaying) { Stop(backgroundChannel); } } } /// /// Plays coalizion sound /// /// KeyNotFoundException public void PlayCoalizionSound() { FMOD.Sound coalizionSound = sounds["CoalizionSound"]; FMOD.Channel coalizionChannel = channels["CoalizionChannel"]; if (_CoalizionSound != string.Empty) Play(_CoalizionSound, ref coalizionChannel, ref coalizionSound, FMOD.MODE.LOOP_OFF); } /// /// Stops playing coalizinion sound /// /// KeyNotFoundException public void StopCoalizionSound() { FMOD.Channel coalizionChannel = channels["CoalizionChannel"]; if (coalizionChannel != null) { bool isPlaying = false; coalizionChannel.isPlaying(ref isPlaying); if (isPlaying) { Stop(coalizionChannel); } } } /// /// Plays jump sound /// /// KeyNotFoundException public void PlayJumpSound() { FMOD.Sound jumpSound = sounds["JumpSound"]; FMOD.Channel jumpChannel = channels["JumpChannel"]; if (_JumpSound != string.Empty) Play(_JumpSound, ref jumpChannel, ref jumpSound, FMOD.MODE.LOOP_OFF); } /// /// Stops playing jump sound /// /// KeyNotFoundException public void StopJumpSound() { FMOD.Channel jumpChannel = channels["JumpChannel"]; if (jumpChannel != null) { bool isPlaying = false; jumpChannel.isPlaying(ref isPlaying); if (isPlaying) { Stop(jumpChannel); } } } /// /// Checks for errors /// /// FMOD result private void ERRCHECK(FMOD.RESULT result) { if (result != FMOD.RESULT.OK) { Environment.Exit(-1); } } #endregion } }
Well, it works. Ok, lets try to use it:
SoundSystem soundSystem = new SoundSystem(); soundSystem.BackgroundSound = "Madonna - Hung Up.mp3"; soundSystem.CoalizionSound = "Atb - Long Way Home.mp3"; soundSystem.InitSoundSystem(); soundSystem.PlayBackgroundSound(); soundSystem.PlayCoalizionSound(); Console.ReadLine(); soundSystem.Dispose(true);
As a result we have two songs being played at the same moment :D Sounds weird... :D But nice ;]

Komentarai

Is it just me or this whole 'sound system' thing looks backwards for me? Why not just to take SDL and have everything done for you? :))

SDL works for XBOX?
But still - you have to use an external library. XNA has features for playing sound, but we need more advanced features, so we need more powerful library :)

Komentuoti