janky and wip audio stuff; getting closer to having it work properly
This commit is contained in:
parent
47966aa6d5
commit
35e81b1b7d
Binary file not shown.
|
|
@ -54,7 +54,7 @@ public class WDAudioHandler implements CefAudioHandler {
|
|||
// the size of the |data| array in bytes.
|
||||
//
|
||||
@Override
|
||||
public void onAudioStreamPacket(CefBrowser cefBrowser, long pointer, int frames, long pts) {
|
||||
public void onAudioStreamPacket(CefBrowser cefBrowser, DataPointer pointer, int frames, long pts) {
|
||||
ClientProxy proxy = ((ClientProxy) WebDisplays.PROXY);
|
||||
|
||||
for (ScreenBlockEntity tes : proxy.getScreens()) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package net.montoyo.wd.client.audio;
|
||||
|
||||
import com.mojang.blaze3d.audio.Channel;
|
||||
import net.minecraft.client.sounds.AudioStream;
|
||||
import org.cef.misc.CefAudioParameters;
|
||||
import org.cef.misc.CefChannelLayout;
|
||||
|
|
@ -12,7 +13,7 @@ import java.io.IOException;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayDeque;
|
||||
|
||||
public class WDAudioStream implements AudioStream {
|
||||
public class WDAudioStream implements AudioStream, WDExtendedAudioStream {
|
||||
AudioFormat currentFormat = new AudioFormat(
|
||||
AudioFormat.Encoding.PCM_SIGNED,
|
||||
44100, 16,
|
||||
|
|
@ -49,35 +50,44 @@ public class WDAudioStream implements AudioStream {
|
|||
|
||||
ArrayDeque<float[]> buffers = new ArrayDeque<>();
|
||||
|
||||
public void setData(long data) {
|
||||
// DataPointer ptr = data.forCapacity(currentFormat.getChannels() << 3);
|
||||
// ptr.setAlignment(3);
|
||||
long baseAddr = data;
|
||||
public void setData(DataPointer data) {
|
||||
int cap = fpb;
|
||||
DataPointer ptr = data
|
||||
.forCapacity(currentFormat.getChannels() << 3)
|
||||
.withAlignment(3);
|
||||
|
||||
for (int i = 0; i < 1; i++) {
|
||||
long addr = MemoryUtil.memGetLong(baseAddr + (i << 3));
|
||||
int cap = fpb;
|
||||
DataPointer subPtr = ptr.getData(i)
|
||||
.withAlignment(2)
|
||||
.forCapacity(cap << 2);
|
||||
|
||||
float[] flts = new float[cap];
|
||||
for (int i1 = 0; i1 < cap; i1++) {
|
||||
flts[i1] = MemoryUtil.memGetFloat(addr + (i1 << 2));
|
||||
}
|
||||
for (int i1 = 0; i1 < cap; i1++)
|
||||
flts[i1] = subPtr.getFloat(i1);
|
||||
|
||||
buffers.add(flts);
|
||||
}
|
||||
}
|
||||
|
||||
boolean checking = false;
|
||||
|
||||
@Override
|
||||
public ByteBuffer read(int pSize) throws IOException {
|
||||
System.out.println(buffers.size());
|
||||
int sz = 2048 * buffers.size();
|
||||
if (sz < 2048) sz = 4096;
|
||||
pSize = sz;
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(pSize);
|
||||
if (!buffers.isEmpty()) {
|
||||
final int MAX_16_BIT = 32767;
|
||||
final int MIN_16_BIT = -32768;
|
||||
|
||||
int i0 = 0;
|
||||
loopBufs:
|
||||
while (true) {
|
||||
if (!buffers.isEmpty()) {
|
||||
for (float v : buffers.pop()) {
|
||||
if (buffer.position() >= pSize) break loopBufs;
|
||||
// buffer.putFloat(v);
|
||||
|
||||
// Scale and clip the float value to the range of a signed 16-bit int
|
||||
float floatSample = v;
|
||||
|
|
@ -93,9 +103,11 @@ public class WDAudioStream implements AudioStream {
|
|||
buffer.put((byte) (intSample & 0xFF));
|
||||
buffer.put((byte) ((intSample >> 8) & 0xFF));
|
||||
}
|
||||
i0++;
|
||||
} else break;
|
||||
}
|
||||
buffer.position(0);
|
||||
System.out.println("Consumed " + i0 + " buffers");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
|
@ -103,4 +115,27 @@ public class WDAudioStream implements AudioStream {
|
|||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
Channel channel;
|
||||
|
||||
@Override
|
||||
public void attach(Channel channel) {
|
||||
WDExtendedAudioStream.super.attach(channel);
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPumpCount(int defaultAmount) {
|
||||
return defaultAmount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int computeSize(AudioFormat format, int streamingBufferSize) {
|
||||
return 2048;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canStop() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package net.montoyo.wd.client.audio;
|
||||
|
||||
import com.mojang.blaze3d.audio.Channel;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
|
||||
public interface WDExtendedAudioStream {
|
||||
default int getPumpCount(int defaultAmount) {
|
||||
return defaultAmount;
|
||||
}
|
||||
|
||||
default void attach(Channel channel) {
|
||||
return;
|
||||
}
|
||||
|
||||
default int computeSize(AudioFormat format, int streamingBufferSize) {
|
||||
return streamingBufferSize;
|
||||
}
|
||||
|
||||
default boolean canStop() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package net.montoyo.wd.mixins.audio;
|
||||
|
||||
import com.mojang.blaze3d.audio.Channel;
|
||||
import net.minecraft.client.sounds.AudioStream;
|
||||
import net.montoyo.wd.client.audio.WDExtendedAudioStream;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
|
||||
@Mixin(Channel.class)
|
||||
public class AudioChannelMixin {
|
||||
@Shadow
|
||||
@Nullable
|
||||
private AudioStream stream;
|
||||
|
||||
@Shadow private int streamingBufferSize;
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "attachBufferStream")
|
||||
public void postAttach(AudioStream pStream, CallbackInfo ci) {
|
||||
((WDExtendedAudioStream) pStream).attach((Channel) (Object) this);
|
||||
}
|
||||
|
||||
@Inject(at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/audio/Channel;pumpBuffers(I)V", shift = At.Shift.BEFORE), method = "attachBufferStream")
|
||||
public void postComputeSize(AudioStream pStream, CallbackInfo ci) {
|
||||
streamingBufferSize = ((WDExtendedAudioStream) pStream).computeSize(
|
||||
pStream.getFormat(), streamingBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
@ModifyVariable(argsOnly = true, ordinal = 0, at = @At("HEAD"), method = "pumpBuffers")
|
||||
public int prePump(int defaultAmount) {
|
||||
return ((WDExtendedAudioStream) stream).getPumpCount(defaultAmount);
|
||||
}
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "stopped", cancellable = true)
|
||||
public void preCheckStopped(CallbackInfoReturnable<Boolean> cir) {
|
||||
if (stream != null)
|
||||
if (!((WDExtendedAudioStream) stream).canStop())
|
||||
cir.setReturnValue(false);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package net.montoyo.wd.mixins.audio;
|
||||
|
||||
import net.minecraft.client.sounds.AudioStream;
|
||||
import net.montoyo.wd.client.audio.WDExtendedAudioStream;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
@Mixin(AudioStream.class)
|
||||
public interface AudioStreamMixin extends WDExtendedAudioStream {
|
||||
}
|
||||
|
|
@ -8,7 +8,9 @@
|
|||
],
|
||||
"client": [
|
||||
"MouseHandlerMixin",
|
||||
"OverlayMixin"
|
||||
"OverlayMixin",
|
||||
"audio.AudioChannelMixin",
|
||||
"audio.AudioStreamMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user