示例#1
0
        /// <summary>
        /// Construct a new <see cref="SoundBuffer"/>.
        /// </summary>
        /// <param name="stream">Stream that contains sound buffer data.</param>
        /// <param name="bufferSize">The sound buffer size.</param>
        public SoundBuffer(Stream stream, int bufferSize = DEFAULT_BUFFER_COUNT)
        {
            // Generate handles
            _bufferIds = AL.GenBuffers(bufferSize);
            _stream    = stream;

            // Use XRAM Extensions if available
            if (SoundSystem.XRam.IsInitialized)
            {
                SoundSystem.XRam.SetBufferMode(bufferSize, ref _bufferIds[0], XRamExtension.XRamStorage.Hardware);
            }

            // Use EFX Extensions if available
            if (SoundSystem.Efx.IsInitialized)
            {
                _filterId = SoundSystem.Efx.GenFilter();
                SoundSystem.Efx.Filter(_filterId, EfxFilteri.FilterType, (int)EfxFilterType.Lowpass);
                SoundSystem.Efx.Filter(_filterId, EfxFilterf.LowpassGain, 1);
                LowPassHFGain = 1;
            }

            // TODO: Do we really do it here?
            // Identify the buffer format if its not known yet
            byte[] headerBytes = new byte[4];

            // Reset position and read the header
            _stream.Position = 0;
            _stream.Read(headerBytes, 0, headerBytes.Length);

            // Fetch header string
            string header = Encoding.UTF8.GetString(headerBytes);

            // Go back to zero position
            _stream.Position = 0;

            if (header == "OggS")
            {
                Format = SoundFormat.Vorbis;
            }
            else if (header == "RIFF")
            {
                Format = SoundFormat.Wav;
            }
            else
            {
                _isUnknownFormat = true;
                Format           = SoundFormat.Unknown;
            }

            // Use approriate Sound Stream Reader
            if (Format == SoundFormat.Vorbis)
            {
                Reader = new VorbisStreamReader(this, false);
            }
            else if (Format == SoundFormat.Wav)
            {
                Reader = new WaveStreamReader(this, false);
            }
        }
示例#2
0
        /// <summary>
        /// Queue the Buffer Data.
        /// </summary>
        /// <param name="precache">Specify whether the buffer should be pre-cached.</param>
        protected void QueueBuffer(bool precache = false)
        {
            // Reset position of the Stream
            Stream.Seek(0, SeekOrigin.Begin);

            // Use approriate Sound Stream Reader
            if (Reader == null)
            {
                if (Format == SoundFormat.Vorbis)
                {
                    Reader = new VorbisStreamReader(this, false);
                }
                else if (Format == SoundFormat.Wav)
                {
                    Reader = new WaveStreamReader(this, false);
                }
                else if (Format == SoundFormat.Unknown)
                {
                    // You need to implement your own Sound Stream Reader
                    // Inherit ISoundStreamReader and pass it via QueueBuffer(ISoundStreamReader, bool)
                    // Or set the implementation under SoundBuffer.Reader property
                    throw new NotSupportedException("Unknown sound data buffer format.");
                }
            }

            if (precache)
            {
                // Fill first buffer synchronously
                Reader.BufferData(SoundSystem.Instance.BufferSize, Buffers[0]);

                // Here we attach the Source to the Buffer
                ALChecker.Check(() => AL.SourceQueueBuffer(Source, Buffers[0]));

                // Schedule the others buffer asynchronously as the game update
                if (Deferred)
                {
                    SoundSystem.Instance.Add(this);
                }
                else
                {
                    SoundSystem.Instance.AddUndeferredSource(Source);
                }
            }

            IsReady = true;
        }