using System; using System.Linq; using System.Runtime.InteropServices; namespace NAudio.Dmo.Effect { internal struct DsFxParamEq { public float Center; public float BandWidth; public float Gain; } [ComImport, System.Security.SuppressUnmanagedCodeSecurity, Guid("c03ca9fe-fe90-4204-8078-82334cd177da"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDirectSoundFxParamEq { [PreserveSig] int SetAllParameters([In] ref DsFxParamEq param); [PreserveSig] int GetAllParameters(out DsFxParamEq param); } /// /// DMO Parametric Equalizer Effect /// public class DmoParamEq : IDmoEffector { /// /// DMO ParamEq Params /// public struct Params { /// /// DSFXPARAMEQ_CENTER_MIN /// public const float CenterMin = 80.0f; /// /// DSFXPARAMEQ_CENTER_MAX /// public const float CenterMax = 16000.0f; /// /// DSFXPARAMEQ_CENTER_DEFAULT /// public const float CenterDefault = 8000.0f; /// /// DSFXPARAMEQ_BANDWIDTH_MIN /// public const float BandWidthMin = 1.0f; /// /// DSFXPARAMEQ_BANDWIDTH_MAX /// public const float BandWidthMax = 36.0f; /// /// DSFXPARAMEQ_BANDWIDTH_DEFAULT /// public const float BandWidthDefault = 12.0f; /// /// DSFXPARAMEQ_GAIN_MIN /// public const float GainMin = -15.0f; /// /// DSFXPARAMEQ_GAIN_MAX /// public const float GainMax = 15.0f; /// /// DSFXPARAMEQ_GAIN_DEFAULT /// public const float GainDefault = 0.0f; /// /// Center frequency, in hertz /// public float Center { get { var param = GetAllParameters(); return param.Center; } set { var param = GetAllParameters(); param.Center = Math.Max(Math.Min(CenterMax, value), CenterMin); SetAllParameters(param); } } /// /// Bandwidth, in semitones. /// public float BandWidth { get { var param = GetAllParameters(); return param.BandWidth; } set { var param = GetAllParameters(); param.BandWidth = Math.Max(Math.Min(BandWidthMax, value), BandWidthMin); SetAllParameters(param); } } /// /// Gain /// public float Gain { get { var param = GetAllParameters(); return param.Gain; } set { var param = GetAllParameters(); param.Gain = Math.Max(Math.Min(GainMax, value), GainMin); SetAllParameters(param); } } private readonly IDirectSoundFxParamEq fxParamEq; internal Params(IDirectSoundFxParamEq dsFxObject) { fxParamEq = dsFxObject; } private void SetAllParameters(DsFxParamEq param) { Marshal.ThrowExceptionForHR(fxParamEq.SetAllParameters(ref param)); } private DsFxParamEq GetAllParameters() { Marshal.ThrowExceptionForHR(fxParamEq.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 ParamEq /// public DmoParamEq() { var guidParamEq = new Guid("120CED89-3BF4-4173-A132-3CB406CF3231"); var targetDescriptor = DmoEnumerator.GetAudioEffectNames().First(descriptor => Equals(descriptor.Clsid, guidParamEq)); if (targetDescriptor != null) { var mediaComObject = Activator.CreateInstance(Type.GetTypeFromCLSID(targetDescriptor.Clsid)); mediaObject = new MediaObject((IMediaObject) mediaComObject); mediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace) mediaComObject); effectParams = new Params((IDirectSoundFxParamEq) mediaComObject); } } /// /// Dispose code /// public void Dispose() { mediaObjectInPlace?.Dispose(); mediaObject?.Dispose(); } } }