Compress stream audio before Opus using radio settings

This commit is contained in:
evgeny committed 2026-10-13 13:39:12 +02:00
1 parent 149e4d43e5
commit 6bd14c53b5
3 files changed
+64 -7

No files matched your search

@@ -12,6 +12,7 @@ import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.runBlocking
import java.net.Inet4Address
import java.net.Inet6Address
@@ -34,6 +35,8 @@ data class VoiceSettings(
val targetLevelDb: Int = -3
)
data class RadioCompressorSettings(val enabled: Boolean = true, val maxGainDb: Int = 15, val riseRate: Int = 6)
class ConfigProvider(private val context: Context) {
private val nodePrivateKey = stringPreferencesKey("node_private_key")
private val nodePublicKey = stringPreferencesKey("node_public_key")
@@ -88,6 +91,12 @@ class ConfigProvider(private val context: Context) {
private val radioCompressorEnabledKey = intPreferencesKey("radio_compressor_enabled")
private val radioCompressorMaxGainDbKey = intPreferencesKey("radio_compressor_max_gain_db")
// Трансляция наблюдает те же настройки TX, что и рация; история PCM у каждого владельца своя.
val radioCompressorSettings: Flow<RadioCompressorSettings> = context.dataStore.data.map { prefs ->
RadioCompressorSettings((prefs[radioCompressorEnabledKey] ?: 1) != 0,
prefs[radioCompressorMaxGainDbKey] ?: 15, prefs[chatCompressorRiseRate] ?: 6)
}.distinctUntilChanged()
private fun keysFilePath(): String {
val dir = runBlocking { context.dataStore.data.first()[dbPath] } ?: context.filesDir.absolutePath
return "$dir/keys.conf"
@@ -209,7 +209,8 @@ object NativeLib {
fun streamStop(id: Long) { nativeStreamStop(id) }
fun streamPull(id: Long): ByteArray? = nativeStreamPull(id)
fun streamOpusCreate(encode: Boolean): Long = nativeStreamOpusCreate(encode)
fun streamOpusEncode(handle: Long, pcm: ShortArray): ByteArray? = nativeStreamOpusEncode(handle, pcm)
fun streamOpusEncode(handle: Long, pcm: ShortArray, compressor: RadioCompressorSettings): ByteArray? =
nativeStreamOpusEncode(handle, pcm, compressor.enabled, compressor.maxGainDb, compressor.riseRate)
fun streamOpusDecode(handle: Long, packet: ByteArray): ShortArray? = nativeStreamOpusDecode(handle, packet)
fun streamOpusClose(handle: Long) { nativeStreamOpusClose(handle) }
private external fun nativeStreamOpen(id: Long, group: Long, source: Long, width: Int, height: Int, rotation: Int,
@@ -220,7 +221,7 @@ object NativeLib {
private external fun nativeStreamStop(id: Long)
private external fun nativeStreamPull(id: Long): ByteArray?
private external fun nativeStreamOpusCreate(encode: Boolean): Long
private external fun nativeStreamOpusEncode(handle: Long, pcm: ShortArray): ByteArray?
private external fun nativeStreamOpusEncode(handle: Long, pcm: ShortArray, compressor: Boolean, maxGainDb: Int, riseRate: Int): ByteArray?
private external fun nativeStreamOpusDecode(handle: Long, packet: ByteArray): ShortArray?
private external fun nativeStreamOpusClose(handle: Long)