Soundmanager ( fmod)

Soyez le premier à donner votre avis sur cette source.

Snippet vu 8 641 fois - Téléchargée 20 fois

Contenu du snippet

Ceci est un Sound Engine ( Moteur de sons ) permetant l'utilisation de la bibliotheque Fmod et la rendant encore plus facile a utilser.

Prochainement une sources utilisant ce moteur ^^
bonne chance

Source / Exemple :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMOD;
namespace Mehdi_Media_Player
{
    static class SoundManager
    {
        #region Fields
        
        private static FMOD.System system = null;

        private static Sound music = null;
        
        private static string currentPath;
        private static Channel Channel = null;
      
        public delegate void EndMusicEventHandler(object sender, EventArgs e);
        public delegate void ErrorEventHandler(RESULT result);

        private static CHANNEL_CALLBACK channelCallback;
        public static event EndMusicEventHandler EndMusic;
        public static event ErrorEventHandler EngineError;
    
        #endregion
        #region Properties

        public static string CurrentPath
        {
            get { return currentPath; }
            set { currentPath = value; }
        }
        public static Sound Music
        {
            get { return music;}
        }
        #endregion

        #region Init-UpDate-Release
       
        public static void Initialize(int NbChannel)
        {
            RESULT result;
                   
            result = Factory.System_Create(ref system);
            if(EngineError!=null)EngineError(result);
            
            uint version = 0;
            
            result = system.getVersion(ref version);
            if(EngineError!=null)EngineError(result);

            if (version < VERSION.number)
            {
                throw new ApplicationException("Error! You are using an old version of FMOD " + version.ToString("X") + ". This program requires " + VERSION.number.ToString("X") + ".");
            }
          
            result = system.init(NbChannel, INITFLAG.NORMAL, (IntPtr)null);
            if(EngineError!=null)EngineError(result);           
            channelCallback = new CHANNEL_CALLBACK(OnEndMusic);

        }
        public static void Update()
        {
           RESULT result= system.update();
           if (EngineError != null) EngineError(result);
        }
        public static void Release()
        {
            RESULT result= RESULT.OK;
            if (music != null)
            {
                result = music.release();
               if (EngineError != null) EngineError(result);
            }
            if(system!=null)
            result = system.release();
            if (EngineError != null) EngineError(result);
        }

        #endregion
        #region MainFunctions
        #region Play Functions

        public static void Play()
        {
            Play(currentPath);
        }
        public static void Play(string path)
        {
            Play(path, false);
        }
        public static void Play(string path, bool paused)
        {
            bool isPlaying = false;
            RESULT result= RESULT.OK;

            if (Channel != null)
            {
                //si la musique existe
                result = Channel.isPlaying(ref isPlaying);
            }
            else
            {
                isPlaying = false;
            }

            if (EngineError != null) EngineError(result);

            if ((currentPath == path) && isPlaying)//si la musique du chemin courant est entrain detre joué
            {
                return;
            }
            else if (currentPath == path)//sinon la musique du chemin courant nest pas entrain detre joué
            {
                result = system.playSound(CHANNELINDEX.FREE, music, false, ref Channel);
                if(EngineError!=null)EngineError(result);
                if(Channel!=null)
                   result= Channel.setCallback(FMOD.CHANNEL_CALLBACKTYPE.END, channelCallback, 0);
                if (EngineError != null) EngineError(result);
            }
            else
            {
                
                //si cest une nouvelle musique
                if (music != null)
                {

                    if (Channel != null)
                    {
                        Channel.stop();
                        Channel = null;
                    }
                    
                    result = music.release();
                    music = null;
                    if(EngineError!=null)EngineError(result);
                }
                
                result = system.createStream(path, MODE.SOFTWARE | MODE.CREATECOMPRESSEDSAMPLE | MODE.LOOP_OFF, ref music);
                if(EngineError!=null)EngineError(result);

                result = system.playSound(CHANNELINDEX.FREE, music, paused, ref Channel);
                if(EngineError!=null)EngineError(result);
                if (Channel != null)
                    result = Channel.setCallback(FMOD.CHANNEL_CALLBACKTYPE.END, channelCallback, 0);
                if (EngineError != null) EngineError(result);
                currentPath = path;
            }
        }
        #endregion
        #region Stop Functions
        public static void Stop()
        {
            if (Channel != null)
            {
                RESULT result = Channel.stop();
                Channel = null;
                if(EngineError!=null)EngineError(result);
            }
        }
        #endregion
        #region Pause Functions

