Search in sources :

Example 1 with AudioManager

use of net.robinfriedli.aiode.audio.AudioManager in project aiode by robinfriedli.

the class AnalyticsCommand method doRun.

@Override
public void doRun() {
    ShardManager shardManager = Aiode.get().getShardManager();
    List<Guild> guilds = shardManager.getGuilds();
    Aiode aiode = Aiode.get();
    AudioManager audioManager = aiode.getAudioManager();
    GuildManager guildManager = aiode.getGuildManager();
    SpringPropertiesConfig springPropertiesConfig = aiode.getSpringPropertiesConfig();
    Session session = getContext().getSession();
    Runtime runtime = Runtime.getRuntime();
    int guildCount = guilds.size();
    long playingCount = guilds.stream().map(audioManager::getPlaybackForGuild).filter(AudioPlayback::isPlaying).count();
    long commandCount = session.createQuery("select count(*) from " + CommandHistory.class.getName(), Long.class).uniqueResult();
    long playlistCount = session.createQuery("select count(*) from " + Playlist.class.getName(), Long.class).uniqueResult();
    long trackCount = session.createQuery("select count(*) from " + Song.class.getName(), Long.class).uniqueResult() + session.createQuery("select count(*) from " + Video.class.getName(), Long.class).uniqueResult() + session.createQuery("select count(*) from " + UrlTrack.class.getName(), Long.class).uniqueResult();
    long playedCount = session.createQuery("select count(*) from " + PlaybackHistory.class.getName(), Long.class).uniqueResult();
    // convert to MB by right shifting by 20 bytes (same as dividing by 2^20)
    long maxMemory = runtime.maxMemory() >> 20;
    long allocatedMemory = runtime.totalMemory() >> 20;
    long unallocatedMemory = maxMemory - allocatedMemory;
    long allocFreeMemory = runtime.freeMemory() >> 20;
    long usedMemory = allocatedMemory - allocFreeMemory;
    long totalFreeMemory = maxMemory - usedMemory;
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    int threadCount = threadMXBean.getThreadCount();
    int daemonThreadCount = threadMXBean.getDaemonThreadCount();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.addField("Guilds", String.valueOf(guildCount), true);
    embedBuilder.addField("Guilds active", String.valueOf(guildManager.getActiveGuilds(session).size()), true);
    embedBuilder.addField("Guilds playing now", String.valueOf(playingCount), true);
    embedBuilder.addField("Total commands entered", String.valueOf(commandCount), true);
    embedBuilder.addField("Saved playlists", String.valueOf(playlistCount), true);
    embedBuilder.addField("Saved tracks", String.valueOf(trackCount), true);
    embedBuilder.addField("Total tracks played", String.valueOf(playedCount), true);
    embedBuilder.addField("Thread count", String.format("%d (%d daemons)", threadCount, daemonThreadCount), true);
    String shardRange = springPropertiesConfig.getApplicationProperty("aiode.preferences.shard_range");
    if (!Strings.isNullOrEmpty(shardRange)) {
        embedBuilder.addField("Shards", shardRange, true);
    }
    embedBuilder.addField("Memory (in MB)", "Total: " + maxMemory + System.lineSeparator() + "Allocated: " + allocatedMemory + System.lineSeparator() + "Unallocated: " + unallocatedMemory + System.lineSeparator() + "Free allocated: " + allocFreeMemory + System.lineSeparator() + "Currently used: " + usedMemory + System.lineSeparator() + "Total free: " + totalFreeMemory, false);
    sendWithLogo(embedBuilder);
}
Also used : CommandHistory(net.robinfriedli.aiode.entities.CommandHistory) ThreadMXBean(java.lang.management.ThreadMXBean) GuildManager(net.robinfriedli.aiode.discord.GuildManager) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) AudioManager(net.robinfriedli.aiode.audio.AudioManager) Playlist(net.robinfriedli.aiode.entities.Playlist) Song(net.robinfriedli.aiode.entities.Song) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) Video(net.robinfriedli.aiode.entities.Video) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) Session(org.hibernate.Session)

Example 2 with AudioManager

