-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
364 lines (321 loc) · 13.6 KB
/
MainWindow.xaml.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using CommunityToolkit.Mvvm.Messaging;
using LibVLCSharp.Shared;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using RipShout.Helpers;
using RipShout.Models;
using RipShout.Services;
using RipShout.ViewModels;
using RipShout.Views;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls.Interfaces;
using Wpf.Ui.Interop.WinDef;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.Mvvm.Services;
using Wpf.Ui.TaskBar;
using static CommunityToolkit.Mvvm.ComponentModel.__Internals.__TaskExtensions.TaskAwaitableWithoutEndValidation;
namespace RipShout;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : INavigationWindow
{
public ViewModels.MainViewModel ViewModel { get; set; }
private bool initialized = false;
public MainWindow(MainViewModel viewModel, INavigationService navigationService,
IPageService pageService, ISnackbarService snackbarService)
{
ViewModel = viewModel;
DataContext = this;
InitializeComponent();
navigationService.SetNavigationControl(RootNavigation);
SetPageService(pageService);
snackbarService.SetSnackbarControl(RootSnackbar);
}
private void InvokeSplashScreen()
{
if(initialized)
{
return;
}
initialized = true;
// Location has to happen outside constructor
//if(App.MySettings.LastWindowY >= 0 && App.MySettings.LastWindowX >= 0)
//{
// this.Left = App.MySettings.LastWindowX;
// this.Top = App.MySettings.LastWindowY;
//}
RootMainGrid.Visibility = Visibility.Collapsed;
RootWelcomeGrid.Visibility = Visibility.Visible;
Wpf.Ui.TaskBar.TaskBarProgress.SetValue(this, Wpf.Ui.TaskBar.TaskBarProgressState.Indeterminate, 0);
Task.Run(async () =>
{
// Remember to always include Delays and Sleeps in
// your applications to be able to charge the client for optimizations later. ;)
try
{
await App.LoadChannels();
await Dispatcher.InvokeAsync(() =>
{
RootWelcomeGrid.Visibility = Visibility.Hidden;
RootMainGrid.Visibility = Visibility.Visible;
RootNavigation.Navigate(typeof(StationsPage));
Wpf.Ui.TaskBar.TaskBarProgress.SetValue(this, Wpf.Ui.TaskBar.TaskBarProgressState.None, 0);
});
}
catch(Exception ex)
{
GeneralHelpers.WriteLogEntry(ex.ToString(), GeneralHelpers.LogFileType.Exception);
}
return true;
});
}
public Frame GetFrame()
{
return RootFrame;
}
public INavigation GetNavigation()
{
return RootNavigation;
}
public bool Navigate(Type pageType)
{
return RootNavigation.Navigate(pageType);
}
public void SetPageService(IPageService pageService)
{
RootNavigation.PageService = pageService;
}
public void ShowWindow()
{
Show();
}
public void CloseWindow()
{
Close();
}
private void UiWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.SavePlacement();
}
private void UiWindow_Loaded(object sender, RoutedEventArgs e)
{
InvokeSplashScreen();
//Test();
}
#region This was a test to get the audio from the sound card output because DI / RadioTunes doesn't
// Let you do 2 streams at once on premium accounts && using the custom stream with the overridden Read
// causes issues with buffering that I simply can't find a way to fix. This was plan B but it's not
// great beacuse it's not a direct stream from the source, it's the sound card output which is affected
// by the volume of the sound card. I'm leaving this here for now because it's a good example of how to
// do this if I ever need to do it again.
//void Test()
//{
// var url = "http://prem4.radiotunes.com:80/poprock?fd1ce46ac9b3ce6b357cb5c1";
// Core.Initialize();
// using(var libVLC = new LibVLC(enableDebugLogs: true, "--no-video", "--network-caching=4000"))
// {
// libVLC.Log += (s, e) =>
// {
// Trace.WriteLine($"[{e.Level}] {e.Module}:{e.Message}");
// };
// using(var mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(libVLC))
// {
// mediaPlayer.Volume = 40;
// using(var media = new Media(libVLC, new Uri(url)))
// {
// media.StateChanged += (s, e) =>
// {
// Trace.WriteLine($"Media state changed to {e}");
// };
// mediaPlayer.ChapterChanged += (s, e) =>
// {
// Trace.WriteLine($"Chapter changed to {e}");
// };
// media.MetaChanged += (s, e) =>
// {
// Trace.WriteLine($"Meta changed to {e}");
// };
// media.AddOption(":no-video");
// media.AddOption(":network-caching=4000");
// mediaPlayer.Media = media;
// mediaPlayer.Play();
// var outputFolder = "c:\\Test\\";
// var outputFilePath = System.IO.Path.Combine(outputFolder, "fontCut.wav");
// //var waveIn = new WaveInEvent();
// var waveIn = new WasapiLoopbackCapture();
// WaveFileWriter writer = new WaveFileWriter(outputFilePath, waveIn.WaveFormat);
// waveIn.DataAvailable += (s, a) =>
// {
// writer.Write(a.Buffer, 0, a.BytesRecorded);
// };
// waveIn.StartRecording();
// var currentTitle = "";
// while(true)
// {
// var foo5 = media.Meta(MetadataType.NowPlaying);
// if(!string.IsNullOrEmpty(foo5))
// {
// if(foo5 != currentTitle)
// {
// //waveIn.StopRecording();
// writer.Dispose();
// outputFilePath = System.IO.Path.Combine(outputFolder, foo5 + ".wav");
// writer = new WaveFileWriter(outputFilePath, waveIn.WaveFormat);
// if(!string.IsNullOrEmpty(currentTitle))
// {
// outputFilePath = System.IO.Path.Combine(outputFolder, currentTitle + ".wav");
// var mp3Path = System.IO.Path.Combine(outputFolder, currentTitle + ".mp3");
// try
// {
// using(var reader = new WaveFileReader(outputFilePath))
// {
// MediaFoundationEncoder.EncodeToMp3(reader, mp3Path);
// }
// }
// catch(Exception ex)
// {
// var loooo = ex;
// }
// }
// //waveIn.StartRecording();
// currentTitle = foo5;
// }
// }
// Thread.Sleep(500);
// }
// }
// }
// }
//}
#endregion
#region A test of how to do all this with one stream because of audioaddict limiting premimum accounts 1 stream at a time
//void Test()
//{
// var url = "http://prem4.radiotunes.com:80/the80s?fd1ce46ac9b3ce6b357cb5c1";
// Core.Initialize();
// using(var libVLC = new LibVLC(enableDebugLogs: true, "--no-video", "--network-caching=4000"))
// {
// libVLC.Log += (s, e) =>
// {
// Trace.WriteLine($"[{e.Level}] {e.Module}:{e.Message}");
// };
// using(var mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(libVLC))
// {
// mediaPlayer.Volume = 100;
// using(var media = new Media(libVLC, new Uri(url)))
// {
// media.AddOption(":no-video");
// media.AddOption(":network-caching=4000");
// mediaPlayer.Media = media;
// var outputFolder = "c:\\Test\\";
// var outputFilePath = System.IO.Path.Combine(outputFolder, "frontCut.wav");
// using var outputDevice = new WaveOutEvent();
// var waveFormat = new WaveFormat(8000, 16, 1);
// var writer = new WaveFileWriter(outputFilePath, waveFormat);
// var waveProvider = new BufferedWaveProvider(waveFormat);
// outputDevice.Init(waveProvider);
// mediaPlayer.SetAudioFormatCallback(AudioSetup, AudioCleanup);
// mediaPlayer.SetAudioCallbacks(PlayAudio, PauseAudio, ResumeAudio, FlushAudio, DrainAudio);
// mediaPlayer.Play();
// outputDevice.Play();
// var currentTitle = "frontCut";
// bool isFrontCut = true;
// while(true)
// {
// Thread.Sleep(500);
// var foo5 = media.Meta(MetadataType.NowPlaying);
// var foo56 = media.Meta(MetadataType.Genre);
// if(!string.IsNullOrEmpty(foo5))
// {
// if(foo5 != currentTitle)
// {
// writer.Flush();
// waveProvider.ClearBuffer();
// writer.Dispose();
// outputFilePath = System.IO.Path.Combine(outputFolder, foo5 + ".wav");
// writer = new WaveFileWriter(outputFilePath, waveFormat);
// if(currentTitle != "frontCut")
// {
// outputFilePath = System.IO.Path.Combine(outputFolder, currentTitle + ".wav");
// if(isFrontCut)
// {
// currentTitle = "[FrontCut]" + currentTitle;
// }
// var mp3Path = System.IO.Path.Combine(outputFolder, currentTitle + ".mp3");
// using(var reader = new WaveFileReader(outputFilePath))
// {
// MediaFoundationEncoder.EncodeToMp3(reader, mp3Path);
// }
// isFrontCut = false;
// }
// currentTitle = foo5;
// }
// }
// }
// void PlayAudio(IntPtr data, IntPtr samples, uint count, long pts)
// {
// int bytes = (int)count * 2; // (16 bit, 1 channel)
// var buffer = new byte[bytes];
// Marshal.Copy(samples, buffer, 0, bytes);
// waveProvider.AddSamples(buffer, 0, bytes);
// writer.Write(buffer, 0, bytes);
// }
// int AudioSetup(ref IntPtr opaque, ref IntPtr format, ref uint rate, ref uint channels)
// {
// channels = (uint)waveFormat.Channels;
// rate = (uint)waveFormat.SampleRate;
// return 0;
// }
// void DrainAudio(IntPtr data)
// {
// writer.Flush();
// }
// void FlushAudio(IntPtr data, long pts)
// {
// writer.Flush();
// waveProvider.ClearBuffer();
// }
// void ResumeAudio(IntPtr data, long pts)
// {
// outputDevice.Play();
// }
// void PauseAudio(IntPtr data, long pts)
// {
// outputDevice.Pause();
// }
// void AudioCleanup(IntPtr opaque)
// {
// }
// }
// }
// }
//}
#endregion
private void UiWindow_SourceInitialized(object sender, EventArgs e)
{
this.ApplyPlacement();
}
}