Compare commits

...

2 Commits

Author SHA1 Message Date
Nekojimi ed610ebdae Reformat code. 2021-09-24 18:30:00 +01:00
Jim b8b7a99a8d Moved MusicHandler to it's own source file. 2021-09-24 18:26:46 +01:00
2 changed files with 212 additions and 219 deletions

View File

@ -22,9 +22,7 @@ import javax.security.auth.login.LoginException;
import javax.sound.sampled.*;
import net.dv8tion.jda.api.JDA;
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.CombinedAudio;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -85,11 +83,8 @@ public class Main extends ListenerAdapter
{
String arg = content.substring("!join ".length());
onJoinCommand(event, guild, arg);
}
else if (content.equals("!join"))
{
} else if (content.equals("!join"))
onJoinCommand(event);
}
else if (content.startsWith("!play "))
{
String arg = content.substring("!join ".length());
@ -113,12 +108,9 @@ public class Main extends ListenerAdapter
{
connectTo(channel); // Join the channel of the user
onConnecting(channel, event.getChannel()); // Tell the user about our success
}
else
{
} else
onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
}
}
/**
* Handle command with arguments.
@ -135,9 +127,8 @@ public class Main extends ListenerAdapter
boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
VoiceChannel channel = null;
if (isNumber) // The input is an id?
{
channel = guild.getVoiceChannelById(arg);
}
if (channel == null) // Then the input must be a name?
{
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true);
@ -189,7 +180,6 @@ public class Main extends ListenerAdapter
return destination;
}
/**
* Inform user about successful connection.
*
@ -207,7 +197,8 @@ public class Main extends ListenerAdapter
* The channel to connect to is not known to us.
*
* @param channel
* The message channel (text channel abstraction) to send failure information to
* The message channel (text channel abstraction) to send failure
* information to
* @param comment
* The information of this channel
*/
@ -232,7 +223,6 @@ public class Main extends ListenerAdapter
// EchoHandler handler = new EchoHandler();
// The order of the following instructions does not matter!
// Set the sending handler to our echo system
audioManager.setSendingHandler(musicHandler);
// Set the receiving handler to the same echo system, otherwise we can't echo anything
@ -242,141 +232,4 @@ public class Main extends ListenerAdapter
}
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();
}
}
}

View File

@ -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();
}
}