-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
184 lines (147 loc) · 5.51 KB
/
Copy pathProgram.cs
File metadata and controls
184 lines (147 loc) · 5.51 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace TelegramDesktopAudioVolumeFix
{
class Program
{
#region Fields
private static MMDevice device;
private static ConsoleCtrlDelegate consoleHandler;
private static float previousVolume;
private static bool isTelegramPlaying;
private static readonly string appName = "TelegramDesktopAudioVolumeFix";
private static readonly string appVersion = "v2";
private static readonly string telegramProcessName = "Telegram";
private static readonly int checkInterval = 1000;
private static readonly float defaultMaxVolume = 1.0f;
#endregion
#region Main Life Cycle
private static void Main()
{
Console.WriteLine($"Starting {appName} {appVersion} ...\n");
Mutex mutex = new Mutex(true, appName, out bool createdNew);
if (!createdNew)
return;
InitializeApp();
RunApp();
}
private static void InitializeApp()
{
Console.Title = appName;
consoleHandler = new ConsoleCtrlDelegate(ConsoleCtrlHandler);
SetConsoleCtrlHandler(consoleHandler, true);
}
private static void RunApp()
{
Console.WriteLine($"{appName} {appVersion} has been started!\n");
while (true)
{
DebugLog("Checking ...");
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
bool isPlaying = IsTelegramPlaying();
if (isPlaying && (!isTelegramPlaying))
{
DebugLog($"Telegram started playing");
previousVolume = device.AudioEndpointVolume.MasterVolumeLevelScalar;
if (previousVolume > 0)
{
device.AudioEndpointVolume.MasterVolumeLevelScalar = defaultMaxVolume;
DebugLog($"Volume set to {defaultMaxVolume:P0} (was {previousVolume:P0})");
}
isTelegramPlaying = true;
}
else if (!isPlaying && isTelegramPlaying)
{
DebugLog($"Telegram stopped playing");
if (previousVolume > 0)
{
device.AudioEndpointVolume.MasterVolumeLevelScalar = previousVolume;
DebugLog($"Volume restored to {previousVolume:P0}");
}
isTelegramPlaying = false;
}
Thread.Sleep(checkInterval);
}
}
private static bool IsTelegramPlaying()
{
AudioSessionManager sessionManager = device.AudioSessionManager;
SessionCollection sessions = sessionManager.Sessions;
for (int i = 0; i < sessions.Count; i++)
{
AudioSessionControl session = sessions[i];
try
{
uint pid = session.GetProcessID;
DebugLog($"Current pid name: {Process.GetProcessById((int)pid)}");
if (pid == 0)
continue;
using (Process process = Process.GetProcessById((int)pid))
{
if (process.ProcessName == telegramProcessName)
{
if (session.State == AudioSessionState.AudioSessionStateActive)
return true;
}
}
}
catch (Exception ex)
{
DebugLog(ex.Message);
}
}
return false;
}
#endregion
#region Exit Handle
private delegate bool ConsoleCtrlDelegate(CtrlTypes ctrlType);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handlerRoutine, bool add);
private static bool ConsoleCtrlHandler(CtrlTypes ctrlType)
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
return true;
case CtrlTypes.CTRL_BREAK_EVENT:
return true;
case CtrlTypes.CTRL_CLOSE_EVENT:
ExitHandler();
return false;
case CtrlTypes.CTRL_LOGOFF_EVENT:
ExitHandler();
return false;
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
ExitHandler();
return false;
}
return false;
}
private static void ExitHandler()
{
if (previousVolume > 0)
device.AudioEndpointVolume.MasterVolumeLevelScalar = previousVolume;
}
private enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
#endregion
#region Secondary
[Conditional("DEBUG")]
private static void DebugLog(string message)
{
Console.WriteLine(message);
}
#endregion
}
}