NAudio.Wave.WaveFormat.MarshalFromPtr C# (CSharp) Method

MarshalFromPtr() public static method

Helper function to retrieve a WaveFormat structure from a pointer
public static MarshalFromPtr ( IntPtr pointer ) : WaveFormat
pointer System.IntPtr WaveFormat structure
return WaveFormat
        public static WaveFormat MarshalFromPtr(IntPtr pointer)
        {
            WaveFormat waveFormat = (WaveFormat)Marshal.PtrToStructure(pointer, typeof(WaveFormat));
            switch (waveFormat.Encoding)
            {
                case WaveFormatEncoding.Pcm:
                    // can't rely on extra size even being there for PCM so blank it to avoid reading
                    // corrupt data
                    waveFormat.extraSize = 0;
                    break;
                case WaveFormatEncoding.Extensible:
                    waveFormat = (WaveFormatExtensible)Marshal.PtrToStructure(pointer, typeof(WaveFormatExtensible));
                    break;
                case WaveFormatEncoding.Adpcm:
                    waveFormat = (AdpcmWaveFormat)Marshal.PtrToStructure(pointer, typeof(AdpcmWaveFormat));
                    break;
                case WaveFormatEncoding.Gsm610:
                    waveFormat = (Gsm610WaveFormat)Marshal.PtrToStructure(pointer, typeof(Gsm610WaveFormat));
                    break;
                default:
                    if (waveFormat.ExtraSize > 0)
                    {
                        waveFormat = (WaveFormatExtraData)Marshal.PtrToStructure(pointer, typeof(WaveFormatExtraData));
                    }
                    break;
            }
            return waveFormat;            
        }

Usage Example

Example #1
0
 /// <summary>
 /// Marshal Native to Managed
 /// </summary>
 public object MarshalNativeToManaged(IntPtr pNativeData)
 {
     return(WaveFormat.MarshalFromPtr(pNativeData));
 }