diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicCore.java b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicCore.java index 3c94e5f9..523bcb7d 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicCore.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicCore.java @@ -1,7 +1,6 @@ package com.coloryr.allmusic.client.core; import com.coloryr.allmusic.client.core.objs.ConfigObj; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; import com.coloryr.allmusic.codec.CommandType; import com.coloryr.allmusic.codec.HudPosObj; import com.coloryr.allmusic.codec.MusicPack; @@ -26,6 +25,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; /** * AllMusic核心 @@ -52,6 +53,8 @@ public class AllMusicCore { */ private static AllMusicHud hud; + public static final ScheduledExecutorService service = Executors.newScheduledThreadPool(4); + /** * 更新音频缓存 */ @@ -135,6 +138,8 @@ public class AllMusicCore { } public static void stop() { + service.close(); + if (player != null) { player.stop(); } diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicHud.java b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicHud.java index 40d83237..f8fb9dad 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicHud.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicHud.java @@ -82,8 +82,6 @@ public class AllMusicHud { private BufferedImage bg2; private boolean isRun; - private final ScheduledExecutorService service1; - /** * 是否有图片 */ @@ -114,10 +112,9 @@ public class AllMusicHud { thread.setName("allmusic_pic"); thread.start(); - service1 = Executors.newScheduledThreadPool(3); - service1.scheduleAtFixedRate(this::picRotateTick, 0, 1, TimeUnit.MILLISECONDS); - service1.scheduleAtFixedRate(this::lyricTick, 0, 10, TimeUnit.MILLISECONDS); - service1.scheduleAtFixedRate(this::loopTick, 0, 100, TimeUnit.MILLISECONDS); + AllMusicCore.service.scheduleAtFixedRate(this::picRotateTick, 0, 1, TimeUnit.MILLISECONDS); + AllMusicCore.service.scheduleAtFixedRate(this::lyricTick, 0, 10, TimeUnit.MILLISECONDS); + AllMusicCore.service.scheduleAtFixedRate(this::loopTick, 0, 100, TimeUnit.MILLISECONDS); picRender = AllMusicCore.bridge.makePictureRender(size); @@ -149,7 +146,6 @@ public class AllMusicHud { public void stop() { isRun = false; - service1.close(); semaphore.release(); } diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/AllMusicPlayer.java b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicPlayer.java similarity index 75% rename from client/src/main/java/com/coloryr/allmusic/client/core/player/AllMusicPlayer.java rename to client/src/main/java/com/coloryr/allmusic/client/core/AllMusicPlayer.java index 73257eef..47e5b07b 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/AllMusicPlayer.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/AllMusicPlayer.java @@ -1,6 +1,5 @@ -package com.coloryr.allmusic.client.core.player; +package com.coloryr.allmusic.client.core; -import com.coloryr.allmusic.client.core.AllMusicCore; import com.coloryr.allmusic.client.core.objs.PlayTaskObj; import com.coloryr.allmusic.client.core.player.decoder.BuffPack; import com.coloryr.allmusic.client.core.player.decoder.IDecoder; @@ -41,21 +40,16 @@ public class AllMusicPlayer extends InputStream { private boolean isPlay = false; private boolean wait = false; private int index = -1; - private IntBuffer source; + private final IntBuffer source; private long local; private boolean isRun; private boolean isChat; - private ScheduledExecutorService scheduler; + private int chatCount = 0; public AllMusicPlayer(IntBuffer source) { - try { - this.source = source; - new Thread(this::run, "allmusic_run").start(); - scheduler = Executors.newSingleThreadScheduledExecutor(); - scheduler.scheduleAtFixedRate(this::timerTick, 0, 10, TimeUnit.MILLISECONDS); - } catch (Exception e) { - e.printStackTrace(); - } + this.source = source; + new Thread(this::run, "allmusic_run").start(); + AllMusicCore.service.scheduleAtFixedRate(this::timerTick, 0, 10, TimeUnit.MILLISECONDS); } public void stop() { @@ -63,7 +57,6 @@ public class AllMusicPlayer extends InputStream { isClose = true; semaphore.release(); semaphoreReload.release(); - scheduler.close(); } public void setChat() { @@ -84,12 +77,9 @@ public class AllMusicPlayer extends InputStream { if (nowTask == null) { return; } - String url = nowTask.url; - closePlayer(); - PlayTaskObj task = new PlayTaskObj(); - task.url = url; - task.time = time; - tasks.push(task); + isClose = true; + nowTask.time = time; + tasks.push(nowTask); semaphore.release(); } @@ -110,23 +100,49 @@ public class AllMusicPlayer extends InputStream { } private void resetSource() { - if (index != -1) { - AL10.alSourceStop(index); - AL10.alSourcei(index, AL10.AL_BUFFER, AL10.AL_NONE); + AL10.alSourceStop(index); + AL10.alSourcei(index, AL10.AL_BUFFER, AL10.AL_NONE); - int queued; - do { - queued = AL10.alGetSourcei(index, AL10.AL_BUFFERS_QUEUED); - if (queued > 0) { - int buffer = AL10.alSourceUnqueueBuffers(index); - if (buffer != 0) { - AL10.alDeleteBuffers(buffer); - } + int queued; + do { + queued = AL10.alGetSourcei(index, AL10.AL_BUFFERS_QUEUED); + if (queued > 0) { + int buffer = AL10.alSourceUnqueueBuffers(index); + if (buffer != 0) { + AL10.alDeleteBuffers(buffer); } - } while (queued > 0); + } + } while (queued > 0); + } - AL10.alSourcef(index, AL10.AL_GAIN, AllMusicCore.bridge.getVolume()); - AL10.alSourcef(index, AL10.AL_PITCH, 1.0f); + private void checkChat() { + if (isChat) { + chatCount++; + if (chatCount >= 200) { + isChat = false; + chatCount = 0; + } + } + } + + private void dequeue() { + int processed = AL10.alGetSourcei(index, AL10.AL_BUFFERS_PROCESSED); + for (int i = 0; i < processed; i++) { + int buffer = AL10.alSourceUnqueueBuffers(index); + if (buffer != 0) { + AL10.alDeleteBuffers(buffer); + } + } + } + + private void checkVolume() { + float temp = AllMusicCore.bridge.getVolume(); + float now = AL10.alGetSourcef(index, AL10.AL_GAIN); + if (isChat) { + temp *= 0.2F; + } + if (now != temp) { + AL10.alSourcef(index, AL10.AL_GAIN, temp); } } @@ -156,10 +172,11 @@ public class AllMusicPlayer extends InputStream { } } + dequeue(); resetSource(); - PlayTaskObj task = tasks.pop(); - if (task == null || task.url == null || task.url.isEmpty()) continue; + nowTask = tasks.pop(); + if (nowTask == null || nowTask.url == null || nowTask.url.isEmpty()) continue; tasks.clear(); try { local = 0; @@ -194,25 +211,27 @@ public class AllMusicPlayer extends InputStream { int frequency = decoder.getOutputFrequency(); int channels = decoder.getOutputChannels(); if (channels != 1 && channels != 2) continue; - if (task.time != 0) { - decoder.set(task.time); + if (nowTask.time != 0) { + decoder.set(nowTask.time); } reload = false; isClose = false; - int chatCount = 0; while (true) { if (!isRun) { return; } + if (isClose) { + break; + } try { - if (isClose) break; - while (AL10.alGetSourcei(index, AL10.AL_BUFFERS_QUEUED) < AllMusicCore.config.queueSize) { if (!isRun) { return; } - if (isClose) break; + if (isClose) { + break; + } BuffPack output = decoder.decodeFrame(); if (output == null) break; ByteBuffer byteBuffer = BufferUtils.createByteBuffer(output.len) @@ -238,32 +257,11 @@ public class AllMusicPlayer extends InputStream { } } - float temp = AllMusicCore.bridge.getVolume(); - float now = AL10.alGetSourcef(index, AL10.AL_GAIN); - if (isChat) { - temp *= 0.2F; - } - if (now != temp) { - AL10.alSourcef(index, AL10.AL_GAIN, temp); - } - - int processed = AL10.alGetSourcei(index, AL10.AL_BUFFERS_PROCESSED); - for (int i = 0; i < processed; i++) { - int buffer = AL10.alSourceUnqueueBuffers(index); - if (buffer != 0) { - AL10.alDeleteBuffers(buffer); - } - } - Thread.sleep(5); - if (isChat) { - chatCount++; - if (chatCount >= 200) { - isChat = false; - chatCount = 0; - } - } + checkChat(); + checkVolume(); + dequeue(); } catch (Exception e) { if (!isClose) { e.printStackTrace(); @@ -277,6 +275,9 @@ public class AllMusicPlayer extends InputStream { while (!isClose && AL10.alGetSourcei(index, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING) { Thread.sleep(50); + checkChat(); + checkVolume(); + dequeue(); } if (!reload) { @@ -286,26 +287,18 @@ public class AllMusicPlayer extends InputStream { break; } if (reload) { - tasks.push(task); + tasks.push(nowTask); semaphore.release(); continue; } } isPlay = false; - AL10.alSourceStop(index); - AL10.alSourcei(index, AL10.AL_BUFFER, AL10.AL_NONE); - int queued = AL10.alGetSourcei(index, AL10.AL_BUFFERS_QUEUED); - while (queued > 0) { - int buffer = AL10.alSourceUnqueueBuffers(index); - if (buffer != 0) { - AL10.alDeleteBuffers(buffer); - } - queued--; - } + dequeue(); + resetSource(); } else { - tasks.push(task); index = -1; + tasks.push(nowTask); semaphore.release(); } } catch (Exception e) { @@ -353,6 +346,13 @@ public class AllMusicPlayer extends InputStream { } } + public void setReload() { + if (isPlay) { + reload = true; + isClose = true; + } + } + @Override public int read() throws IOException { local++; @@ -406,11 +406,4 @@ public class AllMusicPlayer extends InputStream { this.local = local; connect(); } - - public void setReload() { - if (isPlay) { - reload = true; - isClose = true; - } - } } \ No newline at end of file diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/FlacDecoder.java b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/FlacDecoder.java index 9b1eaf62..d258a3c1 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/FlacDecoder.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/FlacDecoder.java @@ -18,7 +18,7 @@ package com.coloryr.allmusic.client.core.player.decoder.flac; import com.coloryr.allmusic.client.core.AllMusicCore; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; +import com.coloryr.allmusic.client.core.AllMusicPlayer; import com.coloryr.allmusic.client.core.player.decoder.BuffPack; import com.coloryr.allmusic.client.core.player.decoder.IDecoder; diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/SeekableFileFlacInput.java b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/SeekableFileFlacInput.java index c2b57d0d..fde53259 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/SeekableFileFlacInput.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/flac/SeekableFileFlacInput.java @@ -17,7 +17,7 @@ package com.coloryr.allmusic.client.core.player.decoder.flac; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; +import com.coloryr.allmusic.client.core.AllMusicPlayer; import java.io.BufferedInputStream; import java.io.IOException; diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Bitstream.java b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Bitstream.java index 7dd4dce1..fa2c0013 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Bitstream.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Bitstream.java @@ -27,7 +27,7 @@ package com.coloryr.allmusic.client.core.player.decoder.mp3; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; +import com.coloryr.allmusic.client.core.AllMusicPlayer; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Mp3Decoder.java b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Mp3Decoder.java index b0827adc..2499f783 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Mp3Decoder.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/mp3/Mp3Decoder.java @@ -18,7 +18,7 @@ package com.coloryr.allmusic.client.core.player.decoder.mp3; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; +import com.coloryr.allmusic.client.core.AllMusicPlayer; import com.coloryr.allmusic.client.core.player.decoder.BuffPack; import com.coloryr.allmusic.client.core.player.decoder.IDecoder; diff --git a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/ogg/OggDecoder.java b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/ogg/OggDecoder.java index 3b438698..e6c73202 100644 --- a/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/ogg/OggDecoder.java +++ b/client/src/main/java/com/coloryr/allmusic/client/core/player/decoder/ogg/OggDecoder.java @@ -1,7 +1,7 @@ package com.coloryr.allmusic.client.core.player.decoder.ogg; import com.coloryr.allmusic.client.core.AllMusicCore; -import com.coloryr.allmusic.client.core.player.AllMusicPlayer; +import com.coloryr.allmusic.client.core.AllMusicPlayer; import com.coloryr.allmusic.client.core.player.decoder.BuffPack; import com.coloryr.allmusic.client.core.player.decoder.IDecoder; import com.coloryr.allmusic.client.core.player.decoder.ogg.jcraft.jogg.Packet; diff --git a/settings.gradle.kts b/settings.gradle.kts index 27136c01..93d8e9f7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -55,19 +55,19 @@ include(":server:folia") include(":server:paper") include(":server:velocity") -include(":onejar") - -include(":onejar:fabric_1_16_5") -include(":onejar:fabric_1_20_1") -include(":onejar:fabric_1_21") -include(":onejar:fabric_1_21_6") -include(":onejar:fabric_1_21_11") -include(":onejar:fabric_26_1") -include(":onejar:fabric_26_2") -include(":onejar:neoforge_1_21") -include(":onejar:neoforge_1_21_6") -include(":onejar:neoforge_1_21_11") -include(":onejar:neoforge_26_1") +//include(":onejar") +// +//include(":onejar:fabric_1_16_5") +//include(":onejar:fabric_1_20_1") +//include(":onejar:fabric_1_21") +//include(":onejar:fabric_1_21_6") +//include(":onejar:fabric_1_21_11") +//include(":onejar:fabric_26_1") +//include(":onejar:fabric_26_2") +//include(":onejar:neoforge_1_21") +//include(":onejar:neoforge_1_21_6") +//include(":onejar:neoforge_1_21_11") +//include(":onejar:neoforge_26_1") pluginManagement { repositories {