Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance & General Housekeeping #1206

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions NAudio.Wasapi/CoreAudioApi/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace NAudio.CoreAudioApi
/// </summary>
public class AudioClient : IDisposable
{
private static readonly Guid ID_AudioStreamVolume = new Guid("93014887-242D-4068-8A15-CF5E93B90FE3");
private static readonly Guid ID_AudioClockClient = new Guid("CD63314F-3FBA-4a1b-812C-EF96358728E7");
private static readonly Guid ID_AudioRenderClient = new Guid("F294ACFC-3146-4483-A7BF-ADDCA7C260E2");
private static readonly Guid ID_AudioCaptureClient = new Guid("c8adbd64-e71e-48a0-a4de-185c395cd317");
private static readonly Guid IID_IAudioClient2 = new Guid("726778CD-F60A-4eda-82DE-E47610CD78AA");
private IAudioClient audioClientInterface;
private WaveFormat mixFormat;
private AudioRenderClient audioRenderClient;
Expand Down Expand Up @@ -50,8 +55,7 @@ public static async Task<AudioClient> ActivateAsync(string deviceInterfacePath,
AudioClientStreamFlags.EventCallback | AudioClientStreamFlags.NoPersist,
10000000, 0, wfx, IntPtr.Zero);*/
});
var IID_IAudioClient2 = new Guid("726778CD-F60A-4eda-82DE-E47610CD78AA");
NativeMethods.ActivateAudioInterfaceAsync(deviceInterfacePath, IID_IAudioClient2, IntPtr.Zero, icbh, out var activationOperation);
NativeMethods.ActivateAudioInterfaceAsync(deviceInterfacePath, IID_IAudioClient2, IntPtr.Zero, icbh, out _);
var audioClient2 = await icbh;
return new AudioClient((IAudioClient)audioClient2);
}
Expand Down Expand Up @@ -186,8 +190,7 @@ public AudioStreamVolume AudioStreamVolume
}
if (audioStreamVolume == null)
{
var audioStreamVolumeGuid = new Guid("93014887-242D-4068-8A15-CF5E93B90FE3");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioStreamVolumeGuid, out var audioStreamVolumeInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(ID_AudioStreamVolume, out var audioStreamVolumeInterface));
audioStreamVolume = new AudioStreamVolume((IAudioStreamVolume)audioStreamVolumeInterface);
}
return audioStreamVolume;
Expand All @@ -203,8 +206,7 @@ public AudioClockClient AudioClockClient
{
if (audioClockClient == null)
{
var audioClockClientGuid = new Guid("CD63314F-3FBA-4a1b-812C-EF96358728E7");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioClockClientGuid, out var audioClockClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(ID_AudioClockClient, out var audioClockClientInterface));
audioClockClient = new AudioClockClient((IAudioClock)audioClockClientInterface);
}
return audioClockClient;
Expand All @@ -220,8 +222,7 @@ public AudioRenderClient AudioRenderClient
{
if (audioRenderClient == null)
{
var audioRenderClientGuid = new Guid("F294ACFC-3146-4483-A7BF-ADDCA7C260E2");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioRenderClientGuid, out var audioRenderClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(ID_AudioRenderClient, out var audioRenderClientInterface));
audioRenderClient = new AudioRenderClient((IAudioRenderClient)audioRenderClientInterface);
}
return audioRenderClient;
Expand All @@ -237,8 +238,7 @@ public AudioCaptureClient AudioCaptureClient
{
if (audioCaptureClient == null)
{
var audioCaptureClientGuid = new Guid("c8adbd64-e71e-48a0-a4de-185c395cd317");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioCaptureClientGuid, out var audioCaptureClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(ID_AudioCaptureClient, out var audioCaptureClientInterface));
audioCaptureClient = new AudioCaptureClient((IAudioCaptureClient)audioCaptureClientInterface);
}
return audioCaptureClient;
Expand Down
2 changes: 1 addition & 1 deletion NAudio.Wasapi/CoreAudioApi/Interfaces/IMMDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NAudio.CoreAudioApi.Interfaces
interface IMMDevice
{
// activationParams is a propvariant
int Activate(ref Guid id, ClsCtx clsCtx, IntPtr activationParams,
int Activate(in Guid id, ClsCtx clsCtx, IntPtr activationParams,
[MarshalAs(UnmanagedType.IUnknown)] out object interfacePointer);

int OpenPropertyStore(StorageAccessMode stgmAccess, out IPropertyStore properties);
Expand Down
20 changes: 10 additions & 10 deletions NAudio.Wasapi/CoreAudioApi/MMDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class MMDevice : IDisposable

#region Guids
// ReSharper disable InconsistentNaming
private static Guid IID_IAudioMeterInformation = new Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064");
private static Guid IID_IAudioEndpointVolume = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");
private static Guid IID_IAudioClient = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2");
private static Guid IDD_IAudioSessionManager = new Guid("BFA971F1-4D5E-40BB-935E-967039BFBEE4");
private static Guid IDD_IDeviceTopology = new Guid("2A07407E-6497-4A18-9787-32F79BD0D98F");
private static readonly Guid IID_IAudioMeterInformation = new Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064");
private static readonly Guid IID_IAudioEndpointVolume = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");
private static readonly Guid IID_IAudioClient = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2");
private static readonly Guid IDD_IAudioSessionManager = new Guid("BFA971F1-4D5E-40BB-935E-967039BFBEE4");
private static readonly Guid IDD_IDeviceTopology = new Guid("2A07407E-6497-4A18-9787-32F79BD0D98F");
// ReSharper restore InconsistentNaming
#endregion

Expand All @@ -66,31 +66,31 @@ public void GetPropertyInformation(StorageAccessMode stgmAccess = StorageAccessM

private AudioClient GetAudioClient()
{
Marshal.ThrowExceptionForHR(deviceInterface.Activate(ref IID_IAudioClient, ClsCtx.ALL, IntPtr.Zero, out var result));
Marshal.ThrowExceptionForHR(deviceInterface.Activate(IID_IAudioClient, ClsCtx.ALL, IntPtr.Zero, out var result));
return new AudioClient(result as IAudioClient);
}

private void GetAudioMeterInformation()
{
Marshal.ThrowExceptionForHR(deviceInterface.Activate(ref IID_IAudioMeterInformation, ClsCtx.ALL, IntPtr.Zero, out var result));
Marshal.ThrowExceptionForHR(deviceInterface.Activate(IID_IAudioMeterInformation, ClsCtx.ALL, IntPtr.Zero, out var result));
audioMeterInformation = new AudioMeterInformation(result as IAudioMeterInformation);
}

private void GetAudioEndpointVolume()
{
Marshal.ThrowExceptionForHR(deviceInterface.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out var result));
Marshal.ThrowExceptionForHR(deviceInterface.Activate(IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out var result));
audioEndpointVolume = new AudioEndpointVolume(result as IAudioEndpointVolume);
}

private void GetAudioSessionManager()
{
Marshal.ThrowExceptionForHR(deviceInterface.Activate(ref IDD_IAudioSessionManager, ClsCtx.ALL, IntPtr.Zero, out var result));
Marshal.ThrowExceptionForHR(deviceInterface.Activate(IDD_IAudioSessionManager, ClsCtx.ALL, IntPtr.Zero, out var result));
audioSessionManager = new AudioSessionManager(result as IAudioSessionManager);
}

private void GetDeviceTopology()
{
Marshal.ThrowExceptionForHR(deviceInterface.Activate(ref IDD_IDeviceTopology, ClsCtx.ALL, IntPtr.Zero, out var result));
Marshal.ThrowExceptionForHR(deviceInterface.Activate(IDD_IDeviceTopology, ClsCtx.ALL, IntPtr.Zero, out var result));
deviceTopology = new DeviceTopology(result as IDeviceTopology);
}

Expand Down
8 changes: 4 additions & 4 deletions NAudio.Wasapi/CoreAudioApi/MMDeviceEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MMDeviceEnumerator : IDisposable
/// </summary>
public MMDeviceEnumerator()
{
if (System.Environment.OSVersion.Version.Major < 6)
if (Environment.OSVersion.Version.Major < 6)
{
throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
}
Expand All @@ -66,7 +66,7 @@ public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState
/// <returns>Device</returns>
public MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)
{
Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)realEnumerator).GetDefaultAudioEndpoint(dataFlow, role, out var device));
Marshal.ThrowExceptionForHR(realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out var device));
return new MMDevice(device);
}

