[1.16] Allow sound instances to customize stream source (#8595)

This commit is contained in:
Aeronica authored and GitHub committed 2022-06-05 15:48:51 +01:00
1 parent 9a21260575
commit 0403cc8147
6 files changed
+138 -2

No files matched your search

@@ -0,0 +1,13 @@
--- a/net/minecraft/client/audio/ISound.java
+++ b/net/minecraft/client/audio/ISound.java
@@ -48,4 +_,10 @@
NONE,
LINEAR;
}
+
+ /*================================ FORGE START ================================================*/
+
+ default java.util.concurrent.CompletableFuture<net.minecraft.client.audio.IAudioStream> getStream(net.minecraft.client.audio.AudioStreamManager soundBuffers, Sound sound, boolean looping) {
+ return soundBuffers.func_217917_b(sound.func_188721_b(), looping);
+ }
}
@@ -26,7 +26,7 @@
SoundEventAccessor soundeventaccessor = p_148611_1_.func_184366_a(this.field_148622_c);
ResourceLocation resourcelocation = p_148611_1_.func_147650_b();
if (soundeventaccessor == null) {
@@ -331,11 +_,13 @@
@@ -331,18 +_,21 @@
p_239543_8_.func_216420_a(vector3d);
p_239543_8_.func_216432_b(flag);
});
@@ -40,7 +40,8 @@
});
});
} else {
@@ -343,6 +_,7 @@
- this.field_217939_i.func_217917_b(sound.func_188721_b(), flag2).thenAccept((p_217928_1_) -> {
+ isound.getStream(this.field_217939_i, sound, flag2).thenAccept((p_217928_1_) -> {
channelmanager$entry.func_217888_a((p_217935_1_) -> {
p_217935_1_.func_216433_a(p_217928_1_);
p_217935_1_.func_216438_c();
@@ -0,0 +1,110 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.client;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.AudioStreamManager;
import net.minecraft.client.audio.IAudioStream;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.LocatableSound;
import net.minecraft.client.audio.Sound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.vector.Vector3d;
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.lwjgl.BufferUtils;
import javax.annotation.Nonnull;
import javax.sound.sampled.AudioFormat;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;
/**
* Tests support for custom {@link IAudioStream}s ({@link ISound#getStream(AudioStreamManager, 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);
Minecraft mc = Minecraft.getInstance();
mc.getSoundManager().play(new SineSound(mc.player.position()));
}
}
}
public static class SineSound extends LocatableSound
{
protected SineSound(Vector3d position)
{
super(new ResourceLocation(MOD_ID, "sine_wave"), SoundCategory.BLOCKS);
x = position.x;
y = position.y;
z = position.z;
}
@Nonnull
@Override
public CompletableFuture<IAudioStream> getStream(@Nonnull AudioStreamManager soundBuffers, @Nonnull Sound sound, boolean looping)
{
return CompletableFuture.completedFuture(new SineStream());
}
}
public static class SineStream implements IAudioStream
{
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;
@Nonnull
@Override
public AudioFormat getFormat()
{
return FORMAT;
}
@Nonnull
@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()
{
}
}
}
+2
View File
@@ -150,5 +150,7 @@ license="LGPL v2.1"
modId="forge_spawnegg_test"
[[mods]]
modId="duplicate_optional_tag_test"
[[mods]]
modId="audio_stream_test"
# ADD ABOVE THIS LINE
@@ -0,0 +1,10 @@
{
"sine_wave": {
"sounds": [
{
"name": "audio_stream_test:empty",
"stream": true
}
]
}
}