diff --git a/Elementary/Audio/AudioManager.cs b/Elementary/Audio/AudioManager.cs index 14722c8..ac398c9 100644 --- a/Elementary/Audio/AudioManager.cs +++ b/Elementary/Audio/AudioManager.cs @@ -7,6 +7,7 @@ using Discord.WebSocket; using ManagedBass; using Microsoft.Extensions.DependencyInjection; using NAudio.Wave; +using NLog; namespace Elementary.Audio; @@ -25,6 +26,8 @@ public class AudioManager private PlaybackQueue _playbackQueue; // private AudioMixer _audioMixer; + private ILogger _logger; + public bool isConnected; public AudioManager(IServiceProvider services, DiscordSocketClient client, SozaiAPI sozaiApi, @@ -35,16 +38,27 @@ public class AudioManager _sozaiAPI = sozaiApi; _voicevoxAPI = voicevoxApi; _audioConverter = new(); + _logger = LogManager.GetCurrentClassLogger(); } public async Task JoinChannel(IVoiceChannel channel) { _audioClient = await channel.ConnectAsync(true); + // _audioClient.ClientDisconnected += async (exception) => + // { + // isConnected = false; + // await JoinChannel(channel); + // }; + _audioClient.Disconnected += async (exception) => + { + isConnected = false; + await JoinChannel(channel); + }; _audioStream = _audioClient.CreatePCMStream(AudioApplication.Music, 128 * 1024); _playbackQueue = _services.GetRequiredService(); - + // _audioMixer = new AudioMixer(_audioStream); isConnected = true; @@ -53,14 +67,20 @@ public class AudioManager public async Task LeaveChannel() { await _audioClient.StopAsync(); + await _audioStream.FlushAsync(); + _audioStream.Dispose(); + _audioClient.Dispose(); } public async Task PlayAudio(string path) { await using var wave = _audioConverter.CreateStreamFromFilePath(path, 0.15f); + _logger.Log(LogLevel.Info, $"Playing {path} {wave.Length} bytes"); + // _audioMixer.AddStream(wave); await wave.CopyToAsync(_audioStream); + // GC.Collect(); } public async Task PlayText(string text) @@ -68,6 +88,7 @@ public class AudioManager if (text.Contains("```")) text = "コードブロック"; if (text.StartsWith("!") || text.StartsWith(".")) return; text = Regex.Replace(text, @"https?://[\w/:%#\$&\?\(\)~\.=\+\-]+", "URL"); + text = Regex.Replace(text, @"<:[\w]+:[\d]+>", m => m.Value.Split(":")[1]); float volume = 0.12f; @@ -82,6 +103,7 @@ public class AudioManager await using var wave = _audioConverter.CreateStreamFromStream(stream, volume); // _audioMixer.AddStream(wave); await wave.CopyToAsync(_audioStream); + // GC.Collect(); } public async Task StopAudio()