using System; using System.Linq; using System.Runtime.InteropServices; namespace NAudio.Dmo.Effect { internal struct DsFxGargle { public uint RateHz; public GargleWaveShape WaveShape; } [ComImport, System.Security.SuppressUnmanagedCodeSecurity, Guid("d616f352-d622-11ce-aac5-0020af0b99a3"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDirectSoundFXGargle { [PreserveSig] int SetAllParameters([In] ref DsFxGargle param); [PreserveSig] int GetAllParameters(out DsFxGargle param); } /// /// DMO Gargle Effect /// public class DmoGargle : IDmoEffector { /// /// DMO Gargle Params /// public struct Params { /// /// DSFXGARGLE_RATEHZ_MIN /// public const uint RateHzMin = 1; /// /// DSFXGARGLE_RATEHZ_MAX /// public const uint RateHzMax = 1000; /// /// DSFXGARGLE_RATEHZ_DEFAULT /// public const uint RateHzDefault = 20; /// /// DSFXGARGLE_WAVE_DEFAULT /// public const GargleWaveShape WaveShapeDefault = GargleWaveShape.Triangle; /// /// Rate of modulation in hz /// public uint RateHz { get { var param = GetAllParameters(); return param.RateHz; } set { var param = GetAllParameters(); param.RateHz = Math.Max(Math.Min(RateHzMax, value), RateHzMin); SetAllParameters(param); } } /// /// Gargle Wave Shape /// public GargleWaveShape WaveShape { get { var param = GetAllParameters(); return param.WaveShape; } set { var param = GetAllParameters(); if (Enum.IsDefined(typeof(GargleWaveShape), value)) { param.WaveShape = value; } SetAllParameters(param); } } private readonly IDirectSoundFXGargle fxGargle; internal Params(IDirectSoundFXGargle dsFxObject) { fxGargle = dsFxObject; } private void SetAllParameters(DsFxGargle param) { Marshal.ThrowExceptionForHR(fxGargle.SetAllParameters(ref param)); } private DsFxGargle GetAllParameters() { Marshal.ThrowExceptionForHR(fxGargle.GetAllParameters(out var param)); return param; } } private readonly MediaObject mediaObject; private readonly MediaObjectInPlace mediaObjectInPlace; private readonly Params effectParams; /// /// Media Object /// public MediaObject MediaObject => mediaObject; /// /// Media Object InPlace /// public MediaObjectInPlace MediaObjectInPlace => mediaObjectInPlace; /// /// Effect Parameter /// public Params EffectParams => effectParams; /// /// Create new DMO Gargle /// public DmoGargle() { var guidGargle = new Guid("DAFD8210-5711-4B91-9FE3-F75B7AE279BF"); var targetDescriptor = DmoEnumerator.GetAudioEffectNames().First(descriptor => Equals(descriptor.Clsid, guidGargle)); if (targetDescriptor != null) { var mediaComObject = Activator.CreateInstance(Type.GetTypeFromCLSID(targetDescriptor.Clsid)); mediaObject = new MediaObject((IMediaObject) mediaComObject); mediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace) mediaComObject); effectParams = new Params((IDirectSoundFXGargle) mediaComObject); } } /// /// Dispose code /// public void Dispose() { mediaObjectInPlace?.Dispose(); mediaObject?.Dispose(); } } }