You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
7.4 KiB
248 lines
7.4 KiB
/*
|
|
* 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.*;
|
|
import java.nio.ByteBuffer;
|
|
import java.util.function.Consumer;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import javax.sound.sampled.*;
|
|
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
|
import org.apache.commons.io.input.buffer.CircularByteBuffer;
|
|
|
|
/**
|
|
*
|
|
* @author jimj316
|
|
*/
|
|
public class MusicHandler implements AudioSendHandler, Closeable
|
|
{
|
|
|
|
private QueueManager queueManager;
|
|
// private final LinkedList<Song> songQueue = new LinkedList<>();
|
|
// private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
|
|
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
|
|
private boolean shouldPlay = true;
|
|
private int byteCount;
|
|
|
|
private boolean arrayErr = false;
|
|
private Consumer<Song> nowPlayingConsumer;
|
|
|
|
public void setNowPlayingConsumer(Consumer<Song> nowPlayingConsumer)
|
|
{
|
|
this.nowPlayingConsumer = nowPlayingConsumer;
|
|
}
|
|
|
|
private Song currentSong;
|
|
private TrackPlayer player;
|
|
|
|
private File debugOutFile;
|
|
private BufferedOutputStream debugOut;
|
|
|
|
public MusicHandler()
|
|
{
|
|
try
|
|
{
|
|
debugOutFile = new File("debug.wav");
|
|
if (debugOutFile.exists())
|
|
debugOutFile.delete();
|
|
debugOutFile.createNewFile();
|
|
debugOut = new BufferedOutputStream(new FileOutputStream(debugOutFile));
|
|
} catch (IOException ex)
|
|
{
|
|
Logger.getLogger(MusicHandler.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
}
|
|
|
|
void setQueueManager(QueueManager manager)
|
|
{
|
|
queueManager = manager;
|
|
}
|
|
|
|
public void playNext()
|
|
{
|
|
nextSong(true);
|
|
}
|
|
|
|
// public boolean restartSong()
|
|
// {
|
|
//// songQueue.addFirst(currentSong);
|
|
// currentSong = null;
|
|
// return nextSong(true);
|
|
// }
|
|
|
|
private boolean nextSong()
|
|
{
|
|
return nextSong(false);
|
|
}
|
|
|
|
public boolean nextSong(boolean immediate)
|
|
{
|
|
if (immediate)
|
|
{
|
|
System.out.println("Immediate next - clearing buffer");
|
|
audioBuffer.clear();
|
|
}
|
|
|
|
try
|
|
{
|
|
if (currentSong != null)
|
|
{
|
|
if (!currentSong.isKept())
|
|
currentSong.delete();
|
|
currentSong = null;
|
|
}
|
|
currentSong = queueManager.nextSongNeeded();
|
|
if (nowPlayingConsumer != null)
|
|
nowPlayingConsumer.accept(currentSong);
|
|
if (currentSong == null)
|
|
{
|
|
System.out.println("End of queue.");
|
|
debugOut.flush();
|
|
return false;
|
|
}
|
|
System.out.println("Playing song " + currentSong.getLocation().getAbsolutePath());
|
|
arrayErr = false;
|
|
byteCount = 3840;
|
|
player = new TrackPlayer(currentSong);
|
|
// System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes());
|
|
return true;
|
|
} catch (UnsupportedAudioFileException | IOException ex)
|
|
{
|
|
Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
|
|
} finally
|
|
{
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean isPlaying()
|
|
{
|
|
return player != null;
|
|
}
|
|
|
|
public boolean isShouldPlay()
|
|
{
|
|
return shouldPlay;
|
|
}
|
|
|
|
public void setShouldPlay(boolean shouldPlay)
|
|
{
|
|
if (!this.shouldPlay && shouldPlay)
|
|
nextSong();
|
|
this.shouldPlay = shouldPlay;
|
|
}
|
|
|
|
@Override
|
|
public boolean canProvide()
|
|
{
|
|
return player != null && player.has(1);
|
|
// If we have something in our buffer we can provide it to the send system
|
|
// return audioBuffer.getCurrentNumberOfBytes() > byteCount && shouldPlay;
|
|
}
|
|
|
|
@Override
|
|
public ByteBuffer provide20MsAudio()
|
|
{
|
|
ByteBuffer ret = ByteBuffer.allocate(byteCount);
|
|
while (ret.position() < byteCount && player != null)
|
|
{
|
|
// System.out.println("Position: " + ret.position() + " Remaining: " + ret.remaining());
|
|
try
|
|
{
|
|
ByteBuffer read = player.read(ret.remaining());
|
|
// System.out.println("SAMPLES from player: " + Util.printSamples(read));
|
|
// System.out.println("Wanted: " + byteCount + " Space:" + space + " Available: " + din.available() + " To read: " + bytesToRead + " Read: " + read);
|
|
|
|
// System.out.println("Read: " + read.remaining());
|
|
ret.put(read);
|
|
} catch (TrackPlayer.OutOfInputException | IOException ex)
|
|
{
|
|
// System.out.println("Track ended, starting next.");
|
|
boolean foundNext = nextSong();
|
|
|
|
if (!foundNext)
|
|
{
|
|
// System.out.println("Out of tracks!");
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
// System.out.println("Buffer filled, submitting.");
|
|
ret.rewind(); // required apparently, if returned buf has pos > 0 you get silence
|
|
assert ret.hasArray(); // output MUST be array backed
|
|
return ret;
|
|
|
|
}
|
|
|
|
// private void fillBuffer(boolean canSkip)
|
|
// {
|
|
// // use what we have in our buffer to send audio as PCM
|
|
// while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
|
|
// if (!readData())
|
|
// if (!canSkip || !nextSong())
|
|
// break;
|
|
// }
|
|
//
|
|
// private boolean readData()
|
|
// {
|
|
// if (din == null)
|
|
// return false;
|
|
// try
|
|
// {
|
|
// // if (din.available() == 0)
|
|
// // return false;
|
|
// 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);
|
|
//
|
|
// audioBuffer.add(bytes, 0, read);
|
|
// } catch (IOException ex)
|
|
// {
|
|
// Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
|
|
// return false;
|
|
// } catch (ArrayIndexOutOfBoundsException ex)
|
|
// {
|
|
// if (!arrayErr)
|
|
// arrayErr = true;
|
|
// else
|
|
// {
|
|
// Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
|
|
// return false;
|
|
// }
|
|
// }
|
|
// return true;
|
|
// }
|
|
public Song getCurrentSong()
|
|
{
|
|
return currentSong;
|
|
}
|
|
|
|
@Override
|
|
public boolean isOpus()
|
|
{
|
|
return false; //To change body of generated methods, choose Tools | Templates.
|
|
}
|
|
|
|
@Override
|
|
public void close() throws IOException
|
|
{
|
|
}
|
|
|
|
}
|
|
|