-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
310 lines (261 loc) · 12.8 KB
/
Program.cs
File metadata and controls
310 lines (261 loc) · 12.8 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using Diyokee;
using Diyokee.Components;
using Diyokee.Data;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Enc;
using Un4seen.Bass.AddOn.EncMp3;
using Un4seen.Bass.AddOn.Mix;
internal class Program {
public const int SAMPLING_FREQUENCY = 44100;
public static List<(int Handle, int DeviceIndex)> BassMixHandles = [];
public static int BassLatencyMs = 0;
public static ILogger Logger = null!;
public static Diyokee.Settings Settings = new();
public static List<MidiControllerProfile> MidiControllersProfiles = [];
public static MidiTools MidiTools = new();
private static async Task Main(string[] args) {
string workingDirectory = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
#if !DEBUG
Directory.SetCurrentDirectory(workingDirectory);
#endif
if(args.Length > 0 && ProcessArguments(args, workingDirectory)) return;
Settings = await Diyokee.Settings.Load();
MidiControllersProfiles = await MidiControllerProfile.LoadAll();
AutoSave();
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("CacheDB");
builder.WebHost.ConfigureKestrel(serverOptions => {
int kestrelPort = 5001;
string[] tokens = Settings.WebHostUrl.Split(":");
if(tokens.Length > 2 && int.TryParse(tokens[2], out int port)) kestrelPort = port;
serverOptions.ListenAnyIP(kestrelPort, listenOptions => {
if(File.Exists(Settings.CertFile)) {
listenOptions.UseHttps(Settings.CertFile, Settings.CertPassword);
}
});
});
if(Settings.WebHostUrl != "") {
builder.WebHost.UseUrls(Settings.WebHostUrl);
}
builder.Services.AddHttpContextAccessor();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options => {
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<SessionState>();
#if DEBUG
builder.Services.AddSassCompiler();
#endif
builder.Services.AddDbContextFactory<CacheDbContext>(options => options.UseSqlite(connectionString));
var app = builder.Build();
Logger = app.Logger;
Logger.LogInformation("Setting up BASS...");
InitBASS(workingDirectory);
SetupBASS();
app.Logger.LogInformation("Validating Cache Database...");
using(IServiceScope? scope = app.Services.CreateScope()) {
using(CacheDbContext? context = scope.ServiceProvider.GetService<CacheDbContext>()) {
context?.Database.Migrate();
//if(!context?.Database.EnsureCreated() ?? false) {
// if(context?.Database.GetPendingMigrations().Any() ?? false) {
// context.Database.Migrate();
// }
//}
}
}
if(!app.Environment.IsDevelopment()) {
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
if(File.Exists(Settings.CertFile)) app.UseHttpsRedirection();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Lifetime.ApplicationStopping.Register(() => {
Logger.LogInformation("Application stopping, saving settings...");
Settings.Save().Wait();
MidiControllerProfile.SaveAll(MidiControllersProfiles).Wait();
});
app.Lifetime.ApplicationStarted.Register(() => {
bool autoStart = Settings.AutoStartBrowser;
#if DEBUG
autoStart = false;
#endif
string line = new('—', 57 + Settings.WebHostUrl.Length);
Logger.LogInformation(
$"""
{line}
You may now open your browser and navigate to: {Settings.WebHostUrl}
{line}
""");
if(autoStart && Settings.WebHostUrl != "") {
Process.Start(new ProcessStartInfo {
FileName = Settings.WebHostUrl,
UseShellExecute = true
});
}
});
app.Run();
}
private static bool ProcessArguments(string[] args, string workingDirectory) {
foreach(string arg in args) {
switch(arg) {
case "--help":
case "-h":
Console.WriteLine("Usage: Diyokee [options]");
Console.WriteLine("Options:");
Console.WriteLine(" --help, -h Show this help message");
Console.WriteLine(" --version, -v Show version information");
Console.WriteLine(" --info Show version information about the files in the working directory");
return true;
case "--version":
case "-v":
string[] ignore = ["microsoft.", "system.", "diyokee", "mono."];
List<(string FileName, string FileVerson)> versions = [];
versions.Add(("Diyokee", typeof(Program).Assembly.GetName().Version?.ToString() ?? "N/A"));
FileInfo[] files = new DirectoryInfo(workingDirectory).GetFiles();
foreach(FileInfo file in files.Where(f => !ignore.Any(f.Name.ToLower().Contains)).OrderBy(f => f.Name)) {
try {
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(file.FullName);
string? ver = fvi.ProductVersion == "" ? fvi.FileVersion : fvi.ProductVersion;
if(ver != null) {
if(ver.IndexOf("+") > 0) ver = ver.Split("+")[0];
versions.Add((file.Name, ver));
}
} catch { }
}
int maxNameLength = versions.Max(v => v.FileName.Length) + 2;
foreach((string FileName, string FileVerson) in versions) {
Console.WriteLine($"{FileName}:{" ".PadRight(maxNameLength - FileName.Length)} {FileVerson}");
}
return true;
default:
Console.WriteLine($"Unknown argument: {arg}");
return false;
}
}
return false;
}
private static void AutoSave() { // FIXME: This is just... wrong...
Task.Run(async () => {
while(true) {
await Task.Delay(5000);
await Settings.Save();
await MidiControllerProfile.SaveAll(MidiControllersProfiles);
}
});
}
// TODO: Implement support to add/remove audio outputs without restarting the application
// For this to work this function needs to be called every time the Settings.Audio object changes
// and the BassMixHandles should be used to not re-initialize devices that have already been initialized and
// to remove those are not longer in use.
// Then, the Player.CreateStreams() function needs to be modified to update it's splitter streams accordingly.
private static void SetupBASS() {
int index = 0;
var devices = Settings.Audio.MainOutputDevice.Concat(Settings.Audio.MonitorDevice).ToList();
for(int i = 0; i < devices.Count; i++) {
AudioDevice device = devices[i];
int bassDeviceIndex = GetDeviceIndexByName(device.Name);
bool r = Bass.BASS_SetDevice(bassDeviceIndex);
if(!r || Bass.BASS_ErrorGetCode() != BASSError.BASS_OK) {
Logger.LogError($"Failed to set BASS device '{device.Name}': {Bass.BASS_ErrorGetCode()}");
if(i < Settings.Audio.MainOutputDevice.Count) {
Settings.Audio.MainOutputDevice.RemoveAt(i);
} else {
Settings.Audio.MonitorDevice.RemoveAt(i - Settings.Audio.MainOutputDevice.Count);
}
continue;
}
BASS_INFO basInfo = new();
Bass.BASS_GetInfo(basInfo);
BassLatencyMs = Math.Max(BassLatencyMs, basInfo.latency);
int handle = BassMix.BASS_Mixer_StreamCreate(SAMPLING_FREQUENCY, 8, BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_MIXER_NORAMPIN);
Bass.BASS_ChannelSetAttribute(handle, BASSAttribute.BASS_ATTRIB_BUFFER, 0);
Bass.BASS_ChannelPlay(handle, true);
BassMixHandles.Add((handle, index++));
}
// Attach to the first device, for now...
if(Settings.Encoder.Enabled && Runtime.Platform != Runtime.Platforms.MacApple) {
int encodeHandle = BassEnc_Mp3.BASS_Encode_MP3_Start(BassMixHandles.First().Handle, $"-b{Settings.Encoder.Bitrate}", BASSEncode.BASS_ENCODE_NOHEAD | BASSEncode.BASS_ENCODE_AUTOFREE, null, IntPtr.Zero);
_ = BassEnc.BASS_Encode_ServerInit(encodeHandle, $"{Settings.Encoder.Port}/{Settings.Encoder.Url}", 16384 / 2, 16384, BASSEncodeServer.BASS_ENCODE_SERVER_DEFAULT, null, IntPtr.Zero);
}
}
public static int GetDeviceIndexByName(string name) {
for(int i = 0; i < Bass.BASS_GetDeviceCount(); i++) {
BASS_DEVICEINFO deviceInfo = Bass.BASS_GetDeviceInfo(i);
if(deviceInfo.name == name) return i;
}
return 0; // No Sound
}
private static bool InitBASS(string workingDirectory) {
char c = Runtime.PathSeparator;
string platform = Runtime.Platform.ToString().ToLower();
string architecture = Environment.Is64BitProcess
|| Runtime.Platform == Runtime.Platforms.MacIntel
|| Runtime.Platform == Runtime.Platforms.MacApple
? "x64" : "x86";
if(platform.StartsWith("arm")) {
platform = "arm";
architecture = platform.EndsWith("hard") ? "hardfp" : "softfp"; // "armhf" : "armel";
} else if(platform.StartsWith("aarch64")) {
platform = "arm";
architecture = "aarch64";
} else if(platform == "macapple") {
platform = "mac";
architecture = "arm64";
} else if(platform == "macintel") {
platform = "mac";
architecture = "x64";
}
string srcDir = Path.Combine(Runtime.RunningDirectory, $"bass{c}{platform}{c}{architecture}{c}");
Logger.LogInformation(
$$"""
Platform: {{Runtime.Platform}}
Architecture: {{architecture}}
Libraries: {{Path.GetRelativePath(workingDirectory, srcDir)}}
""");
foreach(string srcFile in Directory.GetFiles(srcDir)) {
string trgFile = Path.Combine(Runtime.RunningDirectory, Path.GetFileName(srcFile));
if(File.Exists(trgFile)) File.Delete(trgFile);
try {
File.Copy(srcFile, trgFile, true);
} catch {
return false;
}
}
if(Settings.BassNetRegEmail != "" && Settings.BassNetRegKey != "") {
BassNet.Registration(Settings.BassNetRegEmail, Settings.BassNetRegKey);
}
Bass.BASS_PluginLoadDirectory(workingDirectory);
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_DEV_NONSTOP, 1);
SetupDevice(Settings.Audio.MainOutputDevice);
SetupDevice(Settings.Audio.MonitorDevice, false);
return true;
}
private static void SetupDevice(List<AudioDevice> devices, bool createIfNotSet = true) {
bool deviceIsSet = false;
int defaultDeviceIndex = -1;
for(int i = 0; i < Bass.BASS_GetDeviceCount(); i++) {
BASS_DEVICEINFO deviceInfo = Bass.BASS_GetDeviceInfo(i);
if(deviceInfo.IsDefault) defaultDeviceIndex = i;
Bass.BASS_GetDeviceInfo(i, deviceInfo);
if(devices.Any(d => d.Name == deviceInfo.name)) {
if(!deviceInfo.IsInitialized) {
deviceIsSet = Bass.BASS_Init(i, SAMPLING_FREQUENCY, BASSInit.BASS_DEVICE_DEFAULT | BASSInit.BASS_DEVICE_LATENCY, IntPtr.Zero);
if(!deviceIsSet) Logger.LogError($"Failed to initialize BASS device '{deviceInfo.name}': {Bass.BASS_ErrorGetCode()}");
}
}
}
if(!deviceIsSet && createIfNotSet) {
BASS_DEVICEINFO deviceInfo = Bass.BASS_GetDeviceInfo(defaultDeviceIndex);
devices.Add(new(deviceInfo.name, AudioDevice.DeviceSpeakers.FrontStereo));
SetupDevice(devices, false);
}
}
}