mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 08:17:35 +00:00
Allow sound instances to play custom audio streams (#8295)
This commit is contained in:
1 parent
de1050e420
commit
7f23b3b525
7 files changed
+139
-2
No files matched your search
Executable → Regular
File mode changed.
@@ -0,0 +1,13 @@
|
||||
--- a/net/minecraft/client/resources/sounds/SoundInstance.java
|
||||
+++ b/net/minecraft/client/resources/sounds/SoundInstance.java
|
||||
@@ -50,4 +_,10 @@
|
||||
NONE,
|
||||
LINEAR;
|
||||
}
|
||||
+
|
||||
+ /*================================ FORGE START ================================================*/
|
||||
+
|
||||
+ default java.util.concurrent.CompletableFuture<net.minecraft.client.sounds.AudioStream> getStream(net.minecraft.client.sounds.SoundBufferLibrary soundBuffers, Sound sound, boolean looping) {
|
||||
+ return soundBuffers.m_120204_(sound.m_119787_(), looping);
|
||||
+ }
|
||||
}
|
||||
@@ -26,7 +26,7 @@
|
||||
WeighedSoundEvents weighedsoundevents = p_120313_.m_6775_(this.f_120217_);
|
||||
ResourceLocation resourcelocation = p_120313_.m_7904_();
|
||||
if (weighedsoundevents == null) {
|
||||
@@ -389,11 +_,13 @@
|
||||
@@ -389,18 +_,21 @@
|
||||
p_194488_.m_83654_(vec3);
|
||||
p_194488_.m_83670_(flag);
|
||||
});
|
||||
@@ -40,7 +40,8 @@
|
||||
});
|
||||
});
|
||||
} else {
|
||||
@@ -401,6 +_,7 @@
|
||||
- this.f_120222_.m_120204_(sound.m_119790_(), flag2).thenAccept((p_194504_) -> {
|
||||
+ soundinstance.getStream(this.f_120222_, sound, flag2).thenAccept((p_194504_) -> {
|
||||
channelaccess$channelhandle.m_120154_((p_194498_) -> {
|
||||
p_194498_.m_83658_(p_194504_);
|
||||
p_194498_.m_83672_();
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Minecraft Forge - Forge Development LLC
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.client;
|
||||
|
||||
import com.mojang.math.Vector3d;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.sounds.AbstractSoundInstance;
|
||||
import net.minecraft.client.resources.sounds.Sound;
|
||||
import net.minecraft.client.resources.sounds.SoundInstance;
|
||||
import net.minecraft.client.sounds.AudioStream;
|
||||
import net.minecraft.client.sounds.SoundBufferLibrary;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.client.event.ClientChatEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.lwjgl.BufferUtils;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Tests support for custom {@link AudioStream}s ({@link SoundInstance#getStream(SoundBufferLibrary, Sound, boolean)}.
|
||||
*
|
||||
* When the message "sine wave" is sent in chat, this should play a sine wave of 220Hz at the player's current position.
|
||||
*/
|
||||
@Mod(AudioStreamTest.MOD_ID)
|
||||
public class AudioStreamTest
|
||||
{
|
||||
private static final boolean ENABLED = true;
|
||||
static final String MOD_ID = "audio_stream_test";
|
||||
|
||||
public AudioStreamTest()
|
||||
{
|
||||
if (ENABLED && FMLLoader.getDist().isClient())
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.addListener(ClientHandler::onClientChatEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ClientHandler
|
||||
{
|
||||
public static void onClientChatEvent(ClientChatEvent event)
|
||||
{
|
||||
if (event.getMessage().equalsIgnoreCase("sine wave"))
|
||||
{
|
||||
event.setCanceled(true);
|
||||
var mc = Minecraft.getInstance();
|
||||
mc.getSoundManager().play(new SineSound(mc.player.position()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class SineSound extends AbstractSoundInstance
|
||||
{
|
||||
protected SineSound(Vec3 position)
|
||||
{
|
||||
super(new ResourceLocation(MOD_ID, "sine_wave"), SoundSource.BLOCKS);
|
||||
x = position.x;
|
||||
y = position.y;
|
||||
z = position.z;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CompletableFuture<AudioStream> getStream(@NotNull SoundBufferLibrary soundBuffers, @NotNull Sound sound, boolean looping)
|
||||
{
|
||||
return CompletableFuture.completedFuture(new SineStream());
|
||||
}
|
||||
}
|
||||
|
||||
public static class SineStream implements AudioStream
|
||||
{
|
||||
private static final AudioFormat FORMAT = new AudioFormat(44100, 8, 1, false, false);
|
||||
private static final double DT = 2 * Math.PI * 220 / 44100;
|
||||
|
||||
private static double value = 0;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public AudioFormat getFormat()
|
||||
{
|
||||
return FORMAT;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ByteBuffer read(int capacity)
|
||||
{
|
||||
ByteBuffer buffer = BufferUtils.createByteBuffer(capacity);
|
||||
for(int i = 0; i < capacity; i++)
|
||||
{
|
||||
buffer.put(i, (byte) (Math.sin(value) * 127));
|
||||
value = (value + DT) % Math.PI;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,6 +174,8 @@ license="LGPL v2.1"
|
||||
modId="part_entity_test"
|
||||
[[mods]]
|
||||
modId="capabilities_test"
|
||||
[[mods]]
|
||||
modId="audio_stream_test"
|
||||
[[mods]]
|
||||
modId="custom_mob_bucket_test"
|
||||
[[mods]]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"sine_wave": {
|
||||
"sounds": [
|
||||
{
|
||||
"name": "audio_stream_test:empty",
|
||||
"stream": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user