Channel .java

com.mojang.blaze3d.audio.Channel (class, 172 lines): members: BUFFER_DURATION_SECONDS, QUEUED_BUFFER_COUNT, streamingBufferSize, attachBufferStream, pause.

com.mojang.blaze3d.audioclass172 linesclient4.6 KB

API Surface (17)

void destroy()
void play()
void pause()
void unpause()
void stop()
boolean playing()
boolean stopped()
void setSelfPosition(final Vec3 newPosition)
void setPitch(final float pitch)
void setLooping(final boolean looping)
void setVolume(final float volume)
void disableAttenuation()
void linearAttenuation(final float maxDistance)
void setRelative(final boolean relative)
void attachStaticBuffer(final SoundBuffer buffer)
void attachBufferStream(final AudioStream stream)
void updateStream()

Fields & Constants (7)

LOGGER
QUEUED_BUFFER_COUNT
BUFFER_DURATION_SECONDS
source
initialized
streamingBufferSize
stream

String literals (7)

"Allocate new source"
"Stop"
"Failed to close audio stream"
"Cleanup"
"Failed to read from audio stream"
"Unqueue buffers"
"Remove processed buffers"

Code Graph: Dependencies (4)

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

Full Source

Channel.java172 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d.audio;
2
3
import com.mojang.logging.LogUtils;
4
import java.io.IOException;
5
import java.nio.ByteBuffer;
6
import java.util.concurrent.atomic.AtomicBoolean;
7
import javax.sound.sampled.AudioFormat;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import net.minecraft.client.sounds.AudioStream;
11
import net.minecraft.world.phys.Vec3;
12
import org.jspecify.annotations.Nullable;
13
import org.lwjgl.openal.AL10;
14
import org.slf4j.Logger;
15
16
@Environment(EnvType.CLIENT)
17
public class Channel {
18
	private static final Logger LOGGER = LogUtils.getLogger();
19
	private static final int QUEUED_BUFFER_COUNT = 4;
20
	public static final int BUFFER_DURATION_SECONDS = 1;
21
	private final int source;
22
	private final AtomicBoolean initialized = new AtomicBoolean(true);
23
	private int streamingBufferSize = 16384;
24
	private @Nullable AudioStream stream;
25
26
	static @Nullable Channel create() {
27
		int[] newId = new int[1];
28
		AL10.alGenSources(newId);
29
		return OpenAlUtil.checkALError("Allocate new source") ? null : new Channel(newId[0]);
30
	}
31
32
	private Channel(final int src) {
33
		this.source = src;
34
	}
35
36
	public void destroy() {
37
		if (this.initialized.compareAndSet(true, false)) {
38
			AL10.alSourceStop(this.source);
39
			OpenAlUtil.checkALError("Stop");
40
			if (this.stream != null) {
41
				try {
42
					this.stream.close();
43
				} catch (IOException e) {
44
					LOGGER.error("Failed to close audio stream", e);
45
				}
46
47
				this.removeProcessedBuffers();
48
				this.stream = null;
49
			}
50
51
			AL10.alDeleteSources(new int[]{this.source});
52
			OpenAlUtil.checkALError("Cleanup");
53
		}
54
	}
55
56
	public void play() {
57
		AL10.alSourcePlay(this.source);
58
	}
59
60
	private int getState() {
61
		return !this.initialized.get() ? 4116 : AL10.alGetSourcei(this.source, 4112);
62
	}
63
64
	public void pause() {
65
		if (this.getState() == 4114) {
66
			AL10.alSourcePause(this.source);
67
		}
68
	}
69
70
	public void unpause() {
71
		if (this.getState() == 4115) {
72
			AL10.alSourcePlay(this.source);
73
		}
74
	}
75
76
	public void stop() {
77
		if (this.initialized.get()) {
78
			AL10.alSourceStop(this.source);
79
			OpenAlUtil.checkALError("Stop");
80
		}
81
	}
82
83
	public boolean playing() {
84
		return this.getState() == 4114;
85
	}
86
87
	public boolean stopped() {
88
		return this.getState() == 4116;
89
	}
90
91
	public void setSelfPosition(final Vec3 newPosition) {
92
		AL10.alSourcefv(this.source, 4100, new float[]{(float)newPosition.x, (float)newPosition.y, (float)newPosition.z});
93
	}
94
95
	public void setPitch(final float pitch) {
96
		AL10.alSourcef(this.source, 4099, pitch);
97
	}
98
99
	public void setLooping(final boolean looping) {
100
		AL10.alSourcei(this.source, 4103, looping ? 1 : 0);
101
	}
102
103
	public void setVolume(final float volume) {
104
		AL10.alSourcef(this.source, 4106, volume);
105
	}
106
107
	public void disableAttenuation() {
108
		AL10.alSourcei(this.source, 53248, 0);
109
	}
110
111
	public void linearAttenuation(final float maxDistance) {
112
		AL10.alSourcei(this.source, 53248, 53251);
113
		AL10.alSourcef(this.source, 4131, maxDistance);
114
		AL10.alSourcef(this.source, 4129, 1.0F);
115
		AL10.alSourcef(this.source, 4128, 0.0F);
116
	}
117
118
	public void setRelative(final boolean relative) {
119
		AL10.alSourcei(this.source, 514, relative ? 1 : 0);
120
	}
121
122
	public void attachStaticBuffer(final SoundBuffer buffer) {
123
		buffer.getAlBuffer().ifPresent(bufferId -> AL10.alSourcei(this.source, 4105, bufferId));
124
	}
125
126
	public void attachBufferStream(final AudioStream stream) {
127
		this.stream = stream;
128
		AudioFormat format = stream.getFormat();
129
		this.streamingBufferSize = calculateBufferSize(format, 1);
130
		this.pumpBuffers(4);
131
	}
132
133
	private static int calculateBufferSize(final AudioFormat format, final int seconds) {
134
		return (int)(seconds * format.getSampleSizeInBits() / 8.0F * format.getChannels() * format.getSampleRate());
135
	}
136
137
	private void pumpBuffers(final int size) {
138
		if (this.stream != null) {
139
			try {
140
				for (int i = 0; i < size; i++) {
141
					ByteBuffer buffer = this.stream.read(this.streamingBufferSize);
142
					if (buffer != null) {
143
						new SoundBuffer(buffer, this.stream.getFormat()).releaseAlBuffer().ifPresent(bufferId -> AL10.alSourceQueueBuffers(this.source, new int[]{bufferId}));
144
					}
145
				}
146
			} catch (IOException e) {
147
				LOGGER.error("Failed to read from audio stream", e);
148
			}
149
		}
150
	}
151
152
	public void updateStream() {
153
		if (this.stream != null) {
154
			int processedBuffers = this.removeProcessedBuffers();
155
			this.pumpBuffers(processedBuffers);
156
		}
157
	}
158
159
	private int removeProcessedBuffers() {
160
		int processed = AL10.alGetSourcei(this.source, 4118);
161
		if (processed > 0) {
162
			int[] ids = new int[processed];
163
			AL10.alSourceUnqueueBuffers(this.source, ids);
164
			OpenAlUtil.checkALError("Unqueue buffers");
165
			AL10.alDeleteBuffers(ids);
166
			OpenAlUtil.checkALError("Remove processed buffers");
167
		}
168
169
		return processed;
170
	}
171
}
172

Decompiled Minecraft 26.3 source of Channel (com/mojang/blaze3d/audio/Channel.java) with Mojang official mappings, stored verbatim on the site.