2023-09-16 15:34:45 -06:00
|
|
|
|
using System;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using BarRaider.SdTools;
|
|
|
|
|
using System.Drawing;
|
2023-09-16 15:34:45 -06:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
|
|
|
|
namespace FocusVolumeControl.AudioSessions;
|
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public sealed class ActiveAudioSessionWrapper : IAudioSession
|
2023-08-20 20:52:48 -06:00
|
|
|
|
{
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string ExecutablePath { get; set; }
|
2023-09-16 15:34:45 -06:00
|
|
|
|
private List<IAudioSessionControl2> Sessions { get; } = new List<IAudioSessionControl2>();
|
|
|
|
|
private IEnumerable<ISimpleAudioVolume> Volume => Sessions.Cast<ISimpleAudioVolume>();
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
|
|
|
|
string _icon;
|
|
|
|
|
|
|
|
|
|
public string GetIcon()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_icon))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var tmp = Icon.ExtractAssociatedIcon(ExecutablePath);
|
|
|
|
|
_icon = Tools.ImageToBase64(tmp.ToBitmap(), true);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2023-08-21 23:20:24 -06:00
|
|
|
|
_icon = "Image/encoderIcon";
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Any()
|
|
|
|
|
{
|
|
|
|
|
return Volume.Any();
|
|
|
|
|
}
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public int Count => Sessions.Count;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public void AddSession(IAudioSessionControl2 session)
|
2023-08-20 20:52:48 -06:00
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
Sessions.Add(session);
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ToggleMute()
|
|
|
|
|
{
|
|
|
|
|
//when all volumes are muted, Volume.All will return true
|
|
|
|
|
//so we swap from muted to false (opposite of Volume.All)
|
|
|
|
|
|
|
|
|
|
//when any volumes are unmuted, Volume.All will return false
|
|
|
|
|
//so we set muted to true (opposite of Volume.All)
|
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var muted = IsMuted();
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
foreach(var v in Volume)
|
|
|
|
|
{
|
|
|
|
|
var guid = Guid.Empty;
|
|
|
|
|
v.SetMute(!muted, ref guid);
|
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMuted()
|
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
return Volume.All(x =>
|
|
|
|
|
{
|
|
|
|
|
x.GetMute(out var mute);
|
|
|
|
|
return mute;
|
|
|
|
|
});
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void IncrementVolumeLevel(int step, int ticks)
|
|
|
|
|
{
|
|
|
|
|
//if you have more than one volume. they will all get set based on the first volume control
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var volume = Volume.FirstOrDefault();
|
|
|
|
|
var level = 0f;
|
|
|
|
|
if (volume != null)
|
|
|
|
|
{
|
|
|
|
|
volume.GetMasterVolume(out level);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-10 22:06:38 -06:00
|
|
|
|
level = VolumeHelpers.GetAdjustedVolume(level, step, ticks);
|
2023-09-16 15:34:45 -06:00
|
|
|
|
|
|
|
|
|
foreach(var v in Volume)
|
|
|
|
|
{
|
|
|
|
|
var guid = Guid.Empty;
|
|
|
|
|
v.SetMasterVolume(level, ref guid);
|
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetVolumeLevel()
|
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var volume = Volume.FirstOrDefault();
|
|
|
|
|
var level = 0f;
|
|
|
|
|
if(volume != null)
|
|
|
|
|
{
|
|
|
|
|
volume.GetMasterVolume(out level);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-10 22:06:38 -06:00
|
|
|
|
return VolumeHelpers.GetVolumePercentage(level);
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
}
|