From b8b7a99a8da60eb6f7b2fdf63ecc9651c303a849 Mon Sep 17 00:00:00 2001 From: Jim Date: Fri, 24 Sep 2021 18:26:46 +0100 Subject: [PATCH] Moved MusicHandler to it's own source file. --- src/main/java/moe/nekojimi/chords/Main.java | 139 ----------------- .../moe/nekojimi/chords/MusicHandler.java | 140 ++++++++++++++++++ 2 files changed, 140 insertions(+), 139 deletions(-) create mode 100644 src/main/java/moe/nekojimi/chords/MusicHandler.java diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 973b71a..d0d3fab 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -22,9 +22,7 @@ import javax.security.auth.login.LoginException; import javax.sound.sampled.*; import net.dv8tion.jda.api.JDA; 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.CombinedAudio; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -242,141 +240,4 @@ public class Main extends ListenerAdapter } private MusicHandler musicHandler; - public static class MusicHandler implements AudioSendHandler, Closeable - { - private final Queue songQueue = new ConcurrentLinkedQueue<>(); - private File currentSong; - - private AudioInputStream din = null; - private final Queue queue = new ConcurrentLinkedQueue<>(); - private int byteCount; - - public MusicHandler() - { - // load whatever songs we have in WAV format -// File folder = new File("/home/jimj316/Music/"); -// List 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(); - } - - } } diff --git a/src/main/java/moe/nekojimi/chords/MusicHandler.java b/src/main/java/moe/nekojimi/chords/MusicHandler.java new file mode 100644 index 0000000..d8b25ba --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/MusicHandler.java @@ -0,0 +1,140 @@ +/* + * 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 songQueue = new ConcurrentLinkedQueue<>(); + private File currentSong; + private AudioInputStream din = null; + private final Queue 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(); + } + +}