Expand All @@ -79,7 +79,7 @@ public MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)
public bool HasDefaultAudioEndpoint(DataFlow dataFlow, Role role)
{
const int E_NOTFOUND = unchecked((int)0x80070490);
int hresult = ((IMMDeviceEnumerator)realEnumerator).GetDefaultAudioEndpoint(dataFlow, role, out var device);
int hresult = realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out var device);
if (hresult == 0x0)
{
Marshal.ReleaseComObject(device);
Expand All @@ -100,7 +100,7 @@ public bool HasDefaultAudioEndpoint(DataFlow dataFlow, Role role)
/// <returns>Device</returns>
public MMDevice GetDevice(string id)
{
Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)realEnumerator).GetDevice(id, out var device));
Marshal.ThrowExceptionForHR(realEnumerator.GetDevice(id, out var device));
return new MMDevice(device);
}

Expand Down
6 changes: 3 additions & 3 deletions NAudio.Wasapi/CoreAudioApi/PropertyKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ public static class PropertyKeys
/// <summary>
/// PKEY _Device_IconPath
/// </summary>
public static readonly PropertyKey PKEY_Device_IconPath = new PropertyKey(new Guid(unchecked((int)0x259abffc), unchecked((short)0x50a7), 0x47ce, 0xaf, 0x8, 0x68, 0xc9, 0xa7, 0xd7, 0x33, 0x66), 12);
public static readonly PropertyKey PKEY_Device_IconPath = new PropertyKey(new Guid(unchecked(0x259abffc), unchecked(0x50a7), 0x47ce, 0xaf, 0x8, 0x68, 0xc9, 0xa7, 0xd7, 0x33, 0x66), 12);
/// <summary>
/// Device description property.
/// </summary>
public static readonly PropertyKey PKEY_Device_DeviceDesc = new PropertyKey(new Guid(unchecked((int)0xa45c254e), unchecked((short)0xdf1c), 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 2);
/// <summary>
/// Id of controller device for endpoint device property.
/// </summary>
public static readonly PropertyKey PKEY_Device_ControllerDeviceId = new PropertyKey(new Guid(unchecked((int)0xb3f8fa53), unchecked((short)0x0004), 0x438e, 0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc), 2);
public static readonly PropertyKey PKEY_Device_ControllerDeviceId = new PropertyKey(new Guid(unchecked((int)0xb3f8fa53), unchecked(0x0004), 0x438e, 0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc), 2);
/// <summary>
/// Device interface key property.
/// </summary>
public static readonly PropertyKey PKEY_Device_InterfaceKey = new PropertyKey(new Guid(unchecked((int)0x233164c8), unchecked((short)0x1b2c), 0x4c7d, 0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67), 1);
public static readonly PropertyKey PKEY_Device_InterfaceKey = new PropertyKey(new Guid(unchecked(0x233164c8), unchecked(0x1b2c), 0x4c7d, 0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67), 1);
/// <summary>
/// System-supplied device instance identification string, assigned by PnP manager, persistent across system restarts.
/// </summary>
Expand Down
26 changes: 11 additions & 15 deletions NAudio.Wasapi/Dmo/Effect/DmoChorus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal interface IDirectSoundFXChorus
/// </summary>
public class DmoChorus : IDmoEffector<DmoChorus.Params>
{
private static readonly Guid ID_Chorus = new Guid("EFE6629C-81F7-4281-BD91-C9D604A95AF6");

/// <summary>
/// DMO Chorus Params
/// </summary>
Expand Down Expand Up @@ -265,42 +267,36 @@ private DsFxChorus GetAllParameters()
}
}

