Example #1
0
        /// <summary>
        /// Fills the buffer with new data.
        /// </summary>
        /// <param name="index">The starting index from where to fill the buffer.</param>
        /// <param name="data">The new content of the buffers.</param>
        public void FillBuffer(int index, SoundBufferData data)
        {
            if (index < 0 || index >= this.handles.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (data.Buffers.Count > this.handles.Length)
            {
                throw new ArgumentException("This data does not fit in the buffer.", nameof(data));
            }

            for (int i = 0; i < data.Buffers.Count; i++)
            {
                this.fillBufferRaw((index + i) % this.handles.Length, data.Buffers[i], data.Format, data.SampleRate);
            }
        }
Example #2
0
        /// <summary>
        /// Extracts the bufferdata from an uncompressed wave-file.
        /// </summary>
        /// <param name="file">The file to load the data from.</param>
        /// <returns>A SoundBufferData object containing the data from the specified file.</returns>
        public static SoundBufferData FromWav(Stream file)
        {
            using (var reader = new BinaryReader(file)) {
                // RIFF header
                var signature = new string(reader.ReadChars(4));
                if (signature != "RIFF")
                {
                    throw new NotSupportedException("Specified stream is not a wave file.");
                }

                reader.ReadInt32(); // riffChunkSize

                var format = new string(reader.ReadChars(4));
                if (format != "WAVE")
                {
                    throw new NotSupportedException("Specified stream is not a wave file.");
                }

                // WAVE header
                var formatSignature = new string(reader.ReadChars(4));
                if (formatSignature != "fmt ")
                {
                    throw new NotSupportedException("Specified wave file is not supported.");
                }

                int formatChunkSize = reader.ReadInt32();
                reader.ReadInt16(); // audioFormat
                int numChannels = reader.ReadInt16();
                int sampleRate  = reader.ReadInt32();
                reader.ReadInt32(); // byteRate
                reader.ReadInt16(); // blockAlign
                int bitsPerSample = reader.ReadInt16();

                if (formatChunkSize > 16)
                {
                    reader.ReadBytes(formatChunkSize - 16);
                }

                var dataSignature = new string(reader.ReadChars(4));

                if (dataSignature != "data")
                {
                    throw new NotSupportedException("Only uncompressed wave files are supported.");
                }

                reader.ReadInt32(); // dataChunkSize

                var alFormat = SoundBufferData.getSoundFormat(numChannels, bitsPerSample);

                var       data    = reader.ReadBytes((int)reader.BaseStream.Length);
                var       buffers = new List <short[]>();
                int       count;
                int       i          = 0;
                const int bufferSize = 16384;

                while ((count = (Math.Min(data.Length, (i + 1) * bufferSize * 2) - i * bufferSize * 2) / 2) > 0)
                {
                    var buffer = new short[bufferSize];
                    SoundBufferData.convertBuffer(data, buffer, count, i * bufferSize * 2);
                    buffers.Add(buffer);
                    i++;
                }

                return(new SoundBufferData(buffers, alFormat, sampleRate));
            }
        }
Example #3
0
 /// <summary>
 /// Extracts the bufferdata from an uncompressed wave-file.
 /// </summary>
 /// <param name="file">The file to load the data from.</param>
 /// <returns>A SoundBufferData object containing the data from the specified file.</returns>
 public static SoundBufferData FromWav(string file)
 {
     return(SoundBufferData.FromWav(File.OpenRead(file)));
 }
Example #4
0
 /// <summary>
 /// Fills the buffer with new data.
 /// </summary>
 /// <param name="data">The new content of the buffers.</param>
 public void FillBuffer(SoundBufferData data)
 {
     this.FillBuffer(0, data);
 }
Example #5
0
 /// <summary>
 /// Generates a news sound buffer and fills it.
 /// </summary>
 /// <param name="data">The data to load the buffers with.</param>
 public SoundBuffer(SoundBufferData data)
     : this(data.Buffers.Count)
 {
     this.FillBuffer(data);
 }