Moved MusicHandler to it's own source file.

crossfading
Jim 3 years ago
parent 358b4e11c8
commit b8b7a99a8d
  1. 139
      src/main/java/moe/nekojimi/chords/Main.java
  2. 140
      src/main/java/moe/nekojimi/chords/MusicHandler.java

@ -22,9 +22,7 @@ import javax.security.auth.login.LoginException;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.audio.AudioReceiveHandler;
import net.dv8tion.jda.api.audio.AudioSendHandler; import net.dv8tion.jda.api.audio.AudioSendHandler;
import net.dv8tion.jda.api.audio.CombinedAudio;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -242,141 +240,4 @@ public class Main extends ListenerAdapter
} }
private MusicHandler musicHandler; private MusicHandler musicHandler;
public static 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()
{
// load whatever songs we have in WAV format
// File folder = new File("/home/jimj316/Music/");
// List<File> wavs = Arrays.asList(folder.listFiles((file, string) ->
// {
// return string.endsWith(".wav");
// }));
// Collections.shuffle(wavs);
// songQueue.addAll(wavs);
// nextSong();
}
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();
}
}
} }

@ -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…
Cancel
Save