-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio.cs
More file actions
132 lines (118 loc) · 4.77 KB
/
Copy pathAudio.cs
File metadata and controls
132 lines (118 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
namespace SoundManager;
public record AudioDevice(string Id, string Name, bool IsDefault, bool IsDefaultComms);
public class DeviceVM
{
public string Id { get; set; } = "";
public string Name { get; set; } = "";
public DataFlow Flow { get; set; }
public bool IsDefault { get; set; }
public bool IsDefaultComms { get; set; }
public bool IsActive => IsDefault || IsDefaultComms;
public Visibility DefaultVis => IsDefault ? Visibility.Visible : Visibility.Collapsed;
public Visibility CommsVis => IsDefaultComms ? Visibility.Visible : Visibility.Collapsed;
}
public static class AudioManager
{
public static List<AudioDevice> GetDevices(DataFlow flow)
{
var list = new List<AudioDevice>();
using var en = new MMDeviceEnumerator();
string defId = "", commId = "";
try { using var d = en.GetDefaultAudioEndpoint(flow, Role.Multimedia); defId = d.ID; } catch { }
try { using var d = en.GetDefaultAudioEndpoint(flow, Role.Communications); commId = d.ID; } catch { }
var col = en.EnumerateAudioEndPoints(flow, DeviceState.Active);
foreach (var d in col)
{
list.Add(new AudioDevice(d.ID, d.FriendlyName, d.ID == defId, d.ID == commId));
d.Dispose();
}
return list;
}
public static bool DeviceExists(string id)
{
if (string.IsNullOrEmpty(id)) return false;
try
{
using var en = new MMDeviceEnumerator();
var col = en.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
foreach (var d in col)
{
bool match = d.ID == id;
d.Dispose();
if (match) return true;
}
}
catch { }
return false;
}
public static string? NameById(string id)
{
if (string.IsNullOrEmpty(id)) return null;
string? result = null;
try
{
using var en = new MMDeviceEnumerator();
var col = en.EnumerateAudioEndPoints(DataFlow.All, DeviceState.All);
foreach (var d in col)
{
if (d.ID == id) result = d.FriendlyName;
d.Dispose();
}
}
catch { }
return result;
}
public static void SetDefault(string deviceId)
{
var cfg = (IPolicyConfig)new CPolicyConfigClient();
try
{
cfg.SetDefaultEndpoint(deviceId, ERole.eConsole);
cfg.SetDefaultEndpoint(deviceId, ERole.eMultimedia);
}
finally { Marshal.FinalReleaseComObject(cfg); }
}
public static void SetDefaultComms(string deviceId)
{
var cfg = (IPolicyConfig)new CPolicyConfigClient();
try { cfg.SetDefaultEndpoint(deviceId, ERole.eCommunications); }
finally { Marshal.FinalReleaseComObject(cfg); }
}
}
public class DeviceWatcher : IMMNotificationClient
{
public Action? Changed;
public Action<DataFlow, Role, string>? DefaultChanged;
public void OnDeviceStateChanged(string deviceId, DeviceState newState) => Changed?.Invoke();
public void OnDeviceAdded(string pwstrDeviceId) => Changed?.Invoke();
public void OnDeviceRemoved(string deviceId) => Changed?.Invoke();
public void OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId)
=> DefaultChanged?.Invoke(flow, role, defaultDeviceId);
public void OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key) { }
}
enum ERole { eConsole = 0, eMultimedia = 1, eCommunications = 2 }
[Guid("f8679f50-850a-41cf-9c72-430f290290c8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPolicyConfig
{
[PreserveSig] int GetMixFormat(string n, nint f);
[PreserveSig] int GetDeviceFormat(string n, bool d, nint f);
[PreserveSig] int ResetDeviceFormat(string n);
[PreserveSig] int SetDeviceFormat(string n, nint e, nint m);
[PreserveSig] int GetProcessingPeriod(string n, bool d, nint def, nint min);
[PreserveSig] int SetProcessingPeriod(string n, nint p);
[PreserveSig] int GetShareMode(string n, nint m);
[PreserveSig] int SetShareMode(string n, nint m);
[PreserveSig] int GetPropertyValue(string n, bool fx, nint key, nint pv);
[PreserveSig] int SetPropertyValue(string n, bool fx, nint key, nint pv);
[PreserveSig] int SetDefaultEndpoint(string n, ERole role);
[PreserveSig] int SetEndpointVisibility(string n, bool visible);
}
[ComImport, Guid("870af99c-171d-4f9e-af0d-e63df40c2bc9")]
class CPolicyConfigClient { }