Compare commits

..

No commits in common. "579fb01a203360c9fdbb6ef1821621f6b93d5917" and "603b7e2cd8d6432e25c55d1d305a24a9a570c63d" have entirely different histories.

3 changed files with 17 additions and 36 deletions

View File

@ -6,6 +6,7 @@
package moe.nekojimi.chords; package moe.nekojimi.chords;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
@ -58,7 +59,7 @@ public class Downloader implements Consumer<Song>
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();
boolean done = exec.waitFor(300, TimeUnit.SECONDS); boolean done = exec.waitFor(30, TimeUnit.SECONDS);
if (!done) if (!done)
{ {
exec.destroyForcibly(); exec.destroyForcibly();
@ -67,7 +68,7 @@ public class Downloader implements Consumer<Song>
InputStream in = exec.getInputStream(); InputStream in = exec.getInputStream();
String output = new String(in.readAllBytes(), Charset.defaultCharset()); String output = new String(in.readAllBytes(), Charset.defaultCharset());
System.out.println(output); System.out.println(output);
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output); Matcher matcher = Pattern.compile("Destination: (.*)$").matcher(output);
if (matcher.find()) if (matcher.find())
song.setLocation(new File(matcher.group(1))); song.setLocation(new File(matcher.group(1)));
else if (exec.exitValue() != 0) else if (exec.exitValue() != 0)

View File

@ -93,10 +93,6 @@ public class Main extends ListenerAdapter
String content = message.getContentRaw(); String content = message.getContentRaw();
Guild guild = event.getGuild(); Guild guild = event.getGuild();
// Ignore message if it's not in a music channel
if (!event.getChannel().getName().contains("music"))
return;
// Ignore message if bot // Ignore message if bot
if (author.isBot()) if (author.isBot())
return; return;

View File

@ -10,6 +10,7 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -18,7 +19,6 @@ import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import net.dv8tion.jda.api.audio.AudioSendHandler; import net.dv8tion.jda.api.audio.AudioSendHandler;
import org.apache.commons.io.input.buffer.CircularByteBuffer;
/** /**
* *
@ -27,13 +27,10 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song> public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
{ {
private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
private final LinkedList<Song> songQueue = new LinkedList<>(); private final LinkedList<Song> songQueue = new LinkedList<>();
private Song currentSong; private Song currentSong;
private AudioInputStream din = null; private AudioInputStream din = null;
// private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>(); private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
private boolean playing = true; private boolean playing = true;
private int byteCount; private int byteCount;
@ -83,7 +80,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
public boolean nextSong(boolean immediate) public boolean nextSong(boolean immediate)
{ {
if (immediate) if (immediate)
audioBuffer.clear(); queue.clear();
AudioInputStream in = null; AudioInputStream in = null;
try try
@ -107,8 +104,10 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT; AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
din = AudioSystem.getAudioInputStream(decodedFormat, in); din = AudioSystem.getAudioInputStream(decodedFormat, in);
byteCount = 3840; byteCount = 3840;
fillBuffer(false); while (queue.size() < 500)
System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes()); if (!readData())
break;
System.out.println("Queue filled to " + queue.size());
return true; return true;
} catch (UnsupportedAudioFileException | IOException ex) } catch (UnsupportedAudioFileException | IOException ex)
{ {
@ -135,26 +134,19 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
public boolean canProvide() public boolean canProvide()
{ {
// If we have something in our buffer we can provide it to the send system // If we have something in our buffer we can provide it to the send system
return audioBuffer.getCurrentNumberOfBytes() > byteCount && playing; return !queue.isEmpty() && playing;
} }
@Override @Override
public ByteBuffer provide20MsAudio() public ByteBuffer provide20MsAudio()
{
fillBuffer(true);
byte[] data = new byte[byteCount];
audioBuffer.read(data, 0, data.length);
// byte[] data = queue.poll();
return ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private void fillBuffer(boolean canSkip)
{ {
// use what we have in our buffer to send audio as PCM // use what we have in our buffer to send audio as PCM
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) while (queue.size() < 500)
if (!readData()) if (!readData())
if (!canSkip || !nextSong()) if (!nextSong())
break; break;
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
} }
private boolean readData() private boolean readData()
@ -165,23 +157,15 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
{ {
// if (din.available() == 0) // if (din.available() == 0)
// return false; // return false;
int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes(); int bytesToRead = byteCount;
int space = audioBuffer.getSpace();
if (din.available() > 0 && din.available() < bytesToRead) if (din.available() > 0 && din.available() < bytesToRead)
bytesToRead = din.available(); bytesToRead = din.available();
if (bytesToRead > space)
bytesToRead = space;
if (bytesToRead == 0)
return false;
byte[] bytes = new byte[bytesToRead]; byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead); // byte[] bytes = din.readNBytes(bytesToRead);
int read = din.read(bytes); int read = din.read(bytes);
System.out.println("Wanted: " + byteCount + " Space:" + space + " Available: " + din.available() + " To read: " + bytesToRead + " Read: " + read);
if (read < 0) if (read < 0)
return false; return false;
// queue.add(bytes); queue.add(bytes);
audioBuffer.add(bytes, 0, read);
} catch (IOException ex) } catch (IOException ex)
{ {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);