parent
358b4e11c8
commit
b8b7a99a8d
@ -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<File> songQueue = new ConcurrentLinkedQueue<>(); |
||||
private File currentSong; |
||||
private AudioInputStream din = null; |
||||
private final Queue<byte[]> 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(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue