Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushmead committed Dec 28, 2024
1 parent e9b3f40 commit bb32d41
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package dev.imabad.theatrical.client.sound;

import com.google.common.base.Preconditions;
import com.mojang.blaze3d.audio.Channel;
import dev.imabad.theatrical.client.sound.mic.MicrophoneManager;
import io.netty.buffer.ByteBuf;
import net.labymod.opus.OpusCodec;
import net.minecraft.client.Minecraft;
import net.minecraft.client.sounds.AudioStream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -16,19 +13,11 @@
import javax.sound.sampled.AudioSystem;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.InvalidMarkException;
import java.nio.ShortBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Executor;

public class OpusStreamedAudioStream implements AudioStream {
private static final AudioFormat STEREO_16 = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, MicrophoneManager.SAMPLE_RATE, 16, 1, MicrophoneManager.FRAME_SIZE, MicrophoneManager.SAMPLE_RATE, false);
private final Queue<ByteBuffer> buffers = new ArrayDeque<>(2);

private final OpusCodec opusCodec = OpusCodec.newBuilder()
.withFrameSize(MicrophoneManager.FRAME_SIZE)
.withChannels(1)
Expand All @@ -52,9 +41,6 @@ public class OpusStreamedAudioStream implements AudioStream {

void push(byte[] input) {
byte[] bytes = opusCodec.decodeFrame(input);
// try {
// Files.write(Path.of(Minecraft.getInstance().gameDirectory.getPath(), "raw_audio_data_received"), bytes, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
// } catch (IOException e) {}
if(inputStream == null || inputStream.isClosed()){
createStreams();
}
Expand Down Expand Up @@ -94,21 +80,4 @@ public ByteBuffer read(int size) throws IOException {
public void close() throws IOException {
audioInputStream.close();
}

public boolean ready(){
return true;
}

public boolean isEmpty() {
return buffers.isEmpty();
}
private byte[] shortToByteTwiddle(final short[] input) {
final int len = input.length;
final byte[] buffer = new byte[len * 2];
for (int i = 0; i < len; i++) {
buffer[(i * 2) + 1] = (byte) (input[i]);
buffer[(i * 2)] = (byte) (input[i] >> 8);
}
return buffer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public synchronized void pushAudio(byte[] buffer){
if(opusStream == null){
opusStream = new OpusStreamedAudioStream();
}
boolean isExhausted = opusStream.isEmpty();
opusStream.push(buffer);
if(isExhausted && sound != null && sound.stream == opusStream && opusStream.channel != null && opusStream.executor != null){
if(sound != null && sound.stream == opusStream && opusStream.channel != null && opusStream.executor != null){
OpusStreamedAudioStream actualStream = sound.stream;
opusStream.executor.execute(() -> {
if(actualStream.channel != null){
Expand All @@ -32,11 +31,6 @@ public synchronized void pushAudio(byte[] buffer){
});
}
}

public boolean isReady(){
return opusStream.ready();
}

public void playAudio(BlockPos pos){
SoundManager soundManager = Minecraft.getInstance().getSoundManager();
if(sound != null && sound.stream != opusStream){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public void write(FriendlyByteBuf buf) {
@Override
public void handle(NetworkManager.PacketContext context) {
SpeakerManager.getSound(sourceId).pushAudio(data);
if(SpeakerManager.getSound(sourceId).isReady()) {
SpeakerManager.getSound(sourceId).playAudio(pos);
}
SpeakerManager.getSound(sourceId).playAudio(pos);
}
}

0 comments on commit bb32d41

Please sign in to comment.