private readonly MediaObject mediaObject;
private readonly MediaObjectInPlace mediaObjectInPlace;
private readonly Params effectParams;

/// <summary>
/// Media Object
/// </summary>
public MediaObject MediaObject => mediaObject;
public MediaObject MediaObject { get; }

/// <summary>
/// Media Object InPlace
/// </summary>
public MediaObjectInPlace MediaObjectInPlace => mediaObjectInPlace;
public MediaObjectInPlace MediaObjectInPlace { get; }

/// <summary>
/// Effect Parameter
/// </summary>
public Params EffectParams => effectParams;
public Params EffectParams { get; }

/// <summary>
/// Create new DMO Chorus
/// </summary>
public DmoChorus()
{
var guidChorus = new Guid("EFE6629C-81F7-4281-BD91-C9D604A95AF6");

var targetDescriptor = DmoEnumerator.GetAudioEffectNames().First(descriptor =>
Equals(descriptor.Clsid, guidChorus));
Equals(descriptor.Clsid, ID_Chorus));

if (targetDescriptor != null)
{
var mediaComObject = Activator.CreateInstance(Type.GetTypeFromCLSID(targetDescriptor.Clsid));

mediaObject = new MediaObject((IMediaObject) mediaComObject);
mediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace) mediaComObject);
effectParams = new Params((IDirectSoundFXChorus) mediaComObject);
MediaObject = new MediaObject((IMediaObject) mediaComObject);
MediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace) mediaComObject);
EffectParams = new Params((IDirectSoundFXChorus) mediaComObject);
}
}

Expand All @@ -309,8 +305,8 @@ public DmoChorus()
/// </summary>
public void Dispose()
{
mediaObjectInPlace?.Dispose();
mediaObject?.Dispose();
MediaObjectInPlace?.Dispose();
MediaObject?.Dispose();
}
}
}
26 changes: 11 additions & 15 deletions NAudio.Wasapi/Dmo/Effect/DmoCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface IDirectSoundFXCompressor
/// </summary>
public class DmoCompressor : IDmoEffector<DmoCompressor.Params>
{
private static readonly Guid Id_Compressor = new Guid("EF011F79-4000-406D-87AF-BFFB3FC39D57");

/// <summary>
/// DMO Compressor Params
/// </summary>
Expand Down Expand Up @@ -242,42 +244,36 @@ private DsFxCompressor GetAllParameters()
}
}

