commit
9a6520f426
@ -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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,144 @@ |
|||||||
|
/* |
||||||
|
* 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.IOException; |
||||||
|
import java.nio.ByteBuffer; |
||||||
|
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; |
||||||
|
import org.apache.commons.io.input.buffer.CircularByteBuffer; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @author jimj316 |
||||||
|
*/ |
||||||
|
public class TrackPlayer implements Closeable |
||||||
|
{ |
||||||
|
|
||||||
|
private static final int DESIRED_BUFFER_SIZE = 3840 * 500; |
||||||
|
private static final int MAX_READ_FAILS = 3; |
||||||
|
|
||||||
|
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024); |
||||||
|
|
||||||
|
private final AudioInputStream input; |
||||||
|
|
||||||
|
private boolean arrayErr = false; // supresses ArrayIndexOutOfBoundsException after the first time, to prevent spam
|
||||||
|
|
||||||
|
public TrackPlayer(Song song) throws UnsupportedAudioFileException, IOException |
||||||
|
{ |
||||||
|
AudioInputStream in = AudioSystem.getAudioInputStream(song.getLocation()); |
||||||
|
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT; |
||||||
|
input = AudioSystem.getAudioInputStream(decodedFormat, in); |
||||||
|
fillBuffer(false); |
||||||
|
} |
||||||
|
|
||||||
|
public TrackPlayer(AudioInputStream input) throws IOException |
||||||
|
{ |
||||||
|
this.input = input; |
||||||
|
fillBuffer(false); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean has(int byteCount) |
||||||
|
{ |
||||||
|
// return true;
|
||||||
|
return audioBuffer.getCurrentNumberOfBytes() >= byteCount; |
||||||
|
} |
||||||
|
|
||||||
|
public ByteBuffer read(int length) throws IOException |
||||||
|
{ |
||||||
|
boolean filled = fillBuffer(true); |
||||||
|
// if (!filled)
|
||||||
|
// throw new OutOfInputException();
|
||||||
|
|
||||||
|
int toRead = Math.min(length, audioBuffer.getCurrentNumberOfBytes()); |
||||||
|
System.out.println("To read: " + toRead + " from " + audioBuffer.getCurrentNumberOfBytes()); |
||||||
|
if (toRead <= 0) |
||||||
|
throw new OutOfInputException(); |
||||||
|
|
||||||
|
byte[] data = new byte[toRead]; |
||||||
|
audioBuffer.read(data, 0, data.length); |
||||||
|
// byte[] data = queue.poll();
|
||||||
|
return ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @param canSkip |
||||||
|
* @return true if the buffer is not empty; false otherwise |
||||||
|
*/ |
||||||
|
private boolean fillBuffer(boolean canSkip) throws IOException |
||||||
|
{ |
||||||
|
int fails = 0; |
||||||
|
// use what we have in our buffer to send audio as PCM
|
||||||
|
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) |
||||||
|
{ |
||||||
|
boolean read = readData(); |
||||||
|
if (!read && !canSkip) |
||||||
|
return false; |
||||||
|
else if (fails < MAX_READ_FAILS) |
||||||
|
fails++; |
||||||
|
else |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @return true if any data was read; false otherwise |
||||||
|
*/ |
||||||
|
private boolean readData() throws IOException |
||||||
|
{ |
||||||
|
if (input == null) |
||||||
|
return false; |
||||||
|
try |
||||||
|
{ |
||||||
|
// if (din.available() == 0)
|
||||||
|
// return false;
|
||||||
|
int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes(); |
||||||
|
int space = audioBuffer.getSpace(); |
||||||
|
if (input.available() > 0 && input.available() < bytesToRead) |
||||||
|
bytesToRead = input.available(); |
||||||
|
if (bytesToRead > space) |
||||||
|
bytesToRead = space; |
||||||
|
if (bytesToRead == 0) |
||||||
|
return false; |
||||||
|
byte[] bytes = new byte[bytesToRead]; |
||||||
|
// byte[] bytes = din.readNBytes(bytesToRead);
|
||||||
|
int read = input.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); |
||||||
|
// System.out.println("SAMPLES player buff: " + Util.printSamples(audioBuffer));
|
||||||
|
} catch (ArrayIndexOutOfBoundsException ex) |
||||||
|
{ |
||||||
|
if (!arrayErr) |
||||||
|
arrayErr = true; |
||||||
|
else |
||||||
|
throw ex; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public void close() throws IOException |
||||||
|
{ |
||||||
|
input.close(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static class OutOfInputException extends RuntimeException |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
package moe.nekojimi.chords; |
||||||
|
|
||||||
|
import java.nio.ByteBuffer; |
||||||
|
|
||||||
|
/* |
||||||
|
* 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/>.
|
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @author jimj316 |
||||||
|
*/ |
||||||
|
public class Util |
||||||
|
{ |
||||||
|
public static String printSamples(ByteBuffer buf) |
||||||
|
{ |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
for (int i = 0; i < buf.limit(); i += 128) |
||||||
|
{ |
||||||
|
sb.append(String.format("%x ", buf.get(i))); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue