Compare commits

...

3 Commits

Author SHA1 Message Date
AwesomeToaster 579fb01a20 Don't respond to messages unless they're in a text channel with "music"
in the name.
2021-09-29 22:10:25 +01:00
Nekojimi 42a9f0047b MusicHandler: rewrite to use circular byte buffer.
Also now attempts to initially fill buffer in a single read.
2021-09-29 22:08:53 +01:00
Nekojimi 2412d57f4d Downloader: change timeout to 300 seconds, and fix destination parsing. 2021-09-29 22:07:56 +01:00
3 changed files with 36 additions and 17 deletions

View File

@ -6,7 +6,6 @@
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;
@ -59,7 +58,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(30, TimeUnit.SECONDS);
boolean done = exec.waitFor(300, TimeUnit.SECONDS);
if (!done)
{
exec.destroyForcibly();
@ -68,7 +67,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: (.*)$").matcher(output);
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
if (matcher.find())
song.setLocation(new File(matcher.group(1)));
else if (exec.exitValue() != 0)

View File

@ -93,6 +93,10 @@ 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,7 +10,6 @@ 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;
@ -19,6 +18,7 @@ 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,10 +27,13 @@ import net.dv8tion.jda.api.audio.AudioSendHandler;
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 Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
private boolean playing = true;
private int byteCount;
@ -80,7 +83,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
public boolean nextSong(boolean immediate)
{
if (immediate)
queue.clear();
audioBuffer.clear();
AudioInputStream in = null;
try
@ -104,10 +107,8 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
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());
fillBuffer(false);
System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
@ -134,19 +135,26 @@ 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 !queue.isEmpty() && playing;
return audioBuffer.getCurrentNumberOfBytes() > byteCount && 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 (queue.size() < 500)
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
if (!readData())
if (!nextSong())
if (!canSkip || !nextSong())
break;
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private boolean readData()
@ -157,15 +165,23 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
{
// if (din.available() == 0)
// return false;
int bytesToRead = byteCount;
int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes();
int space = audioBuffer.getSpace();
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);
// queue.add(bytes);
audioBuffer.add(bytes, 0, read);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);