using System; using System.Runtime.InteropServices; namespace NAudio.Wave { /// /// WaveBuffer class use to store wave datas. Data can be manipulated with arrays /// (,,, ) that are pointing to the /// same memory buffer. Use the associated Count property based on the type of buffer to get the number of /// data in the buffer. /// Implicit casting is now supported to float[], byte[], int[], short[]. /// You must not use Length on returned arrays. /// /// n.b. FieldOffset is 8 now to allow it to work natively on 64 bit /// [StructLayout(LayoutKind.Explicit, Pack = 2)] public class WaveBuffer : IWaveBuffer { /// /// Number of Bytes /// [FieldOffset(0)] public int numberOfBytes; [FieldOffset(8)] private byte[] byteBuffer; [FieldOffset(8)] private float[] floatBuffer; [FieldOffset(8)] private short[] shortBuffer; [FieldOffset(8)] private int[] intBuffer; /// /// Initializes a new instance of the class. /// /// The number of bytes. The size of the final buffer will be aligned on 4 Bytes (upper bound) public WaveBuffer(int sizeToAllocateInBytes) { int aligned4Bytes = sizeToAllocateInBytes%4; sizeToAllocateInBytes = (aligned4Bytes == 0) ? sizeToAllocateInBytes : sizeToAllocateInBytes + 4 - aligned4Bytes; // Allocating the byteBuffer is co-allocating the floatBuffer and the intBuffer byteBuffer = new byte[sizeToAllocateInBytes]; numberOfBytes = 0; } /// /// Initializes a new instance of the class binded to a specific byte buffer. /// /// A byte buffer to bound the WaveBuffer to. public WaveBuffer(byte[] bufferToBoundTo) { BindTo(bufferToBoundTo); } /// /// Binds this WaveBuffer instance to a specific byte buffer. /// /// A byte buffer to bound the WaveBuffer to. public void BindTo(byte[] bufferToBoundTo) { /* WaveBuffer assumes the caller knows what they are doing. We will let this pass * if ( (bufferToBoundTo.Length % 4) != 0 ) { throw new ArgumentException("The byte buffer to bound must be 4 bytes aligned"); }*/ byteBuffer = bufferToBoundTo; numberOfBytes = 0; } /// /// Performs an implicit conversion from to . /// /// The wave buffer. /// The result of the conversion. public static implicit operator byte[](WaveBuffer waveBuffer) { return waveBuffer.byteBuffer; } /// /// Performs an implicit conversion from to . /// /// The wave buffer. /// The result of the conversion. public static implicit operator float[](WaveBuffer waveBuffer) { return waveBuffer.floatBuffer; } /// /// Performs an implicit conversion from to . /// /// The wave buffer. /// The result of the conversion. public static implicit operator int[](WaveBuffer waveBuffer) { return waveBuffer.intBuffer; } /// /// Performs an implicit conversion from to . /// /// The wave buffer. /// The result of the conversion. public static implicit operator short[](WaveBuffer waveBuffer) { return waveBuffer.shortBuffer; } /// /// Gets the byte buffer. /// /// The byte buffer. public byte[] ByteBuffer { get { return byteBuffer; } } /// /// Gets the float buffer. /// /// The float buffer. public float[] FloatBuffer { get { return floatBuffer; } } /// /// Gets the short buffer. /// /// The short buffer. public short[] ShortBuffer { get { return shortBuffer; } } /// /// Gets the int buffer. /// /// The int buffer. public int[] IntBuffer { get { return intBuffer; } } /// /// Gets the max size in bytes of the byte buffer.. /// /// Maximum number of bytes in the buffer. public int MaxSize { get { return byteBuffer.Length; } } /// /// Gets or sets the byte buffer count. /// /// The byte buffer count. public int ByteBufferCount { get { return numberOfBytes; } set { numberOfBytes = CheckValidityCount("ByteBufferCount", value, 1); } } /// /// Gets or sets the float buffer count. /// /// The float buffer count. public int FloatBufferCount { get { return numberOfBytes / 4; } set { numberOfBytes = CheckValidityCount("FloatBufferCount", value, 4); } } /// /// Gets or sets the short buffer count. /// /// The short buffer count. public int ShortBufferCount { get { return numberOfBytes / 2; } set { numberOfBytes = CheckValidityCount("ShortBufferCount", value, 2); } } /// /// Gets or sets the int buffer count. /// /// The int buffer count. public int IntBufferCount { get { return numberOfBytes / 4; } set { numberOfBytes = CheckValidityCount("IntBufferCount", value, 4); } } /// /// Clears the associated buffer. /// public void Clear() { Array.Clear(byteBuffer, 0, byteBuffer.Length); } /// /// Copy this WaveBuffer to a destination buffer up to ByteBufferCount bytes. /// public void Copy(Array destinationArray) { Array.Copy(byteBuffer, destinationArray, numberOfBytes); } /// /// Checks the validity of the count parameters. /// /// Name of the arg. /// The value. /// The size of value. private int CheckValidityCount(string argName, int value, int sizeOfValue) { int newNumberOfBytes = value * sizeOfValue; if ( (newNumberOfBytes % 4) != 0 ) { throw new ArgumentOutOfRangeException(argName, String.Format("{0} cannot set a count ({1}) that is not 4 bytes aligned ", argName, newNumberOfBytes)); } if (value < 0 || value > (byteBuffer.Length / sizeOfValue)) { throw new ArgumentOutOfRangeException(argName, String.Format("{0} cannot set a count that exceed max count {1}", argName, byteBuffer.Length / sizeOfValue)); } return newNumberOfBytes; } } }