Skip to content

Commit

Permalink
feat(USB): Add detection if USB audio device
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed Apr 2, 2021
1 parent 240ba91 commit 13286bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .idea/.idea.SoundSwitch/.idea/contentModel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions SoundSwitch.Common/Framework/Audio/Device/DeviceFullInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NAudio.CoreAudioApi;
using Newtonsoft.Json;
using SoundSwitch.Common.Framework.Audio.Icon;

namespace SoundSwitch.Common.Framework.Audio.Device
Expand All @@ -11,7 +12,8 @@ public class DeviceFullInfo : DeviceInfo
public System.Drawing.Icon LargeIcon => AudioDeviceIconExtractor.ExtractIconFromPath(IconPath, Type, true);
public System.Drawing.Icon SmallIcon => AudioDeviceIconExtractor.ExtractIconFromPath(IconPath, Type, false);

public DeviceFullInfo(string name, string id, DataFlow type, string iconPath, DeviceState state) : base(name, id, type)
[JsonConstructor]
public DeviceFullInfo(string name, string id, DataFlow type, string iconPath, DeviceState state, bool isUsb) : base(name, id, type, isUsb)
{
IconPath = iconPath;
State = state;
Expand All @@ -22,6 +24,5 @@ public DeviceFullInfo(MMDevice device) : base(device)
IconPath = device.IconPath;
State = device.State;
}

}
}
29 changes: 20 additions & 9 deletions SoundSwitch.Common/Framework/Audio/Device/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public class DeviceInfo : IEquatable<DeviceInfo>, IComparable<DeviceInfo>

private static readonly Regex NameCleanerRegex = new Regex(@"^\d+\s?-\s?", RegexOptions.Compiled | RegexOptions.Singleline);

private string _nameClean;
private string _nameClean;

[Obsolete("Use " + nameof(NameClean))]
public string Name { get; }
public string Id { get; }
public DataFlow Type { get; }
public string Name { get; }

public string Id { get; }
public DataFlow Type { get; }

public bool IsUsb { get; }

public string NameClean
{
Expand All @@ -32,25 +36,30 @@ public string NameClean
{
return _nameClean = Name;
}

var friendlyName = match.Groups["friendlyName"].Value;
var deviceName = match.Groups["deviceName"].Value;
return _nameClean = $"{NameCleanerRegex.Replace(friendlyName, "")} ({deviceName})";
}
}

[JsonConstructor]
public DeviceInfo(string name, string id, DataFlow type)
public DeviceInfo(string name, string id, DataFlow type, bool isUsb)
{
Name = name;
Id = id;
Type = type;
Id = id;
Type = type;
IsUsb = isUsb;
}

public DeviceInfo(MMDevice device)
{
Name = device.FriendlyName;
Id = device.ID;
Type = device.DataFlow;
Id = device.ID;
Type = device.DataFlow;
var deviceProperties = device.Properties;
var enumerator = deviceProperties.Contains(PropertyKeys.DEVPKEY_Device_EnumeratorName) ? (string) deviceProperties[PropertyKeys.DEVPKEY_Device_EnumeratorName].Value : "";
IsUsb = enumerator == "USB";
}


Expand Down Expand Up @@ -81,13 +90,15 @@ public int CompareTo(DeviceInfo other)
return Type.CompareTo(other.Type);
}


public bool Equals(DeviceInfo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return (Id == other.Id || NameClean == other.NameClean) && Type == other.Type;
}


public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is DeviceInfo other && Equals(other);
Expand Down
11 changes: 11 additions & 0 deletions SoundSwitch.Common/Framework/Audio/Device/PropertyKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using NAudio.CoreAudioApi;

namespace SoundSwitch.Common.Framework.Audio.Device
{
public static class PropertyKeys
{
public static readonly PropertyKey DEVPKEY_Device_EnumeratorName = new (new Guid(unchecked((int)0xa45c254e), unchecked((short)0xdf1c), 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 24);

}
}

0 comments on commit 13286bd

Please sign in to comment.