Exemplo n.º 1
0
        public WaveFmtChunk Decode(IRiffChunk chunk)
        {
            var data = chunk.Data;

            return(new WaveFmtChunk
            {
                Format = Bitter.ToInt16(data, 0),
                Channels = Bitter.ToInt16(data, 2),
                SampleRate = Bitter.ToInt32(data, 4),
                ByteRate = Bitter.ToInt32(data, 8),
                BlockAlign = Bitter.ToInt16(data, 12),
                BitsPerSample = Bitter.ToInt16(data, 14),
                ExtraData = data.Length > 16 ? data.AsSpan(16).ToArray() : new byte[0]
            });
        }
Exemplo n.º 2
0
 public IRiffFormat Decode(IRiffChunk chunk)
 {
     using (var stream = new ReadOnlyMemoryStream(chunk.Data))
         using (var reader = new BinaryReader(stream))
         {
             return(new RiffFormat
             {
                 Format = reader.ReadInt16(),
                 Channels = reader.ReadInt16(),
                 SampleRate = reader.ReadInt32(),
                 ByteRate = reader.ReadInt32(),
                 BlockAlign = reader.ReadInt16(),
                 BitsPerSample = reader.ReadInt16(),
                 ExtraData = reader.ReadBytes(chunk.Data.Length - 16)
             });
         }
 }
        public RiffWaveFormat(ushort audioFormat = 1, ushort numChannels = 2, uint sampleRate = 44100, ushort bitsPerSample = 16, uint numSamples = 0)
        {
            var dataChunkSize = numSamples * numChannels * bitsPerSample / 8;

            RiffChunk      = new RiffDescriptorChunk(RiffFormat.WAVE);
            FormatSubchunk = new RiffWaveFormatSubchunk(audioFormat, numChannels, sampleRate, bitsPerSample);
            DataSubchunk   = new RiffWaveDataSubchunk(dataChunkSize);

            Chunks = new IRiffChunk[]
            {
                RiffChunk,
                FormatSubchunk,
                DataSubchunk,
            };

            var fileLength = (uint)Chunks.Sum(m =>
            {
                return(m.ChunkID.Length + m.ChunkSize.Length + m.Data.Length);
            });

            RiffChunk.FileLength = fileLength;
        }