Beispiel #1
0
        /// <summary>
        ///     Reads a WAV file.
        /// </summary>
        /// <param name="stream">
        ///     The stream to be read.
        /// </param>
        /// <param name="format">
        ///     The format of the WAV file.
        /// </param>
        /// <param name="data">
        ///     The WAV file data.
        /// </param>
        /// <param name="size">
        ///     The size of the WAV file data.
        /// </param>
        /// <param name="frequency">
        ///     The frequency of the WAV file.
        /// </param>
        /// <param name="loop">
        ///     Does the WAV file loop?
        /// </param>
        private static void ReadWavFile(Stream stream, out int format, out byte[] data, out int size, out int frequency, out int loop)
        {
            bool success = true;

            format    = Al.AL_FORMAT_MONO16;
            data      = null;
            size      = 0;
            frequency = 22050;
            loop      = Al.AL_FALSE;

            BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);

            try {
                WavFileHeader  fileHeader  = new WavFileHeader();
                WavChunkHeader chunkHeader = new WavChunkHeader();
                WavFormatChunk formatChunk = new WavFormatChunk();

                // Read WAV file header
                fileHeader.Id     = reader.ReadChars(4);
                fileHeader.Length = reader.ReadInt32();
                fileHeader.Type   = reader.ReadChars(4);

                if (new string(fileHeader.Id) != "RIFF" && fileHeader.Length <= 0 && new string(fileHeader.Type) != "WAVE")
                {
                    success = false;
                }
                else
                {
                    while (fileHeader.Length > 8)
                    {
                        // Read WAV chunk header
                        chunkHeader.Id     = reader.ReadChars(4);
                        chunkHeader.Length = reader.ReadInt32();

                        // Determine chunk action
                        if (new string(chunkHeader.Id) == "fmt ")
                        {
                            // Read WAV format header
                            formatChunk.Format           = reader.ReadInt16();
                            formatChunk.Channels         = reader.ReadInt16();
                            formatChunk.SamplesPerSecond = reader.ReadInt32();
                            formatChunk.BytesPerSecond   = reader.ReadInt32();
                            formatChunk.BytesPerSample   = reader.ReadInt16();
                            formatChunk.BitsPerSample    = reader.ReadInt16();

                            if (chunkHeader.Length > 16)
                            {
                                formatChunk.ExtraBytesLength = reader.ReadInt16();
                                formatChunk.ExtraBytes       = reader.ReadBytes(formatChunk.ExtraBytesLength);
                            }
                            else
                            {
                                formatChunk.ExtraBytesLength = 0;
                                formatChunk.ExtraBytes       = null;
                            }

                            if (formatChunk.Format == 0x0001)
                            {
                                if (formatChunk.Channels == 1)
                                {
                                    if (formatChunk.BitsPerSample == 8)
                                    {
                                        format = Al.AL_FORMAT_MONO8;
                                    }
                                    else
                                    {
                                        format = Al.AL_FORMAT_MONO16;
                                    }
                                }
                                else
                                {
                                    if (formatChunk.BitsPerSample == 8)
                                    {
                                        format = Al.AL_FORMAT_STEREO8;
                                    }
                                    else
                                    {
                                        format = Al.AL_FORMAT_STEREO16;
                                    }
                                }
                            }
                            frequency = formatChunk.SamplesPerSecond;
                        }
                        else if (new string(chunkHeader.Id) == "data")
                        {
                            if (formatChunk.Format == 0x0001)
                            {
                                size = chunkHeader.Length - 8;
                                data = reader.ReadBytes(size);
                            }
                        }
                        else
                        {
                            if (chunkHeader.Length <= fileHeader.Length && chunkHeader.Length > 0)
                            {
                                reader.ReadBytes(chunkHeader.Length);
                            }
                        }

                        if (chunkHeader.Length <= fileHeader.Length && chunkHeader.Length > 0)
                        {
                            fileHeader.Length -= chunkHeader.Length;
                        }
                        else
                        {
                            fileHeader.Length = 0;
                        }
                    }
                }
                success = true;
            }
            catch {
                success = false;
            }
            finally {
                reader.Close();
            }

            if (!success)
            {
                format    = -1;
                data      = null;
                size      = -1;
                frequency = -1;
                loop      = -1;
            }
        }
        /// <summary>
        ///     Reads a WAV file.
        /// </summary>
        /// <param name="stream">
        ///     The stream to be read.
        /// </param>
        /// <param name="format">
        ///     The format of the WAV file.
        /// </param>
        /// <param name="data">
        ///     The WAV file data.
        /// </param>
        /// <param name="size">
        ///     The size of the WAV file data.
        /// </param>
        /// <param name="frequency">
        ///     The frequency of the WAV file.
        /// </param>
        /// <param name="loop">
        ///     Does the WAV file loop?
        /// </param>
        private static void ReadWavFile(Stream stream, out int format, out byte[] data, out int size, out int frequency, out int loop)
        {
            bool success = true;
            format = Al.AL_FORMAT_MONO16;
            data = null;
            size = 0;
            frequency = 22050;
            loop = Al.AL_FALSE;

            BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);

            try {
                WavFileHeader fileHeader = new WavFileHeader();
                WavChunkHeader chunkHeader = new WavChunkHeader();
                WavFormatChunk formatChunk = new WavFormatChunk();

                // Read WAV file header
                fileHeader.Id = reader.ReadChars(4);
                fileHeader.Length = reader.ReadInt32();
                fileHeader.Type = reader.ReadChars(4);

                if(new string(fileHeader.Id) != "RIFF" && fileHeader.Length <= 0 && new string(fileHeader.Type) != "WAVE") {
                    success = false;
                }
                else {
                    while(fileHeader.Length > 8) {
                        // Read WAV chunk header
                        chunkHeader.Id = reader.ReadChars(4);
                        chunkHeader.Length = reader.ReadInt32();

                        // Determine chunk action
                        if(new string(chunkHeader.Id) == "fmt ") {
                            // Read WAV format header
                            formatChunk.Format = reader.ReadInt16();
                            formatChunk.Channels = reader.ReadInt16();
                            formatChunk.SamplesPerSecond = reader.ReadInt32();
                            formatChunk.BytesPerSecond = reader.ReadInt32();
                            formatChunk.BytesPerSample = reader.ReadInt16();
                            formatChunk.BitsPerSample = reader.ReadInt16();

                            if(chunkHeader.Length > 16) {
                                formatChunk.ExtraBytesLength = reader.ReadInt16();
                                formatChunk.ExtraBytes = reader.ReadBytes(formatChunk.ExtraBytesLength);
                            }
                            else {
                                formatChunk.ExtraBytesLength = 0;
                                formatChunk.ExtraBytes = null;
                            }

                            if(formatChunk.Format == 0x0001) {
                                if(formatChunk.Channels == 1) {
                                    if(formatChunk.BitsPerSample == 8) {
                                        format = Al.AL_FORMAT_MONO8;
                                    }
                                    else {
                                        format = Al.AL_FORMAT_MONO16;
                                    }
                                }
                                else {
                                    if(formatChunk.BitsPerSample == 8) {
                                        format = Al.AL_FORMAT_STEREO8;
                                    }
                                    else {
                                        format = Al.AL_FORMAT_STEREO16;
                                    }
                                }
                            }
                            frequency = formatChunk.SamplesPerSecond;
                        }
                        else if(new string(chunkHeader.Id) == "data") {
                            if(formatChunk.Format == 0x0001) {
                                size = chunkHeader.Length - 8;
                                data = reader.ReadBytes(size);
                            }
                        }
                        else {
                            if(chunkHeader.Length <= fileHeader.Length && chunkHeader.Length > 0) {
                                reader.ReadBytes(chunkHeader.Length);
                            }
                        }

                        if(chunkHeader.Length <= fileHeader.Length && chunkHeader.Length > 0) {
                            fileHeader.Length -= chunkHeader.Length;
                        }
                        else {
                            fileHeader.Length = 0;
                        }
                    }
                }
                success = true;
            }
            catch {
                success = false;
            }
            finally {
                reader.Close();
            }

            if(!success) {
                format = -1;
                data = null;
                size = -1;
                frequency = -1;
                loop = -1;
            }
        }