Skip to content

Commit

Permalink
core audio api code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Apr 11, 2018
1 parent a10edf5 commit 10e188d
Show file tree
Hide file tree
Showing 46 changed files with 134 additions and 416 deletions.
11 changes: 3 additions & 8 deletions NAudio/CoreAudioApi/AudioCaptureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public IntPtr GetBuffer(
out long devicePosition,
out long qpcPosition)
{
IntPtr bufferPointer;
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition));
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out var bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition));
return bufferPointer;
}

Expand All @@ -41,10 +40,7 @@ public IntPtr GetBuffer(
out int numFramesToRead,
out AudioClientBufferFlags bufferFlags)
{
IntPtr bufferPointer;
long devicePosition;
long qpcPosition;
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition));
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out var bufferPointer, out numFramesToRead, out bufferFlags, out _, out _));
return bufferPointer;
}

Expand All @@ -53,8 +49,7 @@ public IntPtr GetBuffer(
/// </summary>
public int GetNextPacketSize()
{
int numFramesInNextPacket;
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetNextPacketSize(out numFramesInNextPacket));
Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetNextPacketSize(out var numFramesInNextPacket));
return numFramesInNextPacket;
}

Expand Down
37 changes: 10 additions & 27 deletions NAudio/CoreAudioApi/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public WaveFormat MixFormat
{
if (mixFormat == null)
{
IntPtr waveFormatPointer;
Marshal.ThrowExceptionForHR(audioClientInterface.GetMixFormat(out waveFormatPointer));
Marshal.ThrowExceptionForHR(audioClientInterface.GetMixFormat(out var waveFormatPointer));
var waveFormat = WaveFormat.MarshalFromPtr(waveFormatPointer);
Marshal.FreeCoTaskMem(waveFormatPointer);
mixFormat = waveFormat;
Expand Down Expand Up @@ -83,13 +82,7 @@ public int BufferSize
/// <summary>
/// Retrieves the maximum latency for the current stream and can be called any time after the stream has been initialized.
/// </summary>
public long StreamLatency
{
get
{
return audioClientInterface.GetStreamLatency();
}
}
public long StreamLatency => audioClientInterface.GetStreamLatency();

/// <summary>
/// Retrieves the number of frames of padding in the endpoint buffer (must initialize first)
Expand All @@ -98,8 +91,7 @@ public int CurrentPadding
{
get
{
int currentPadding;
Marshal.ThrowExceptionForHR(audioClientInterface.GetCurrentPadding(out currentPadding));
Marshal.ThrowExceptionForHR(audioClientInterface.GetCurrentPadding(out var currentPadding));
return currentPadding;
}
}
Expand All @@ -112,9 +104,7 @@ public long DefaultDevicePeriod
{
get
{
long defaultDevicePeriod;
long minimumDevicePeriod;
Marshal.ThrowExceptionForHR(audioClientInterface.GetDevicePeriod(out defaultDevicePeriod, out minimumDevicePeriod));
Marshal.ThrowExceptionForHR(audioClientInterface.GetDevicePeriod(out var defaultDevicePeriod, out _));
return defaultDevicePeriod;
}
}
Expand All @@ -127,9 +117,7 @@ public long MinimumDevicePeriod
{
get
{
long defaultDevicePeriod;
long minimumDevicePeriod;
Marshal.ThrowExceptionForHR(audioClientInterface.GetDevicePeriod(out defaultDevicePeriod, out minimumDevicePeriod));
Marshal.ThrowExceptionForHR(audioClientInterface.GetDevicePeriod(out _, out var minimumDevicePeriod));
return minimumDevicePeriod;
}
}
Expand Down Expand Up @@ -158,9 +146,8 @@ public AudioStreamVolume AudioStreamVolume
}
if (audioStreamVolume == null)
{
object audioStreamVolumeInterface;
var audioStreamVolumeGuid = new Guid("93014887-242D-4068-8A15-CF5E93B90FE3");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioStreamVolumeGuid, out audioStreamVolumeInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioStreamVolumeGuid, out var audioStreamVolumeInterface));
audioStreamVolume = new AudioStreamVolume((IAudioStreamVolume)audioStreamVolumeInterface);
}
return audioStreamVolume;
Expand All @@ -176,9 +163,8 @@ public AudioClockClient AudioClockClient
{
if (audioClockClient == null)
{
object audioClockClientInterface;
var audioClockClientGuid = new Guid("CD63314F-3FBA-4a1b-812C-EF96358728E7");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioClockClientGuid, out audioClockClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioClockClientGuid, out var audioClockClientInterface));
audioClockClient = new AudioClockClient((IAudioClock)audioClockClientInterface);
}
return audioClockClient;
Expand All @@ -194,9 +180,8 @@ public AudioRenderClient AudioRenderClient
{
if (audioRenderClient == null)
{
object audioRenderClientInterface;
var audioRenderClientGuid = new Guid("F294ACFC-3146-4483-A7BF-ADDCA7C260E2");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioRenderClientGuid, out audioRenderClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioRenderClientGuid, out var audioRenderClientInterface));
audioRenderClient = new AudioRenderClient((IAudioRenderClient)audioRenderClientInterface);
}
return audioRenderClient;
Expand All @@ -212,9 +197,8 @@ public AudioCaptureClient AudioCaptureClient
{
if (audioCaptureClient == null)
{
object audioCaptureClientInterface;
var audioCaptureClientGuid = new Guid("c8adbd64-e71e-48a0-a4de-185c395cd317");
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioCaptureClientGuid, out audioCaptureClientInterface));
Marshal.ThrowExceptionForHR(audioClientInterface.GetService(audioCaptureClientGuid, out var audioCaptureClientInterface));
audioCaptureClient = new AudioCaptureClient((IAudioCaptureClient)audioCaptureClientInterface);
}
return audioCaptureClient;
Expand All @@ -230,8 +214,7 @@ public AudioCaptureClient AudioCaptureClient
public bool IsFormatSupported(AudioClientShareMode shareMode,
WaveFormat desiredFormat)
{
WaveFormatExtensible closestMatchFormat;
return IsFormatSupported(shareMode, desiredFormat, out closestMatchFormat);
return IsFormatSupported(shareMode, desiredFormat, out _);
}

