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;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.concurrent.LinkedBlockingDeque;
@ -58,7 +59,7 @@ public class Downloader implements Consumer<Song>
System.out.println("Running command: " + cmd);
// Process exec = Runtime.getRuntime().exec().split(" "));
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)
{
exec.destroyForcibly();
@ -67,7 +68,7 @@ public class Downloader implements Consumer<Song>
InputStream in = exec.getInputStream();
String output = new String(in.readAllBytes(), Charset.defaultCharset());
System.out.println(output);
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
Matcher matcher = Pattern.compile("Destination: (.*)$").matcher(output);
if (matcher.find())
song.setLocation(new File(matcher.group(1)));
else if (exec.exitValue() != 0)

View File

@ -93,10 +93,6 @@ public class Main extends ListenerAdapter
String content = message.getContentRaw();
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
if (author.isBot())
return;

View File

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