private readonly MediaObject mediaObject;
private readonly MediaObjectInPlace mediaObjectInPlace;
private readonly Params effectParams;

/// <summary>
/// Media Object
/// </summary>
public MediaObject MediaObject => mediaObject;
public MediaObject MediaObject { get; }

/// <summary>
/// Media Object InPlace
/// </summary>
public MediaObjectInPlace MediaObjectInPlace => mediaObjectInPlace;
public MediaObjectInPlace MediaObjectInPlace { get; }

/// <summary>
/// Effect Parameter
/// </summary>
public Params EffectParams => effectParams;
public Params EffectParams { get; }

/// <summary>
/// Create new DMO Compressor
/// </summary>
public DmoCompressor()
{
var guidChorus = new Guid("EF011F79-4000-406D-87AF-BFFB3FC39D57");

var targetDescriptor = DmoEnumerator.GetAudioEffectNames().First(descriptor =>
Equals(descriptor.Clsid, guidChorus));
Equals(descriptor.Clsid, Id_Compressor));

if (targetDescriptor != null)
{
var mediaComObject = Activator.CreateInstance(Type.GetTypeFromCLSID(targetDescriptor.Clsid));

mediaObject = new MediaObject((IMediaObject)mediaComObject);
mediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace)mediaComObject);
effectParams = new Params((IDirectSoundFXCompressor)mediaComObject);
MediaObject = new MediaObject((IMediaObject)mediaComObject);
MediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace)mediaComObject);
EffectParams = new Params((IDirectSoundFXCompressor)mediaComObject);
}
}

Expand All @@ -286,8 +282,8 @@ public DmoCompressor()
/// </summary>
public void Dispose()
{
mediaObjectInPlace?.Dispose();
mediaObject?.Dispose();
MediaObjectInPlace?.Dispose();
MediaObject?.Dispose();
}
}
}
26 changes: 11 additions & 15 deletions NAudio.Wasapi/Dmo/Effect/DmoDistortion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ interface IDirectSoundFXDistortion
/// </summary>
public class DmoDistortion : IDmoEffector<DmoDistortion.Params>
{
private static readonly Guid Id_Distortion = new Guid("EF114C90-CD1D-484E-96E5-09CFAF912A21");

/// <summary>
/// DMO Distortion Params
/// </summary>
Expand Down Expand Up @@ -210,42 +212,36 @@ private DsFxDistortion GetAllParameters()
}
}

private readonly MediaObject mediaObject;
private readonly MediaObjectInPlace mediaObjectInPlace;
private readonly Params effectParams;

/// <summary>
/// Media Object
/// </summary>
public MediaObject MediaObject => mediaObject;
public MediaObject MediaObject { get; }

/// <summary>
/// Media Object InPlace
/// </summary>
public MediaObjectInPlace MediaObjectInPlace => mediaObjectInPlace;
public MediaObjectInPlace MediaObjectInPlace { get; }

/// <summary>
/// Effect Parameter
/// </summary>
public Params EffectParams => effectParams;
public Params EffectParams { get; }

/// <summary>
/// Create new DMO Distortion
/// </summary>
public DmoDistortion()
{
var guidDistortion = new Guid("EF114C90-CD1D-484E-96E5-09CFAF912A21");

var targetDescriptor = DmoEnumerator.GetAudioEffectNames().First(descriptor =>
Equals(descriptor.Clsid, guidDistortion));
Equals(descriptor.Clsid, Id_Distortion));

if (targetDescriptor != null)
{
var mediaComObject = Activator.CreateInstance(Type.GetTypeFromCLSID(targetDescriptor.Clsid));

mediaObject = new MediaObject((IMediaObject)mediaComObject);
mediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace)mediaComObject);
effectParams = new Params((IDirectSoundFXDistortion)mediaComObject);
MediaObject = new MediaObject((IMediaObject)mediaComObject);
MediaObjectInPlace = new MediaObjectInPlace((IMediaObjectInPlace)mediaComObject);
EffectParams = new Params((IDirectSoundFXDistortion)mediaComObject);
}
}

Expand All @@ -254,8 +250,8 @@ public DmoDistortion()
/// </summary>
public void Dispose()
{
mediaObjectInPlace?.Dispose();
mediaObject?.Dispose();
MediaObjectInPlace?.Dispose();
MediaObject?.Dispose();
}
}
}
Loading