Пример #1
0
 private static AiffChunk ReadChunkHeader(BinaryReader br)
 {
     var chunk = new AiffChunk((uint)br.BaseStream.Position, ReadChunkName(br), ConvertInt(br.ReadBytes(4)));
     return chunk;
 }
Пример #2
0
        private static AiffChunk ReadChunkHeader(BinaryReader br)
        {
            var chunk = new AiffChunk((uint)br.BaseStream.Position, ReadChunkName(br), ConvertInt(br.ReadBytes(4)));

            return(chunk);
        }
Пример #3
0
        /// <summary>
        /// Ensures valid AIFF header and then finds data offset.
        /// </summary>
        /// <param name="stream">The stream, positioned at the start of audio data</param>
        /// <param name="format">The format found</param>
        /// <param name="dataChunkPosition">The position of the data chunk</param>
        /// <param name="dataChunkLength">The length of the data chunk</param>
        /// <param name="chunks">Additional chunks found</param>
        public static void ReadAiffHeader(System.IO.Stream stream, out WaveFormatProvider format, out long dataChunkPosition, out int dataChunkLength, List <AiffChunk> chunks)
        {
            dataChunkPosition = -1;
            format            = null;
            BinaryReader br = new BinaryReader(stream);

            if (ReadChunkName(br) != "FORM")
            {
                throw new FormatException("Not an AIFF file - no FORM header.");
            }
            uint   fileSize = ConvertInt(br.ReadBytes(4));
            string formType = ReadChunkName(br);

            if (formType != "AIFC" && formType != "AIFF")
            {
                throw new FormatException("Not an AIFF file - no AIFF/AIFC header.");
            }

            dataChunkLength = 0;

            while (br.BaseStream.Position < br.BaseStream.Length)
            {
                AiffChunk nextChunk = ReadChunkHeader(br);
                if (nextChunk.ChunkName == "COMM")
                {
                    short  numChannels     = ConvertShort(br.ReadBytes(2));
                    uint   numSampleFrames = ConvertInt(br.ReadBytes(4));
                    short  sampleSize      = ConvertShort(br.ReadBytes(2));
                    double sampleRate      = IEEE.ConvertFromIeeeExtended(br.ReadBytes(10));

                    format = new WaveFormatProvider((int)sampleRate, (int)sampleSize, (int)numChannels);

                    if (nextChunk.ChunkLength > 18 && formType == "AIFC")
                    {
                        // In an AIFC file, the compression format is tacked on to the COMM chunk
                        string compress = new string(br.ReadChars(4)).ToLower();
                        if (compress != "none")
                        {
                            throw new FormatException("Compressed AIFC is not supported.");
                        }
                        br.ReadBytes((int)nextChunk.ChunkLength - 22);
                    }
                    else
                    {
                        br.ReadBytes((int)nextChunk.ChunkLength - 18);
                    }
                }
                else if (nextChunk.ChunkName == "SSND")
                {
                    uint offset    = ConvertInt(br.ReadBytes(4));
                    uint blockSize = ConvertInt(br.ReadBytes(4));
                    dataChunkPosition = nextChunk.ChunkStart + 16 + offset;
                    dataChunkLength   = (int)nextChunk.ChunkLength - 8;

                    br.ReadBytes((int)nextChunk.ChunkLength - 8);
                }
                else
                {
                    if (chunks != null)
                    {
                        chunks.Add(nextChunk);
                    }
                    br.ReadBytes((int)nextChunk.ChunkLength);
                }

                if (nextChunk.ChunkName == "\0\0\0\0")
                {
                    break;
                }
            }

            if (format == null)
            {
                throw new FormatException("Invalid AIFF file - No COMM chunk found.");
            }
            if (dataChunkPosition == -1)
            {
                throw new FormatException("Invalid AIFF file - No SSND chunk found.");
            }
        }
Пример #4
0
        /// <summary>
        /// Ensures valid AIFF header and then finds data offset.
        /// </summary>
        /// <param name="stream">The stream, positioned at the start of audio data</param>
        /// <param name="format">The format found</param>
        /// <param name="dataChunkPosition">The position of the data chunk</param>
        /// <param name="dataChunkLength">The length of the data chunk</param>
        /// <param name="chunks">Additional chunks found</param>
        public static void ReadAiffHeader(Stream stream, out NAudio.Wave.WaveFormat format, out long dataChunkPosition, out int dataChunkLength, List <AiffChunk> chunks)
        {
            dataChunkPosition = -1;
            format            = null;
            BinaryReader br = new BinaryReader(stream);

            if (ReadChunkName(br) != "FORM")
            {
                throw new FormatException("Not an AIFF file - no FORM header.");
            }
            uint fileSize = ConvertLong(br.ReadBytes(4));

            if (ReadChunkName(br) != "AIFF")
            {
                throw new FormatException("Not an AIFF file - no AIFF header.");
            }

            dataChunkLength = 0;

            while (br.BaseStream.Position < br.BaseStream.Length)
            {
                AiffChunk nextChunk = ReadChunkHeader(br);
                if (nextChunk.chunkName == "COMM")
                {
                    short  numChannels     = ConvertShort(br.ReadBytes(2));
                    uint   numSampleFrames = ConvertLong(br.ReadBytes(4));
                    short  sampleSize      = ConvertShort(br.ReadBytes(2));
                    double sampleRate      = ConvertExtended(br.ReadBytes(10));

                    format = new NAudio.Wave.WaveFormat((int)sampleRate, (int)sampleSize, (int)numChannels);

                    br.ReadBytes((int)nextChunk.chunkLength - 18);
                }
                else if (nextChunk.chunkName == "SSND")
                {
                    uint offset    = ConvertLong(br.ReadBytes(4));
                    uint blockSize = ConvertLong(br.ReadBytes(4));
                    dataChunkPosition = nextChunk.chunkStart + 16 + offset;
                    dataChunkLength   = (int)nextChunk.chunkLength - 8;

                    br.ReadBytes((int)nextChunk.chunkLength - 8);
                }
                else
                {
                    if (chunks != null)
                    {
                        chunks.Add(nextChunk);
                    }
                    br.ReadBytes((int)nextChunk.chunkLength);
                }

                if (nextChunk.chunkName == "\0\0\0\0")
                {
                    break;
                }
                //Console.WriteLine("Read chunk {0} with length {1}", nextChunk.chunkName, nextChunk.chunkLength);
            }

            if (format == null)
            {
                throw new FormatException("Invalid AIFF file - No COMM chunk found.");
            }
            if (dataChunkPosition == -1)
            {
                throw new FormatException("Invalid AIFF file - No SSND chunk found.");
            }
        }