2022-11-11 16:57:50 +00:00
|
|
|
|
using System;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2022-11-11 16:57:50 +00:00
|
|
|
|
using System.Threading;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2022-11-11 16:57:50 +00:00
|
|
|
|
using MpcNET.Commands.Database;
|
|
|
|
|
using MpcNET.Commands.Queue;
|
|
|
|
|
using MpcNET.Commands.Reflection;
|
|
|
|
|
using MpcNET.Tags;
|
|
|
|
|
using MpcNET.Types;
|
|
|
|
|
using MpcNET.Types.Filters;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
|
|
|
|
namespace unison
|
|
|
|
|
{
|
|
|
|
|
class ShuffleHandler
|
|
|
|
|
{
|
|
|
|
|
private MPDHandler _mpd;
|
|
|
|
|
public List<string> _songList { get; }
|
|
|
|
|
public int AddedSongs = 0;
|
|
|
|
|
|
|
|
|
|
public ShuffleHandler()
|
|
|
|
|
{
|
|
|
|
|
_songList = new();
|
|
|
|
|
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
/*private bool IsOnMainThread()
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
return Application.Current.Dispatcher.Thread == System.Threading.Thread.CurrentThread;
|
2022-11-11 16:57:50 +00:00
|
|
|
|
}*/
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
public async Task GetSongsFromFilter(List<IFilter> filter, CancellationToken token)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
2022-11-11 16:57:50 +00:00
|
|
|
|
//Debug.WriteLine("[GetSongsFromFilterBefore] is on main thread => " + IsOnMainThread());
|
|
|
|
|
//await Task.Run(async() =>
|
|
|
|
|
//{
|
|
|
|
|
//Debug.WriteLine("[GetSongsFromFilterAfter] is on main thread => " + IsOnMainThread());
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
return;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
_songList.Clear();
|
|
|
|
|
int song = _mpd.GetStats().Songs;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
Debug.WriteLine("before search command / song == " + song);
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
//var response = await Task.Run(async () =>
|
|
|
|
|
//{
|
|
|
|
|
/*return*/ IEnumerable<IMpdFile> response = await _mpd.SafelySendCommandAsync(new SearchCommand(filter, 0, song + 1));
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("got response => " + response.Count());
|
|
|
|
|
|
|
|
|
|
foreach (IMpdFile file in response)
|
|
|
|
|
{
|
|
|
|
|
_songList.Add(file.Path);
|
|
|
|
|
//Debug.WriteLine(file.Path);
|
|
|
|
|
}
|
|
|
|
|
//});
|
2022-04-17 23:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
public async Task AddToQueueRandom(int SongNumber, CancellationToken token)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Add To Queue Random");
|
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
return;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
//await Task.Run(async () =>
|
|
|
|
|
//{
|
|
|
|
|
int AddedSongs = 0;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
Debug.WriteLine("song to add => " + SongNumber);
|
|
|
|
|
for (int i = 0; i < SongNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
// generate random number
|
|
|
|
|
int song = new Random().Next(0, _mpd.GetStats().Songs - 1);
|
|
|
|
|
Debug.WriteLine("song " + song + " - song total " + _mpd.GetStats().Songs);
|
|
|
|
|
IEnumerable<IMpdFile> Response = await _mpd.SafelySendCommandAsync(new SearchCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1));
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
Debug.WriteLine("got response");
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
await Task.Delay(1);
|
|
|
|
|
if (Response.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
string filePath = Response.First().Path;
|
|
|
|
|
_mpd.AddSong(filePath);
|
|
|
|
|
Debug.WriteLine("song path => " + filePath);
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
if (i == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!_mpd.IsPlaying())
|
|
|
|
|
_mpd.Play(0);
|
2022-04-17 23:02:26 +00:00
|
|
|
|
}
|
2022-11-11 16:57:50 +00:00
|
|
|
|
|
|
|
|
|
AddedSongs++;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
}
|
2022-11-11 16:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
//});
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
|
|
|
|
Debug.WriteLine("Add To Queue Random - finished");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
public async Task AddToQueueFilter(int SongNumber, CancellationToken token)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Add To Queue Filter");
|
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//await Task.Run(async () =>
|
|
|
|
|
//{
|
|
|
|
|
int AddedSongs = 0;
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("song to add => " + SongNumber);
|
|
|
|
|
// more requested songs than available => add everything
|
|
|
|
|
if (SongNumber > _songList.Count)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
2022-11-11 16:57:50 +00:00
|
|
|
|
var commandList = new CommandList();
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
2022-11-11 16:57:50 +00:00
|
|
|
|
foreach (string path in _songList)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
2022-11-11 16:57:50 +00:00
|
|
|
|
commandList.Add(new AddCommand(path));
|
|
|
|
|
AddedSongs++;
|
2022-04-17 23:02:26 +00:00
|
|
|
|
}
|
2022-11-11 16:57:50 +00:00
|
|
|
|
Trace.WriteLine("Added " + AddedSongs + " songs");
|
|
|
|
|
await _mpd.SafelySendCommandAsync(commandList);
|
|
|
|
|
}
|
|
|
|
|
// more available songs than requested =>
|
|
|
|
|
// we add unique indexes until we reach the requested amount
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HashSet<int> SongIndex = new();
|
|
|
|
|
Debug.WriteLine("while - before");
|
|
|
|
|
while (SongIndex.Count < SongNumber)//_songList.Count - 1)//SongNumber)
|
2022-04-17 23:02:26 +00:00
|
|
|
|
{
|
2022-11-11 16:57:50 +00:00
|
|
|
|
int MaxIndex = new Random().Next(0, _songList.Count - 1);
|
|
|
|
|
SongIndex.Add(MaxIndex);
|
2022-04-17 23:02:26 +00:00
|
|
|
|
}
|
2022-11-11 16:57:50 +00:00
|
|
|
|
Debug.WriteLine("while - middle");
|
|
|
|
|
var commandList = new CommandList();
|
|
|
|
|
|
|
|
|
|
foreach (int index in SongIndex)
|
|
|
|
|
commandList.Add(new AddCommand(_songList[index]));
|
|
|
|
|
await _mpd.SafelySendCommandAsync(commandList);
|
|
|
|
|
//_mpd.AddSong(_songList[index]);
|
|
|
|
|
Debug.WriteLine("while - after");
|
|
|
|
|
}
|
|
|
|
|
//});
|
2022-04-17 23:02:26 +00:00
|
|
|
|
|
|
|
|
|
Debug.WriteLine("Add To Queue Filter - finished");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|