示例#1
0
        protected override WaveStructure ReadFile(Stream stream, bool readAudioData = true)
        {
            var structure = new WaveStructure();
            var parser    = new RiffParser {
                ReadDataChunk = readAudioData
            };

            parser.ParseRiff(stream);

            ValidateWaveFile(parser);

            WaveFmtChunk  fmt  = parser.GetSubChunk <WaveFmtChunk>("fmt ");
            WaveDataChunk data = parser.GetSubChunk <WaveDataChunk>("data");
            WaveSmplChunk smpl = parser.GetSubChunk <WaveSmplChunk>("smpl");

            int bytesPerSample = fmt.BitsPerSample.DivideByRoundUp(8);

            structure.RiffSubChunks = parser.GetAllSubChunks();
            structure.SampleCount   = data.SubChunkSize / bytesPerSample / fmt.ChannelCount;
            structure.SampleRate    = fmt.SampleRate;
            structure.BitsPerSample = fmt.BitsPerSample;
            structure.ChannelCount  = fmt.ChannelCount;

            if (smpl?.Loops?.FirstOrDefault() != null)
            {
                structure.LoopStart = smpl.Loops[0].Start;
                structure.LoopEnd   = smpl.Loops[0].End;
                structure.Looping   = structure.LoopEnd > structure.LoopStart;
            }

            if (!readAudioData)
            {
                return(structure);
            }

            switch (fmt.BitsPerSample)
            {
            case 16:
                structure.AudioData16 = data.Data.InterleavedByteToShort(fmt.ChannelCount);
                break;

            case 8:
                structure.AudioData8 = data.Data.DeInterleave(bytesPerSample, fmt.ChannelCount);
                break;
            }
            return(structure);
        }