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 0000000000..4f168c98f7 Binary files /dev/null and b/src/test/resources/assets/audio_stream_test/sounds/empty.ogg differ