|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. MangoRage |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 20 | + * OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package org.mangorage.mangobotplugin.commands.music; |
| 24 | + |
| 25 | +import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; |
| 26 | +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; |
| 27 | +import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayer; |
| 28 | +import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; |
| 29 | +import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter; |
| 30 | +import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; |
| 31 | +import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; |
| 32 | +import com.sedmelluq.discord.lavaplayer.track.AudioReference; |
| 33 | +import com.sedmelluq.discord.lavaplayer.track.AudioTrack; |
| 34 | +import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason; |
| 35 | +import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame; |
| 36 | +import net.dv8tion.jda.api.audio.AudioSendHandler; |
| 37 | +import org.mangorage.commonutils.log.LogHelper; |
| 38 | + |
| 39 | +import java.nio.ByteBuffer; |
| 40 | +import java.util.ArrayDeque; |
| 41 | +import java.util.Deque; |
| 42 | +import java.util.HashMap; |
| 43 | +import java.util.function.Consumer; |
| 44 | + |
| 45 | +public class MusicPlayer extends AudioEventAdapter implements AudioSendHandler { |
| 46 | + private static final HashMap<String, MusicPlayer> MUSIC_PLAYERS = new HashMap<>(); |
| 47 | + |
| 48 | + public static MusicPlayer getInstance(String guildID) { |
| 49 | + if (!MUSIC_PLAYERS.containsKey(guildID)) { |
| 50 | + MusicPlayer player = new MusicPlayer(guildID); |
| 51 | + MUSIC_PLAYERS.put(guildID, player); |
| 52 | + return player; |
| 53 | + } |
| 54 | + return MUSIC_PLAYERS.get(guildID); |
| 55 | + } |
| 56 | + |
| 57 | + private final DefaultAudioPlayerManager manager; |
| 58 | + private final DefaultAudioPlayer audioPlayer; |
| 59 | + private final Deque<AudioTrack> TRACKS_QUEUE = new ArrayDeque<>(); |
| 60 | + private final String guildID; |
| 61 | + private AudioStatus status = AudioStatus.STOPPED; |
| 62 | + private AudioFrame lastFrame; |
| 63 | + |
| 64 | + |
| 65 | + private MusicPlayer(String guildID) { |
| 66 | + this.guildID = guildID; |
| 67 | + this.manager = new DefaultAudioPlayerManager(); |
| 68 | + this.audioPlayer = new DefaultAudioPlayer(manager); |
| 69 | + |
| 70 | + MusicUtil.registerRemoteSources(manager); |
| 71 | + |
| 72 | + audioPlayer.addListener(this); |
| 73 | + } |
| 74 | + |
| 75 | + public AudioTrack getPlaying() { |
| 76 | + return audioPlayer.getPlayingTrack(); |
| 77 | + } |
| 78 | + |
| 79 | + public void setVolume(int volume) { |
| 80 | + audioPlayer.setVolume(volume); |
| 81 | + } |
| 82 | + |
| 83 | + public boolean isPlaying() { |
| 84 | + if (audioPlayer.getPlayingTrack() == null) |
| 85 | + return false; |
| 86 | + return audioPlayer.getPlayingTrack() != null; |
| 87 | + } |
| 88 | + |
| 89 | + public boolean isQueueEmpty() { |
| 90 | + return TRACKS_QUEUE.isEmpty(); |
| 91 | + } |
| 92 | + |
| 93 | + public void load(String URL, Consumer<AudioTrackEvent> eventConsumer) { |
| 94 | + |
| 95 | + manager.loadItem(new AudioReference(URL.trim(), null), new AudioLoadResultHandler() { |
| 96 | + @Override |
| 97 | + public void trackLoaded(AudioTrack track) { |
| 98 | + eventConsumer.accept(new AudioTrackEvent(track, AudioTrackEvent.Info.SUCCESS)); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void playlistLoaded(AudioPlaylist playlist) { |
| 103 | + // Allow playlists maybe? |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void noMatches() { |
| 108 | + eventConsumer.accept(new AudioTrackEvent(null, AudioTrackEvent.Info.NO_MATCHES)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void loadFailed(FriendlyException exception) { |
| 113 | + eventConsumer.accept(new AudioTrackEvent(null, AudioTrackEvent.Info.FAILED)); |
| 114 | + LogHelper.info(exception.getMessage()); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + public AudioStatus getStatus() { |
| 120 | + return this.status; |
| 121 | + } |
| 122 | + |
| 123 | + public void play() { |
| 124 | + AudioTrack track = TRACKS_QUEUE.poll(); |
| 125 | + if (track != null) |
| 126 | + audioPlayer.playTrack(track); |
| 127 | + } |
| 128 | + |
| 129 | + public void playNext() { |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + public void add(AudioTrack track) { |
| 134 | + TRACKS_QUEUE.add(track); |
| 135 | + } |
| 136 | + |
| 137 | + public void pause() { |
| 138 | + audioPlayer.setPaused(true); |
| 139 | + } |
| 140 | + |
| 141 | + public void resume() { |
| 142 | + audioPlayer.setPaused(false); |
| 143 | + } |
| 144 | + |
| 145 | + public void stop() { |
| 146 | + audioPlayer.stopTrack(); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + public void onPlayerPause(AudioPlayer player) { |
| 151 | + this.status = AudioStatus.PAUSED; |
| 152 | + } |
| 153 | + |
| 154 | + public void onPlayerResume(AudioPlayer player) { |
| 155 | + this.status = AudioStatus.PLAYING; |
| 156 | + } |
| 157 | + |
| 158 | + public void onTrackStart(AudioPlayer player, AudioTrack track) { |
| 159 | + this.status = AudioStatus.PLAYING; |
| 160 | + } |
| 161 | + |
| 162 | + public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) { |
| 163 | + this.status = AudioStatus.STOPPED; |
| 164 | + if (endReason.mayStartNext) |
| 165 | + playNext(); |
| 166 | + } |
| 167 | + |
| 168 | + public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) { |
| 169 | + LogHelper.info(exception.getMessage()); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + @Override |
| 174 | + public boolean canProvide() { |
| 175 | + lastFrame = audioPlayer.provide(); |
| 176 | + return lastFrame != null; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public ByteBuffer provide20MsAudio() { |
| 181 | + return ByteBuffer.wrap(lastFrame.getData()); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean isOpus() { |
| 186 | + return true; |
| 187 | + } |
| 188 | +} |
0 commit comments