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