Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Effect name</param>
 internal GameAudioEffect(GameAudio gameAudio, string name)
 {
     this.GameAudio = gameAudio;
     this.Name      = name;
 }