use of net.robinfriedli.aiode.audio.AudioManager in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadTrack.

private void loadTrack(AudioManager audioManager) throws Exception {
    int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
    Callable<List<Track>> loadTrackCallable = () -> getSpotifyService().searchTrack(getCommandInput(), argumentSet("own"), limit);
    List<Track> found;
    if (argumentSet("own")) {
        found = runWithLogin(loadTrackCallable);
    } else {
        found = runWithCredentials(loadTrackCallable);
    }
    if (found.size() == 1) {
        createPlayableForTrack(found.get(0), audioManager);
    } else if (found.isEmpty()) {
        throw new NoSpotifyResultsFoundException(String.format("No Spotify track found for '%s'", getCommandInput()));
    } else {
        if (argumentSet("select")) {
            askQuestion(found, track -> {
                String artistString = StringList.create(track.getArtists(), ArtistSimplified::getName).toSeparatedString(", ");
                return String.format("%s by %s", track.getName(), artistString);
            }, track -> track.getAlbum().getName());
        } else {
            SpotifyTrackResultHandler resultHandler = new SpotifyTrackResultHandler(getContext().getGuild(), getContext().getSession());
            createPlayableForTrack(resultHandler.getBestResult(getCommandInput(), found), audioManager);
        }
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) IOException(java.io.IOException) AudioManager(net.robinfriedli.aiode.audio.AudioManager) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) List(java.util.List) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 3 with AudioManager

use of net.robinfriedli.aiode.audio.AudioManager in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadSpotifyEpisode.

private void loadSpotifyEpisode(AudioManager audioManager) throws Exception {
    int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
    Callable<List<Episode>> loadTrackCallable = () -> getSpotifyService().searchEpisode(getCommandInput(), argumentSet("own"), limit);
    List<Episode> found;
    if (argumentSet("own")) {
        found = runWithLogin(loadTrackCallable);
    } else {
        found = runWithCredentials(loadTrackCallable);
    }
    if (found.size() == 1) {
        createPlayableForEpisode(found.get(0), audioManager);
    } else if (found.isEmpty()) {
        throw new NoSpotifyResultsFoundException(String.format("No Spotify episode found for '%s'", getCommandInput()));
    } else {
        askQuestion(found, episode -> String.format("%s by %s", episode.getName(), episode.getShow().getName()));
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) IOException(java.io.IOException) AudioManager(net.robinfriedli.aiode.audio.AudioManager) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) List(java.util.List) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 4 with AudioManager

use of net.robinfriedli.aiode.audio.AudioManager in project aiode by robinfriedli.

the class AudioTrafficSimulationCommand method runAdmin.

