Example #1
0
        private void TestWaveFormatExWithData2()
        {
            int iDataSize = 10;

            WaveFormatExWithData w1 = new WaveFormatExWithData();
            FillWave(w1, 0);

            w1.byteData = new byte[iDataSize];
            FillByteData(w1.byteData, 0);
            w1.cbSize = (short)(iDataSize);

            IntPtr ip = w1.GetPtr();
            WaveFormatExWithData w2 = WaveFormatEx.PtrToWave(ip) as WaveFormatExWithData;

            Marshal.FreeCoTaskMem(ip);

            Debug.Assert(w1 == w2);
        }
Example #2
0
        private void TestEqual()
        {
            WaveFormatEx a1 = new WaveFormatEx();
            WaveFormatEx a2 = new WaveFormatEx();
            WaveFormatEx a3 = new WaveFormatEx();
            WaveFormatEx a4 = null;

            WaveFormatExWithData b1 = new WaveFormatExWithData();
            WaveFormatExWithData b2 = new WaveFormatExWithData();
            WaveFormatExWithData b3 = new WaveFormatExWithData();
            WaveFormatEx b4 = null;

            WaveFormatExtensible c1 = new WaveFormatExtensible();
            WaveFormatExtensible c2 = new WaveFormatExtensible();
            WaveFormatExtensible c3 = new WaveFormatExtensible();
            WaveFormatEx c4 = null;

            WaveFormatExtensibleWithData d1 = new WaveFormatExtensibleWithData();
            WaveFormatExtensibleWithData d2 = new WaveFormatExtensibleWithData();
            WaveFormatExtensibleWithData d3 = new WaveFormatExtensibleWithData();
            WaveFormatEx d4 = null;

            FillWave(a1, 1);
            FillWave(a2, 1);
            FillWave(a3, 2);

            FillWave(b1, 1);
            FillWave(b2, 1);
            FillWave(b3, 2);

            FillWave(c1, 1);
            FillWave(c2, 1);
            FillWave(c3, 2);

            FillWave(d1, 1);
            FillWave(d2, 1);
            FillWave(d3, 2);

            b1.byteData = new byte[3];
            b2.byteData = new byte[3];

            d1.byteData = new byte[3];
            d2.byteData = new byte[3];

            FillByteData(b1.byteData, 2);
            FillByteData(b2.byteData, 2);

            FillByteData(d1.byteData, 2);
            FillByteData(d2.byteData, 2);

            Debug.Assert(!a1.Equals(null));
            Debug.Assert(!a1.Equals(this));
            Debug.Assert(a1.Equals(a1));
            Debug.Assert(a1.Equals(a2));
            Debug.Assert(!a1.Equals(a3));
            Debug.Assert(!a1.Equals(a4));
            Debug.Assert(a1 != null);
            Debug.Assert(null != a1);
            Debug.Assert(a1 == a2);
            Debug.Assert(a2 == a1);
            Debug.Assert(a1 != a3);
            Debug.Assert(a3 != a1);
            Debug.Assert(a4 != a1);
            Debug.Assert(a1 != a4);

            Debug.Assert(!b1.Equals(null));
            Debug.Assert(!b1.Equals(this));
            Debug.Assert(b1.Equals(b1));
            Debug.Assert(b1.Equals(b2));
            Debug.Assert(!b1.Equals(b3));
            Debug.Assert(!b1.Equals(b4));
            Debug.Assert(b1 != null);
            Debug.Assert(null != b1);
            Debug.Assert(b1 == b2);
            Debug.Assert(b2 == b1);
            Debug.Assert(b1 != b3);
            Debug.Assert(b3 != b1);
            Debug.Assert(b4 != b1);
            Debug.Assert(b1 != b4);

            Debug.Assert(!c1.Equals(null));
            Debug.Assert(!c1.Equals(this));
            Debug.Assert(c1.Equals(c1));
            Debug.Assert(c1.Equals(c2));
            Debug.Assert(!c1.Equals(c3));
            Debug.Assert(!c1.Equals(c4));
            Debug.Assert(c1 != null);
            Debug.Assert(null != c1);
            Debug.Assert(c1 == c2);
            Debug.Assert(c2 == c1);
            Debug.Assert(c1 != c3);
            Debug.Assert(c3 != c1);
            Debug.Assert(c4 != c1);
            Debug.Assert(c1 != c4);

            Debug.Assert(!d1.Equals(null));
            Debug.Assert(!d1.Equals(this));
            Debug.Assert(d1.Equals(d1));
            Debug.Assert(d1.Equals(d2));
            Debug.Assert(!d1.Equals(d3));
            Debug.Assert(!d1.Equals(d4));
            Debug.Assert(d1 != null);
            Debug.Assert(null != d1);
            Debug.Assert(d1 == d2);
            Debug.Assert(d2 == d1);
            Debug.Assert(d1 != d3);
            Debug.Assert(d3 != d1);
            Debug.Assert(d4 != d1);
            Debug.Assert(d1 != d4);

            Debug.Assert(!a1.Equals(b1));
            Debug.Assert(!a1.Equals(c1));
            Debug.Assert(!a1.Equals(d1));

            Debug.Assert(!b1.Equals(a1));
            Debug.Assert(!b1.Equals(c1));
            Debug.Assert(!b1.Equals(d1));

            Debug.Assert(!c1.Equals(a1));
            Debug.Assert(!c1.Equals(b1));
            Debug.Assert(!c1.Equals(d1));

            Debug.Assert(!d1.Equals(a1));
            Debug.Assert(!d1.Equals(b1));
            Debug.Assert(!d1.Equals(c1));

            Debug.Assert(a1 != b1);
            Debug.Assert(a1 != c1);
            Debug.Assert(a1 != d1);

            Debug.Assert(b1 != a1);
            Debug.Assert(b1 != c1);
            Debug.Assert(b1 != d1);

            Debug.Assert(c1 != a1);
            Debug.Assert(c1 != b1);
            Debug.Assert(c1 != d1);

            Debug.Assert(d1 != a1);
            Debug.Assert(d1 != b1);
            Debug.Assert(d1 != c1);
        }
        public static WaveFormatEx PtrToWave(IntPtr pNativeData)
        {
            short wFormatTag = Marshal.ReadInt16(pNativeData);
            WaveFormatEx wfe;

            // WAVE_FORMAT_EXTENSIBLE == -2
            if (wFormatTag != -2)
            {
                short cbSize;

                // By spec, PCM has no cbSize element
                if (wFormatTag != 1)
                {
                    cbSize = Marshal.ReadInt16(pNativeData, 16);
                }
                else
                {
                    cbSize = 0;
                }

                // Does the structure contain extra data?
                if (cbSize == 0)
                {
                    // Create a simple WaveFormatEx struct
                    wfe = new WaveFormatEx();
                    Marshal.PtrToStructure(pNativeData, wfe);

                    // It probably already has the right value, but there is a special case
                    // where it might not, so, just to be safe...
                    wfe.cbSize = 0;
                }
                else
                {
                    WaveFormatExWithData dat = new WaveFormatExWithData();

                    // Manually parse the data into the structure
                    dat.wFormatTag = wFormatTag;
                    dat.nChannels = Marshal.ReadInt16(pNativeData, 2);
                    dat.nSamplesPerSec = Marshal.ReadInt32(pNativeData, 4);
                    dat.nAvgBytesPerSec = Marshal.ReadInt32(pNativeData, 8);
                    dat.nBlockAlign = Marshal.ReadInt16(pNativeData, 12);
                    dat.wBitsPerSample = Marshal.ReadInt16(pNativeData, 14);
                    dat.cbSize = cbSize;

                    dat.byteData = new byte[dat.cbSize];
                    IntPtr ip2 = new IntPtr(pNativeData.ToInt64() + 18);
                    Marshal.Copy(ip2, dat.byteData, 0, dat.cbSize);

                    wfe = dat as WaveFormatEx;
                }
            }
            else
            {
                short cbSize;
                int extrasize = Marshal.SizeOf(typeof(WaveFormatExtensible)) - Marshal.SizeOf(typeof(WaveFormatEx));

                cbSize = Marshal.ReadInt16(pNativeData, 16);
                if (cbSize == extrasize)
                {
                    WaveFormatExtensible ext = new WaveFormatExtensible();
                    Marshal.PtrToStructure(pNativeData, ext);
                    wfe = ext as WaveFormatEx;
                }
                else
                {
                    WaveFormatExtensibleWithData ext = new WaveFormatExtensibleWithData();
                    int iExtraBytes = cbSize - extrasize;

                    ext.wFormatTag = wFormatTag;
                    ext.nChannels = Marshal.ReadInt16(pNativeData, 2);
                    ext.nSamplesPerSec = Marshal.ReadInt32(pNativeData, 4);
                    ext.nAvgBytesPerSec = Marshal.ReadInt32(pNativeData, 8);
                    ext.nBlockAlign = Marshal.ReadInt16(pNativeData, 12);
                    ext.wBitsPerSample = Marshal.ReadInt16(pNativeData, 14);
                    ext.cbSize = cbSize;

                    ext.wValidBitsPerSample = Marshal.ReadInt16(pNativeData, 18);
                    ext.dwChannelMask = (WaveMask)Marshal.ReadInt16(pNativeData, 20);

                    // Read the Guid
                    byte[] byteGuid = new byte[16];
                    Marshal.Copy(new IntPtr(pNativeData.ToInt64() + 24), byteGuid, 0, 16);
                    ext.SubFormat = new Guid(byteGuid);

                    ext.byteData = new byte[iExtraBytes];
                    IntPtr ip2 = new IntPtr(pNativeData.ToInt64() + Marshal.SizeOf(typeof(WaveFormatExtensible)));
                    Marshal.Copy(ip2, ext.byteData, 0, iExtraBytes);

                    wfe = ext as WaveFormatEx;
                }
            }

            return wfe;
        }