SoundBufferLibrary .java

net.minecraft.client.sounds.SoundBufferLibrary (class): members: getCompleteBuffer, DebugOutput, enumerate, preload, getStream, resourceManager, cache.

net.minecraft.client.soundsclass77 linesclient2.6 KB

API Surface (5)

CompletableFuture<SoundBuffer> getCompleteBuffer(final Identifier location)
CompletableFuture<AudioStream> getStream(final Identifier location, final boolean looping)
void clear()
CompletableFuture<?> preload(final Collection<Sound> sounds)
void enumerate(final SoundBufferLibrary.DebugOutput debugOutput)

Fields & Constants (2)

resourceManager
cache

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

SoundBufferLibrary.java77 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.sounds;
2
3
import com.google.common.collect.Maps;
4
import com.mojang.blaze3d.audio.SoundBuffer;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.nio.ByteBuffer;
8
import java.util.Collection;
9
import java.util.Map;
10
import java.util.concurrent.CompletableFuture;
11
import java.util.concurrent.CompletionException;
12
import javax.sound.sampled.AudioFormat;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import net.minecraft.client.resources.sounds.Sound;
16
import net.minecraft.resources.Identifier;
17
import net.minecraft.server.packs.resources.ResourceProvider;
18
import net.minecraft.util.Util;
19
20
@Environment(EnvType.CLIENT)
21
public class SoundBufferLibrary {
22
	private final ResourceProvider resourceManager;
23
	private final Map<Identifier, CompletableFuture<SoundBuffer>> cache = Maps.newHashMap();
24
25
	public SoundBufferLibrary(final ResourceProvider resourceProvider) {
26
		this.resourceManager = resourceProvider;
27
	}
28
29
	public CompletableFuture<SoundBuffer> getCompleteBuffer(final Identifier location) {
30
		return this.cache.computeIfAbsent(location, l -> CompletableFuture.supplyAsync(() -> {
31
			try (
32
				InputStream is = this.resourceManager.open(l);
33
				FiniteAudioStream as = new JOrbisAudioStream(is);
34
			) {
35
				ByteBuffer data = as.readAll();
36
				return new SoundBuffer(data, as.getFormat());
37
			} catch (IOException e) {
38
				throw new CompletionException(e);
39
			}
40
		}, Util.nonCriticalIoPool()));
41
	}
42
43
	public CompletableFuture<AudioStream> getStream(final Identifier location, final boolean looping) {
44
		return CompletableFuture.supplyAsync(() -> {
45
			try {
46
				InputStream is = this.resourceManager.open(location);
47
				return looping ? new LoopingAudioStream(JOrbisAudioStream::new, is) : new JOrbisAudioStream(is);
48
			} catch (IOException e) {
49
				throw new CompletionException(e);
50
			}
51
		}, Util.nonCriticalIoPool());
52
	}
53
54
	public void clear() {
55
		this.cache.values().forEach(future -> future.thenAccept(SoundBuffer::discardAlBuffer));
56
		this.cache.clear();
57
	}
58
59
	public CompletableFuture<?> preload(final Collection<Sound> sounds) {
60
		return CompletableFuture.allOf(sounds.stream().map(sound -> this.getCompleteBuffer(sound.getPath())).toArray(CompletableFuture[]::new));
61
	}
62
63
	public void enumerate(final SoundBufferLibrary.DebugOutput debugOutput) {
64
		this.cache.forEach((id, bufferFuture) -> {
65
			SoundBuffer buffer = bufferFuture.getNow(null);
66
			if (buffer != null && buffer.isValid()) {
67
				debugOutput.accountBuffer(id, buffer.size(), buffer.format());
68
			}
69
		});
70
	}
71
72
	@Environment(EnvType.CLIENT)
73
	public interface DebugOutput {
74
		void accountBuffer(Identifier id, int size, AudioFormat format);
75
	}
76
}
77

Decompiled Minecraft 26.3 source of SoundBufferLibrary (net/minecraft/client/sounds/SoundBufferLibrary.java) with Mojang official mappings, stored verbatim on the site.