2023-08-06 13:46:36 -06:00
|
|
|
|
using CoreAudio;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
using FocusVolumeControl;
|
2023-08-06 13:46:36 -06:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
namespace SoundBrowser;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for MainWindow.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
AudioHelper _audioHelper;
|
|
|
|
|
Native.WinEventDelegate _delegate;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_audioHelper = new AudioHelper();
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
//normally you can just pass a lambda, but for some reason, that seems to get garbage collected
|
|
|
|
|
_delegate = new Native.WinEventDelegate(WinEventProc);
|
|
|
|
|
Native.RegisterForForegroundWindowChangedEvent(_delegate);
|
|
|
|
|
}
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
|
|
|
|
{
|
|
|
|
|
SetupCurrentAppFields();
|
|
|
|
|
SetupAllSessionFields();
|
|
|
|
|
}
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
private void SetupCurrentAppFields()
|
|
|
|
|
{
|
|
|
|
|
var handle = Native.GetForegroundWindow();
|
|
|
|
|
var sb = new StringBuilder();
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
if (handle != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
//use this in debug to help there be less events
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
/*
|
|
|
|
|
Native.GetWindowThreadProcessId(handle, out var fpid);
|
|
|
|
|
var fp = Process.GetProcessById(fpid);
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
if(!fp.ProcessName.Contains("FSD"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
*/
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
var processes = _audioHelper.GetPossibleProcesses();
|
|
|
|
|
var session = _audioHelper.FindSession(processes);
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
foreach (var p in processes)
|
|
|
|
|
{
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
sb.AppendLine($"pid: {p.Id}");
|
|
|
|
|
sb.AppendLine($"\tprocessName: {p.ProcessName}");
|
|
|
|
|
try
|
2023-08-06 21:51:04 -06:00
|
|
|
|
{
|
2023-08-20 20:52:48 -06:00
|
|
|
|
sb.AppendLine($"\tFileDescription: {p!.MainModule!.FileVersionInfo.FileDescription}");
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
catch
|
2023-08-06 21:51:04 -06:00
|
|
|
|
{
|
2023-08-20 20:52:48 -06:00
|
|
|
|
sb.AppendLine("\tFileDescription: ##ERROR##");
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
|
|
|
|
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
sb.AppendLine();
|
|
|
|
|
if (session != null)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("picked the following best match");
|
|
|
|
|
sb.AppendLine($"\tsession: {session.DisplayName}");
|
|
|
|
|
sb.AppendLine($"\tvolume: {session.GetVolumeLevel()}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("No Match");
|
|
|
|
|
}
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
_tf.Text = sb.ToString();
|
|
|
|
|
}
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
private void SetupAllSessionFields()
|
|
|
|
|
{
|
|
|
|
|
_tf2.Text = "";
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine("-------------------------------------------------------------------------------");
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
var deviceEnumerator = new MMDeviceEnumerator(Guid.NewGuid());
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
using var device = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
|
|
|
|
|
using var manager = device.AudioSessionManager2;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
var sessions = manager!.Sessions;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
foreach (var session in sessions!)
|
|
|
|
|
{
|
|
|
|
|
var audioProcess = Process.GetProcessById((int)session.ProcessID);
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
var displayName = audioProcess!.MainModule!.FileVersionInfo.FileDescription;
|
2023-08-06 21:51:04 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
sb.AppendLine($"pid: {audioProcess.Id}");
|
|
|
|
|
sb.AppendLine($"\tprocessName: {audioProcess.ProcessName}");
|
|
|
|
|
sb.AppendLine($"\tsession: {displayName}");
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
_tf2.Text = sb.ToString();
|
2023-08-06 21:51:04 -06:00
|
|
|
|
}
|
2023-08-06 13:46:36 -06:00
|
|
|
|
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-08-06 13:46:36 -06:00
|
|
|
|
}
|