        public static bool GetPaused()
        {
            bool pause = false;
            if (Channel != null)
            {
                RESULT result = Channel.getPaused(ref pause);
                if(EngineError!=null)EngineError(result);
            }
            return pause;
        }
        public static void SetPaused(bool stat)
        {
            if (Channel != null)
            {
                RESULT result = Channel.setPaused(stat);
                if(EngineError!=null)EngineError(result);
            }
        }
        public static void SetPaused()
        {
            bool paused = false;
            if (Channel != null)
            {
                RESULT result = Channel.getPaused(ref paused);
                if(EngineError!=null)EngineError(result);
                result = Channel.setPaused(!paused);
                if(EngineError!=null)EngineError(result);
            }
        }

        #endregion
        #region Volume Functions
        public static void SetMute()
        {
            bool mute = false;
            if (Channel != null)
            {
                RESULT result = Channel.getMute(ref mute);
                if(EngineError!=null)EngineError(result);
                result = Channel.setMute(!mute);
                if(EngineError!=null)EngineError(result);
            }
        }
        public static void SetMute(bool value)
        {
            if (Channel != null)
            {
                RESULT result = Channel.setMute(value);
                if(EngineError!=null)EngineError(result);
            }
        }
        public static bool GetMute()
        {
            bool mute = false;
            if (Channel != null)
            {
                RESULT result = Channel.getMute(ref mute);
                if(EngineError!=null)EngineError(result);
            }
            return mute;
        }
        public static float GetVolume()
        {
            float vol = 1.0f;
           
                if (Channel != null)
                {
                    RESULT result = Channel.getVolume(ref vol);
                    if (EngineError != null) EngineError(result);
                }
            return vol;
        }
        public static void SetVolume(float Value)
        {
            Value = Math.Abs(Value);
            if (Channel != null)
            {
                RESULT result = Channel.setVolume(Value);
                if (EngineError != null) EngineError(result);
            }
        }
        #endregion
        #region Frequence Functions

        public static float GetFrequency()
        {
            float frq = 0;
            if (Channel != null)
            {
                RESULT result = Channel.getFrequency(ref frq);
                if (EngineError != null) EngineError(result);
            }
            return ((frq * 100.0f) / 48000);
        }
        public static void SetFrequency(float freq)
        {
            if (Channel != null)
            {
                RESULT result = Channel.setFrequency(freq * 48000 / 100.0f);
                if(EngineError!=null)EngineError(result);
            }
        }

        #endregion
        #region Position Functions

        public static void SetPosition(uint pos)
        {
            uint ln = 0;
            if (music != null) music.getLength(ref ln, TIMEUNIT.MS);
            pos = (ln * pos / 100);
            if (Channel != null)
            {
                RESULT result = Channel.setPosition(pos, TIMEUNIT.MS);
                if(EngineError!=null)EngineError(result);
            }
        }
        public static uint GetPosition()
        {
            uint ln = 0;
            uint ms = 0;
            if (music != null)
            {
                RESULT result = music.getLength(ref ln, TIMEUNIT.MS);
                if (EngineError != null) EngineError(result);
                ms = GetMsPosition();
                ms = (100 * ms / ln);
            }
            else
                ms = 0;
            return ms;
        }
        public static void SetMsPosition(uint ms)
        {
            if (Channel != null)
            {
                RESULT result = Channel.setPosition(ms, TIMEUNIT.MS);
                if(EngineError!=null)EngineError(result);
            }
        }
        public static uint GetMsPosition()
        {
            uint ms = 0;
            if (Channel != null)
            {
                RESULT result = Channel.getPosition(ref ms, TIMEUNIT.MS);
                if(EngineError!=null)EngineError(result);
            }
            return ms;
        }
        public static uint GetLength()
        {
            uint ln = 0;
            if (music != null) music.getLength(ref ln, TIMEUNIT.MS);
            return ln;
        }

        #endregion
        #endregion
        #region Event Functions
        private static RESULT OnEndMusic(IntPtr channelraw, FMOD.CHANNEL_CALLBACKTYPE type, int command, uint commanddata1, uint commanddata2)
        {
            Channel = null;// en premier pour ne pas avoir erreur lors d'un MessageBox :) ; cas particulier
            if (EndMusic != null)
                EndMusic(currentPath, new EventArgs());
            
            return RESULT.OK;
        }
        #endregion
        #region Error Functions

        public static void ShowErrorException(RESULT result)
        {
            if (result != RESULT.OK)
            {
                throw new ApplicationException("Sound Manager Error! " + result + " - " + Error.String(result));
            }
        }

        public static void ShowErrorDialogBox(RESULT result, string AppName)
        {
            if (result != RESULT.OK)
            {
                System.Windows.Forms.MessageBox.Show("Sound Manager Error! " + result + " - " + Error.String(result), AppName, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        #endregion
        #region Other Functions

        public static bool IsPlaying()
        {
            bool ply = false;
            if (Channel != null)
            {
                RESULT result = Channel.isPlaying(ref ply);
                if(EngineError!=null)EngineError(result);
            }
            return ply;
        }

        #endregion
    }
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.