From 2c5381b7d9c4edddf1c705b3add8d01b3414fc09 Mon Sep 17 00:00:00 2001 From: Navoei Date: Tue, 19 Jul 2022 21:25:58 -0500 Subject: [PATCH] Begin work on mp3 support Add mp3spi as a dependency. --- build.gradle | 4 ++++ gradle.properties | 2 ++ .../customdiscsplugin/command/CustomDisc.java | 4 ++-- .../customdiscsplugin/event/JukeBox.java | 19 +++++++++++++------ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 7be32a9..d103771 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,10 @@ processResources { dependencies { + implementation("com.googlecode.soundlibs:mp3spi:${mp3spi_version}") { + exclude group: 'junit', module: 'junit' + } + implementation 'com.google.code.findbugs:jsr305:3.0.2' // To use this dependency, you need to compile bukkit by yourself diff --git a/gradle.properties b/gradle.properties index 7264ae1..1f2ac67 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,6 +2,8 @@ org.gradle.jvmargs=-Xmx2G java_version=17 +mp3spi_version=1.9.5.4 + bukkit_api_version=1.19 bukkit_version=1.19-R0.1-SNAPSHOT diff --git a/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java b/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java index ff79ebd..ea085a0 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java +++ b/src/main/java/me/Navoei/customdiscsplugin/command/CustomDisc.java @@ -46,10 +46,10 @@ public class CustomDisc implements CommandExecutor { File getDirectory = new File(CustomDiscs.getInstance().getDataFolder(), "musicdata"); File songFile = new File(getDirectory.getPath(), filename); if (songFile.exists()) { - if (getFileExtension(filename).equals("wav")) { + if (getFileExtension(filename).equals("wav") || getFileExtension(filename).equals("mp3")) { songname = args[0]; } else { - p.sendMessage(ChatColor.RED + "File is not in wav format!"); + p.sendMessage(ChatColor.RED + "File is not in wav or mp3 format!"); return true; } } else { diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java index 964ca24..4427367 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java +++ b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java @@ -7,7 +7,6 @@ import me.Navoei.customdiscsplugin.VoicePlugin; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.*; import org.bukkit.block.Block; @@ -39,8 +38,7 @@ import java.util.concurrent.ConcurrentHashMap; public class JukeBox implements Listener { private final Map playerMap = new ConcurrentHashMap<>(); - public static AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000.0F, 16, 1, 2, 48000.0F, false); - + public static AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 1, 2, 48000F, false); @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInsert(PlayerInteractEvent event) throws IOException { @@ -98,6 +96,7 @@ public class JukeBox implements Listener { } catch (Exception e) { player.sendMessage(ChatColor.RED + "An error occurred while trying to play the music!"); + e.printStackTrace(); } } else { player.sendMessage(ChatColor.RED + "Sound file not found."); @@ -217,9 +216,17 @@ public class JukeBox implements Listener { } public static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException { - AudioInputStream inputStream = AudioSystem.getAudioInputStream(file.toFile()); - AudioInputStream convertedInputStream = AudioSystem.getAudioInputStream(FORMAT, inputStream); - return VoicePlugin.voicechatApi.getAudioConverter().bytesToShorts(convertedInputStream.readAllBytes()); + return VoicePlugin.voicechatApi.getAudioConverter().bytesToShorts(convertFormat(file, FORMAT)); + } + + public static byte[] convertFormat(Path file, AudioFormat audioFormat) throws UnsupportedAudioFileException, IOException { + try (AudioInputStream source = AudioSystem.getAudioInputStream(file.toFile())) { + AudioFormat sourceFormat = source.getFormat(); + AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false); + AudioInputStream stream1 = AudioSystem.getAudioInputStream(convertFormat, source); + AudioInputStream stream2 = AudioSystem.getAudioInputStream(audioFormat, stream1); + return stream2.readAllBytes(); + } } public boolean isCustomMusicDisc(PlayerInteractEvent e) {