-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioService.cs
279 lines (264 loc) · 11.3 KB
/
AudioService.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
using Discord;
using Discord.Audio;
using Discord.WebSocket;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using YoutubeExplode;
using YoutubeExplode.Common;
namespace MusicBot
{
public class AudioService
{
private readonly ConcurrentDictionary<ulong, IAudioClient> _connectedChannels = new ConcurrentDictionary<ulong, IAudioClient>();
private ConcurrentQueue<string> _userRequests;
private ConcurrentQueue<string> _autoPlaylist;
private string _playlistId;
private CancellationTokenSource _tokenSource;
private CancellationToken _cancellationToken;
private YoutubeClient _ytClient;
private AudioOutStream _audioStream;
public bool IsPlaying { get; set; }
public string CurrentlyPlaying { get; set; }
public AudioService()
{
_ytClient = new YoutubeClient();
_userRequests = new ConcurrentQueue<string>();
_autoPlaylist = new ConcurrentQueue<string>();
_playlistId = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText("config.json")).PlaylistId;
IsPlaying = false;
}
public async Task<bool> AddToPlaylistAsync(string url)
{
if (await VideoExistsAsync(url))
{
_userRequests.Enqueue(url);
Console.WriteLine($"[ {DateTime.Now,0:t} ] {url} added, {_userRequests.Count} songs in the user requests list.");
return true;
}
Console.WriteLine($"[ {DateTime.Now,0:t} ] Error, could not find song at url: {url}");
return false;
}
public async Task CreateAutoPlaylistAsync()
{
try
{
if (_playlistId != null)
{
// get list of videos in playlist and shuffle them
var videos = await _ytClient.Playlists.GetVideosAsync(_playlistId);
var rng = new Random();
foreach (var video in videos.OrderBy(x => rng.Next()))
{
_autoPlaylist.Enqueue($"https://youtu.be/{video.Id}");
}
}
else
Console.WriteLine($"[ {DateTime.Now,0:t} ] Error getting playlist ID from config file.");
}
catch (Exception e)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Exception while creating playlist from Youtube: {e}");
}
}
public async Task StartPlaying(SocketGuild guild, DiscordSocketClient discordClient, IVoiceChannel channel)
{
IAudioClient client;
if (!_connectedChannels.TryGetValue(guild.Id, out client))
await JoinAudioChannelAsync(guild, channel);
if (_connectedChannels.TryGetValue(guild.Id, out client))
{
// set or reset cancellation token
_tokenSource = new CancellationTokenSource();
_cancellationToken = _tokenSource.Token;
IsPlaying = true;
Console.WriteLine($"[ {DateTime.Now,0:t} ] Creating Audio Stream");
if (_audioStream != null)
{
await _audioStream.ClearAsync(new CancellationToken());
await _audioStream.DisposeAsync();
await _audioStream.FlushAsync();
_audioStream = null;
}
_audioStream = client.CreatePCMStream(AudioApplication.Music, 96 * 1024); // default bitrate is 96 * 1024 if not specified
bool autoPlaylistHasSongs = true;
bool requestsPlaylistHasSongs = true;
string autoplayUrl;
string requestUrl;
// check if auto playlist has songs in it, if not create playlist from config
autoPlaylistHasSongs = _autoPlaylist.TryDequeue(out autoplayUrl);
if (!autoPlaylistHasSongs)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Retrieving auto playlist from Youtube.");
await CreateAutoPlaylistAsync();
Console.WriteLine($"[ {DateTime.Now,0:t} ] Playlist retrieval complete. Number of songs in list: {_autoPlaylist.Count}");
autoPlaylistHasSongs = _autoPlaylist.TryDequeue(out autoplayUrl);
}
requestsPlaylistHasSongs = _userRequests.TryDequeue(out requestUrl);
while (IsPlaying && (requestsPlaylistHasSongs || autoPlaylistHasSongs))
{
if (requestsPlaylistHasSongs)
{
while (IsPlaying && requestsPlaylistHasSongs)
{
await CheckVideoTitleAsync(requestUrl);
await discordClient.SetGameAsync(CurrentlyPlaying);
Console.WriteLine($"[ {DateTime.Now,0:t} ] Retrieving: {requestUrl}");
await SendAudioAsync(guild, requestUrl);
requestsPlaylistHasSongs = _userRequests.TryDequeue(out requestUrl);
}
}
else if (autoPlaylistHasSongs)
{
//auto playlist has music and user requests are empty
while (IsPlaying && autoPlaylistHasSongs && !requestsPlaylistHasSongs)
{
await CheckVideoTitleAsync(autoplayUrl);
await discordClient.SetGameAsync(CurrentlyPlaying);
Console.WriteLine($"[ {DateTime.Now,0:t} ] Retrieving {CurrentlyPlaying}");
Console.WriteLine($"[ {DateTime.Now,0:t} ] Number of songs left in auto playlist: {_autoPlaylist.Count}");
await SendAudioAsync(guild, autoplayUrl);
autoPlaylistHasSongs = _autoPlaylist.TryDequeue(out autoplayUrl);
//check user requests playlist for new requests
requestsPlaylistHasSongs = _userRequests.TryDequeue(out requestUrl);
}
}
else if (!autoPlaylistHasSongs)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Retrieving auto playlist from Youtube.");
await CreateAutoPlaylistAsync();
Console.WriteLine($"[ {DateTime.Now,0:t} ] Playlist retrieval complete. Number of songs in list: {_autoPlaylist.Count}");
autoPlaylistHasSongs = _autoPlaylist.TryDequeue(out autoplayUrl);
}
}
// clear out stream if loop has stopped
await _audioStream.ClearAsync(new CancellationToken());
await _audioStream.DisposeAsync();
await _audioStream.FlushAsync();
_audioStream = null;
IsPlaying = false;
}
}
public void StopPlaying()
{
if (IsPlaying)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Stopping stream.");
_tokenSource.Cancel();
IsPlaying = false;
}
}
public void SkipSong()
{
if (IsPlaying)
{
StopPlaying();
_tokenSource = new CancellationTokenSource();
_cancellationToken = _tokenSource.Token;
IsPlaying = true;
}
}
public async Task JoinAudioChannelAsync(IGuild guild, IVoiceChannel target)
{
IAudioClient client;
if (_connectedChannels.TryGetValue(guild.Id, out client))
return; // bot already connected to voice
try
{
var audioClient = await target.ConnectAsync();
if (_connectedChannels.TryAdd(guild.Id, audioClient))
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Connected to voice in server: {guild.Name}; Voice Channel: {target.Name}");
}
}
catch (Exception e)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Exception while joining audio channel: {e}");
}
}
public async Task LeaveAudioChannelAsync(IGuild guild)
{
IAudioClient client;
if (_connectedChannels.TryRemove(guild.Id, out client))
{
if (IsPlaying)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Stopping Music");
StopPlaying();
IsPlaying = false;
}
await client.StopAsync();
Console.WriteLine($"[ {DateTime.Now,0:t} ] Audio Client stopped; Bot left voice");
}
}
private async Task CheckVideoTitleAsync(string url)
{
if (await VideoExistsAsync(url))
{
var info = await _ytClient.Videos.GetAsync(url);
CurrentlyPlaying = info.Title;
}
else
CurrentlyPlaying = null;
}
private async Task<bool> VideoExistsAsync(string url)
{
try
{
var info = await _ytClient.Videos.GetAsync(url);
if (info != null)
return true;
return false;
}
catch (Exception e)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Exception while checking if video exists: {e}");
return false;
}
}
private async Task SendAudioAsync(IGuild guild, string url)
{
IAudioClient client;
if (_connectedChannels.TryGetValue(guild.Id, out client))
{
try
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Playing: {CurrentlyPlaying}");
var output = CreateStream(url).StandardOutput.BaseStream;
await output.CopyToAsync(_audioStream, _cancellationToken);
}
catch (OperationCanceledException)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Audio Stream stopped");
}
catch (Exception ex)
{
Console.WriteLine($"[ {DateTime.Now,0:t} ] Exception while trying to send audio: {ex.Message}");
}
finally
{
await _audioStream.FlushAsync();
}
}
}
private Process CreateStream(string url)
{
Process currentSong = new Process();
currentSong.StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/C youtube-dl.exe -o - {url} | ffmpeg -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
currentSong.Start();
return currentSong;
}
}
}