private IntPtr GetPointerToPointer()
Expand Down
11 changes: 3 additions & 8 deletions NAudio/CoreAudioApi/AudioClockClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public int Characteristics
{
get
{
uint characteristics;
Marshal.ThrowExceptionForHR(audioClockClientInterface.GetCharacteristics(out characteristics));
Marshal.ThrowExceptionForHR(audioClockClientInterface.GetCharacteristics(out var characteristics));
return (int)characteristics;
}
}
Expand All @@ -40,8 +39,7 @@ public ulong Frequency
{
get
{
ulong freq;
Marshal.ThrowExceptionForHR(audioClockClientInterface.GetFrequency(out freq));
Marshal.ThrowExceptionForHR(audioClockClientInterface.GetFrequency(out var freq));
return freq;
}
}
Expand Down Expand Up @@ -101,10 +99,7 @@ public ulong AdjustedPosition
/// <summary>
/// Can Adjust Position
/// </summary>
public bool CanAdjustPosition
{
get { return Stopwatch.IsHighResolution; }
}
public bool CanAdjustPosition => Stopwatch.IsHighResolution;

#region IDisposable Members

Expand Down
73 changes: 15 additions & 58 deletions NAudio/CoreAudioApi/AudioEndpointVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace NAudio.CoreAudioApi
public class AudioEndpointVolume : IDisposable
{
private readonly IAudioEndpointVolume audioEndPointVolume;
private readonly AudioEndpointVolumeChannels channels;
private readonly AudioEndpointVolumeStepInformation stepInformation;
private readonly AudioEndpointVolumeVolumeRange volumeRange;
private readonly EEndpointHardwareSupport hardwareSupport;
private AudioEndpointVolumeCallback callBack;

private Guid notificationGuid = Guid.Empty;
Expand All @@ -44,14 +40,8 @@ public class AudioEndpointVolume : IDisposable
/// GUID to pass to AudioEndpointVolumeCallback
/// </summary>
public Guid NotificationGuid {
get
{
return notificationGuid;
}
set
{
notificationGuid = value;
}
get => notificationGuid;
set => notificationGuid = value;
}

/// <summary>
Expand All @@ -62,46 +52,22 @@ public Guid NotificationGuid {
/// <summary>
/// Volume Range
/// </summary>
public AudioEndpointVolumeVolumeRange VolumeRange
{
get
{
return volumeRange;
}
}
public AudioEndpointVolumeVolumeRange VolumeRange { get; }

/// <summary>
/// Hardware Support
/// </summary>
public EEndpointHardwareSupport HardwareSupport
{
get
{
return hardwareSupport;
}
}
public EEndpointHardwareSupport HardwareSupport { get; }

/// <summary>
/// Step Information
/// </summary>
public AudioEndpointVolumeStepInformation StepInformation
{
get
{
return stepInformation;
}
}
public AudioEndpointVolumeStepInformation StepInformation { get; }

/// <summary>
/// Channels
/// </summary>
public AudioEndpointVolumeChannels Channels
{
get
{
return channels;
}
}
public AudioEndpointVolumeChannels Channels { get; }

/// <summary>
/// Master Volume Level
Expand All @@ -110,8 +76,7 @@ public float MasterVolumeLevel
{
get
{
float result;
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMasterVolumeLevel(out result));
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMasterVolumeLevel(out var result));
return result;
}
set
Expand All @@ -127,8 +92,7 @@ public float MasterVolumeLevelScalar
{
get
{
float result;
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMasterVolumeLevelScalar(out result));
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMasterVolumeLevelScalar(out var result));
return result;
}
set
Expand All @@ -144,8 +108,7 @@ public bool Mute
{
get
{
bool result;
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMute(out result));
Marshal.ThrowExceptionForHR(audioEndPointVolume.GetMute(out var result));
return result;
}
set
Expand Down Expand Up @@ -176,25 +139,19 @@ public void VolumeStepDown()
/// <param name="realEndpointVolume">IAudioEndpointVolume COM interface</param>
internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
{
uint hardwareSupp;

audioEndPointVolume = realEndpointVolume;
channels = new AudioEndpointVolumeChannels(audioEndPointVolume);
stepInformation = new AudioEndpointVolumeStepInformation(audioEndPointVolume);
Marshal.ThrowExceptionForHR(audioEndPointVolume.QueryHardwareSupport(out hardwareSupp));
hardwareSupport = (EEndpointHardwareSupport)hardwareSupp;
volumeRange = new AudioEndpointVolumeVolumeRange(audioEndPointVolume);
Channels = new AudioEndpointVolumeChannels(audioEndPointVolume);
StepInformation = new AudioEndpointVolumeStepInformation(audioEndPointVolume);
Marshal.ThrowExceptionForHR(audioEndPointVolume.QueryHardwareSupport(out var hardwareSupp));
HardwareSupport = (EEndpointHardwareSupport)hardwareSupp;
VolumeRange = new AudioEndpointVolumeVolumeRange(audioEndPointVolume);
callBack = new AudioEndpointVolumeCallback(this);
Marshal.ThrowExceptionForHR(audioEndPointVolume.RegisterControlChangeNotify(callBack));
}

