Compare commits

..

No commits in common. "ed610ebdaec3398e5df75f60e95359945b69c28b" and "358b4e11c8555287faf9e75cccfbf559a37abd28" have entirely different histories.

2 changed files with 220 additions and 213 deletions

View File

@ -22,7 +22,9 @@ import javax.security.auth.login.LoginException;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.audio.AudioReceiveHandler;
import net.dv8tion.jda.api.audio.AudioSendHandler; import net.dv8tion.jda.api.audio.AudioSendHandler;
import net.dv8tion.jda.api.audio.CombinedAudio;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -83,8 +85,11 @@ public class Main extends ListenerAdapter
{ {
String arg = content.substring("!join ".length()); String arg = content.substring("!join ".length());
onJoinCommand(event, guild, arg); onJoinCommand(event, guild, arg);
} else if (content.equals("!join")) }
else if (content.equals("!join"))
{
onJoinCommand(event); onJoinCommand(event);
}
else if (content.startsWith("!play ")) else if (content.startsWith("!play "))
{ {
String arg = content.substring("!join ".length()); String arg = content.substring("!join ".length());
@ -108,9 +113,12 @@ public class Main extends ListenerAdapter
{ {
connectTo(channel); // Join the channel of the user connectTo(channel); // Join the channel of the user
onConnecting(channel, event.getChannel()); // Tell the user about our success onConnecting(channel, event.getChannel()); // Tell the user about our success
} else }
else
{
onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
} }
}
/** /**
* Handle command with arguments. * Handle command with arguments.
@ -127,8 +135,9 @@ public class Main extends ListenerAdapter
boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
VoiceChannel channel = null; VoiceChannel channel = null;
if (isNumber) // The input is an id? if (isNumber) // The input is an id?
{
channel = guild.getVoiceChannelById(arg); channel = guild.getVoiceChannelById(arg);
}
if (channel == null) // Then the input must be a name? if (channel == null) // Then the input must be a name?
{ {
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true); List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true);
@ -164,7 +173,7 @@ public class Main extends ListenerAdapter
private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException
{ {
String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + arg; String cmd = "/usr/bin/youtube-dl -x --audio-format=wav "+arg;
System.out.println("Running command: " + cmd); System.out.println("Running command: " + cmd);
// Process exec = Runtime.getRuntime().exec().split(" ")); // Process exec = Runtime.getRuntime().exec().split(" "));
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start(); Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start();
@ -180,6 +189,7 @@ public class Main extends ListenerAdapter
return destination; return destination;
} }
/** /**
* Inform user about successful connection. * Inform user about successful connection.
* *
@ -197,8 +207,7 @@ public class Main extends ListenerAdapter
* The channel to connect to is not known to us. * The channel to connect to is not known to us.
* *
* @param channel * @param channel
* The message channel (text channel abstraction) to send failure * The message channel (text channel abstraction) to send failure information to
* information to
* @param comment * @param comment
* The information of this channel * The information of this channel
*/ */
@ -223,6 +232,7 @@ public class Main extends ListenerAdapter
// EchoHandler handler = new EchoHandler(); // EchoHandler handler = new EchoHandler();
// The order of the following instructions does not matter! // The order of the following instructions does not matter!
// Set the sending handler to our echo system // Set the sending handler to our echo system
audioManager.setSendingHandler(musicHandler); audioManager.setSendingHandler(musicHandler);
// Set the receiving handler to the same echo system, otherwise we can't echo anything // Set the receiving handler to the same echo system, otherwise we can't echo anything
@ -232,4 +242,141 @@ public class Main extends ListenerAdapter
} }
private MusicHandler musicHandler; private MusicHandler musicHandler;
public static class MusicHandler implements AudioSendHandler, Closeable
{
private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
private File currentSong;
private AudioInputStream din = null;
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private int byteCount;
public MusicHandler()
{
// load whatever songs we have in WAV format
// File folder = new File("/home/jimj316/Music/");
// List<File> wavs = Arrays.asList(folder.listFiles((file, string) ->
// {
// return string.endsWith(".wav");
// }));
// Collections.shuffle(wavs);
// songQueue.addAll(wavs);
// nextSong();
}
public void addSong(File file)
{
System.out.println("Song added to queue: " + file.getAbsolutePath());
songQueue.add(file);
if (!canProvide())
nextSong();
}
private boolean nextSong()
{
AudioInputStream in = null;
try
{
if (din != null)
{
din.close();
din = null;
}
if (currentSong != null)
{
currentSong.delete();
currentSong = null;
}
currentSong = songQueue.poll();
if (currentSong == null)
return false;
System.out.println("Playing song " + currentSong.getAbsolutePath());
in = AudioSystem.getAudioInputStream(currentSong);
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
din = AudioSystem.getAudioInputStream(decodedFormat, in);
byteCount = 3840;
while (queue.size() < 500)
{
if (!readData())
break;
}
System.out.println("Queue filled to " + queue.size());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
}
return false;
}
@Override
public boolean canProvide()
{
// If we have something in our buffer we can provide it to the send system
return !queue.isEmpty();
}
@Override
public ByteBuffer provide20MsAudio()
{
// use what we have in our buffer to send audio as PCM
while (queue.size() < 500)
{
if (!readData())
{
if (!nextSong())
break;
}
}
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private boolean readData()
{
if (din == null)
return false;
try
{
// if (din.available() == 0)
// return false;
int bytesToRead = byteCount;
if (din.available() > 0 && din.available() < bytesToRead)
bytesToRead = din.available();
byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead);
int read = din.read(bytes);
if (read < 0)
return false;
queue.add(bytes);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
@Override
public boolean isOpus()
{
return false; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void close() throws IOException
{
din.close();
}
}
} }

View File

@ -1,140 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package moe.nekojimi.chords;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import net.dv8tion.jda.api.audio.AudioSendHandler;
/**
*
* @author jimj316
*/
public class MusicHandler implements AudioSendHandler, Closeable
{
private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
private File currentSong;
private AudioInputStream din = null;
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private int byteCount;
public MusicHandler()
{
}
public void addSong(File file)
{
System.out.println("Song added to queue: " + file.getAbsolutePath());
songQueue.add(file);
if (!canProvide())
nextSong();
}
private boolean nextSong()
{
AudioInputStream in = null;
try
{
if (din != null)
{
din.close();
din = null;
}
if (currentSong != null)
{
currentSong.delete();
currentSong = null;
}
currentSong = songQueue.poll();
if (currentSong == null)
return false;
System.out.println("Playing song " + currentSong.getAbsolutePath());
in = AudioSystem.getAudioInputStream(currentSong);
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
din = AudioSystem.getAudioInputStream(decodedFormat, in);
byteCount = 3840;
while (queue.size() < 500)
if (!readData())
break;
System.out.println("Queue filled to " + queue.size());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
}
return false;
}
@Override
public boolean canProvide()
{
// If we have something in our buffer we can provide it to the send system
return !queue.isEmpty();
}
@Override
public ByteBuffer provide20MsAudio()
{
// use what we have in our buffer to send audio as PCM
while (queue.size() < 500)
if (!readData())
if (!nextSong())
break;
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private boolean readData()
{
if (din == null)
return false;
try
{
// if (din.available() == 0)
// return false;
int bytesToRead = byteCount;
if (din.available() > 0 && din.available() < bytesToRead)
bytesToRead = din.available();
byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead);
int read = din.read(bytes);
if (read < 0)
return false;
queue.add(bytes);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
@Override
public boolean isOpus()
{
return false; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void close() throws IOException
{
din.close();
}
}