From 0403cc81475303dd62d6dd4819bc12b37ab433ac Mon Sep 17 00:00:00 2001 From: Aeronica Date: Sun, 5 Jun 2022 09:48:51 -0500 Subject: [PATCH] [1.16] Allow sound instances to customize stream source (#8595) --- .../minecraft/client/audio/ISound.java.patch | 13 +++ .../client/audio/SoundEngine.java.patch | 5 +- .../debug/client/AudioStreamTest.java | 110 ++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + .../assets/audio_stream_test/sounds.json | 10 ++ .../assets/audio_stream_test/sounds/empty.ogg | Bin 0 -> 4654 bytes 6 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 patches/minecraft/net/minecraft/client/audio/ISound.java.patch create mode 100644 src/test/java/net/minecraftforge/debug/client/AudioStreamTest.java create mode 100644 src/test/resources/assets/audio_stream_test/sounds.json create mode 100644 src/test/resources/assets/audio_stream_test/sounds/empty.ogg diff --git a/patches/minecraft/net/minecraft/client/audio/ISound.java.patch b/patches/minecraft/net/minecraft/client/audio/ISound.java.patch new file mode 100644 index 0000000000..6a63c84d54 --- /dev/null +++ b/patches/minecraft/net/minecraft/client/audio/ISound.java.patch @@ -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 getStream(net.minecraft.client.audio.AudioStreamManager soundBuffers, Sound sound, boolean looping) { ++ return soundBuffers.func_217917_b(sound.func_188721_b(), looping); ++ } + } diff --git a/patches/minecraft/net/minecraft/client/audio/SoundEngine.java.patch b/patches/minecraft/net/minecraft/client/audio/SoundEngine.java.patch index 7012ada43f..43c1555c26 100644 --- a/patches/minecraft/net/minecraft/client/audio/SoundEngine.java.patch +++ b/patches/minecraft/net/minecraft/client/audio/SoundEngine.java.patch @@ -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(); diff --git a/src/test/java/net/minecraftforge/debug/client/AudioStreamTest.java b/src/test/java/net/minecraftforge/debug/client/AudioStreamTest.java new file mode 100644 index 0000000000..41351bad71 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/client/AudioStreamTest.java @@ -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 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() + { + } + } +} \ No newline at end of file diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index b14e168ec7..2f7c75c0fc 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -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 diff --git a/src/test/resources/assets/audio_stream_test/sounds.json b/src/test/resources/assets/audio_stream_test/sounds.json new file mode 100644 index 0000000000..5d37352d8f --- /dev/null +++ b/src/test/resources/assets/audio_stream_test/sounds.json @@ -0,0 +1,10 @@ +{ + "sine_wave": { + "sounds": [ + { + "name": "audio_stream_test:empty", + "stream": true + } + ] + } +} \ No newline at end of file diff --git a/src/test/resources/assets/audio_stream_test/sounds/empty.ogg b/src/test/resources/assets/audio_stream_test/sounds/empty.ogg new file mode 100644 index 0000000000000000000000000000000000000000..4f168c98f7d826776106503187b914f3696e6887 GIT binary patch literal 4654 zcmeGgYfzI{_J(IbpgbZb80i-XkRWsjja^`1CPY(6cz-0+u*+^Cq$Ki?l3+_6XQF_G zh%AOAi;*&r5-?eGDqF0$<4n3%K#+$ZVjsJ;#S&X)yRGa@cYp1k?@M&l=}bG*_Rr4V zxw+hX&bjB@^SbwZ6xGy}A}{1=^M~A%OOBctN;Kur!KNy;h5&i#{_G+7Cxx6`q7)H3 zA2nhpftkWquyE&po@$xiZdSX2V!ygk^;}U?4OXYFDkBA8Vk|XVlN(Ss!;|2 zW?!~clm#bA4TMFGs1O%rH8j`NQGDI!=L+|hQ4kd&T}(dx1Km=O453hj2FHG7K0I^f zyT2VidNzl4xPzFjkL4`A(J=|Ludj?o9i9W22IE(6N5}{1xKvBiUEx<(zKdpTgq1`k zP7|~C&J-h~J4sRYA%<&MPc!(m9Ut>T)r3g~Ir{aq?t8i2acaXW zE#{g|%h}VG`}ToAk_1A_hd2$o`?}usdUs8WfgCNPYRkxv?E=+IA`*Ztr(b2ddrlME zpS17{0-p+S_H)V)D&NkGssbPuyES+?fWLo8nm z+G%H2-tUy2Ntm?vozX0$yZY-E80-C-1@7$WYlEkkESd#zkj3VnwS=GXKWASC7{k>c zGR3F_I`y)}-~Qo#*ehSMY@D5>!K~AGCShVy>gGo>)hyV5+rRLB`e(4C^P@AhlK~Yl z`*(H!*Yg1c4CH!-05W_8;J3;<^r^RwVMn_Cs%WecR&TWS_aVK5yczCr^vUiQCHB^0 zoRMJD9wRl!5NZp3Jc?Pz=~W%9(f+2u#IwT&ec~+VPd8HMl89mK>O#imJ6A*Z2Jzlq zmL%<2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