| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import com.google.common.collect.HashMultimap;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.common.collect.Multimap;
|
| 7 | import com.google.common.collect.Sets;
|
| 8 | import com.mojang.blaze3d.audio.Channel;
|
| 9 | import com.mojang.blaze3d.audio.DeviceList;
|
| 10 | import com.mojang.blaze3d.audio.DeviceTracker;
|
| 11 | import com.mojang.blaze3d.audio.Library;
|
| 12 | import com.mojang.blaze3d.audio.Listener;
|
| 13 | import com.mojang.blaze3d.audio.ListenerTransform;
|
| 14 | import com.mojang.logging.LogUtils;
|
| 15 | import it.unimi.dsi.fastutil.objects.Object2FloatMap;
|
| 16 | import it.unimi.dsi.fastutil.objects.Object2FloatOpenHashMap;
|
| 17 | import java.util.Iterator;
|
| 18 | import java.util.List;
|
| 19 | import java.util.Map;
|
| 20 | import java.util.Objects;
|
| 21 | import java.util.Set;
|
| 22 | import java.util.Map.Entry;
|
| 23 | import java.util.concurrent.CompletableFuture;
|
| 24 | import net.fabricmc.api.EnvType;
|
| 25 | import net.fabricmc.api.Environment;
|
| 26 | import net.minecraft.SharedConstants;
|
| 27 | import net.minecraft.client.Camera;
|
| 28 | import net.minecraft.client.Options;
|
| 29 | import net.minecraft.client.resources.sounds.Sound;
|
| 30 | import net.minecraft.client.resources.sounds.SoundInstance;
|
| 31 | import net.minecraft.client.resources.sounds.TickableSoundInstance;
|
| 32 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 33 | import net.minecraft.resources.Identifier;
|
| 34 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 35 | import net.minecraft.sounds.SoundEvent;
|
| 36 | import net.minecraft.sounds.SoundEvents;
|
| 37 | import net.minecraft.sounds.SoundSource;
|
| 38 | import net.minecraft.util.Mth;
|
| 39 | import net.minecraft.util.Util;
|
| 40 | import net.minecraft.world.phys.Vec3;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 | import org.slf4j.Logger;
|
| 43 | import org.slf4j.Marker;
|
| 44 | import org.slf4j.MarkerFactory;
|
| 45 |
|
| 46 | @Environment(EnvType.CLIENT)
|
| 47 | public class SoundEngine {
|
| 48 | private static final Marker MARKER = MarkerFactory.getMarker("SOUNDS");
|
| 49 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 50 | private static final float PITCH_MIN = 0.5F;
|
| 51 | private static final float PITCH_MAX = 2.0F;
|
| 52 | private static final float VOLUME_MIN = 0.0F;
|
| 53 | private static final float VOLUME_MAX = 1.0F;
|
| 54 | private static final int MIN_SOURCE_LIFETIME = 20;
|
| 55 | private static final Set<Identifier> ONLY_WARN_ONCE = Sets.newHashSet();
|
| 56 | public static final String MISSING_SOUND = "FOR THE DEBUG!";
|
| 57 | public static final int LOOPING_SOUND_SUBTITLE_INTERVAL_TICKS = 10;
|
| 58 | public static final String OPEN_AL_SOFT_PREFIX = "OpenAL Soft on ";
|
| 59 | public static final int OPEN_AL_SOFT_PREFIX_LENGTH = "OpenAL Soft on ".length();
|
| 60 | private final SoundManager soundManager;
|
| 61 | private final Options options;
|
| 62 | private boolean loaded;
|
| 63 | private final Library library = new Library();
|
| 64 | private final Listener listener = this.library.getListener();
|
| 65 | private final SoundBufferLibrary soundBuffers;
|
| 66 | private final SoundEngineExecutor executor = new SoundEngineExecutor();
|
| 67 | private final ChannelAccess channelAccess = new ChannelAccess(this.library, this.executor);
|
| 68 | private int tickCount;
|
| 69 | private DeviceList lastSeenDevices;
|
| 70 | private final DeviceTracker deviceTracker = Library.createDeviceTracker();
|
| 71 | private final Map<SoundInstance, ChannelAccess.ChannelHandle> instanceToChannel = Maps.newHashMap();
|
| 72 | private final Multimap<SoundSource, SoundInstance> instanceBySource = HashMultimap.create();
|
| 73 | private final Object2FloatMap<SoundSource> gainBySource = Util.make(new Object2FloatOpenHashMap<>(), map -> map.defaultReturnValue(1.0F));
|
| 74 | private final List<TickableSoundInstance> tickingSounds = Lists.newArrayList();
|
| 75 | private final Map<SoundInstance, Integer> queuedSounds = Maps.newHashMap();
|
| 76 | private final Map<SoundInstance, Integer> soundDeleteTime = Maps.newHashMap();
|
| 77 | private final List<SoundEventListener> listeners = Lists.newArrayList();
|
| 78 | private final List<TickableSoundInstance> queuedTickableSounds = Lists.newArrayList();
|
| 79 | private final List<Sound> preloadQueue = Lists.newArrayList();
|
| 80 |
|
| 81 | public SoundEngine(final SoundManager soundManager, final Options options, final ResourceProvider resourceProvider) {
|
| 82 | this.soundManager = soundManager;
|
| 83 | this.options = options;
|
| 84 | this.soundBuffers = new SoundBufferLibrary(resourceProvider);
|
| 85 | this.lastSeenDevices = this.deviceTracker.currentDevices();
|
| 86 | }
|
| 87 |
|
| 88 | public void reload() {
|
| 89 | ONLY_WARN_ONCE.clear();
|
| 90 |
|
| 91 | for (SoundEvent sound : BuiltInRegistries.SOUND_EVENT) {
|
| 92 | if (sound != SoundEvents.EMPTY) {
|
| 93 | Identifier location = sound.location();
|
| 94 | if (this.soundManager.getSoundEvent(location) == null) {
|
| 95 | LOGGER.warn("Missing sound for event: {}", BuiltInRegistries.SOUND_EVENT.getKey(sound));
|
| 96 | ONLY_WARN_ONCE.add(location);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | this.destroy();
|
| 102 | this.loadLibrary();
|
| 103 | }
|
| 104 |
|
| 105 | private synchronized void loadLibrary() {
|
| 106 | if (!this.loaded) {
|
| 107 | try {
|
| 108 | String soundDevice = this.options.soundDevice().get();
|
| 109 | DeviceList currentDevices = this.deviceTracker.currentDevices();
|
| 110 | this.library.init(Options.isSoundDeviceDefault(soundDevice) ? null : soundDevice, currentDevices, this.options.directionalAudio().get());
|
| 111 | this.listener.reset();
|
| 112 | this.soundBuffers.preload(this.preloadQueue).thenRun(this.preloadQueue::clear);
|
| 113 | this.loaded = true;
|
| 114 | LOGGER.info(MARKER, "Sound engine started");
|
| 115 | } catch (RuntimeException e) {
|
| 116 | LOGGER.error(MARKER, "Error starting SoundSystem. Turning off sounds & music", e);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | public void refreshCategoryVolume(final SoundSource source) {
|
| 122 | if (this.loaded) {
|
| 123 | this.instanceToChannel.forEach((soundInstance, channelHandle) -> {
|
| 124 | if (source == soundInstance.getSource() || source == SoundSource.MASTER) {
|
| 125 | float newVolume = this.calculateVolume(soundInstance);
|
| 126 | channelHandle.execute(channel -> channel.setVolume(newVolume));
|
| 127 | }
|
| 128 | });
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | public void destroy() {
|
| 133 | if (this.loaded) {
|
| 134 | this.stopAll();
|
| 135 | this.soundBuffers.clear();
|
| 136 | this.library.cleanup();
|
| 137 | this.loaded = false;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | public void emergencyShutdown() {
|
| 142 | if (this.loaded) {
|
| 143 | this.library.cleanup();
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | public void stop(final SoundInstance soundInstance) {
|
| 148 | if (this.loaded) {
|
| 149 | ChannelAccess.ChannelHandle handle = this.instanceToChannel.get(soundInstance);
|
| 150 | if (handle != null) {
|
| 151 | handle.execute(Channel::stop);
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | public void updateCategoryVolume(final SoundSource source, final float gain) {
|
| 157 | this.gainBySource.put(source, Mth.clamp(gain, 0.0F, 1.0F));
|
| 158 | this.refreshCategoryVolume(source);
|
| 159 | }
|
| 160 |
|
| 161 | public void stopAll() {
|
| 162 | if (this.loaded) {
|
| 163 | this.executor.shutDown();
|
| 164 | this.instanceToChannel.clear();
|
| 165 | this.channelAccess.clear();
|
| 166 | this.queuedSounds.clear();
|
| 167 | this.tickingSounds.clear();
|
| 168 | this.instanceBySource.clear();
|
| 169 | this.soundDeleteTime.clear();
|
| 170 | this.queuedTickableSounds.clear();
|
| 171 | this.gainBySource.clear();
|
| 172 | this.executor.startUp();
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public void addEventListener(final SoundEventListener listener) {
|
| 177 | this.listeners.add(listener);
|
| 178 | }
|
| 179 |
|
| 180 | public void removeEventListener(final SoundEventListener listener) {
|
| 181 | this.listeners.remove(listener);
|
| 182 | }
|
| 183 |
|
| 184 | private boolean shouldChangeDevice() {
|
| 185 | if (this.library.isCurrentDeviceDisconnected()) {
|
| 186 | LOGGER.info("Audio device was lost!");
|
| 187 | this.deviceTracker.forceRefresh();
|
| 188 | return true;
|
| 189 | }
|
| 190 |
|
| 191 | this.deviceTracker.tick();
|
| 192 | boolean shouldChangeDevice = false;
|
| 193 | DeviceList currentDevices = this.deviceTracker.currentDevices();
|
| 194 | if (!currentDevices.equals(this.lastSeenDevices)) {
|
| 195 | String currentDeviceName = this.library.currentDeviceName();
|
| 196 | if (!currentDevices.allDevices().contains(currentDeviceName)) {
|
| 197 | LOGGER.info("Current audio device has disappeared!");
|
| 198 | shouldChangeDevice = true;
|
| 199 | }
|
| 200 |
|
| 201 | String userSelectedDevice = this.options.soundDevice().get();
|
| 202 | if (Options.isSoundDeviceDefault(userSelectedDevice)) {
|
| 203 | String newDefault = currentDevices.defaultDevice();
|
| 204 | if (!Objects.equals(currentDeviceName, newDefault)) {
|
| 205 | LOGGER.info("System default audio device has changed!");
|
| 206 | shouldChangeDevice = true;
|
| 207 | }
|
| 208 | } else if (!Objects.equals(currentDeviceName, userSelectedDevice) && currentDevices.allDevices().contains(userSelectedDevice)) {
|
| 209 | LOGGER.info("Preferred audio device has become available!");
|
| 210 | shouldChangeDevice = true;
|
| 211 | }
|
| 212 |
|
| 213 | this.lastSeenDevices = currentDevices;
|
| 214 | }
|
| 215 |
|
| 216 | return shouldChangeDevice;
|
| 217 | }
|
| 218 |
|
| 219 | public void tick(final boolean paused) {
|
| 220 | if (this.shouldChangeDevice()) {
|
| 221 | this.reload();
|
| 222 | }
|
| 223 |
|
| 224 | if (!paused) {
|
| 225 | this.tickInGameSound();
|
| 226 | } else {
|
| 227 | this.tickMusicWhenPaused();
|
| 228 | }
|
| 229 |
|
| 230 | this.channelAccess.scheduleTick();
|
| 231 | }
|
| 232 |
|
| 233 | private void tickInGameSound() {
|
| 234 | this.tickCount++;
|
| 235 | this.queuedTickableSounds.stream().filter(SoundInstance::canPlaySound).forEach(this::play);
|
| 236 | this.queuedTickableSounds.clear();
|
| 237 |
|
| 238 | for (TickableSoundInstance instance : this.tickingSounds) {
|
| 239 | if (!instance.canPlaySound()) {
|
| 240 | this.stop(instance);
|
| 241 | }
|
| 242 |
|
| 243 | instance.tick();
|
| 244 | if (instance.isStopped()) {
|
| 245 | this.stop(instance);
|
| 246 | } else {
|
| 247 | float volume = this.calculateVolume(instance);
|
| 248 | float pitch = this.calculatePitch(instance);
|
| 249 | Vec3 position = new Vec3(instance.getX(), instance.getY(), instance.getZ());
|
| 250 | ChannelAccess.ChannelHandle handle = this.instanceToChannel.get(instance);
|
| 251 | if (handle != null) {
|
| 252 | handle.execute(channel -> {
|
| 253 | channel.setVolume(volume);
|
| 254 | channel.setPitch(pitch);
|
| 255 | channel.setSelfPosition(position);
|
| 256 | });
|
| 257 | if (instance.isLooping() && this.tickCount % 10 == 0 && volume > 0.0F && !this.listeners.isEmpty()) {
|
| 258 | Sound sound = instance.getSound();
|
| 259 | if (sound != null) {
|
| 260 | WeighedSoundEvents soundEvent = instance.getSoundEvent();
|
| 261 | if (soundEvent != null) {
|
| 262 | float attenuationDistance = sound.getAttenuationDistance(instance.getVolume());
|
| 263 | this.notifyListeners(instance, soundEvent, attenuationDistance);
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | Iterator<Entry<SoundInstance, ChannelAccess.ChannelHandle>> iterator = this.instanceToChannel.entrySet().iterator();
|
| 272 |
|
| 273 | while (iterator.hasNext()) {
|
| 274 | Entry<SoundInstance, ChannelAccess.ChannelHandle> entry = iterator.next();
|
| 275 | ChannelAccess.ChannelHandle handle = entry.getValue();
|
| 276 | SoundInstance instance = entry.getKey();
|
| 277 | if (handle.isStopped()) {
|
| 278 | int minDeleteTime = this.soundDeleteTime.get(instance);
|
| 279 | if (minDeleteTime <= this.tickCount) {
|
| 280 | if (shouldLoopManually(instance)) {
|
| 281 | this.queuedSounds.put(instance, this.tickCount + instance.getDelay());
|
| 282 | }
|
| 283 |
|
| 284 | iterator.remove();
|
| 285 | LOGGER.debug(MARKER, "Removed channel {} because it's not playing anymore", handle);
|
| 286 | this.soundDeleteTime.remove(instance);
|
| 287 |
|
| 288 | try {
|
| 289 | this.instanceBySource.remove(instance.getSource(), instance);
|
| 290 | } catch (RuntimeException var10) {
|
| 291 | }
|
| 292 |
|
| 293 | if (instance instanceof TickableSoundInstance) {
|
| 294 | this.tickingSounds.remove(instance);
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | Iterator<Entry<SoundInstance, Integer>> queueIterator = this.queuedSounds.entrySet().iterator();
|
| 301 |
|
| 302 | while (queueIterator.hasNext()) {
|
| 303 | Entry<SoundInstance, Integer> next = queueIterator.next();
|
| 304 | if (this.tickCount >= next.getValue()) {
|
| 305 | SoundInstance instance = next.getKey();
|
| 306 | if (instance instanceof TickableSoundInstance tickableSoundInstance) {
|
| 307 | tickableSoundInstance.tick();
|
| 308 | }
|
| 309 |
|
| 310 | this.play(instance);
|
| 311 | queueIterator.remove();
|
| 312 | }
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | private void notifyListeners(final SoundInstance instance, final WeighedSoundEvents soundEvent, final float attenuationDistance) {
|
| 317 | float range = getRange(instance.isRelative(), instance.getAttenuation(), attenuationDistance);
|
| 318 |
|
| 319 | for (SoundEventListener listener : this.listeners) {
|
| 320 | listener.onPlaySound(instance, soundEvent, range);
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | private static float getRange(final boolean isRelative, final SoundInstance.Attenuation instance, final float attenuationDistance) {
|
| 325 | return !isRelative && instance != SoundInstance.Attenuation.NONE ? attenuationDistance : Float.POSITIVE_INFINITY;
|
| 326 | }
|
| 327 |
|
| 328 | private void tickMusicWhenPaused() {
|
| 329 | Iterator<Entry<SoundInstance, ChannelAccess.ChannelHandle>> iterator = this.instanceToChannel.entrySet().iterator();
|
| 330 |
|
| 331 | while (iterator.hasNext()) {
|
| 332 | Entry<SoundInstance, ChannelAccess.ChannelHandle> entry = iterator.next();
|
| 333 | ChannelAccess.ChannelHandle handle = entry.getValue();
|
| 334 | SoundInstance instance = entry.getKey();
|
| 335 | if (instance.getSource() == SoundSource.MUSIC && handle.isStopped()) {
|
| 336 | iterator.remove();
|
| 337 | LOGGER.debug(MARKER, "Removed channel {} because it's not playing anymore", handle);
|
| 338 | this.soundDeleteTime.remove(instance);
|
| 339 | this.instanceBySource.remove(instance.getSource(), instance);
|
| 340 | }
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | private static boolean requiresManualLooping(final SoundInstance instance) {
|
| 345 | return instance.getDelay() > 0;
|
| 346 | }
|
| 347 |
|
| 348 | private static boolean shouldLoopManually(final SoundInstance instance) {
|
| 349 | return instance.isLooping() && requiresManualLooping(instance);
|
| 350 | }
|
| 351 |
|
| 352 | private static boolean shouldLoopAutomatically(final SoundInstance instance) {
|
| 353 | return instance.isLooping() && !requiresManualLooping(instance);
|
| 354 | }
|
| 355 |
|
| 356 | public boolean isActive(final SoundInstance instance) {
|
| 357 | if (!this.loaded) {
|
| 358 | return false;
|
| 359 | } else {
|
| 360 | return this.soundDeleteTime.containsKey(instance) && this.soundDeleteTime.get(instance) <= this.tickCount
|
| 361 | ? true
|
| 362 | : this.instanceToChannel.containsKey(instance);
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | public SoundEngine.PlayResult play(final SoundInstance instance) {
|
| 367 | if (!this.loaded) {
|
| 368 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 369 | }
|
| 370 |
|
| 371 | if (!instance.canPlaySound()) {
|
| 372 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 373 | }
|
| 374 |
|
| 375 | WeighedSoundEvents soundEvent = instance.getOrResolve(this.soundManager);
|
| 376 | Identifier eventLocation = instance.getIdentifier();
|
| 377 | if (soundEvent == null) {
|
| 378 | if (ONLY_WARN_ONCE.add(eventLocation)) {
|
| 379 | LOGGER.warn(MARKER, "Unable to play unknown soundEvent: {}", eventLocation);
|
| 380 | }
|
| 381 |
|
| 382 | if (!SharedConstants.DEBUG_SUBTITLES) {
|
| 383 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 384 | }
|
| 385 |
|
| 386 | soundEvent = new WeighedSoundEvents(eventLocation, "FOR THE DEBUG!");
|
| 387 | }
|
| 388 |
|
| 389 | Sound sound = instance.getSound();
|
| 390 | if (sound == SoundManager.INTENTIONALLY_EMPTY_SOUND) {
|
| 391 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 392 | }
|
| 393 |
|
| 394 | if (sound == SoundManager.EMPTY_SOUND) {
|
| 395 | if (ONLY_WARN_ONCE.add(eventLocation)) {
|
| 396 | LOGGER.warn(MARKER, "Unable to play empty soundEvent: {}", eventLocation);
|
| 397 | }
|
| 398 |
|
| 399 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 400 | } else {
|
| 401 | float instanceVolume = instance.getVolume();
|
| 402 | float attenuationDistance = sound.getAttenuationDistance(instanceVolume);
|
| 403 | if (!this.listeners.isEmpty()) {
|
| 404 | this.notifyListeners(instance, soundEvent, attenuationDistance);
|
| 405 | }
|
| 406 |
|
| 407 | SoundSource soundSource = instance.getSource();
|
| 408 | float volume = this.calculateVolume(instanceVolume, soundSource);
|
| 409 | boolean startedSilently = false;
|
| 410 | if (volume == 0.0F) {
|
| 411 | if (!instance.canStartSilent() && soundSource != SoundSource.MUSIC) {
|
| 412 | LOGGER.debug(MARKER, "Skipped playing sound {}, volume was zero.", sound.getLocation());
|
| 413 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 414 | }
|
| 415 |
|
| 416 | startedSilently = true;
|
| 417 | }
|
| 418 |
|
| 419 | Vec3 position = new Vec3(instance.getX(), instance.getY(), instance.getZ());
|
| 420 | boolean isLooping = shouldLoopAutomatically(instance);
|
| 421 | boolean isStreaming = sound.shouldStream();
|
| 422 | CompletableFuture<ChannelAccess.ChannelHandle> handleFuture = this.channelAccess
|
| 423 | .createHandle(sound.shouldStream() ? Library.Pool.STREAMING : Library.Pool.STATIC);
|
| 424 | ChannelAccess.ChannelHandle handle = handleFuture.join();
|
| 425 | if (handle == null) {
|
| 426 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 427 | LOGGER.warn("Failed to create new sound handle");
|
| 428 | }
|
| 429 |
|
| 430 | return SoundEngine.PlayResult.NOT_STARTED;
|
| 431 | } else {
|
| 432 | LOGGER.debug(MARKER, "Playing sound {} for event {}", sound.getLocation(), eventLocation);
|
| 433 | this.soundDeleteTime.put(instance, this.tickCount + 20);
|
| 434 | this.instanceToChannel.put(instance, handle);
|
| 435 | this.instanceBySource.put(soundSource, instance);
|
| 436 | float pitch = this.calculatePitch(instance);
|
| 437 | handle.execute(channel -> {
|
| 438 | channel.setPitch(pitch);
|
| 439 | channel.setVolume(volume);
|
| 440 | if (instance.getAttenuation() == SoundInstance.Attenuation.LINEAR) {
|
| 441 | channel.linearAttenuation(attenuationDistance);
|
| 442 | } else {
|
| 443 | channel.disableAttenuation();
|
| 444 | }
|
| 445 |
|
| 446 | channel.setLooping(isLooping && !isStreaming);
|
| 447 | channel.setSelfPosition(position);
|
| 448 | channel.setRelative(instance.isRelative());
|
| 449 | });
|
| 450 | if (!isStreaming) {
|
| 451 | this.soundBuffers.getCompleteBuffer(sound.getPath()).thenAccept(soundBuffer -> handle.execute(channel -> {
|
| 452 | channel.attachStaticBuffer(soundBuffer);
|
| 453 | channel.play();
|
| 454 | }));
|
| 455 | } else {
|
| 456 | this.soundBuffers.getStream(sound.getPath(), isLooping).thenAccept(stream -> handle.execute(channel -> {
|
| 457 | channel.attachBufferStream(stream);
|
| 458 | channel.play();
|
| 459 | }));
|
| 460 | }
|
| 461 |
|
| 462 | if (instance instanceof TickableSoundInstance tickableSoundInstance) {
|
| 463 | this.tickingSounds.add(tickableSoundInstance);
|
| 464 | }
|
| 465 |
|
| 466 | return startedSilently ? SoundEngine.PlayResult.STARTED_SILENTLY : SoundEngine.PlayResult.STARTED;
|
| 467 | }
|
| 468 | }
|
| 469 | }
|
| 470 |
|
| 471 | public void queueTickingSound(final TickableSoundInstance tickableSoundInstance) {
|
| 472 | this.queuedTickableSounds.add(tickableSoundInstance);
|
| 473 | }
|
| 474 |
|
| 475 | public void requestPreload(final Sound sound) {
|
| 476 | this.preloadQueue.add(sound);
|
| 477 | }
|
| 478 |
|
| 479 | private float calculatePitch(final SoundInstance instance) {
|
| 480 | return Mth.clamp(instance.getPitch(), 0.5F, 2.0F);
|
| 481 | }
|
| 482 |
|
| 483 | private float calculateVolume(final SoundInstance instance) {
|
| 484 | return this.calculateVolume(instance.getVolume(), instance.getSource());
|
| 485 | }
|
| 486 |
|
| 487 | private float calculateVolume(final float volume, final SoundSource source) {
|
| 488 | return Mth.clamp(volume, 0.0F, 1.0F) * Mth.clamp(this.options.getFinalSoundSourceVolume(source), 0.0F, 1.0F) * this.gainBySource.getFloat(source);
|
| 489 | }
|
| 490 |
|
| 491 | public void pauseAllExcept(final SoundSource... ignoredSources) {
|
| 492 | if (this.loaded) {
|
| 493 | for (Entry<SoundInstance, ChannelAccess.ChannelHandle> instance : this.instanceToChannel.entrySet()) {
|
| 494 | if (!List.of(ignoredSources).contains(instance.getKey().getSource())) {
|
| 495 | instance.getValue().execute(Channel::pause);
|
| 496 | }
|
| 497 | }
|
| 498 | }
|
| 499 | }
|
| 500 |
|
| 501 | public void resume() {
|
| 502 | if (this.loaded) {
|
| 503 | this.channelAccess.executeOnChannels(channels -> channels.forEach(Channel::unpause));
|
| 504 | }
|
| 505 | }
|
| 506 |
|
| 507 | public void playDelayed(final SoundInstance instance, final int delay) {
|
| 508 | this.queuedSounds.put(instance, this.tickCount + delay);
|
| 509 | }
|
| 510 |
|
| 511 | public void updateSource(final Camera camera) {
|
| 512 | if (this.loaded && camera.isInitialized()) {
|
| 513 | ListenerTransform transform = new ListenerTransform(camera.position(), new Vec3(camera.forwardVector()), new Vec3(camera.upVector()));
|
| 514 | this.executor.execute(() -> this.listener.setTransform(transform));
|
| 515 | }
|
| 516 | }
|
| 517 |
|
| 518 | public void stop(final @Nullable Identifier sound, final @Nullable SoundSource source) {
|
| 519 | if (source != null) {
|
| 520 | for (SoundInstance instance : this.instanceBySource.get(source)) {
|
| 521 | if (sound == null || instance.getIdentifier().equals(sound)) {
|
| 522 | this.stop(instance);
|
| 523 | }
|
| 524 | }
|
| 525 | } else if (sound == null) {
|
| 526 | this.stopAll();
|
| 527 | } else {
|
| 528 | for (SoundInstance instance : this.instanceToChannel.keySet()) {
|
| 529 | if (instance.getIdentifier().equals(sound)) {
|
| 530 | this.stop(instance);
|
| 531 | }
|
| 532 | }
|
| 533 | }
|
| 534 | }
|
| 535 |
|
| 536 | public String getChannelDebugString() {
|
| 537 | return this.library.getChannelDebugString();
|
| 538 | }
|
| 539 |
|
| 540 | public void getSoundCacheDebugStats(final SoundBufferLibrary.DebugOutput output) {
|
| 541 | this.soundBuffers.enumerate(output);
|
| 542 | }
|
| 543 |
|
| 544 | public List<String> getAvailableSoundDevices() {
|
| 545 | return this.deviceTracker.currentDevices().allDevices();
|
| 546 | }
|
| 547 |
|
| 548 | public ListenerTransform getListenerTransform() {
|
| 549 | return this.listener.getTransform();
|
| 550 | }
|
| 551 |
|
| 552 | @Environment(EnvType.CLIENT)
|
| 553 | public enum PlayResult {
|
| 554 | STARTED,
|
| 555 | STARTED_SILENTLY,
|
| 556 | NOT_STARTED;
|
| 557 | }
|
| 558 | }
|
| 559 |
|