summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2025-03-18 00:13:46 -0300
committerSoniEx2 <endermoneymod@gmail.com>2025-03-18 00:13:46 -0300
commitfec48e3c41fc40be626182653fc0c4726bd91c4a (patch)
treeb0e77b1ae8453b8c05692b070dd92ba4733a5d97
parent80c81c6c63155065dd5a406fce36206edbda23df (diff)
Use native audio by default
-rw-r--r--gradle.properties2
-rw-r--r--src/client/kotlin/space/autistic/radio/client/fmsim/FmFullThread.kt37
-rw-r--r--src/client/kotlin/space/autistic/radio/client/sound/ReceiverAudioStream.kt8
3 files changed, 43 insertions, 4 deletions
diff --git a/gradle.properties b/gradle.properties
index dc33772..72b9c28 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -10,7 +10,7 @@ loader_version=0.16.10
 fabric_kotlin_version=1.13.1+kotlin.2.1.10
 
 # Mod Properties
-mod_version=1.0.1
+mod_version=1.0.2
 maven_group=space.autistic.radio
 archives_base_name=pirate-radio
 
diff --git a/src/client/kotlin/space/autistic/radio/client/fmsim/FmFullThread.kt b/src/client/kotlin/space/autistic/radio/client/fmsim/FmFullThread.kt
index 0a6184f..b9af938 100644
--- a/src/client/kotlin/space/autistic/radio/client/fmsim/FmFullThread.kt
+++ b/src/client/kotlin/space/autistic/radio/client/fmsim/FmFullThread.kt
@@ -1,16 +1,17 @@
 package space.autistic.radio.client.fmsim
 
+import net.minecraft.util.math.MathHelper
 import org.joml.Vector2f
 import space.autistic.radio.client.PirateRadioClient
 import space.autistic.radio.client.complex.cmul
 import space.autistic.radio.client.sound.PirateRadioSoundInstance
 import space.autistic.radio.client.sound.ReceiverAudioStream
 import space.autistic.radio.entity.DisposableTransmitterEntity
+import java.nio.ByteBuffer
 import java.nio.ByteOrder
 import java.nio.FloatBuffer
 import java.util.concurrent.ArrayBlockingQueue
 import kotlin.math.max
-import kotlin.math.min
 
 object FmFullThread : Runnable {
     class FmTask(
@@ -33,6 +34,8 @@ object FmFullThread : Runnable {
 
     // 3 seconds
     private const val REPEAT_TIMEOUT = 8000 * 3
+    // default to 0.05s (1/20)
+    private val bufferSize = System.getProperty("space.autistic.radio.buffer.size", "").toIntOrNull() ?: 2400
 
     override fun run() {
         var currentTask = EMPTY_TASK
@@ -51,6 +54,15 @@ object FmFullThread : Runnable {
 
         val demodulator = FmFullDemodulator()
 
+        // for native audio only
+        val outputBytes = ByteBuffer.allocate(bufferSize * 2 * 2).order(ByteOrder.LITTLE_ENDIAN)
+        val nativeAudio = if (ReceiverAudioStream.useNativeAudio) {
+            javax.sound.sampled.AudioSystem.getSourceDataLine(ReceiverAudioStream.format)
+                .apply { open(ReceiverAudioStream.format, FmFullThread.bufferSize * 2 * 2) }
+        } else {
+            null
+        }
+
         while (!Thread.interrupted()) {
             currentTask = trackedTransmitterQueue.poll() ?: currentTask
             if (currentTask === EMPTY_TASK) {
@@ -178,7 +190,28 @@ object FmFullThread : Runnable {
                 // TODO stereo pilot
                 // we *want* backpressure
                 // FIXME use bigger buffers?
-                ReceiverAudioStream.bufferQueue.put(FloatBuffer.allocate(audioBuffer.capacity()).put(audioBuffer).clear())
+                if (ReceiverAudioStream.useNativeAudio) {
+                    while (audioBuffer.hasRemaining()) {
+                        if (!outputBytes.hasRemaining()) {
+                            val written = nativeAudio!!.write(outputBytes.array(), 0, outputBytes.capacity())
+                            outputBytes.position(written).compact()
+                            if (written == 0) {
+                                nativeAudio.start()
+                                continue
+                            }
+                        }
+                        val volume = PirateRadioClient.volume
+                        outputBytes.putShort(
+                            (MathHelper.clamp(
+                                (audioBuffer.get() * 32767.5f - 0.5f).toInt(), -32768, 32767
+                            ) * volume * volume / 100).toShort()
+                        )
+                    }
+                } else {
+                    ReceiverAudioStream.bufferQueue.put(
+                        FloatBuffer.allocate(audioBuffer.capacity()).put(audioBuffer).clear()
+                    )
+                }
             }
         }
     }
diff --git a/src/client/kotlin/space/autistic/radio/client/sound/ReceiverAudioStream.kt b/src/client/kotlin/space/autistic/radio/client/sound/ReceiverAudioStream.kt
index 274e727..dc6d9b7 100644
--- a/src/client/kotlin/space/autistic/radio/client/sound/ReceiverAudioStream.kt
+++ b/src/client/kotlin/space/autistic/radio/client/sound/ReceiverAudioStream.kt
@@ -21,6 +21,8 @@ object ReceiverAudioStream : BufferedAudioStream {
         )
     )
 
+    val useNativeAudio = System.getProperty("space.autistic.radio.output", "native") == "native"
+
     private val skipBuffer = FloatBuffer.allocate(FmFullConstants.FFT_DATA_BLOCK_SIZE_LPF_48K_15K_3K1 * 2)
         get() = field.clear()
 
@@ -34,7 +36,11 @@ object ReceiverAudioStream : BufferedAudioStream {
     }
 
     override fun read(channelList: FloatConsumer): Boolean {
-        val buffer = bufferQueue.poll() ?: skipBuffer
+        val buffer = if (useNativeAudio) {
+            bufferQueue.poll() ?: skipBuffer
+        } else {
+            skipBuffer
+        }
         while (buffer.hasRemaining()) {
             channelList.accept(buffer.get())
         }