Move song queue to new QueueManager class.
This commit is contained in:
parent
d7c48b38fa
commit
594ffa9ba9
|
@ -37,6 +37,7 @@ public class Main extends ListenerAdapter
|
||||||
private MusicHandler musicHandler;
|
private MusicHandler musicHandler;
|
||||||
private final Downloader downloader;
|
private final Downloader downloader;
|
||||||
private final Searcher searcher;
|
private final Searcher searcher;
|
||||||
|
private final QueueManager queueManager;
|
||||||
private JDA jda;
|
private JDA jda;
|
||||||
|
|
||||||
private final Map<String, Command> commands = new HashMap<>();
|
private final Map<String, Command> commands = new HashMap<>();
|
||||||
|
@ -81,6 +82,8 @@ public class Main extends ListenerAdapter
|
||||||
public Main()
|
public Main()
|
||||||
{
|
{
|
||||||
log("INFO", "Starting up...");
|
log("INFO", "Starting up...");
|
||||||
|
|
||||||
|
// init downloader
|
||||||
downloader = new Downloader();
|
downloader = new Downloader();
|
||||||
downloader.setMessageHandler((Song song, Exception ex) ->
|
downloader.setMessageHandler((Song song, Exception ex) ->
|
||||||
{
|
{
|
||||||
|
@ -110,8 +113,14 @@ public class Main extends ListenerAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// init searcher
|
||||||
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
|
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
|
||||||
|
|
||||||
|
// init queue manager
|
||||||
|
queueManager = new QueueManager();
|
||||||
|
|
||||||
|
// init commands
|
||||||
addCommand(new JoinCommand(this));
|
addCommand(new JoinCommand(this));
|
||||||
addCommand(new LeaveCommand(this));
|
addCommand(new LeaveCommand(this));
|
||||||
addCommand(new PlayCommand(this));
|
addCommand(new PlayCommand(this));
|
||||||
|
@ -200,7 +209,7 @@ public class Main extends ListenerAdapter
|
||||||
song.setRequestedIn(event.getChannel().getId());
|
song.setRequestedIn(event.getChannel().getId());
|
||||||
song.setNumber(trackNumber);
|
song.setNumber(trackNumber);
|
||||||
trackNumber++;
|
trackNumber++;
|
||||||
downloader.accept(new Downloader.DownloadTask(song, musicHandler));
|
downloader.accept(new Downloader.DownloadTask(song, queueManager));
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,6 +290,10 @@ public class Main extends ListenerAdapter
|
||||||
return currentVoiceChannel;
|
return currentVoiceChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public QueueManager getQueueManager()
|
||||||
|
{
|
||||||
|
return queueManager;
|
||||||
|
}
|
||||||
|
|
||||||
public int getTrackNumber()
|
public int getTrackNumber()
|
||||||
{
|
{
|
||||||
|
|
|
@ -6,11 +6,7 @@
|
||||||
package moe.nekojimi.chords;
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import moe.nekojimi.chords.Util;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Queue;
|
|
||||||
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;
|
||||||
import javax.sound.sampled.*;
|
import javax.sound.sampled.*;
|
||||||
|
@ -21,10 +17,11 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
|
||||||
*
|
*
|
||||||
* @author jimj316
|
* @author jimj316
|
||||||
*/
|
*/
|
||||||
public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
public class MusicHandler implements AudioSendHandler, Closeable
|
||||||
{
|
{
|
||||||
|
|
||||||
private final LinkedList<Song> songQueue = new LinkedList<>();
|
private QueueManager queueManager;
|
||||||
|
// private final LinkedList<Song> songQueue = new LinkedList<>();
|
||||||
// private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
|
// private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
|
||||||
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
|
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
|
||||||
private boolean playing = true;
|
private boolean playing = true;
|
||||||
|
@ -53,37 +50,22 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSong(Song song)
|
void setQueueManager(QueueManager manager)
|
||||||
{
|
{
|
||||||
System.out.println("Song added to queue: " + song.getLocation().getAbsolutePath());
|
queueManager = manager;
|
||||||
songQueue.add(song);
|
|
||||||
if (!canProvide() && playing)
|
|
||||||
nextSong();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeSong(int i)
|
public void playNext()
|
||||||
{
|
{
|
||||||
try
|
nextSong(true);
|
||||||
{
|
|
||||||
songQueue.remove(i);
|
|
||||||
return true;
|
|
||||||
} catch (ArrayIndexOutOfBoundsException ex)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeSong(Song song)
|
// public boolean restartSong()
|
||||||
{
|
// {
|
||||||
return songQueue.remove(song);
|
//// songQueue.addFirst(currentSong);
|
||||||
}
|
// currentSong = null;
|
||||||
|
// return nextSong(true);
|
||||||
public boolean restartSong()
|
// }
|
||||||
{
|
|
||||||
songQueue.addFirst(currentSong);
|
|
||||||
currentSong = null;
|
|
||||||
return nextSong(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean nextSong()
|
private boolean nextSong()
|
||||||
{
|
{
|
||||||
|
@ -106,7 +88,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
||||||
currentSong.delete();
|
currentSong.delete();
|
||||||
currentSong = null;
|
currentSong = null;
|
||||||
}
|
}
|
||||||
currentSong = songQueue.poll();
|
currentSong = queueManager.nextSongNeeded();
|
||||||
if (currentSong == null)
|
if (currentSong == null)
|
||||||
{
|
{
|
||||||
System.out.println("End of queue.");
|
System.out.println("End of queue.");
|
||||||
|
@ -232,12 +214,6 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
||||||
// }
|
// }
|
||||||
// return true;
|
// return true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public Queue<Song> getSongQueue()
|
|
||||||
{
|
|
||||||
return songQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Song getCurrentSong()
|
public Song getCurrentSong()
|
||||||
{
|
{
|
||||||
return currentSong;
|
return currentSong;
|
||||||
|
@ -254,9 +230,4 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(Song t)
|
|
||||||
{
|
|
||||||
addSong(t);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,4 +61,9 @@ public class Playlist
|
||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Song getNextSong()
|
||||||
|
{
|
||||||
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 jimj316
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jimj316
|
||||||
|
*/
|
||||||
|
public class QueueManager implements Consumer<Song>
|
||||||
|
{
|
||||||
|
|
||||||
|
private Mode mode;
|
||||||
|
private final Queue<Song> jukeboxQueue;
|
||||||
|
private Playlist playlist;
|
||||||
|
private MusicHandler handler;
|
||||||
|
|
||||||
|
public QueueManager()
|
||||||
|
{
|
||||||
|
jukeboxQueue = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(Song t)
|
||||||
|
{
|
||||||
|
jukeboxQueue.add(t);
|
||||||
|
|
||||||
|
handler.playNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by the music handler when the current song has ended, or if
|
||||||
|
* playNext is called with nothing playing.
|
||||||
|
*
|
||||||
|
* @return the next track to play, or null to stop playing.
|
||||||
|
*/
|
||||||
|
public Song nextSongNeeded()
|
||||||
|
{
|
||||||
|
// if there's anything in the queue, play that first
|
||||||
|
if (!jukeboxQueue.isEmpty())
|
||||||
|
{
|
||||||
|
return jukeboxQueue.poll();
|
||||||
|
}
|
||||||
|
// otherwise if there's a playlist, shuffle from that
|
||||||
|
else if (playlist != null)
|
||||||
|
{
|
||||||
|
return playlist.getNextSong();
|
||||||
|
}
|
||||||
|
// otherwise stop playing
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MusicHandler getHandler()
|
||||||
|
{
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSong(Song song)
|
||||||
|
{
|
||||||
|
System.out.println("Song added to queue: " + song.getLocation().getAbsolutePath());
|
||||||
|
jukeboxQueue.add(song);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeSong(int i)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return jukeboxQueue.remove((Song) jukeboxQueue.toArray()[i]);
|
||||||
|
} catch (ArrayIndexOutOfBoundsException ex)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeSong(Song song)
|
||||||
|
{
|
||||||
|
return jukeboxQueue.remove(song);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHandler(MusicHandler handler)
|
||||||
|
{
|
||||||
|
this.handler = handler;
|
||||||
|
handler.setQueueManager(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Queue<Song> getJukeboxQueue()
|
||||||
|
{
|
||||||
|
return jukeboxQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Playlist getPlaylist()
|
||||||
|
{
|
||||||
|
return playlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlaylist(Playlist playlist)
|
||||||
|
{
|
||||||
|
this.playlist = playlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mode getMode()
|
||||||
|
{
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMode(Mode mode)
|
||||||
|
{
|
||||||
|
this.mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean restartSong()
|
||||||
|
{
|
||||||
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Mode
|
||||||
|
{
|
||||||
|
JUKEBOX,
|
||||||
|
PLAYLIST;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,8 +8,6 @@ package moe.nekojimi.chords;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.AudioFormat;
|
||||||
import javax.sound.sampled.AudioInputStream;
|
import javax.sound.sampled.AudioInputStream;
|
||||||
import javax.sound.sampled.AudioSystem;
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
@ -47,7 +45,7 @@ public class TrackPlayer implements Closeable
|
||||||
fillBuffer(false);
|
fillBuffer(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean has(int byteCount)
|
public boolean has(int byteCount)
|
||||||
{
|
{
|
||||||
// return true;
|
// return true;
|
||||||
return audioBuffer.getCurrentNumberOfBytes() >= byteCount;
|
return audioBuffer.getCurrentNumberOfBytes() >= byteCount;
|
||||||
|
@ -132,7 +130,6 @@ public class TrackPlayer implements Closeable
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException
|
public void close() throws IOException
|
||||||
{
|
{
|
||||||
input.close();
|
input.close();
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class QueueCommand extends Command
|
||||||
else
|
else
|
||||||
message += ":mute: **Not playing anything right now.**\n";
|
message += ":mute: **Not playing anything right now.**\n";
|
||||||
|
|
||||||
final Queue<Song> songQueue = bot.getMusicHandler().getSongQueue();
|
final Queue<Song> songQueue = bot.getQueueManager().getJukeboxQueue();
|
||||||
if (!songQueue.isEmpty())
|
if (!songQueue.isEmpty())
|
||||||
{
|
{
|
||||||
message += "__Ready to play:__\n";
|
message += "__Ready to play:__\n";
|
||||||
|
|
|
@ -34,8 +34,8 @@ public class RemoveCommand extends Command
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int i = Integer.parseInt(arg.get(0));
|
int i = Integer.parseInt(arg.get(0));
|
||||||
boolean removed = bot.getMusicHandler().removeSong(i - 1);
|
boolean removed = bot.getQueueManager().removeSong(i - 1);
|
||||||
final int size = bot.getMusicHandler().getSongQueue().size();
|
final int size = bot.getQueueManager().getJukeboxQueue().size();
|
||||||
if (removed)
|
if (removed)
|
||||||
event.getChannel().sendMessage("Song removed.").queue();
|
event.getChannel().sendMessage("Song removed.").queue();
|
||||||
else if (size > 1)
|
else if (size > 1)
|
||||||
|
|
|
@ -32,7 +32,7 @@ public class RestartCommand extends Command
|
||||||
public void call(GuildMessageReceivedEvent event, List<String> arg)
|
public void call(GuildMessageReceivedEvent event, List<String> arg)
|
||||||
{
|
{
|
||||||
// TODO: this needs to clear the current data queue
|
// TODO: this needs to clear the current data queue
|
||||||
boolean ok = bot.getMusicHandler().restartSong();
|
boolean ok = bot.getQueueManager().restartSong();
|
||||||
if (ok)
|
if (ok)
|
||||||
event.getChannel().sendMessage("Restarted current song!").queue();
|
event.getChannel().sendMessage("Restarted current song!").queue();
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue