Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void playHaptic(Identifier haptic) {
if (!settings().enabled || playHapticConsumer == null) return;

getSoundData(haptic, hapticBufferLibrary.getCompleteBuffer(haptic))
.thenAccept(playHapticConsumer)
.thenAccept(this::acceptOnClientThread)
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
Expand All @@ -66,13 +66,21 @@ public void playHaptic(SoundEvent sound) {
Identifier soundId = location.withPrefix("sounds/").withSuffix(".ogg");

getSoundData(soundId, bufferLibrary.getCompleteBuffer(soundId))
.thenAccept(playHapticConsumer)
.thenAccept(this::acceptOnClientThread)
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
}

// uncached buffers load off-thread, but the driver must only be used from the client thread
private void acceptOnClientThread(CompleteSoundData data) {
Minecraft.getInstance().execute(() -> {
Consumer<CompleteSoundData> consumer = this.playHapticConsumer;
if (consumer != null) consumer.accept(data);
});
}

public void acceptPlayHaptic(Consumer<CompleteSoundData> consumer) {
this.playHapticConsumer = consumer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.lang.foreign.ValueLayout;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -303,11 +304,11 @@ private void updateDualSense() {
}

private void updateHDHaptic() {
for (int i = 0; i < dualsenseAudioHandles.size(); i++) {
AudioStreamHandle handle = dualsenseAudioHandles.get(i);
for (Iterator<AudioStreamHandle> it = dualsenseAudioHandles.iterator(); it.hasNext(); ) {
AudioStreamHandle handle = it.next();
if (handle.isTimedOut()) {
handle.close();
dualsenseAudioHandles.remove(handle);
it.remove();
} else {
handle.tick();
}
Expand Down Expand Up @@ -357,7 +358,9 @@ private void playHaptic(CompleteSoundData sound) {
break;
}
}
int length = sound.audio().length / spec.frequency() / spec.channels() / byteSs * 20;
// in ticks, rounded up so a clip shorter than a second isn't 0
int byteRate = spec.frequency() * spec.channels() * byteSs;
int length = Math.max(1, (int) Math.ceil(sound.audio().length * 20.0 / byteRate));

if (handle != null) {
handle.queueAudio(sound.audio(), length);
Expand Down