2017-07-01 06:15:58 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Audio;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NLog;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-07-04 13:38:19 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Collections;
|
2017-07-17 02:37:51 +00:00
|
|
|
|
using NadekoBot.Modules.Music.Services;
|
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
2017-07-17 02:37:51 +00:00
|
|
|
|
namespace NadekoBot.Modules.Music.Common
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
|
|
|
|
public enum StreamState
|
|
|
|
|
{
|
|
|
|
|
Resolving,
|
|
|
|
|
Queued,
|
|
|
|
|
Playing,
|
|
|
|
|
Completed
|
|
|
|
|
}
|
2017-07-01 06:16:06 +00:00
|
|
|
|
public class MusicPlayer
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
2017-07-05 16:53:21 +00:00
|
|
|
|
private readonly Thread _player;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
public IVoiceChannel VoiceChannel { get; private set; }
|
2017-07-01 06:15:58 +00:00
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
|
|
|
|
|
private MusicQueue Queue { get; } = new MusicQueue();
|
|
|
|
|
|
|
|
|
|
public bool Exited { get; set; } = false;
|
|
|
|
|
public bool Stopped { get; private set; } = false;
|
|
|
|
|
public float Volume { get; private set; } = 1.0f;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
public bool Paused => pauseTaskSource != null;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
private TaskCompletionSource<bool> pauseTaskSource { get; set; } = null;
|
|
|
|
|
|
2017-07-04 13:38:19 +00:00
|
|
|
|
public string PrettyVolume => $"🔉 {(int)(Volume * 100)}%";
|
|
|
|
|
public string PrettyCurrentTime
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var time = CurrentTime.ToString(@"mm\:ss");
|
|
|
|
|
var hrs = (int)CurrentTime.TotalHours;
|
|
|
|
|
|
|
|
|
|
if (hrs > 0)
|
|
|
|
|
return hrs + ":" + time;
|
|
|
|
|
else
|
|
|
|
|
return time;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string PrettyFullTime => PrettyCurrentTime + " / " + (Queue.Current.Song?.PrettyTotalTime ?? "?");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
private CancellationTokenSource SongCancelSource { get; set; }
|
|
|
|
|
public ITextChannel OutputTextChannel { get; set; }
|
|
|
|
|
public (int Index, SongInfo Current) Current
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Stopped)
|
|
|
|
|
return (0, null);
|
|
|
|
|
return Queue.Current;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RepeatCurrentSong { get; private set; }
|
2017-07-01 19:22:11 +00:00
|
|
|
|
public bool Shuffle { get; private set; }
|
|
|
|
|
public bool Autoplay { get; private set; }
|
2017-07-14 04:15:31 +00:00
|
|
|
|
public bool RepeatPlaylist { get; private set; } = false;
|
2017-07-02 12:49:37 +00:00
|
|
|
|
public uint MaxQueueSize
|
|
|
|
|
{
|
|
|
|
|
get => Queue.MaxQueueSize;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
set { lock (locker) Queue.MaxQueueSize = value; }
|
|
|
|
|
}
|
|
|
|
|
private bool _fairPlay;
|
|
|
|
|
public bool FairPlay
|
|
|
|
|
{
|
|
|
|
|
get => _fairPlay;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
var cur = Queue.Current;
|
|
|
|
|
if (cur.Song != null)
|
|
|
|
|
RecentlyPlayedUsers.Add(cur.Song.QueuerName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RecentlyPlayedUsers.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_fairPlay = value;
|
|
|
|
|
}
|
2017-07-02 12:49:37 +00:00
|
|
|
|
}
|
2017-08-15 21:54:54 +00:00
|
|
|
|
public bool AutoDelete { get; set; }
|
2017-07-02 18:58:45 +00:00
|
|
|
|
public uint MaxPlaytimeSeconds { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int _frameBytes = 3840;
|
|
|
|
|
const float _miliseconds = 20.0f;
|
|
|
|
|
public TimeSpan CurrentTime => TimeSpan.FromSeconds(_bytesSent / (float)_frameBytes / (1000 / _miliseconds));
|
2017-07-03 10:40:12 +00:00
|
|
|
|
|
2017-07-02 18:58:45 +00:00
|
|
|
|
private int _bytesSent = 0;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
|
|
|
|
private IAudioClient _audioClient;
|
|
|
|
|
private readonly object locker = new object();
|
2017-07-01 19:22:11 +00:00
|
|
|
|
private MusicService _musicService;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
|
|
|
|
#region events
|
2017-07-03 10:40:12 +00:00
|
|
|
|
public event Action<MusicPlayer, (int Index, SongInfo Song)> OnStarted;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
public event Action<MusicPlayer, SongInfo> OnCompleted;
|
|
|
|
|
public event Action<MusicPlayer, bool> OnPauseChanged;
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-07-02 13:44:00 +00:00
|
|
|
|
|
|
|
|
|
private bool manualSkip = false;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
private bool manualIndex = false;
|
2017-07-02 14:27:30 +00:00
|
|
|
|
private bool newVoiceChannel = false;
|
2017-07-04 13:38:19 +00:00
|
|
|
|
private readonly IGoogleApiService _google;
|
2017-07-02 13:44:00 +00:00
|
|
|
|
|
2017-07-07 07:54:25 +00:00
|
|
|
|
private bool cancel = false;
|
|
|
|
|
|
2017-07-03 10:40:12 +00:00
|
|
|
|
private ConcurrentHashSet<string> RecentlyPlayedUsers { get; } = new ConcurrentHashSet<string>();
|
2017-07-04 13:42:07 +00:00
|
|
|
|
public TimeSpan TotalPlaytime
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var songs = Queue.ToArray().Songs;
|
|
|
|
|
return songs.Any(s => s.TotalTime == TimeSpan.MaxValue)
|
|
|
|
|
? TimeSpan.MaxValue
|
|
|
|
|
: new TimeSpan(songs.Sum(s => s.TotalTime.Ticks));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 10:40:12 +00:00
|
|
|
|
|
2017-07-04 13:38:19 +00:00
|
|
|
|
public MusicPlayer(MusicService musicService, IGoogleApiService google, IVoiceChannel vch, ITextChannel output, float volume)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
this.Volume = volume;
|
|
|
|
|
this.VoiceChannel = vch;
|
|
|
|
|
this.SongCancelSource = new CancellationTokenSource();
|
|
|
|
|
this.OutputTextChannel = output;
|
2017-07-01 19:22:11 +00:00
|
|
|
|
this._musicService = musicService;
|
2017-07-04 13:38:19 +00:00
|
|
|
|
this._google = google;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Initialized");
|
|
|
|
|
|
2017-07-05 16:53:21 +00:00
|
|
|
|
_player = new Thread(new ThreadStart(PlayerLoop));
|
|
|
|
|
_player.Start();
|
|
|
|
|
_log.Info("Loop started");
|
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
2017-07-05 16:53:21 +00:00
|
|
|
|
private async void PlayerLoop()
|
|
|
|
|
{
|
|
|
|
|
while (!Exited)
|
|
|
|
|
{
|
|
|
|
|
_bytesSent = 0;
|
2017-07-07 07:54:25 +00:00
|
|
|
|
cancel = false;
|
2017-07-05 16:53:21 +00:00
|
|
|
|
CancellationToken cancelToken;
|
|
|
|
|
(int Index, SongInfo Song) data;
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
data = Queue.Current;
|
|
|
|
|
cancelToken = SongCancelSource.Token;
|
|
|
|
|
manualSkip = false;
|
|
|
|
|
manualIndex = false;
|
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
if (data.Song != null)
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Starting");
|
2017-07-10 19:21:45 +00:00
|
|
|
|
AudioOutStream pcm = null;
|
|
|
|
|
SongBuffer b = null;
|
|
|
|
|
try
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-17 02:37:51 +00:00
|
|
|
|
b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == MusicType.Local);
|
2017-07-10 19:21:45 +00:00
|
|
|
|
//_log.Info("Created buffer, buffering...");
|
2017-07-03 19:05:35 +00:00
|
|
|
|
|
2017-07-10 19:21:45 +00:00
|
|
|
|
//var bufferTask = b.StartBuffering(cancelToken);
|
|
|
|
|
//var timeout = Task.Delay(10000);
|
|
|
|
|
//if (Task.WhenAny(bufferTask, timeout) == timeout)
|
|
|
|
|
//{
|
|
|
|
|
// _log.Info("Buffering failed due to a timeout.");
|
|
|
|
|
// continue;
|
|
|
|
|
//}
|
|
|
|
|
//else if (!bufferTask.Result)
|
|
|
|
|
//{
|
|
|
|
|
// _log.Info("Buffering failed due to a cancel or error.");
|
|
|
|
|
// continue;
|
|
|
|
|
//}
|
|
|
|
|
//_log.Info("Buffered. Getting audio client...");
|
|
|
|
|
var ac = await GetAudioClient();
|
|
|
|
|
_log.Info("Got Audio client");
|
|
|
|
|
if (ac == null)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("Can't join");
|
|
|
|
|
await Task.Delay(900, cancelToken);
|
|
|
|
|
// just wait some time, maybe bot doesn't even have perms to join that voice channel,
|
|
|
|
|
// i don't want to spam connection attempts
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pcm = ac.CreatePCMStream(AudioApplication.Music, bufferMillis: 500);
|
|
|
|
|
_log.Info("Created pcm stream");
|
|
|
|
|
OnStarted?.Invoke(this, data);
|
2017-07-02 06:03:24 +00:00
|
|
|
|
|
2017-07-10 19:21:45 +00:00
|
|
|
|
byte[] buffer = new byte[3840];
|
|
|
|
|
int bytesRead = 0;
|
2017-07-03 19:05:35 +00:00
|
|
|
|
|
2017-07-10 19:21:45 +00:00
|
|
|
|
while ((bytesRead = b.Read(buffer, 0, buffer.Length)) > 0
|
|
|
|
|
&& (MaxPlaytimeSeconds <= 0 || MaxPlaytimeSeconds >= CurrentTime.TotalSeconds))
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-10 19:21:45 +00:00
|
|
|
|
AdjustVolume(buffer, Volume);
|
|
|
|
|
await pcm.WriteAsync(buffer, 0, bytesRead, cancelToken).ConfigureAwait(false);
|
|
|
|
|
unchecked { _bytesSent += bytesRead; }
|
|
|
|
|
|
|
|
|
|
await (pauseTaskSource?.Task ?? Task.CompletedTask);
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
2017-07-10 19:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("Song Canceled");
|
|
|
|
|
cancel = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (pcm != null)
|
2017-07-08 11:42:16 +00:00
|
|
|
|
{
|
2017-07-10 19:21:45 +00:00
|
|
|
|
// flush is known to get stuck from time to time,
|
|
|
|
|
// just skip flushing if it takes more than 1 second
|
|
|
|
|
var flushCancel = new CancellationTokenSource();
|
|
|
|
|
var flushToken = flushCancel.Token;
|
|
|
|
|
var flushDelay = Task.Delay(1000, flushToken);
|
|
|
|
|
await Task.WhenAny(flushDelay, pcm.FlushAsync(flushToken));
|
|
|
|
|
flushCancel.Cancel();
|
|
|
|
|
pcm.Dispose();
|
2017-07-08 11:42:16 +00:00
|
|
|
|
}
|
2017-07-03 10:40:12 +00:00
|
|
|
|
|
2017-07-10 19:21:45 +00:00
|
|
|
|
if (b != null)
|
|
|
|
|
b.Dispose();
|
2017-07-07 07:54:25 +00:00
|
|
|
|
|
2017-07-10 19:21:45 +00:00
|
|
|
|
OnCompleted?.Invoke(this, data.Song);
|
|
|
|
|
|
|
|
|
|
if (_bytesSent == 0 && !cancel)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
Queue.RemoveSong(data.Song);
|
|
|
|
|
_log.Info("Song removed because it can't play");
|
2017-07-07 07:54:25 +00:00
|
|
|
|
}
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//if repeating current song, just ignore other settings,
|
|
|
|
|
// and play this song again (don't change the index)
|
|
|
|
|
// ignore rcs if song is manually skipped
|
2017-07-03 10:40:12 +00:00
|
|
|
|
|
2017-07-08 11:42:16 +00:00
|
|
|
|
int queueCount;
|
2017-07-11 18:11:36 +00:00
|
|
|
|
bool stopped;
|
2017-08-15 21:54:54 +00:00
|
|
|
|
int currentIndex;
|
2017-07-08 11:42:16 +00:00
|
|
|
|
lock (locker)
|
2017-07-11 18:11:36 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
queueCount = Queue.Count;
|
2017-07-11 18:11:36 +00:00
|
|
|
|
stopped = Stopped;
|
2017-08-15 21:54:54 +00:00
|
|
|
|
currentIndex = Queue.CurrentIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AutoDelete && !RepeatCurrentSong && !RepeatPlaylist && data.Song != null)
|
|
|
|
|
{
|
|
|
|
|
Queue.RemoveSong(data.Song);
|
2017-07-11 18:11:36 +00:00
|
|
|
|
}
|
2017-07-05 16:53:21 +00:00
|
|
|
|
|
2017-07-08 11:42:16 +00:00
|
|
|
|
if (!manualIndex && (!RepeatCurrentSong || manualSkip))
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
if (Shuffle)
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Random song");
|
|
|
|
|
Queue.Random(); //if shuffle is set, set current song index to a random number
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
else
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
//if last song, and autoplay is enabled, and if it's a youtube song
|
|
|
|
|
// do autplay magix
|
2017-07-17 02:37:51 +00:00
|
|
|
|
if (queueCount - 1 == data.Index && Autoplay && data.Song?.ProviderType == MusicType.YouTube)
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
try
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Loading related song");
|
2017-07-08 23:37:17 +00:00
|
|
|
|
await _musicService.TryQueueRelatedSongAsync(data.Song, OutputTextChannel, VoiceChannel);
|
2017-08-15 21:54:54 +00:00
|
|
|
|
if(!AutoDelete)
|
|
|
|
|
Queue.Next();
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
catch
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Loading related song failed.");
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
else if (FairPlay)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("Next fair song");
|
|
|
|
|
var q = Queue.ToArray().Songs.Shuffle().ToArray();
|
|
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (var i = 0; i < q.Length; i++) //first try to find a queuer who didn't have their song played recently
|
|
|
|
|
{
|
|
|
|
|
var item = q[i];
|
|
|
|
|
if (RecentlyPlayedUsers.Add(item.QueuerName)) // if it's found, set current song to that index
|
|
|
|
|
{
|
|
|
|
|
Queue.CurrentIndex = i;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) //if it's not
|
|
|
|
|
{
|
|
|
|
|
RecentlyPlayedUsers.Clear(); //clear all recently played users (that means everyone from the playlist has had their song played)
|
|
|
|
|
Queue.Random(); //go to a random song (to prevent looping on the first few songs)
|
|
|
|
|
var cur = Current;
|
|
|
|
|
if (cur.Current != null) // add newely scheduled song's queuer to the recently played list
|
|
|
|
|
RecentlyPlayedUsers.Add(cur.Current.QueuerName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (queueCount - 1 == data.Index && !RepeatPlaylist && !manualSkip)
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Stopping because repeatplaylist is disabled");
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
else
|
2017-07-05 16:53:21 +00:00
|
|
|
|
{
|
2017-07-08 11:42:16 +00:00
|
|
|
|
_log.Info("Next song");
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2017-08-15 21:54:54 +00:00
|
|
|
|
if (!Stopped)
|
|
|
|
|
if(!AutoDelete)
|
|
|
|
|
Queue.Next();
|
2017-07-08 11:42:16 +00:00
|
|
|
|
}
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-08 11:42:16 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
}
|
2017-07-05 16:53:21 +00:00
|
|
|
|
}
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(500);
|
|
|
|
|
}
|
|
|
|
|
while ((Queue.Count == 0 || Stopped) && !Exited);
|
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IAudioClient> GetAudioClient(bool reconnect = false)
|
|
|
|
|
{
|
|
|
|
|
if (_audioClient == null ||
|
|
|
|
|
_audioClient.ConnectionState != ConnectionState.Connected ||
|
2017-07-03 10:40:12 +00:00
|
|
|
|
reconnect ||
|
2017-07-02 14:27:30 +00:00
|
|
|
|
newVoiceChannel)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-01 15:16:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-03 18:26:17 +00:00
|
|
|
|
var t = _audioClient?.StopAsync();
|
|
|
|
|
if (t != null)
|
|
|
|
|
{
|
2017-07-05 16:39:54 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Stopping audio client");
|
2017-07-03 18:26:17 +00:00
|
|
|
|
await t;
|
2017-07-05 16:39:54 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Disposing audio client");
|
2017-07-03 19:05:35 +00:00
|
|
|
|
_audioClient.Dispose();
|
2017-07-03 18:26:17 +00:00
|
|
|
|
}
|
2017-07-01 15:16:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-07-02 14:27:30 +00:00
|
|
|
|
newVoiceChannel = false;
|
2017-07-05 16:39:54 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Get current user");
|
2017-07-03 19:05:35 +00:00
|
|
|
|
var curUser = await VoiceChannel.Guild.GetCurrentUserAsync();
|
|
|
|
|
if (curUser.VoiceChannel != null)
|
|
|
|
|
{
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Connecting");
|
2017-07-03 21:27:17 +00:00
|
|
|
|
var ac = await VoiceChannel.ConnectAsync();
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Connected, stopping");
|
2017-07-03 21:27:17 +00:00
|
|
|
|
await ac.StopAsync();
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Disconnected");
|
2017-07-03 19:05:35 +00:00
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Connecting");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
_audioClient = await VoiceChannel.ConnectAsync();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _audioClient;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 10:40:12 +00:00
|
|
|
|
public int Enqueue(SongInfo song)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
2017-07-03 10:40:12 +00:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (Exited)
|
|
|
|
|
return -1;
|
|
|
|
|
Queue.Add(song);
|
2017-08-06 09:49:27 +00:00
|
|
|
|
return Queue.Count - 1;
|
2017-07-03 10:40:12 +00:00
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-05 10:38:53 +00:00
|
|
|
|
public int EnqueueNext(SongInfo song)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (Exited)
|
|
|
|
|
return -1;
|
|
|
|
|
return Queue.AddNext(song);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 17:52:10 +00:00
|
|
|
|
public void SetIndex(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(index));
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (Exited)
|
|
|
|
|
return;
|
2017-08-15 21:54:54 +00:00
|
|
|
|
if (AutoDelete && index >= Queue.CurrentIndex && index > 0)
|
|
|
|
|
index--;
|
2017-07-11 17:52:10 +00:00
|
|
|
|
Queue.CurrentIndex = index;
|
|
|
|
|
manualIndex = true;
|
|
|
|
|
Stopped = false;
|
|
|
|
|
CancelCurrentSong();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 13:44:00 +00:00
|
|
|
|
public void Next(int skipCount = 1)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2017-07-03 10:40:12 +00:00
|
|
|
|
if (Exited)
|
|
|
|
|
return;
|
2017-07-02 13:44:00 +00:00
|
|
|
|
manualSkip = true;
|
2017-07-02 14:20:25 +00:00
|
|
|
|
// if player is stopped, and user uses .n, it should play current song.
|
|
|
|
|
// It's a bit weird, but that's the least annoying solution
|
2017-07-02 15:15:34 +00:00
|
|
|
|
if (!Stopped)
|
2017-07-11 18:11:36 +00:00
|
|
|
|
if (!RepeatPlaylist && Queue.IsLast()) // if it's the last song in the queue, and repeat playlist is disabled
|
|
|
|
|
{ //stop the queue
|
|
|
|
|
Stop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Queue.Next(skipCount - 1);
|
2017-07-11 17:52:10 +00:00
|
|
|
|
else
|
|
|
|
|
Queue.CurrentIndex = 0;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
Stopped = false;
|
2017-07-01 06:16:06 +00:00
|
|
|
|
CancelCurrentSong();
|
2017-07-11 17:52:10 +00:00
|
|
|
|
Unpause();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 19:24:23 +00:00
|
|
|
|
public void Stop(bool clearQueue = false)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2017-07-13 19:24:23 +00:00
|
|
|
|
Stopped = true;
|
2017-07-14 04:15:31 +00:00
|
|
|
|
//Queue.ResetCurrent();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
if (clearQueue)
|
|
|
|
|
Queue.Clear();
|
|
|
|
|
Unpause();
|
2017-07-01 06:16:06 +00:00
|
|
|
|
CancelCurrentSong();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Unpause()
|
|
|
|
|
{
|
2017-07-03 10:40:12 +00:00
|
|
|
|
lock (locker)
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
2017-07-03 10:40:12 +00:00
|
|
|
|
if (pauseTaskSource != null)
|
|
|
|
|
{
|
|
|
|
|
pauseTaskSource.TrySetResult(true);
|
|
|
|
|
pauseTaskSource = null;
|
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TogglePause()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (pauseTaskSource == null)
|
|
|
|
|
pauseTaskSource = new TaskCompletionSource<bool>();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Unpause();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OnPauseChanged?.Invoke(this, pauseTaskSource != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetVolume(int volume)
|
|
|
|
|
{
|
|
|
|
|
if (volume < 0 || volume > 100)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(volume));
|
2017-07-03 10:40:12 +00:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
Volume = ((float)volume) / 100;
|
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SongInfo RemoveAt(int index)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
var cur = Queue.Current;
|
2017-07-10 19:31:47 +00:00
|
|
|
|
var toReturn = Queue.RemoveAt(index);
|
2017-07-01 06:15:58 +00:00
|
|
|
|
if (cur.Index == index)
|
|
|
|
|
Next();
|
2017-07-10 19:31:47 +00:00
|
|
|
|
return toReturn;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CancelCurrentSong()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
var cs = SongCancelSource;
|
|
|
|
|
SongCancelSource = new CancellationTokenSource();
|
|
|
|
|
cs.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearQueue()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
Queue.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (int CurrentIndex, SongInfo[] Songs) QueueArray()
|
2017-07-03 10:40:12 +00:00
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
return Queue.ToArray();
|
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
|
|
|
|
//aidiakapi ftw
|
|
|
|
|
public static unsafe byte[] AdjustVolume(byte[] audioSamples, float volume)
|
|
|
|
|
{
|
|
|
|
|
if (Math.Abs(volume - 1f) < 0.0001f) return audioSamples;
|
|
|
|
|
|
|
|
|
|
// 16-bit precision for the multiplication
|
|
|
|
|
var volumeFixed = (int)Math.Round(volume * 65536d);
|
|
|
|
|
|
|
|
|
|
var count = audioSamples.Length / 2;
|
|
|
|
|
|
|
|
|
|
fixed (byte* srcBytes = audioSamples)
|
|
|
|
|
{
|
|
|
|
|
var src = (short*)srcBytes;
|
|
|
|
|
|
|
|
|
|
for (var i = count; i != 0; i--, src++)
|
|
|
|
|
*src = (short)(((*src) * volumeFixed) >> 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return audioSamples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ToggleRepeatSong()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return RepeatCurrentSong = !RepeatCurrentSong;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 06:16:06 +00:00
|
|
|
|
public async Task Destroy()
|
2017-07-01 06:15:58 +00:00
|
|
|
|
{
|
2017-07-01 06:16:06 +00:00
|
|
|
|
_log.Info("Destroying");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2017-07-01 06:16:06 +00:00
|
|
|
|
Stop();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
Exited = true;
|
|
|
|
|
Unpause();
|
2017-07-01 06:16:06 +00:00
|
|
|
|
|
|
|
|
|
OnCompleted = null;
|
|
|
|
|
OnPauseChanged = null;
|
|
|
|
|
OnStarted = null;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
2017-07-01 06:16:06 +00:00
|
|
|
|
var ac = _audioClient;
|
|
|
|
|
if (ac != null)
|
|
|
|
|
await ac.StopAsync();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 19:22:11 +00:00
|
|
|
|
public bool ToggleShuffle()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return Shuffle = !Shuffle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ToggleAutoplay()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return Autoplay = !Autoplay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ToggleRepeatPlaylist()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return RepeatPlaylist = !RepeatPlaylist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 21:38:11 +00:00
|
|
|
|
public async Task SetVoiceChannel(IVoiceChannel vch)
|
2017-07-01 19:22:11 +00:00
|
|
|
|
{
|
2017-07-03 10:40:12 +00:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (Exited)
|
|
|
|
|
return;
|
|
|
|
|
VoiceChannel = vch;
|
|
|
|
|
}
|
2017-07-04 21:38:11 +00:00
|
|
|
|
_audioClient = await vch.ConnectAsync();
|
2017-07-01 19:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 13:38:19 +00:00
|
|
|
|
public async Task UpdateSongDurationsAsync()
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
var (_, songs) = Queue.ToArray();
|
|
|
|
|
var toUpdate = songs
|
2017-07-17 02:37:51 +00:00
|
|
|
|
.Where(x => x.ProviderType == MusicType.YouTube
|
2017-07-04 13:38:19 +00:00
|
|
|
|
&& x.TotalTime == TimeSpan.Zero);
|
|
|
|
|
|
|
|
|
|
var vIds = toUpdate.Select(x => x.VideoId);
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
_log.Info(sw.Elapsed.TotalSeconds);
|
|
|
|
|
if (!vIds.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var durations = await _google.GetVideoDurationsAsync(vIds);
|
|
|
|
|
|
|
|
|
|
foreach (var x in toUpdate)
|
|
|
|
|
{
|
|
|
|
|
if (durations.TryGetValue(x.VideoId, out var dur))
|
|
|
|
|
x.TotalTime = dur;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 21:38:11 +00:00
|
|
|
|
public SongInfo MoveSong(int n1, int n2)
|
|
|
|
|
=> Queue.MoveSong(n1, n2);
|
|
|
|
|
|
2017-07-01 06:15:58 +00:00
|
|
|
|
//// this should be written better
|
|
|
|
|
//public TimeSpan TotalPlaytime =>
|
|
|
|
|
// _playlist.Any(s => s.TotalTime == TimeSpan.MaxValue) ?
|
|
|
|
|
// TimeSpan.MaxValue :
|
2017-07-03 10:40:12 +00:00
|
|
|
|
// new TimeSpan(_playlist.Sum(s => s.TotalTime.Ticks));
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
2017-07-03 10:40:12 +00:00
|
|
|
|
}
|