Skip to content

VoiceNext adds noise #1500

@9903286

Description

@9903286

Summary

A lot of audio passed through DSharpPlus with VoiceNext has noise in it.
Provided problem.mp4 (recorded from soundcard out) for example, test.mp4 (cut down version because of copyright) is the input

Details

Tried running on both Windows 10 and Ubuntu 20.04 with discord clients tried on both Windows 10 and Android. Any combination has the same issue.

Tried both .NET 6.0 and .NET 7.0

Packages tried with both NuGet and cloning release/4.3
DSharpPlus v4.3.0
DSharpPlus.CommandsNext v4.3.0
DSharpPlus.Interactivity v4.3.0
DSharpPlus.Rest v4.3.0
DSharpPlus.SlashCommands v4.3.0
DSharpPlus.VoiceNext v4.3.0
DSharpPlus.VoiceNext.Natives v1.0.0

FFMPEG: Tried both pre-compiled and self-compiled
Pre-compiled version: ffmpeg version 2022-12-08-git-9ca139b2aa-essentials_build
Self-compiled version: ffmpeg version N-110021-g85b185b504-ffmpeg-windows-build-helpers

Reproduce

Created new project in Visual Studio 2022
Used NuGet to install DSharpPlus v4.3.0 / Natives 1.0.0
Added test.mp4, ffmpeg.exe and a TOKEN.txt to the output bin folder
Added provided code to Program.cs
Run program, join discord channel and do '/join' followed by '/play path: test.mp4'
Listen to noisy audio

Program.cs

using System.Diagnostics;

using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.VoiceNext;

using Microsoft.Extensions.Logging;

internal class Program {
	public DiscordClient _client;
	public SlashCommandsExtension _slashCommands;

	public static void Main(string[] args) {
		Program program = new();
		program.RunAsync().Wait();
	}
	
	public Program() {
		string discordToken = File.ReadAllText("TOKEN.txt");
		_client = new DiscordClient(new DiscordConfiguration {
			Token = discordToken,
			TokenType = TokenType.Bot,
			Intents = DiscordIntents.All,
			MinimumLogLevel = LogLevel.Warning
		});
		
		_client.UseVoiceNext();
		_slashCommands = _client.UseSlashCommands();
		_slashCommands.RegisterCommands<MediaCommands>();
	}

	public async Task RunAsync() {
		await _client.ConnectAsync(new("nothing", ActivityType.Playing), UserStatus.Online);
		await Task.Delay(-1);
	}

	internal class MediaCommands : ApplicationCommandModule {
		public static Stream? ConvertAudioToPcm(string filePath) {
			Process? ffmpeg = Process.Start(new ProcessStartInfo {
				FileName = "ffmpeg",
				Arguments = $@"-i ""{filePath}"" -ac 2 -f s16le -ar 48000 pipe:1",
				RedirectStandardOutput = true,
				UseShellExecute = false
			});

			return ffmpeg?.StandardOutput.BaseStream;
		}

		[SlashCommand("join", "joins")]
		public async Task JoinCommand(InteractionContext ctx) {
			DiscordChannel channel = ctx.Member.VoiceState?.Channel;
			await channel.ConnectAsync();
		}

		[SlashCommand("play", "plays")]
		public async Task PlayCommand(InteractionContext ctx, [Option("path", "path")] string path) {
			VoiceNextExtension vnext = ctx.Client.GetVoiceNext();
			VoiceNextConnection connection = vnext.GetConnection(ctx.Guild);

			VoiceTransmitSink transmit = connection.GetTransmitSink();

			Stream? pcm = ConvertAudioToPcm(path);
			await pcm.CopyToAsync(transmit);
			await pcm.DisposeAsync();
		}

		[SlashCommand("leave", "leaves")]
		public async Task LeaveCommand(InteractionContext ctx) {
			VoiceNextExtension vnext = ctx.Client.GetVoiceNext();
			VoiceNextConnection connection = vnext.GetConnection(ctx.Guild);

			connection.Disconnect();
		}
	}
}

Extra

Did not have this problem with Anarchy, but decided to try and switch to DSharpPlus since it seems more actively maintained and has better documentation. Used the same ffmpeg.exe and libs for that.

I've also tried a bunch of different settings with ffmpeg on the test.mp4 file but could not replicate the result without DSharpPlus and since it doesn't happen in Anarchy i'm assuming there's something wrong with DSharpPlus's VoiceNext module?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Effort

    None yet

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions