-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.vb
More file actions
3117 lines (2415 loc) · 101 KB
/
Copy pathForm1.vb
File metadata and controls
3117 lines (2415 loc) · 101 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.ComponentModel
Imports System.Configuration
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography.X509Certificates
Imports System.Text.Json
Imports System.Threading
Imports System.Timers
Imports gTrackBar
Imports LibVLCSharp.Shared
Imports LibVLCSharp.WinForms
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms
Imports Microsoft.WindowsAPICodePack.Shell
Imports Microsoft.WindowsAPICodePack.Shell.PropertySystem
Imports NAudio.CoreAudioApi
Imports NAudio.Dsp
Imports NAudio.FileFormats
Imports NAudio.Wave
Imports Spectrum.Form1
Imports TagLib
Imports TagLib.Ape
Imports TagLib.Riff
Imports YamlDotNet.Serialization
Imports YamlDotNet.Serialization.NamingConventions
'if you see name pattern changes such as beginning with uppercase or lowercase, it depends on my mood at that time of creating the variables
Public Class Form1
Public ItemsBackup As New List(Of ListViewItem)
Dim _libVLC As New LibVLC("--aout=wasapi")
Public _mediaPlayer As New MediaPlayer(_libVLC)
Public loopback As WasapiLoopbackCapture
Public IsPlaylistLoaded As Boolean = False
Public IsDefaultArt As Boolean = True
Public HowLooping As String = My.Settings.repeatMethod 'none, one, all
Dim ShuffledIndexes As New List(Of Integer)
Dim CurrentShufflePos As Integer = 0
Public MusicCache As New List(Of SongInfo)
Public VideoCache As New List(Of VideoInfo)
'public ImageCache As New List(Of ImageInfo)
Public RadioCache As New List(Of RadioStation)
Public PLCache As New List(Of SpectrumPlaylist)
Public ReadOnly AudioExts As HashSet(Of String) =
New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
".mp3", ".flac", ".wav", ".m4a", ".ogg", ".aac", ".opus"
}
Public ReadOnly VideoExts As HashSet(Of String) =
New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
".mp4", ".avi", ".mov", ".wmv", ".mpg", ".mpeg", ".mkv", ".webm"
}
Public Enum PlaylistKind
AudioOnly
VideoOnly
Mixed
Unknown
End Enum
Public Const BatchSize As Integer = 1
Public lastUiIndex As Integer = 0
Public isUserScrubbing As Boolean = False
Public canSaveSettings = False
Dim WasMutedDuringStart As Boolean = True
Dim capture As WasapiLoopbackCapture
Dim fftSize As Integer = 1024
Dim fftBuffer(fftSize - 1) As NAudio.Dsp.Complex
Dim fftPos As Integer = 0
Dim fftResults(fftSize \ 2 - 1) As Single
Enum TrackType
Music
Video
Radio
Disc
End Enum
Public CurrentTrackType As TrackType
'rivate AlbumArtManager As AlbumArtManager
'AddHandler() _mediaPlayer.EndReached, AddressOf OnMediaEnded
Sub StartAudioCapture()
capture = New WasapiLoopbackCapture()
AddHandler capture.DataAvailable, AddressOf OnDataAvailable
capture.StartRecording()
End Sub
Public Function DetectPlaylistKind(files As List(Of String)) As PlaylistKind
Dim hasAudio = False
Dim hasVideo = False
For Each f In files
Dim ext = IO.Path.GetExtension(f)
If AudioExts.Contains(ext) Then hasAudio = True
If VideoExts.Contains(ext) Then hasVideo = True
If hasAudio AndAlso hasVideo Then
Return PlaylistKind.Mixed
End If
Next
If hasAudio Then Return PlaylistKind.AudioOnly
If hasVideo Then Return PlaylistKind.VideoOnly
Return PlaylistKind.Unknown
End Function
Public Sub OnDataAvailable(sender As Object, e As WaveInEventArgs)
For i = 0 To e.BytesRecorded - 1 Step 4
Dim sample As Single = BitConverter.ToSingle(e.Buffer, i)
fftBuffer(fftPos).X = sample
fftBuffer(fftPos).Y = 0
fftPos += 1
If fftPos >= fftSize Then
fftPos = 0
PerformFFT()
End If
Next
End Sub
Sub PerformFFT()
Dim buffer = CType(fftBuffer.Clone(), NAudio.Dsp.Complex())
FastFourierTransform.FFT(
True,
CInt(Math.Log(fftSize, 2)),
buffer
)
For i = 0 To fftResults.Length - 1
Dim mag = Math.Sqrt(buffer(i).X * buffer(i).X + buffer(i).Y * buffer(i).Y)
fftResults(i) = CSng(Math.Min(mag * 8, 1.0F))
Next
SendFFTToWeb2()
End Sub
Sub SendFFTToWeb()
If visualizer.CoreWebView2 Is Nothing Then Exit Sub
Dim json = System.Text.Json.JsonSerializer.Serialize(fftResults)
visualizer.CoreWebView2.PostWebMessageAsJson(json)
End Sub
Sub SendFFTToWeb2()
If InvokeRequired Then
BeginInvoke(Sub() SendFFTToWeb())
Else
SendFFTToWeb()
End If
End Sub
Sub InitControls()
'If _mediaPlayer.IsPlaying Then
playBtn.Enabled = True
stopBtn.Enabled = True
repeatBtn.Enabled = True
playSlider.Enabled = True
speedBtn.Enabled = True
'playSlider.Value = 0
nextBtn.Enabled = True
previousBtn.Enabled = True
shuffleBtn.Enabled = True
playbackTimer.Start()
If _mediaPlayer.IsPlaying Then
playBtn.Text = "❚❚"
Else
playBtn.Text = "▶"
End If
'End If
End Sub
Public playOrder As New List(Of Integer)
Public currentIndex As Integer = -1
Public isShuffle As Boolean = My.Settings.wantsToShuffle
Public rng As New Random()
Public Sub ShuffleList(list As List(Of Integer))
'MsgBox(ListView1.Items.Count)
For i = ListView1.Items.Count - 1 To 1 Step -1
Dim j = rng.Next(i + 1)
Dim temp = list(i)
list(i) = list(j)
list(j) = temp
Next
End Sub
Sub PlayFromNextBtn()
Dim currentFileIndex = GetIndexByTag(currentFile)
ListView1.SelectedItems.Clear()
Dim ni As Integer = currentFileIndex + 1
ListView1.Items(ni).Selected = True
ListView1.Items(ni).Focused = True
PlayFile(ListView1.Items(ni).Tag.ToString())
currentFile = ListView1.Items(ni).Tag.ToString()
End Sub
Sub PlayFromPrevBtn()
Dim currentFileIndex = GetIndexByTag(currentFile)
ListView1.SelectedItems.Clear()
Dim ni As Integer = currentFileIndex - 1
ListView1.Items(ni).Selected = True
ListView1.Items(ni).Focused = True
PlayFile(ListView1.Items(ni).Tag.ToString())
currentFile = ListView1.Items(ni).Tag.ToString()
End Sub
Public Function GetIndexByTag(ByVal tagValue As Object) As Integer
' Loop through each item in the ListView
For Each item As ListViewItem In ListView1.Items
' Check if the current item's Tag matches the search tagValue
If item.Tag IsNot Nothing AndAlso item.Tag.Equals(tagValue) Then
' If found, return the zero-based index
Return item.Index
End If
Next
' If the item is not found, return -1 (or handle as appropriate)
Return 0
End Function
Public Sub BuildPlayOrder()
playOrder.Clear()
For i = 0 To ListView1.Items.Count - 1
playOrder.Add(i)
Next
If isShuffle Then
ShuffleList(playOrder)
End If
currentIndex = -1
End Sub
Public Sub PlayNext()
If playOrder.Count = 0 Then Exit Sub
If Not isShuffle Then Exit Sub
playingNextSeq = True
currentIndex += 1
If currentIndex >= playOrder.Count Then
If isShuffle Then
ShuffleList(playOrder)
currentIndex = 0
Else
currentIndex = playOrder.Count - 1
Exit Sub
End If
End If
Dim lvIndex = playOrder(currentIndex)
Try
Dim item = ListView1.Items(lvIndex)
item.Selected = True
item.EnsureVisible()
Dim filePath = item.Tag.ToString()
PlayFile(filePath)
currentFile = ListView1.Items(lvIndex).Tag.ToString()
Catch ex As Exception
End Try
End Sub
'Dim bk As New List(Of ListViewItem)()
Sub ShowPlayer()
videoViewPane.Visible = True
videoViewPane.Dock = DockStyle.Fill
ListView1.Visible = False
ListView1.Dock = DockStyle.Top
End Sub
Dim playingNextSeq As Boolean = False
Dim lastIcon As String = ""
Dim curSongInfos() As String = {}
Dim cursonginfos2() As String
Dim hasBeenSwitched As Short = 0
Sub PFCodeMain(path As String, Optional pos As Long = 0)
'MsgBox(hasBeenSwitched)
lastTab = curTabIndex
If Not hasBeenSwitched >= 1 Then ChooseSBTab(8)
InitControls()
If lastTab = 4 Or curTabIndex = 4 Then
labelSong.Text = ""
labelArtist.Text = ""
End If
fieldI = 0
If pos = 0 Then
Using media As New Media(_libVLC, New Uri(path))
_mediaPlayer.Time = pos
'MsgBox(pos & ", " & _mediaPlayer.Time)
hasBeenSwitched += 1
'MsgBox(hasBeenSwitched)
_mediaPlayer.Play(media)
End Using
Else
_mediaPlayer.Time = pos
_mediaPlayer.Play()
End If
playbackTimer.Start()
playBtn.Text = "❚❚"
Dim title = ""
Dim curSong As String = ""
Dim curArtists As String = ""
If ((lastTab = 0 OrElse lastTab = 1) And (curTabIndex <> 6)) Or (HasExtension(currentFile, {".mp3", ".flac", ".wav", ".m4a", ".ogg", ".aac", ".opus"})) Or curTabIndex = 9 Or lastTab = 9 Then
If EnableVisualizationsToolStripMenuItem.Checked Then
ShowVisualizerAsync()
Else
HideVisualizer()
End If
Try
Using t = TagLib.File.Create(path)
title = If(String.IsNullOrEmpty(t.Tag.Title),
IO.Path.GetFileNameWithoutExtension(path),
t.Tag.Title)
Dim artist = String.Join(", ", t.Tag.Performers)
If labelSong.Text <> curSong Then
labelSong.Text = title
labelSong.ForeColor = Color.White
labelArtist.Text = artist
songTextQA.Text = title
Else
labelSong.Text = curSong
labelSong.ForeColor = Color.White
labelArtist.Text = curArtists
songTextQA.Text = curSong
End If
''debug.writeline("artists: " & String.Join(", ", t.Tag.Performers))
curSongInfos = {
"Now Playing: " & title,
If(Not String.IsNullOrEmpty(t.Tag.Album), "From " & t.Tag.Album, ""),
If(t.Tag.Performers IsNot Nothing, "By " & String.Join(", ", t.Tag.Performers), ""),
If(t.Tag.Year = Nothing, "", "Released in " & t.Tag.Year.ToString()),
If(t.Tag.Genres Is Nothing OrElse t.Tag.Genres.Length = 0, "", "Based on " & String.Join(", ", t.Tag.Genres))
}
cursonginfos2 = curSongInfos.Where(Function(s) Not String.IsNullOrWhiteSpace(s)).ToArray()
curSong = title
curArtists = artist
If t.Tag.Pictures.Length > 0 Then
Using ms As New IO.MemoryStream(t.Tag.Pictures(0).Data.Data)
Dim img = Image.FromStream(ms)
IsDefaultArt = False
quickActionsPB.Image = img
playbackAlbumArt.Image = quickActionsPB.Image
'img.Dispose()
End Using
Else
IsDefaultArt = True
If HasExtension(path, {".mp4", ".avi", ".mov", ".wmv", ".mpg", ".mpeg", ".mkv", ".webm"}) Then
quickActionsPB.Image = GetFileThumbnail(path, 128)
Else
GetIcons()
ChooseCorrectIcon()
End If
playbackAlbumArt.Image = quickActionsPB.Image
End If
End Using
Catch ex As Exception
If curTabIndex = 6 Then
MsgBox(currentFile)
End If
End Try
Try
currentFile = ListView1.SelectedItems(0).Tag.ToString()
nowPlayingText.Text = "Now Playing: " & title.ToString()
Dim ext = IO.Path.GetExtension(currentFile).ToLower()
If {".mp3", ".flac", ".wav", ".m4a", ".ogg", ".aac", ".opus"}.Contains(ext) Then
nowPlayingInfo.Visible = True
nowPlayingCycle.Start()
Else
Try
nowPlayingCycle.Stop()
nowPlayingInfo.Visible = False
Catch ex As Exception
End Try
End If
Catch ex As Exception
End Try
ElseIf lastTab = 2 Or (HasExtension(path, {".mp4", ".avi", ".mov", ".wmv", ".mpg", ".mpeg", ".mkv", ".webm"})) Then
nowPlayingCycle.Stop()
nowPlayingInfo.Visible = False
songTextQA.Text = IO.Path.GetFileNameWithoutExtension(currentFile)
labelSong.Text = songTextQA.Text
quickActionsPB.Image = GetFileThumbnail(path, 128)
playbackAlbumArt.Image = quickActionsPB.Image
labelArtist.Text = ""
labelSong.ForeColor = Color.White
nowPlayingInfo.Visible = False
ElseIf lastTab = 4 Then
If ListView1.SelectedItems(0).SubItems(3).Text <> "Offline" And ListView1.SelectedItems(0).SubItems(3).Text <> "Checking" Then
labelSong.Text = songTextQA.Text
labelSong.ForeColor = Color.White
playbackAlbumArt.Image = quickActionsPB.Image
labelArtist.Text = "Live"
playSlider.Enabled = False
If My.Settings.showVizOnStart Then
EnableVisualizationsToolStripMenuItem.Checked = True
nowPlayingText.Visible = False
visualizer.Visible = True
Else
EnableVisualizationsToolStripMenuItem.Checked = False
visualizer.Visible = False
nowPlayingText.Visible = True
End If
If EnableVisualizationsToolStripMenuItem.Checked Then
ShowVisualizerAsync()
Else
HideVisualizer()
End If
Else
MsgBox("Radio is either offline or in an undetermined status. Please check your stream URL or try again later.", MsgBoxStyle.Critical, "Error")
_mediaPlayer.Stop()
capture.StopRecording()
ChooseSBTab(lastTab)
End If
End If
If lastTab = 2 Then
VideoView1.Visible = True
End If
End Sub
Sub PlayFile(path As String, Optional pos As Long = 0)
If lastTab <> 6 Then PFCodeMain(path, pos)
End Sub
Dim songsList As New List(Of ListViewItem)
'public Sub AddSong(path As String)
' Try
' 'ListView1.BeginUpdate()
' Using t = TagLib.File.Create(path)
' Dim title = If(String.IsNullOrEmpty(t.Tag.Title),
' IO.Path.GetFileNameWithoutExtension(path),
' t.Tag.Title)
' Dim artist = String.Join(", ", t.Tag.Performers)
' Dim duration = t.Properties.Duration
' Dim item As New ListViewItem(title)
' item.SubItems.Add(duration.ToString("hh\:mm\:ss"))
' item.SubItems.Add(t.Tag.Album)
' item.SubItems.Add(artist)
' If Not t.Tag.Year = 0 Then
' item.SubItems.Add(CInt(t.Tag.Year).ToString())
' Else
' item.SubItems.Add("")
' End If
' item.Tag = path
' item.SubItems.Add(t.Tag.JoinedGenres)
' ListView1.Items.Add(item)
' If t.Tag.Pictures.Length > 0 Then
' Using ms As New IO.MemoryStream(t.Tag.Pictures(0).Data.Data)
' Dim img = Image.FromStream(ms)
' ImageList1.Images.Add(img)
' item.ImageIndex = ImageList1.Images.Count - 1
' End Using
' End If
' For Each songitem As ListViewItem In ListView1.Items
' songsList.Add(CType(item.Clone(), ListViewItem))
' Next
' End Using
' 'ListView1.EndUpdate()
' Catch ex As TagLib.CorruptFileException
' Catch ex As Exception
' End Try
'End Sub
Public Sub AddSong(filePath As String)
Dim song = ReadSongMetadata(filePath)
If song Is Nothing Then Exit Sub
SyncLock MusicCache
MusicCache.Add(song)
End SyncLock
End Sub
Dim currentFile As String = ""
Public Sub ScaninBG()
End Sub
Public Sub StartMusicScan()
Task.Run(Sub()
ScanMusic(_scanCts.Token)
' scan is DONE here
ScanCompleted()
End Sub)
End Sub
Public Sub StartPLScan()
Task.Run(Sub()
ScanPL(_scanCts.Token)
' scan is DONE here
ScanCompleted()
End Sub)
End Sub
Public Sub ScanPL(token As CancellationToken)
If InvokeRequired Then
BeginInvoke(Sub()
ScanPL(_scanCts.Token)
End Sub)
End If
Dim addedCount As Integer = 0
lastUiIndex = 0
PLCache.Clear()
If InvokeRequired Then
BeginInvoke(Sub() ListView1.Items.Clear())
Else
ListView1.Items.Clear()
End If
For Each file In IO.Directory.EnumerateFiles(
My.Settings.playlistLocs.ToString(), "*.*", SearchOption.AllDirectories)
If token.IsCancellationRequested Then Exit Sub
Dim ext = IO.Path.GetExtension(file).ToLower()
If Not {".specpl", ".specplx", ".spplx"}.Contains(ext) Then
Continue For
End If
Me.BeginInvoke(Sub() AddToToolStripMenuItem.DropDownItems.Clear())
SyncLock PLCache
Dim newPL = SpectrumPlaylist.Load(file)
PLCache.Add(newPL)
Me.BeginInvoke(Sub()
Dim newPX As New ToolStripMenuItem(newPL.Name)
newPX.Tag = newPL.PLPath
cpl2 = newPX.Tag.ToString()
AddHandler newPX.Click, AddressOf newPX_Click
AddToToolStripMenuItem.DropDownItems.Add(newPX)
End Sub)
End SyncLock
Next
''debug.writeline("Songs scanned: " & MusicCache.Count)
'BatchUpdateUI()
End Sub
Public cpl2 As String = ""
Sub newPX_Click()
If ListView1.SelectedItems.Count > 0 Then
If Not String.IsNullOrEmpty(cpl2) Then
Dim newPL = SpectrumPlaylist.Load(cpl2)
For Each item As ListViewItem In ListView1.SelectedItems
If Not newPL.Files.Contains(item.Tag.ToString()) Then
newPL.Files.Add(item.Tag.ToString())
MsgBox("Selected files successfully added to playlist!", MsgBoxStyle.Information, "Success")
Else
MsgBox("Selected files are already in the playlist!", MsgBoxStyle.Critical, "Error")
End If
Next
newPL.Save()
End If
End If
End Sub
Sub GetPlaylistFiles()
ListView1.AllowDrop = True
ListView1.Items.Clear()
curTabIndex = 9
currentPlaylist = SpectrumPlaylist.Load(currentFile)
For Each filePath In currentPlaylist.Files
If Not IO.File.Exists(filePath) Then Continue For
Using t = TagLib.File.Create(filePath)
Dim item As New ListViewItem(If(String.IsNullOrEmpty(t.Tag.Title),
IO.Path.GetFileNameWithoutExtension(filePath),
t.Tag.Title))
item.SubItems.Add(IO.Path.GetExtension(filePath))
item.SubItems.Add(GetVideoDuration(filePath).ToString("hh\:mm\:ss")) 'works for audio + video
item.Tag = filePath
ListView1.Items.Add(item)
End Using
Next
CacheOriginalOrder()
curTabIndex = 9
lastTab = 6
BuildPlayOrder()
End Sub
Public Sub StartVideoScan()
Task.Run(Sub()
ScanVideo(_scanCts.Token)
' scan is DONE here
ScanCompleted()
End Sub)
End Sub
'public Sub StartImageScan()
' Task.Run(Sub()
' ScanImage(_scanCts.Token)
' ' scan is DONE here
' ScanCompleted()
' End Sub)
'End Sub
Public Sub ScanCompleted()
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() PopulateMusicUI())
Me.BeginInvoke(Sub() PopulateVideoUI())
Me.BeginInvoke(Sub() PopulatePLUI())
'Me.BeginInvoke(Sub() PopulateImageUI())
Else
PopulateMusicUI()
PopulateVideoUI()
PopulatePLUI()
End If
End Sub
Dim resDir As String = Application.StartupPath & "\res"
Sub GetIcons()
Try
If labelSong.Text = "Not Playing" And (curTabIndex <> 0 Or curTabIndex <> 2) Then
Select Case curTabIndex
Case 1
playbackAlbumArt.Image = Image.FromFile(resDir & "\music.png")
Case 2
playbackAlbumArt.Image = Image.FromFile(resDir & "\videos.png")
Case 3
playbackAlbumArt.Image = Image.FromFile(resDir + "\pictures.png")
Case 4
playbackAlbumArt.Image = Image.FromFile(resDir + "\radio.png")
Case 5
playbackAlbumArt.Image = Image.FromFile(resDir + "\cd.png")
Case 6
playbackAlbumArt.Image = Image.FromFile(resDir + "\playlists.png")
Case 7
playbackAlbumArt.Image = Image.FromFile(resDir + "\help.png")
Case 8
'gets last icon
Case Else
playbackAlbumArt.Image = Image.FromFile(resDir & "\music.png")
End Select
quickActionsPB.Image = playbackAlbumArt.Image
End If
'playbackAlbumArt.Image = Image.FromFile(resDir & "\cd.png")
Catch ex As Exception
MsgBox("Unable to load resource icons. Exiting." & vbCrLf & "Details: " & ex.Message, MsgBoxStyle.Critical, "Error")
End
End Try
End Sub
Public Function LoadRadios(path As String) As List(Of RadioStation)
If Not IO.File.Exists(path) Then
'debug.writeline("YAML load failed: file does not exist")
Return New List(Of RadioStation)
End If
Dim deserializer = New DeserializerBuilder().
IgnoreUnmatchedProperties().
Build()
Using reader As New StreamReader(path)
Dim root As RadioRoot = Nothing
Try
root = deserializer.Deserialize(Of RadioRoot)(reader)
Catch
End Try
Try
Return root.radios
Catch ex As Exception
End Try
End Using
End Function
Public Sub RefreshRadios()
scanCts?.Cancel()
scanCts = New CancellationTokenSource()
ListView1.Items.Clear()
Task.Run(Sub() ScanRadios(scanCts.Token))
End Sub
Public Sub ScanRadios(token As CancellationToken)
Dim radios As List(Of RadioStation)
Try
radios = LoadRadios(Application.StartupPath & "\radios\radios.yaml") ' YAML parsing here
'debug.writeline("able to parse during scan")
Catch
Try
radios = LoadRadios(Application.StartupPath & "\radios\radios.yml")
Catch
radios = New List(Of RadioStation)
End Try
End Try
If token.IsCancellationRequested Then Exit Sub
SyncLock RadioCache
RadioCache.Clear()
RadioCache.AddRange(radios)
'debug.writeline("able to add all radios")
End SyncLock
NotifyRadioUI()
For Each station In radios
If token.IsCancellationRequested Then Exit Sub
station.status = RadioStatus.Checking
UpdateSingleRadioUI(station)
Task.Run(Async Function()
Await CheckRadioStatusAsync(station, token)
End Function)
Next
End Sub
Public Async Function CheckRadioStatusAsync(
station As RadioStation,
token As CancellationToken) As Task
Dim sv As String = "Unknown"
Try
Using client As New Net.Http.HttpClient()
client.Timeout = TimeSpan.FromSeconds(4)
Using response = Await client.SendAsync(
New Net.Http.HttpRequestMessage(
Net.Http.HttpMethod.Head,
station.stream_url),
token)
station.status =
If(response.IsSuccessStatusCode Or response.StatusCode = 405 Or response.StatusCode <> 404,
RadioStatus.Online,
RadioStatus.Offline)
End Using
End Using
Catch
If Not token.IsCancellationRequested Then
station.status = RadioStatus.Offline
sv = station.status
Else
station.status = RadioStatus.Unknown
End If
End Try
UpdateSingleRadioUI(station)
End Function
Public Sub UpdateSingleRadioUI(station As RadioStation, Optional status As String = "Unknown")
If IsDisposed Then Exit Sub
If InvokeRequired Then
BeginInvoke(Sub() UpdateSingleRadioUI(station))
Return
End If
For Each item As ListViewItem In ListView1.Items
If item.Tag Is station Then
item.SubItems(3).Text = station.status.ToString()
Exit For
End If
Next
End Sub
Public Sub NotifyRadioUI()
If IsDisposed Then Exit Sub
If InvokeRequired Then
BeginInvoke(Sub() NotifyRadioUI())
Return
End If
PopulateRadioUI()
'debug.writeline("able to notify")
End Sub
Enum RadioStatus
Unknown
Checking
Online
Offline
End Enum
Public Sub PopulateRadioUI()
If Me.IsDisposed Then Exit Sub
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() PopulateRadioUI())
Return
End If
If curTabIndex = 4 Then
ListView1.Items.Clear()
ListView1.Columns.Clear()
ListView1.BeginUpdate()
ListView1.Columns.Add("Title", 290, HorizontalAlignment.Left)
ListView1.Columns.Add("Country", 290, HorizontalAlignment.Left)
ListView1.Columns.Add("Genre", 290, HorizontalAlignment.Left)
ListView1.Columns.Add("Status", 150, HorizontalAlignment.Left)
SyncLock RadioCache
For Each station In RadioCache
'debug.writeline("test: " & station.name)
Dim item As New ListViewItem(station.name)
item.SubItems.Add(station.country)
item.SubItems.Add(String.Join(", ", station.genre))
item.SubItems.Add(station.status.ToString())
item.Tag = station
ListView1.Items.Add(item)
Next
End SyncLock
ListView1.EndUpdate()
End If
End Sub
Public Sub SendToWebView(buffer() As Single)
If visualizer Is Nothing OrElse visualizer.CoreWebView2 Is Nothing Then Return
Dim bars(63) As Single
For i = 0 To bars.Length - 1
bars(i) = buffer(i * 16)
Next
Dim js =
$"window.chrome.webview.postMessage({{
type: 'audio',
values: [{String.Join(",", bars.Select(Function(v) v.ToString("0.000", Globalization.CultureInfo.InvariantCulture)))}]
}});"
visualizer.BeginInvoke(Sub()
visualizer.CoreWebView2.ExecuteScriptAsync(js)
End Sub)
End Sub
Public Const FALLBACK_VIZ As String =
"<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<style>
html,body{
margin:0;
width:100%;
height:100%;
background:black;
color:black;
display:flex;
align-items:center;
justify-content:center;
font-family:system-ui;
}
* {
user-select: none;
}
</style>
</head>
<body>
</body>
<script>
document.addEventListener('contextmenu', e => { e.preventDefault(); window.chrome.webview.postMessage( 'contextmenu|' + e.clientX + '|' + e.clientY ); });
document.addEventListener('mousedown', e => {