Example #1
0
        private void Initialize(Stream stream)
        {
            RiffParser riffParser = new RiffParser(stream);

            this.FileFormatName = "Unknown";
            if (!riffParser.MoveNext() || riffParser.Current == null)
            {
                this.ThrowInvalidFileFormat((Exception)null);
            }
            else
            {
                this.FileFormatName = (string)riffParser.Current.Type;
                if (this.FileFormatName != "WAVE" && this.FileFormatName != "XWMA")
                {
                    throw new InvalidOperationException("Unsupported " + this.FileFormatName + " file format. Only WAVE or XWMA");
                }
                riffParser.Descend();
                IList <RiffChunk> allChunks  = riffParser.GetAllChunks();
                RiffChunk         riffChunk1 = this.Chunk((IEnumerable <RiffChunk>)allChunks, "fmt ");
                if ((long)riffChunk1.Size < (long)sizeof(WaveFormat.__PcmNative))
                {
                    this.ThrowInvalidFileFormat((Exception)null);
                }
                try
                {
                    this.Format = WaveFormat.MarshalFrom(riffChunk1.GetData());
                }
                catch (InvalidOperationException ex)
                {
                    this.ThrowInvalidFileFormat((Exception)ex);
                }
                if (this.FileFormatName == "XWMA")
                {
                    if (this.Format.Encoding != WaveFormatEncoding.Wmaudio2 && this.Format.Encoding != WaveFormatEncoding.Wmaudio3)
                    {
                        this.ThrowInvalidFileFormat((Exception)null);
                    }
                    this.DecodedPacketsInfo = this.Chunk((IEnumerable <RiffChunk>)allChunks, "dpds").GetDataAsArray <uint>();
                }
                else
                {
                    switch (this.Format.Encoding)
                    {
                    case WaveFormatEncoding.Extensible:
                    case WaveFormatEncoding.Pcm:
                    case WaveFormatEncoding.Adpcm:
                    case WaveFormatEncoding.IeeeFloat:
                        break;

                    default:
                        this.ThrowInvalidFileFormat((Exception)null);
                        break;
                    }
                }
                RiffChunk riffChunk2 = this.Chunk((IEnumerable <RiffChunk>)allChunks, "data");
                this.startPositionOfData = (long)riffChunk2.DataPosition;
                this.length         = (long)riffChunk2.Size;
                this.input.Position = this.startPositionOfData;
            }
        }
Example #2
0
        /// <summary>
        /// Initializes the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        private unsafe void Initialize(Stream stream)
        {
            var parser = new RiffParser(stream);

            FileFormatName = "Unknown";

            // Parse Header
            if (!parser.MoveNext() || parser.Current == null)
            {
                ThrowInvalidFileFormat();
                return;
            }

            // Check that WAVE or XWMA header is present
            FileFormatName = parser.Current.Type;
            if (FileFormatName != "WAVE" && FileFormatName != "XWMA")
            {
                throw new InvalidOperationException("Unsupported " + FileFormatName + " file format. Only WAVE or XWMA");
            }

            // Parse inside the first chunk
            parser.Descend();

            // Get all the chunk
            var chunks = parser.GetAllChunks();

            // Get "fmt" chunk
            var fmtChunk = Chunk(chunks, "fmt ");

            if (fmtChunk.Size < sizeof(WaveFormat.__PcmNative))
            {
                ThrowInvalidFileFormat();
            }

            try
            {
                Format = WaveFormat.MarshalFrom(fmtChunk.GetData());
            }
            catch (InvalidOperationException ex)
            {
                ThrowInvalidFileFormat(ex);
            }

            // If XWMA
            if (FileFormatName == "XWMA")
            {
                // Check that format is Wma
                if (Format.Encoding != WaveFormatEncoding.Wmaudio2 && Format.Encoding != WaveFormatEncoding.Wmaudio3)
                {
                    ThrowInvalidFileFormat();
                }

                // Check for "dpds" chunk
                // Get the dpds decoded packed cumulative bytes
                var dpdsChunk = Chunk(chunks, "dpds");
                DecodedPacketsInfo = dpdsChunk.GetDataAsArray <uint>();
            }
            else
            {
                switch (Format.Encoding)
                {
                case WaveFormatEncoding.Pcm:
                case WaveFormatEncoding.IeeeFloat:
                case WaveFormatEncoding.Extensible:
                case WaveFormatEncoding.Adpcm:
                    break;

                default:
                    ThrowInvalidFileFormat();
                    break;
                }
            }

            // Check for "data" chunk
            var dataChunk = Chunk(chunks, "data");

            startPositionOfData = dataChunk.DataPosition;
            length = dataChunk.Size;

            input.Position = startPositionOfData;
        }