internal void FireNotification(AudioVolumeNotificationData notificationData)
{
AudioEndpointVolumeNotificationDelegate del = OnVolumeNotification;
if (del != null)
{
del(notificationData);
}
OnVolumeNotification?.Invoke(notificationData);
}
#region IDisposable Members

Expand Down
1 change: 0 additions & 1 deletion NAudio/CoreAudioApi/AudioEndpointVolumeCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 3. This notice may not be removed or altered from any source distribution.

using System;
using NAudio.CoreAudioApi.Interfaces;
using System.Runtime.InteropServices;
using NAudio.Utils;

namespace NAudio.CoreAudioApi
Expand Down
20 changes: 6 additions & 14 deletions NAudio/CoreAudioApi/AudioEndpointVolumeChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ public class AudioEndpointVolumeChannel
/// </summary>
public Guid NotificationGuid
{
get
{
return notificationGuid;
}
set
{
notificationGuid = value;
}
get => notificationGuid;
set => notificationGuid = value;
}

internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel)
Expand All @@ -64,13 +58,12 @@ public float VolumeLevel
{
get
{
float result;
Marshal.ThrowExceptionForHR(audioEndpointVolume.GetChannelVolumeLevel(channel,out result));
Marshal.ThrowExceptionForHR(audioEndpointVolume.GetChannelVolumeLevel(channel,out var result));
return result;
}
set
{
Marshal.ThrowExceptionForHR(audioEndpointVolume.SetChannelVolumeLevel(channel, value, ref this.notificationGuid));
Marshal.ThrowExceptionForHR(audioEndpointVolume.SetChannelVolumeLevel(channel, value, ref notificationGuid));
}
}

Expand All @@ -81,13 +74,12 @@ public float VolumeLevelScalar
{
get
{
float result;
Marshal.ThrowExceptionForHR(audioEndpointVolume.GetChannelVolumeLevelScalar(channel, out result));
Marshal.ThrowExceptionForHR(audioEndpointVolume.GetChannelVolumeLevelScalar(channel, out var result));
return result;
}
set
{
Marshal.ThrowExceptionForHR(audioEndpointVolume.SetChannelVolumeLevelScalar(channel, value, ref this.notificationGuid));
Marshal.ThrowExceptionForHR(audioEndpointVolume.SetChannelVolumeLevelScalar(channel, value, ref notificationGuid));
}
}

Expand Down
Loading

0 comments on commit 10e188d

Please sign in to comment.