#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Nuclex.Audio.Formats.Wave {
/// Carries the informations stored in an extended wave format chunk
[StructLayout(LayoutKind.Sequential)]
public struct WaveFormatEx {
/// FormatTag specifier for normal, uncompressed PCM audio
public const ushort WAVEFORMATPCM = WaveFormat.WAVEFORMATPCM;
/// Size of the WaveFormatEx structure
///
/// The chunk can change size to encompass additional data that may be added
/// to the wave format by Microsoft or whoever is responsible for the wave file
/// format. The number of bytes on disk will be this size plus 8 additional
/// bytes from the chunk's header.
///
public const int Size = 18;
/// Signature by which this chunk can be recognized
public readonly static byte[] Signature = WaveFormat.Signature;
/// The basic WaveFormat structure being extended
public WaveFormat WaveFormat;
/// Number of bits for a single sample
public ushort BitsPerSample;
/// Number of bytes in extra informations appended to this structure
///
/// Also reflected in the structure size, but redundantly specific here for
/// additional programmer nuisance
///
public ushort ExtraInformationSize;
/// Reads the WaveFormat information structure from a binary stream
/// Reader from which to read the WaveFormat structure
/// Wave format structure to be written to the file
/// The total size of the structure as indicated in the size field
public static int Load(BinaryReader reader, out WaveFormatEx waveFormatEx) {
int chunkLength = WaveFormat.Load(reader, out waveFormatEx.WaveFormat);
waveFormatEx.BitsPerSample = reader.ReadUInt16();
waveFormatEx.ExtraInformationSize = reader.ReadUInt16();
// Done, return the chunk's indicated length, the caller might be interested
// in this number to either skip additional data or process it
return chunkLength;
}
/// Saves the WaveFormat information structure into a binary stream
/// Writer through which to write the WaveFormat structure
/// Wave format structure to be written to the file
///
/// The default header has a total size of 26 bytes, 18 bytes are taken up by
/// the actual chunk and 8 additional bytes by the header that specifies the
/// chunk's signature and size.
///
public static void Save(BinaryWriter writer, ref WaveFormatEx waveFormatEx) {
Save(writer, WaveFormatEx.Size, ref waveFormatEx);
}
/// Saves the WaveFormat information structure into a binary stream
/// Writer through which to write the WaveFormat structure
/// Total size of the format info chunk
/// Wave format structure to be written to the file
///
/// Note that this writes chunkSize + 8 bytes. The additional 8 bytes result from
/// the chunk's header that provides the chunk signature and size you indicate.
///
public static void Save(
BinaryWriter writer, int chunkSize, ref WaveFormatEx waveFormatEx
) {
WaveFormat.Save(writer, chunkSize, ref waveFormatEx.WaveFormat);
writer.Write((UInt16)waveFormatEx.BitsPerSample);
writer.Write((UInt16)waveFormatEx.ExtraInformationSize);
}
}
} // namespace Nuclex.Audio.Formats.Wave