/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="soundEffect">Sound effect</param>
        /// <param name="sourceVoice">Source voice</param>
        /// <param name="emitterDescription">Emitter description</param>
        /// <param name="listenerDescription">Listener description</param>
        /// <param name="destroyWhenFinished">Sets whether the instance must be disposed after it's finished</param>
        internal GameAudioEffectInstance(
            GameAudioEffect soundEffect,
            SourceVoice sourceVoice,
            GameAudioSourceDescription emitterDescription,
            GameAudioSourceDescription listenerDescription,
            bool destroyWhenFinished)
        {
            Effect       = soundEffect;
            voice        = sourceVoice;
            paused       = false;
            IsLooped     = false;
            volume       = 1.0f;
            pan          = 0.0f;
            pitch        = 0.0f;
            outputMatrix = null;

            Emitter  = new GameAudioEmitter(emitterDescription);
            Listener = new GameAudioListener(listenerDescription);

            voice.BufferStart += SourceVoice_BufferStart;
            voice.BufferEnd   += SourceVoice_BufferEnd;
            voice.LoopEnd     += SourceVoice_LoopEnd;

            this.destroyWhenFinished = destroyWhenFinished;
        }
Пример #2
0
        /// <summary>
        /// Gets an effect from de audio
        /// </summary>
        /// <param name="name">Effect name</param>
        /// <param name="fileName">File name</param>
        /// <returns>Returns the new created effect</returns>
        internal GameAudioEffect GetEffect(string name, string fileName)
        {
            if (effects.ContainsKey(name))
            {
                return(effects[name]);
            }

            var effect = GameAudioEffect.Load(this, name, fileName);

            effects.Add(name, effect);

            return(effect);
        }
Пример #3
0
        /// <summary>
        /// Loads a file in the audio buffer
        /// </summary>
        /// <param name="gameAudio">Game audio</param>
        /// <param name="name">Effect name</param>
        /// <param name="fileName">File name</param>
        internal static GameAudioEffect Load(GameAudio gameAudio, string name, string fileName)
        {
            GameAudioEffect effect = new GameAudioEffect(gameAudio, name);

            using (var stream = new SoundStream(File.OpenRead(fileName)))
            {
                var buffer = stream.ToDataStream();

                effect.WaveFormat         = stream.Format;
                effect.DecodedPacketsInfo = stream.DecodedPacketsInfo;
                effect.AudioBuffer        = new AudioBuffer
                {
                    Stream     = buffer,
                    AudioBytes = (int)buffer.Length,
                    Flags      = BufferFlags.EndOfStream
                };
                effect.LoopedAudioBuffer = new AudioBuffer
                {
                    Stream     = buffer,
                    AudioBytes = (int)buffer.Length,
                    Flags      = BufferFlags.EndOfStream,
                    LoopCount  = AudioBuffer.LoopInfinite,
                };
                effect.Duration = TimeSpan.Zero;
                if (stream.Format.SampleRate > 0)
                {
                    var samplesDuration = GetSamplesDuration(
                        stream.Format,
                        buffer.Length,
                        stream.DecodedPacketsInfo);

                    var milliseconds = samplesDuration * 1000 / stream.Format.SampleRate;

                    effect.Duration = TimeSpan.FromMilliseconds(milliseconds);
                }
            }

            return(effect);
        }