So much better
This commit is contained in:
parent
518fbe150a
commit
b3784dea10
2 changed files with 7 additions and 43 deletions
|
|
@ -18,12 +18,13 @@ package com.bbn.hadder.audio;
|
||||||
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||||
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
||||||
|
import net.dv8tion.jda.api.audio.AudioReceiveHandler;
|
||||||
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class AudioPlayerSendHandler implements AudioSendHandler {
|
public class AudioPlayerSendHandler implements AudioSendHandler, AudioReceiveHandler {
|
||||||
|
|
||||||
private final AudioPlayer audioPlayer;
|
private final AudioPlayer audioPlayer;
|
||||||
private AudioFrame lastFrame;
|
private AudioFrame lastFrame;
|
||||||
|
|
|
||||||
|
|
@ -16,68 +16,31 @@
|
||||||
|
|
||||||
package com.bbn.hadder.commands.music;
|
package com.bbn.hadder.commands.music;
|
||||||
|
|
||||||
|
import com.bbn.hadder.audio.AudioPlayerSendHandler;
|
||||||
import com.bbn.hadder.commands.Command;
|
import com.bbn.hadder.commands.Command;
|
||||||
import com.bbn.hadder.commands.CommandEvent;
|
import com.bbn.hadder.commands.CommandEvent;
|
||||||
import com.bbn.hadder.core.Perm;
|
import com.bbn.hadder.core.Perm;
|
||||||
import com.bbn.hadder.core.Perms;
|
import com.bbn.hadder.core.Perms;
|
||||||
import net.dv8tion.jda.api.audio.*;
|
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.managers.AudioManager;
|
import net.dv8tion.jda.api.managers.AudioManager;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
||||||
|
|
||||||
@Perms(Perm.BOT_OWNER)
|
@Perms(Perm.BOT_OWNER)
|
||||||
public class EchoCommand implements Command {
|
public class EchoCommand implements Command {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executed(String[] args, CommandEvent event) {
|
public void executed(String[] args, CommandEvent e) {
|
||||||
Guild guild = event.getMember().getVoiceState().getChannel().getGuild();
|
Guild guild = e.getMember().getVoiceState().getChannel().getGuild();
|
||||||
AudioManager audioManager = guild.getAudioManager();
|
AudioManager audioManager = guild.getAudioManager();
|
||||||
EchoHandler handler = new EchoHandler();
|
AudioPlayerSendHandler handler = new AudioPlayerSendHandler(e.getAudioManager().getPlayer(e.getGuild()));
|
||||||
if (!audioManager.isConnected()) {
|
if (!audioManager.isConnected()) {
|
||||||
audioManager.setSendingHandler(handler);
|
audioManager.setSendingHandler(handler);
|
||||||
audioManager.setReceivingHandler(handler);
|
audioManager.setReceivingHandler(handler);
|
||||||
audioManager.openAudioConnection(event.getMember().getVoiceState().getChannel());
|
audioManager.openAudioConnection(e.getMember().getVoiceState().getChannel());
|
||||||
} else {
|
} else {
|
||||||
audioManager.closeAudioConnection();
|
audioManager.closeAudioConnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class EchoHandler implements AudioSendHandler, AudioReceiveHandler {
|
|
||||||
|
|
||||||
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canProvide() {
|
|
||||||
return !queue.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public ByteBuffer provide20MsAudio() {
|
|
||||||
byte[] data = queue.poll();
|
|
||||||
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canReceiveCombined() {
|
|
||||||
return queue.size() < 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleCombinedAudio(@Nonnull CombinedAudio combinedAudio) {
|
|
||||||
if (combinedAudio.getUsers().isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
byte[] data = combinedAudio.getAudioData(1.0f); // volume at 100% = 1.0 (50% = 0.5 / 55% = 0.55)
|
|
||||||
queue.add(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] labels() {
|
public String[] labels() {
|
||||||
return new String[]{"echo"};
|
return new String[]{"echo"};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue