Skip to content

Commit

Permalink
fix: Removed mix to mono function which causes crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaxii authored and kristofbolyai committed Sep 24, 2024
1 parent 989ddb6 commit 5c72f10
Showing 1 changed file with 8 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
*/
package com.wynnvp.wynncraftvp.sound.player;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.sound.sampled.AudioFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.phys.Vec3;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.AL11;

import java.nio.ByteBuffer;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class OpenAlPlayer {
private int sourceID;
private final ExecutorService executorService;
Expand All @@ -39,7 +38,8 @@ public OpenAlPlayer() {
currentSpeaker = new CurrentSpeaker();
this.buffers = new int[3000];
executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {});
executorService.execute(() -> {
});

createOpenAL();

Expand Down Expand Up @@ -78,7 +78,7 @@ private void writeSync(AudioData audioData) {
}

// Convert the audio data to mono
ByteBuffer monoData = convertToMono(audioData);
ByteBuffer monoData = audioData.byteBuffer;

// Always use a mono format (16-bit PCM)
AL11.alBufferData(
Expand All @@ -88,45 +88,6 @@ private void writeSync(AudioData audioData) {
bufferIndex = (bufferIndex + 1) % buffers.length;
}

// Method to convert stereo ByteBuffer to mono
private ByteBuffer convertToMono(AudioData audioData) {
AudioFormat format = audioData.audioFormat;
ByteBuffer originalData = audioData.byteBuffer;

int channels = format.getChannels();
int sampleSizeInBits = format.getSampleSizeInBits();
boolean bigEndian = format.isBigEndian();

if (channels == 1) {
// Already mono, no conversion needed
return originalData;
}

// We are only handling 16-bit PCM audio here
if (sampleSizeInBits != 16) {
throw new IllegalArgumentException("Unsupported sample size: " + sampleSizeInBits + " bits.");
}

// Create a new ByteBuffer for mono audio (half the size of stereo buffer)
ByteBuffer monoData = ByteBuffer.allocate(originalData.remaining() / 2)
.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);

while (originalData.remaining() >= 4) {
// For 16-bit stereo PCM, read left and right samples (2 bytes each)
short leftSample = originalData.getShort();
short rightSample = originalData.getShort();

// Average the left and right channels to get mono
short monoSample = (short) ((leftSample + rightSample) / 2);

// Write the mono sample
monoData.putShort(monoSample);
}

// Flip the buffer to prepare for reading
monoData.flip();
return monoData;
}

private void startPlayingIfStoppedSync() {
if (isStopped()) {
Expand Down

0 comments on commit 5c72f10

Please sign in to comment.