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
src/NadekoBot/credentials.json
src/NadekoBot/project.lock.json
src/NadekoBot/data/*
# NuGet Packages
*.nupkg

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

View File

@ -1,5 +1,6 @@
using Discord.Audio;
using NadekoBot.Extensions;
using NLog;
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
@ -40,11 +41,17 @@ namespace NadekoBot.Modules.Music.Classes
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;
public bool PrintStatusMessage { get; set; } = true;
private int skipTo = 0;
private Logger _log;
public int SkipTo {
get { return SkipTo; }
set {
@ -56,6 +63,7 @@ namespace NadekoBot.Modules.Music.Classes
public Song(SongInfo songInfo)
{
this.SongInfo = songInfo;
this._log = LogManager.GetCurrentClassLogger();
}
public Song Clone()
@ -93,32 +101,32 @@ namespace NadekoBot.Modules.Music.Classes
var t = await Task.WhenAny(prebufferingTask, Task.Delay(5000, cancelToken));
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;
}
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;
}
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[blockSize];
byte[] buffer = new byte[frameBytes];
while (!cancelToken.IsCancellationRequested)
{
//Console.WriteLine($"Read: {songBuffer.ReadPosition}\nWrite: {songBuffer.WritePosition}\nContentLength:{songBuffer.ContentLength}\n---------");
var read = inStream.Read(buffer, 0, buffer.Length);
//await inStream.CopyToAsync(voiceClient.OutputStream);
_log.Debug("read {0}", read);
unchecked
{
bytesSent += (ulong)read;
}
if (read < blockSize)
if (read < frameBytes)
{
if (sb.IsNextFileReady())
{
@ -148,8 +156,10 @@ namespace NadekoBot.Modules.Music.Classes
while (this.MusicPlayer.Paused)
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 Task.Delay(10);
}
}
finally
@ -157,18 +167,17 @@ namespace NadekoBot.Modules.Music.Classes
await bufferTask;
if(inStream != null)
inStream.Dispose();
Console.WriteLine("l");
sb.CleanFiles();
}
}
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);
}
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 =>
{
//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);

View File

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