DSharpPlus/Example-Bots

Volume Command Example?

Artzalez opened this issue ยท 4 comments

Any example to change the volume?

Volume is not a trivial matter. You need to understand PCM to be able to do that. As such, there will be no examples for changing volume.

Im using this, is working

`
private double Volume { get; set; } = 0.10;

    private unsafe void RescaleVolume(byte[] data)
    {
        fixed (byte* ptr8 = data)
        {
            var ptr16 = (short*)ptr8;
            for (var i = 0; i < data.Length / 2; i++)
                *(ptr16 + i) = (short)(*(ptr16 + i) * this.Volume);
        }
    }

    [Command("volume")]
    public async Task VolumeAsync(CommandContext ctx, double vol)
    {
        Volume = vol / 100;
        await ctx.RespondAsync($"Volumen puesto en {(vol).ToString()}%");
    }

And i change the play command while for this:

    [Command("play")]
    public async Task Play(CommandContext ctx, [RemainingText] string file)
    {
        var vnext = ctx.Client.GetVoiceNextClient();

        var vnc = vnext.GetConnection(ctx.Guild);
        if (vnc == null)
            throw new InvalidOperationException("Not connected in this guild.");

        if (!File.Exists(file))
            throw new FileNotFoundException("File was not found.");

        await ctx.RespondAsync("๐Ÿ‘Œ");
        await vnc.SendSpeakingAsync(true); // send a speaking indicator

        var psi = new ProcessStartInfo
        {
            FileName = "ffmpeg",
            Arguments = $@"-i ""{file}"" -ac 2 -f s16le -ar 48000 pipe:1",
            RedirectStandardOutput = true,
            UseShellExecute = false
        };

        var ffmpeg = Process.Start(psi);
        var ffout = ffmpeg.StandardOutput.BaseStream;
        
        var buff = new byte[3840];
        var br = 0;
        while ((br = ffout.Read(buff, 0, buff.Length)) > 0)
        {
            if (br < buff.Length) // not a full sample, mute the rest
                for (var i = br; i < buff.Length; i++)
                    buff[i] = 0;

            if (Volume != 1.0)
                RescaleVolume(buff);

            await vnc.SendAsync(buff, 20);
        }

        await vnc.SendSpeakingAsync(false); // we're not speaking anymore
    }

`

if someone here I can leave it

I know - that's my code. It's still not a clean way, and I am not willing to suggest that. FFmpeg might be offering some options.