예제 #1
0
        /// <summary>
        /// Read the RIFF.
        /// </summary>
        /// <param name="r">The reader.</param>
        public override void Read(FileReader r)
        {
            //Init.
            using (RiffReader rr = new RiffReader(r.BaseStream)) {
                //Format.
                rr.OpenChunk(rr.Chunks.Where(x => x.Magic.Equals("fmt ")).FirstOrDefault());
                if (rr.ReadUInt16() != 1)
                {
                    throw new Exception("Unexpected standard WAV data format.");
                }
                int numChannels = rr.ReadUInt16();
                SampleRate = rr.ReadUInt32();
                rr.ReadUInt32(); //Byte rate.
                rr.ReadUInt16(); //Blocks.
                ushort bitsPerSample = rr.ReadUInt16();
                LoopStart = 0;
                LoopEnd   = 0;
                Loops     = false;
                if (bitsPerSample != 8 && bitsPerSample != 16)
                {
                    throw new Exception("This tool only accepts 8-bit or 16-bit WAV files.");
                }

                //Sample.
                var smpl = rr.Chunks.Where(x => x.Magic.Equals("smpl")).FirstOrDefault();
                if (smpl != null)
                {
                    rr.OpenChunk(smpl);
                    rr.ReadUInt32s(7);
                    Loops = rr.ReadUInt32() > 0;
                    if (Loops)
                    {
                        rr.ReadUInt32s(3);
                        LoopStart = r.ReadUInt32(); //(uint)(r.ReadUInt32() / (bitsPerSample / 8));
                        LoopEnd   = r.ReadUInt32(); //(uint)(r.ReadUInt32() / (bitsPerSample / 8));
                    }
                }

                //Data.
                rr.OpenChunk(rr.Chunks.Where(x => x.Magic.Equals("data")).FirstOrDefault());
                uint dataSize  = rr.Chunks.Where(x => x.Magic.Equals("data")).FirstOrDefault().Size;
                uint numBlocks = (uint)(dataSize / numChannels / (bitsPerSample / 8));
                r.Position = rr.Position;
                Audio.Read(r, bitsPerSample == 16 ? typeof(PCM16) : typeof(PCM8), numChannels, numBlocks, (uint)bitsPerSample / 8, 1, (uint)bitsPerSample / 8, 1, 0);
                Audio.ChangeBlockSize(-1);
            }
        }