Music is playing, not very good though

This commit is contained in:
Kwoth 2016-08-26 03:25:55 +02:00
parent a6d65dee30
commit b48bd16bd0
5 changed files with 25 additions and 15 deletions

1
.gitignore vendored
View File

@ -15,6 +15,7 @@ obj/
Tests/bin Tests/bin
src/NadekoBot/credentials.json src/NadekoBot/credentials.json
src/NadekoBot/project.lock.json src/NadekoBot/project.lock.json
src/NadekoBot/data/*
# NuGet Packages # NuGet Packages
*.nupkg *.nupkg

@ -1 +1 @@
Subproject commit fa06826f92fd020f2af5f48aa25784d10239f668 Subproject commit afa327a502366d00adb4549777d30e4fe039b9ed

View File

@ -1,5 +1,6 @@
using Discord.Audio; using Discord.Audio;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NLog;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
@ -40,11 +41,17 @@ namespace NadekoBot.Modules.Music.Classes
return $"【{(int)time.TotalMinutes}m {time.Seconds}s】"; return $"【{(int)time.TotalMinutes}m {time.Seconds}s】";
} }
const int milliseconds = 10;
const int samplesPerFrame = (48000 / 1000) * milliseconds;
const int frameBytes = 3840; //16-bit, 2 channels
private ulong bytesSent { get; set; } = 0; private ulong bytesSent { get; set; } = 0;
public bool PrintStatusMessage { get; set; } = true; public bool PrintStatusMessage { get; set; } = true;
private int skipTo = 0; private int skipTo = 0;
private Logger _log;
public int SkipTo { public int SkipTo {
get { return SkipTo; } get { return SkipTo; }
set { set {
@ -56,6 +63,7 @@ namespace NadekoBot.Modules.Music.Classes
public Song(SongInfo songInfo) public Song(SongInfo songInfo)
{ {
this.SongInfo = songInfo; this.SongInfo = songInfo;
this._log = LogManager.GetCurrentClassLogger();
} }
public Song Clone() public Song Clone()
@ -93,32 +101,32 @@ namespace NadekoBot.Modules.Music.Classes
var t = await Task.WhenAny(prebufferingTask, Task.Delay(5000, cancelToken)); var t = await Task.WhenAny(prebufferingTask, Task.Delay(5000, cancelToken));
if (t != prebufferingTask) if (t != prebufferingTask)
{ {
Console.WriteLine("Prebuffering timed out or canceled. Cannot get any data from the stream."); _log.Debug("Prebuffering timed out or canceled. Cannot get any data from the stream.");
return; return;
} }
else if(prebufferingTask.IsCanceled) else if(prebufferingTask.IsCanceled)
{ {
Console.WriteLine("Prebuffering timed out. Cannot get any data from the stream."); _log.Debug("Prebuffering timed out. Cannot get any data from the stream.");
return; return;
} }
sw.Stop(); sw.Stop();
Console.WriteLine("Prebuffering successfully completed in "+ sw.Elapsed); _log.Debug("Prebuffering successfully completed in "+ sw.Elapsed);
var outStream = voiceClient.CreatePCMStream(3840); var outStream = voiceClient.CreatePCMStream(960);
const int blockSize = 3840; byte[] buffer = new byte[frameBytes];
byte[] buffer = new byte[blockSize];
while (!cancelToken.IsCancellationRequested) while (!cancelToken.IsCancellationRequested)
{ {
//Console.WriteLine($"Read: {songBuffer.ReadPosition}\nWrite: {songBuffer.WritePosition}\nContentLength:{songBuffer.ContentLength}\n---------"); //Console.WriteLine($"Read: {songBuffer.ReadPosition}\nWrite: {songBuffer.WritePosition}\nContentLength:{songBuffer.ContentLength}\n---------");
var read = inStream.Read(buffer, 0, buffer.Length); var read = inStream.Read(buffer, 0, buffer.Length);
//await inStream.CopyToAsync(voiceClient.OutputStream); //await inStream.CopyToAsync(voiceClient.OutputStream);
_log.Debug("read {0}", read);
unchecked unchecked
{ {
bytesSent += (ulong)read; bytesSent += (ulong)read;
} }
if (read < blockSize) if (read < frameBytes)
{ {
if (sb.IsNextFileReady()) if (sb.IsNextFileReady())
{ {
@ -148,8 +156,10 @@ namespace NadekoBot.Modules.Music.Classes
while (this.MusicPlayer.Paused) while (this.MusicPlayer.Paused)
await Task.Delay(200, cancelToken).ConfigureAwait(false); await Task.Delay(200, cancelToken).ConfigureAwait(false);
buffer = AdjustVolume(buffer, MusicPlayer.Volume); //buffer = AdjustVolume(buffer, MusicPlayer.Volume);
if (read != frameBytes) continue;
await outStream.WriteAsync(buffer, 0, read); await outStream.WriteAsync(buffer, 0, read);
await Task.Delay(10);
} }
} }
finally finally
@ -157,18 +167,17 @@ namespace NadekoBot.Modules.Music.Classes
await bufferTask; await bufferTask;
if(inStream != null) if(inStream != null)
inStream.Dispose(); inStream.Dispose();
Console.WriteLine("l");
sb.CleanFiles(); sb.CleanFiles();
} }
} }
private async Task CheckPrebufferingAsync(Stream inStream, SongBuffer sb, CancellationToken cancelToken) private async Task CheckPrebufferingAsync(Stream inStream, SongBuffer sb, CancellationToken cancelToken)
{ {
while (!sb.BufferingCompleted && inStream.Length < 2.MiB()) while (!sb.BufferingCompleted && inStream.Length < 10.MiB())
{ {
await Task.Delay(100, cancelToken); await Task.Delay(100, cancelToken);
} }
Console.WriteLine("Buffering successfull"); _log.Debug("Buffering successfull");
} }
/* /*

View File

@ -644,7 +644,7 @@ namespace NadekoBot.Modules.Music
var musicPlayer = MusicPlayers.GetOrAdd(textCh.Guild.Id, server => var musicPlayer = MusicPlayers.GetOrAdd(textCh.Guild.Id, server =>
{ {
//todo DB //todo DB
float vol = 100;// SpecificConfigurations.Default.Of(server.Id).DefaultMusicVolume; float vol = 1;// SpecificConfigurations.Default.Of(server.Id).DefaultMusicVolume;
var mp = new MusicPlayer(voiceCh, vol); var mp = new MusicPlayer(voiceCh, vol);

View File

@ -34,7 +34,7 @@ namespace NadekoBot
//create client //create client
Client = new DiscordSocketClient(new DiscordSocketConfig Client = new DiscordSocketClient(new DiscordSocketConfig
{ {
AudioMode = Discord.Audio.AudioMode.Incoming, AudioMode = Discord.Audio.AudioMode.Outgoing,
LargeThreshold = 200, LargeThreshold = 200,
LogLevel = LogSeverity.Warning, LogLevel = LogSeverity.Warning,
}); });