@Override
public void runAdmin() throws Exception {
    Aiode aiode = Aiode.get();
    AudioManager audioManager = aiode.getAudioManager();
    AudioPlayerManager playerManager = audioManager.getPlayerManager();
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    ThreadExecutionQueue threadExecutionQueue = aiode.getExecutionQueueManager().getForGuild(guild);
    SpotifyApi spotifyApi = context.getSpotifyApi();
    AudioTrackLoader audioTrackLoader = new AudioTrackLoader(playerManager);
    int streams = getArgumentValueOrElse("streams", DEFAULT_STREAM_COUNT);
    int durationSecs = getArgumentValueOrElse("duration", DEFAULT_DURATION);
    String playbackUrl = getArgumentValueOrElse("url", DEFAULT_PLAYBACK_URL);
    Integer nativeBuffer = getArgumentValueWithTypeOrElse("nativeBuffer", Integer.class, null);
    List<Playable> playables = playableFactory.createPlayables(playbackUrl, spotifyApi, true);
    if (playables.isEmpty()) {
        throw new InvalidCommandException("No playables found for url " + playbackUrl);
    }
    Playable playable = playables.get(0);
    AudioItem audioItem = audioTrackLoader.loadByIdentifier(playable.getPlaybackUrl());
    AudioTrack track;
    if (audioItem instanceof AudioTrack) {
        track = (AudioTrack) audioItem;
    } else {
        throw new IllegalStateException("Could not get AudioTrack for Playable " + playable);
    }
    LoopTrackListener loopTrackListener = new LoopTrackListener(track);
    List<Thread> playbackThreads = nativeBuffer != null ? null : Lists.newArrayListWithCapacity(streams);
    List<Tuple2<IAudioSendSystem, AudioPlayer>> audioSendSystems = nativeBuffer != null ? Lists.newArrayListWithCapacity(streams) : null;
    NativeAudioSendFactory nativeAudioSendFactory = nativeBuffer != null ? new NativeAudioSendFactory(nativeBuffer) : null;
    LoggingUncaughtExceptionHandler eh = new LoggingUncaughtExceptionHandler();
    for (int i = 0; i < streams; i++) {
        AudioPlayer player = playerManager.createPlayer();
        player.addListener(loopTrackListener);
        player.playTrack(track.makeClone());
        if (nativeAudioSendFactory != null) {
            IAudioSendSystem sendSystem = nativeAudioSendFactory.createSendSystem(new FakePacketProvider(player));
            audioSendSystems.add(new Tuple2<>(sendSystem, player));
        } else {
            Thread playbackThread = new Thread(new PlayerPollingRunnable(player));
            playbackThread.setDaemon(true);
            playbackThread.setUncaughtExceptionHandler(eh);
            playbackThread.setName("simulated-playback-thread-" + i);
            playbackThreads.add(playbackThread);
        }
    }
    QueuedTask playbackThreadsManagementTask = new QueuedTask(threadExecutionQueue, new FakePlayerManagementTask(playbackThreads, audioSendSystems, durationSecs)) {

        @Override
        protected boolean isPrivileged() {
            return true;
        }
    };
    playbackThreadsManagementTask.setName("simulated-playback-management-task");
    threadExecutionQueue.add(playbackThreadsManagementTask, false);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) NativeAudioSendFactory(com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) AudioManager(net.robinfriedli.aiode.audio.AudioManager) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) IAudioSendSystem(net.dv8tion.jda.api.audio.factory.IAudioSendSystem) Aiode(net.robinfriedli.aiode.Aiode) QueuedTask(net.robinfriedli.aiode.concurrent.QueuedTask) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Playable(net.robinfriedli.aiode.audio.Playable) Tuple2(groovy.lang.Tuple2) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 5 with AudioManager

use of net.robinfriedli.aiode.audio.AudioManager in project aiode by robinfriedli.

the class PlayCommand method doRun.

@Override
public void doRun() throws Exception {
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    VoiceChannel channel = context.getVoiceChannel();
    AudioManager audioManager = Aiode.get().getAudioManager();
    MessageChannel messageChannel = getContext().getChannel();
    AudioPlayback playbackForGuild = audioManager.getPlaybackForGuild(guild);
    playbackForGuild.setCommunicationChannel(messageChannel);
    if (getCommandInput().isBlank()) {
        if (playbackForGuild.isPaused() || !audioManager.getQueue(guild).isEmpty()) {
            audioManager.startOrResumePlayback(guild, channel);
        } else {
            throw new InvalidCommandException("Queue is empty. Specify a song you want to play.");
        }
    } else {
        super.doRun();
    }
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) CommandContext(net.robinfriedli.aiode.command.CommandContext) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Guild(net.dv8tion.jda.api.entities.Guild)

Aggregations

AudioManager (net.robinfriedli.aiode.audio.AudioManager)15 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)12 Guild (net.dv8tion.jda.api.entities.Guild)9 Playable (net.robinfriedli.aiode.audio.Playable)7 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)7 Aiode (net.robinfriedli.aiode.Aiode)6 CommandContext (net.robinfriedli.aiode.command.CommandContext)6 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)5 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)5 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)5 AudioTrackLoader (net.robinfriedli.aiode.audio.AudioTrackLoader)5 Playlist (net.robinfriedli.aiode.entities.Playlist)5 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)5 Lists (com.google.common.collect.Lists)4 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)4 IOException (java.io.IOException)4 List (java.util.List)4 Callable (java.util.concurrent.Callable)4 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)4 UrlPlayable (net.robinfriedli.aiode.audio.UrlPlayable)4