Compare commits
No commits in common. "d7c48b38fa1aad6c97e310aedeed83d229b4477d" and "f64ed119a77cf25f3be4197e85557a2d3e3f3b71" have entirely different histories.
d7c48b38fa
...
f64ed119a7
|
@ -5,15 +5,15 @@
|
|||
*/
|
||||
package moe.nekojimi.chords;
|
||||
|
||||
import java.io.*;
|
||||
import moe.nekojimi.chords.Util;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
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.Logger;
|
||||
import javax.sound.sampled.*;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
||||
import org.apache.commons.io.input.buffer.CircularByteBuffer;
|
||||
|
||||
|
@ -35,22 +35,8 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSong(Song song)
|
||||
|
@ -108,11 +94,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
|||
}
|
||||
currentSong = songQueue.poll();
|
||||
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;
|
||||
|
@ -143,7 +125,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
|||
@Override
|
||||
public boolean canProvide()
|
||||
{
|
||||
return player != null && player.has(1);
|
||||
return player != null && player.has(byteCount);
|
||||
// If we have something in our buffer we can provide it to the send system
|
||||
// return audioBuffer.getCurrentNumberOfBytes() > byteCount && playing;
|
||||
}
|
||||
|
@ -154,32 +136,25 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Song>
|
|||
ByteBuffer ret = ByteBuffer.allocate(byteCount);
|
||||
while (ret.position() < byteCount && player != null)
|
||||
{
|
||||
// System.out.println("Position: " + ret.position() + " Remaining: " + ret.remaining());
|
||||
try
|
||||
{
|
||||
System.out.println("Position: " + ret.position() + " Remaining: " + ret.remaining());
|
||||
ByteBuffer read = player.read(ret.remaining());
|
||||
// System.out.println("SAMPLES from player: " + Util.printSamples(read));
|
||||
|
||||
if (read != null)
|
||||
{
|
||||
System.out.println("Read: " + read.remaining());
|
||||
ret.put(read);
|
||||
} catch (TrackPlayer.OutOfInputException | IOException ex)
|
||||
} else if (!nextSong())
|
||||
{
|
||||
System.out.println("Track ended, starting next.");
|
||||
boolean foundNext = nextSong();
|
||||
|
||||
if (!foundNext)
|
||||
{
|
||||
System.out.println("Out of tracks!");
|
||||
System.out.println("Out of songs!");
|
||||
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;
|
||||
|
||||
// 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)
|
||||
|
|
|
@ -25,7 +25,7 @@ public class TrackPlayer implements Closeable
|
|||
{
|
||||
|
||||
private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
|
||||
private static final int MAX_READ_FAILS = 3;
|
||||
private static final int MAX_READ_FAILS = 100;
|
||||
|
||||
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class TrackPlayer implements Closeable
|
|||
fillBuffer(false);
|
||||
}
|
||||
|
||||
public TrackPlayer(AudioInputStream input) throws IOException
|
||||
public TrackPlayer(AudioInputStream input)
|
||||
{
|
||||
this.input = input;
|
||||
fillBuffer(false);
|
||||
|
@ -49,33 +49,22 @@ public class TrackPlayer implements Closeable
|
|||
|
||||
boolean has(int byteCount)
|
||||
{
|
||||
// return true;
|
||||
return audioBuffer.getCurrentNumberOfBytes() >= byteCount;
|
||||
return audioBuffer.getCurrentNumberOfBytes() > byteCount;
|
||||
}
|
||||
|
||||
public ByteBuffer read(int length) throws IOException
|
||||
public ByteBuffer read(int length)
|
||||
{
|
||||
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)
|
||||
if (!filled)
|
||||
throw new OutOfInputException();
|
||||
|
||||
byte[] data = new byte[toRead];
|
||||
byte[] data = new byte[length];
|
||||
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
|
||||
private boolean fillBuffer(boolean canSkip)
|
||||
{
|
||||
int fails = 0;
|
||||
// use what we have in our buffer to send audio as PCM
|
||||
|
@ -92,11 +81,7 @@ public class TrackPlayer implements Closeable
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if any data was read; false otherwise
|
||||
*/
|
||||
private boolean readData() throws IOException
|
||||
private boolean readData()
|
||||
{
|
||||
if (input == null)
|
||||
return false;
|
||||
|
@ -121,13 +106,19 @@ public class TrackPlayer implements Closeable
|
|||
// queue.add(bytes);
|
||||
|
||||
audioBuffer.add(bytes, 0, read);
|
||||
// System.out.println("SAMPLES player buff: " + Util.printSamples(audioBuffer));
|
||||
} catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
} catch (ArrayIndexOutOfBoundsException ex)
|
||||
{
|
||||
if (!arrayErr)
|
||||
arrayErr = true;
|
||||
else
|
||||
throw ex;
|
||||
{
|
||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
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