-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
165 lines (114 loc) · 5.33 KB
/
Program.cs
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
using System;
using System.Windows.Forms;
using CSCore.CoreAudioAPI;
using System.Collections.Generic;
using Fleck;
using System.IO;
using MixerController;
namespace MixerControllerF {
static class Global {
public static string DEFAULT_FOLDER = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MixerController";
public const string SETTINGS = "config.txt";
public static string DEFAULT_PATH = DEFAULT_FOLDER + "\\" + SETTINGS;
public static string COMPUTER_NAME = Environment.MachineName.ToString();
}
static class Program {
public static Dictionary<string, SoundApplication> applications = new Dictionary<string, SoundApplication>();
[MTAThread]
static void Main() {
if (!File.Exists(Global.DEFAULT_PATH)) {
Directory.CreateDirectory(Global.DEFAULT_FOLDER);
using (FileStream fs = File.Create(Global.DEFAULT_PATH)) {
byte[] info = new System.Text.UTF8Encoding(true).GetBytes("0.0.0.0:25565");
fs.Write(info, 0, info.Length);
}
}
string ip = "";
if (File.Exists(Global.DEFAULT_PATH)) {
using (StreamReader sr = File.OpenText(Global.DEFAULT_PATH)) {
ip = sr.ReadLine();
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var server = new WebSocketServer("ws://" + ip);
RefreshApps();
server.Start(socket => {
socket.OnOpen = () => RefreshApps();
socket.OnClose = () => RefreshApps();
socket.OnMessage = message => {
if (message.StartsWith("VOLUME")) {
ChangeVolume(message.Split(':')[1], float.Parse(message.Split(':')[2]));
}
if (message.StartsWith("MUTE")) {
ChangeState(message.Split(':')[1], message.Split(':')[0]);
}
if (message.StartsWith("UNMUTE")) {
ChangeState(message.Split(':')[1], message.Split(':')[0]);
}
if(message.StartsWith("INFO")) {
string to_send = "{\"info\":{\"computer\":\""+Global.COMPUTER_NAME+"\"}}";
socket.Send(to_send);
}
if (message.StartsWith("APPS")) {
RefreshApps();
string to_send = "{\"apps\":{";
foreach (KeyValuePair<string, SoundApplication> entry in applications) {
to_send += "\"" + entry.Key + "\":\"" + entry.Value.Volume + "\",";
}
to_send = to_send.Remove(to_send.Length - 1);
to_send += "}}";
socket.Send(to_send);
}
};
});
Application.Run(new DefaultApp());
}
public static SoundApplication GetApplication(string app_name) {
if (applications.ContainsKey(app_name)) {
applications.TryGetValue(app_name, out SoundApplication app);
return app;
}
return null;
}
public static void ChangeVolume(string app_name, float volume) {
SoundApplication app = GetApplication(app_name);
if (app == null) return;
if (app.Volume > 0 && app.AudioInt.IsMuted) {
app.AudioInt.IsMuted = false;
}
app.AudioInt.MasterVolume = (volume / 100);
RefreshApps();
}
public static void ChangeState(string app_name, string state) {
SoundApplication app = GetApplication(app_name);
if (app == null) return;
app.AudioInt.IsMuted = state == "MUTE";
RefreshApps();
}
public static void RefreshApps() {
applications.Clear();
using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render)) {
using (var sessionEnumerator = sessionManager.GetSessionEnumerator()) {
foreach (var session in sessionEnumerator) {
var simpleVolume = session.QueryInterface<SimpleAudioVolume>();
using (var sessionControl = session.QueryInterface<AudioSessionControl2>()) {
string name = sessionControl.Process.ProcessName;
float current_volume = simpleVolume.IsMuted ? -1 : simpleVolume.MasterVolume;
if (!applications.ContainsKey(name)) {
applications.Add(name, new SoundApplication(current_volume, ref simpleVolume));
}
}
}
}
}
}
private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow) {
using (var enumerator = new MMDeviceEnumerator()) {
using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia)) {
return AudioSessionManager2.FromMMDevice(device);
}
}
}
}
}