Ejemplo n.º 1
0
        /// <summary>
        /// Extract the bufferdata from an ogg-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 FromOgg(Stream file)
        {
            var buffers = new List <short[]>();

            ALFormat format;
            int      sampleRate;

            using (var vorbis = new VorbisReader(file, true))
            {
                // Save format and samplerate for playback
                format     = vorbis.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16;
                sampleRate = vorbis.SampleRate;

                var buffer = new float[16384];
                int count;

                while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0)
                {
                    // Sample value range is -0.99999994f to 0.99999994f
                    // Samples are interleaved (chan0, chan1, chan0, chan1, etc.)

                    // Use the OggStreamer method to cast to the right format
                    var castBuffer = new short[count];
                    SoundBufferData.CastBuffer(buffer, castBuffer, count);
                    buffers.Add(castBuffer);
                }
            }

            return(new SoundBufferData(buffers, format, sampleRate));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fills the buffers of the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="bufferId">The id of the buffer to be filled.</param>
        /// <returns></returns>
        public bool FillBuffer(OggStream stream, int bufferId)
        {
            int readSamples;

            lock (this.readMutex)
            {
                readSamples = stream.Reader.ReadSamples(this.readSampleBuffer, 0, this.BufferSize);
                SoundBufferData.CastBuffer(this.readSampleBuffer, this.castBuffer, readSamples);
            }
            if (readSamples > 0)
            {
                AL.BufferData(bufferId, stream.Reader.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16, this.castBuffer,
                              readSamples * sizeof(short), stream.Reader.SampleRate);
                ALHelper.Check();
            }

            return(readSamples != this.BufferSize);
        }
Ejemplo n.º 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)));
 }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Loads a new soundfile from an uncompressed wave-file.
 /// </summary>
 /// <param name="stream">The filestream containing the sound effect in wave-format.</param>
 public static SoundFile FromWav(Stream stream)
 {
     return(new SoundFile(SoundBufferData.FromWav(stream)));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Loads a new soundfile from an uncompressed wave-file.
 /// </summary>
 /// <param name="file">The filename of the uncompressed wave-file that contains the sound effect.</param>
 public static SoundFile FromWav(string file)
 {
     return(new SoundFile(SoundBufferData.FromWav(file)));
 }
Ejemplo n.º 7
0
 private SoundFile(SoundBufferData bufferData)
 {
     this.buffer = SoundBuffer.FromData(bufferData);
 }
Ejemplo n.º 8
0
 public static void CastBuffer(float[] inBuffer, short[] outBuffer, int length)
 {
     SoundBufferData.CastBuffer(inBuffer, outBuffer, length);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a new soundbuffer from an uncompressed wave-file.
 /// </summary>
 /// <param name="file">The file to load the data from.</param>
 /// <returns>A SoundBuffer object containing the data from the specified file.</returns>
 public static SoundBuffer FromWav(Stream file)
 {
     return(SoundBuffer.FromData(SoundBufferData.FromWav(file)));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new soundbuffer from preloaded sound data.
 /// </summary>
 /// <returns>A SoundBuffer object containing the specified sound data.</returns>
 /// <param name="data">The preloaded sound data.</param>
 public static SoundBuffer FromData(SoundBufferData data)
 {
     return(new SoundBuffer(data.Buffers, data.Format, data.SampleRate));
 }