Compare commits
2
Commits
380ea5acd6
...
19d28f718b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19d28f718b | ||
|
|
2619d9ff3d |
@@ -5,6 +5,7 @@ import eu.lixko.ts3j.client.android.audio.AndroidAudioBackend
|
||||
import eu.lixko.ts3j.client.android.audio.AudioRoute
|
||||
import eu.lixko.ts3j.client.audio.AudioDevice
|
||||
import eu.lixko.ts3j.client.config.Settings
|
||||
import eu.lixko.ts3j.client.sound.SoundEvent
|
||||
import eu.lixko.ts3j.client.sound.SoundNotifier
|
||||
import eu.lixko.ts3j.client.sound.SoundPack
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -53,6 +54,8 @@ class AudioSetup internal constructor(private val context: Context, private val
|
||||
settings.save()
|
||||
}
|
||||
|
||||
fun playTestSound() = sounds.preview(SoundEvent.SPECIAL_SOUND_TEST)
|
||||
|
||||
/** Unpacks the packs the app ships with, when this version has not yet. */
|
||||
fun installSoundPacks() {
|
||||
if (!SoundPackInstaller(context).install()) return
|
||||
|
||||
@@ -2,6 +2,7 @@ package eu.lixko.ts3j.client.android
|
||||
|
||||
import eu.lixko.ts3j.client.audio.AudioBackend
|
||||
import eu.lixko.ts3j.client.audio.InputLevel
|
||||
import eu.lixko.ts3j.client.audio.MicrophoneTest
|
||||
import eu.lixko.ts3j.client.audio.VoiceInput
|
||||
import eu.lixko.ts3j.client.config.Settings
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -17,36 +18,41 @@ data class MicLevel(
|
||||
|
||||
/**
|
||||
* The level meter behind the voice-activation settings. While connected it listens to the
|
||||
* connection's own microphone, so it shows exactly what gets sent; otherwise it runs a test
|
||||
* microphone of its own for as long as someone watches.
|
||||
* connection's own microphone, so it shows exactly what gets sent, until a microphone test
|
||||
* takes over with a capture chain of its own.
|
||||
*/
|
||||
class LevelMonitor(private val settings: Settings, private val audio: AudioBackend) {
|
||||
class LevelMonitor(private val settings: Settings, audio: AudioBackend) {
|
||||
|
||||
private val _level = MutableStateFlow(MicLevel())
|
||||
val level: StateFlow<MicLevel> = _level
|
||||
|
||||
private val _testing = MutableStateFlow(false)
|
||||
val testing: StateFlow<Boolean> = _testing
|
||||
private val _loopback = MutableStateFlow(false)
|
||||
val loopback: StateFlow<Boolean> = _loopback
|
||||
private val _testFailed = MutableStateFlow(false)
|
||||
/** Whether the last test could not get hold of the microphone. */
|
||||
val testFailed: StateFlow<Boolean> = _testFailed
|
||||
|
||||
private val test = MicrophoneTest(audio, ::onLevel) { _level.value = _level.value.copy(transmitting = it) }
|
||||
|
||||
private var mayTest = false
|
||||
/** The connection's microphone, while someone watches. */
|
||||
private var source: VoiceInput? = null
|
||||
/** The connection's microphone, when that is the one listened to. */
|
||||
private var borrowed: VoiceInput? = null
|
||||
private var test: VoiceInput? = null
|
||||
|
||||
/**
|
||||
* Starts or stops listening.
|
||||
*
|
||||
* @param source the connection's microphone while it captures, else null
|
||||
* @param busy whether a connection is under way, which will bring its own microphone
|
||||
*/
|
||||
@Synchronized
|
||||
fun update(watching: Boolean, source: VoiceInput?, busy: Boolean, mayCapture: Boolean) {
|
||||
when {
|
||||
!watching -> release()
|
||||
source != null -> if (borrowed !== source) {
|
||||
release()
|
||||
source.setLevelListener(::onLevel)
|
||||
borrowed = source
|
||||
}
|
||||
busy || !mayCapture -> release()
|
||||
test == null -> startTest()
|
||||
}
|
||||
fun update(watching: Boolean, source: VoiceInput?, mayCapture: Boolean) {
|
||||
this.source = source.takeIf { watching }
|
||||
mayTest = watching && mayCapture
|
||||
if (!mayTest) stopTest()
|
||||
follow()
|
||||
}
|
||||
|
||||
/** The own microphone's transmit state, which the connection reports. */
|
||||
@@ -54,34 +60,54 @@ class LevelMonitor(private val settings: Settings, private val audio: AudioBacke
|
||||
if (borrowed != null) _level.value = _level.value.copy(transmitting = transmitting)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setTesting(on: Boolean) {
|
||||
if (!on) {
|
||||
stopTest()
|
||||
} else if (mayTest && !test.isRunning) {
|
||||
val started = test.start(settings)
|
||||
_testFailed.value = !started
|
||||
if (started) {
|
||||
test.configure { it.setMode(testMode()) }
|
||||
test.setOutputDevice(settings.outputDevice.get())
|
||||
_testing.value = true
|
||||
}
|
||||
}
|
||||
follow()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setLoopback(on: Boolean) {
|
||||
if (!test.isRunning) return
|
||||
test.setLoopback(on)
|
||||
_loopback.value = on
|
||||
}
|
||||
|
||||
/** Puts changed voice settings into effect on the test microphone. */
|
||||
@Synchronized
|
||||
fun apply() {
|
||||
test?.let {
|
||||
test.setOutputDevice(settings.outputDevice.get())
|
||||
test.configure {
|
||||
it.apply(settings)
|
||||
it.setMode(testMode())
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTest() {
|
||||
release()
|
||||
val input = audio.createInput(settings)
|
||||
input.setMode(testMode())
|
||||
input.setLevelListener(::onLevel)
|
||||
input.setTalkListener { _level.value = _level.value.copy(transmitting = it) }
|
||||
try {
|
||||
input.start()
|
||||
} catch (e: RuntimeException) {
|
||||
return // no microphone to be had; the meter stays idle
|
||||
}
|
||||
test = input
|
||||
private fun stopTest() {
|
||||
if (!test.isRunning) return
|
||||
test.stop()
|
||||
_testing.value = false
|
||||
_loopback.value = false
|
||||
_level.value = MicLevel()
|
||||
}
|
||||
|
||||
private fun release() {
|
||||
/** Listens to the connection's microphone unless the test has the meter. */
|
||||
private fun follow() {
|
||||
val wanted = source.takeUnless { test.isRunning }
|
||||
if (borrowed === wanted) return
|
||||
borrowed?.setLevelListener(null)
|
||||
borrowed = null
|
||||
test?.stop()
|
||||
test = null
|
||||
wanted?.setLevelListener(::onLevel)
|
||||
borrowed = wanted
|
||||
_level.value = MicLevel()
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,9 @@ class SessionController internal constructor(
|
||||
}
|
||||
|
||||
val micLevel: StateFlow<MicLevel> = levels.level
|
||||
val micTesting: StateFlow<Boolean> = levels.testing
|
||||
val micLoopback: StateFlow<Boolean> = levels.loopback
|
||||
val micTestFailed: StateFlow<Boolean> = levels.testFailed
|
||||
@Volatile
|
||||
private var watchingLevel = false
|
||||
|
||||
@@ -337,10 +340,17 @@ class SessionController internal constructor(
|
||||
}
|
||||
|
||||
private fun updateLevels() {
|
||||
val owner = _micOwner.value
|
||||
levels.update(watchingLevel, owner?.microphone, owner?.isBusy == true, hasMicPermission())
|
||||
levels.update(watchingLevel, _micOwner.value?.microphone, hasMicPermission())
|
||||
}
|
||||
|
||||
/** Runs the microphone test; it ends by itself when the level is no longer watched. */
|
||||
fun testMicrophone(on: Boolean) {
|
||||
updateLevels() // the permission may have just been granted
|
||||
levels.setTesting(on)
|
||||
}
|
||||
|
||||
fun setMicLoopback(on: Boolean) = levels.setLoopback(on)
|
||||
|
||||
// ---- notifications ----
|
||||
|
||||
/** Shows a private chat a notification was tapped for. */
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package eu.lixko.ts3j.client.android.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION
|
||||
@@ -7,6 +8,7 @@ import android.provider.Settings.canDrawOverlays
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
@@ -15,13 +17,16 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableFloatStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.CornerRadius
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
@@ -102,9 +107,7 @@ fun SoundSettingsScreen(settings: Settings, sessions: SessionController, audio:
|
||||
if (inputMode == Settings.InputMode.PUSH_TO_TALK) FloatingButtonToggle(audio)
|
||||
val gated = inputMode == Settings.InputMode.VOICE_ACTIVATION && vadMode != Settings.VadMode.AUTOMATIC
|
||||
LevelMeter(level, threshold.takeIf { gated })
|
||||
if (!level.live) {
|
||||
Hint(if (sessions.hasMicPermission()) "The microphone is not available right now." else "Allow the microphone to see its level.")
|
||||
}
|
||||
MicrophoneTestControls(sessions, level.transmitting)
|
||||
|
||||
if (inputMode == Settings.InputMode.VOICE_ACTIVATION) {
|
||||
Section("Voice activation")
|
||||
@@ -156,11 +159,16 @@ fun SoundSettingsScreen(settings: Settings, sessions: SessionController, audio:
|
||||
if (packs.isEmpty()) {
|
||||
Hint("This build of the app came without sound packs.")
|
||||
} else {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(Modifier.weight(1f)) {
|
||||
Picker("Sound pack", packs.map { it.id() to it.name() }, pack) { id ->
|
||||
packs.firstOrNull { it.id() == id }?.let(audio::setSoundPack)
|
||||
pack = id
|
||||
}
|
||||
}
|
||||
TextButton(audio::playTestSound, Modifier.padding(end = 8.dp)) { Text("Test") }
|
||||
}
|
||||
}
|
||||
Level("Volume", settings.soundVolume.get().toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
|
||||
change(settings.soundVolume, it.toDouble())
|
||||
}
|
||||
@@ -208,6 +216,35 @@ private val VAD_MODES = listOf(
|
||||
Settings.VadMode.HYBRID to "Hybrid",
|
||||
)
|
||||
|
||||
/** Runs the capture chain as a connection would, optionally playing back what would be sent. */
|
||||
@Composable
|
||||
private fun MicrophoneTestControls(sessions: SessionController, transmitting: Boolean) {
|
||||
val testing by sessions.micTesting.collectAsStateWithLifecycle()
|
||||
val loopback by sessions.micLoopback.collectAsStateWithLifecycle()
|
||||
val failed by sessions.micTestFailed.collectAsStateWithLifecycle()
|
||||
var denied by remember { mutableStateOf(false) }
|
||||
val allow = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
denied = !granted
|
||||
if (granted) sessions.testMicrophone(true)
|
||||
}
|
||||
Row(Modifier.padding(horizontal = 16.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
OutlinedButton({
|
||||
if (testing || sessions.hasMicPermission()) sessions.testMicrophone(!testing)
|
||||
else allow.launch(Manifest.permission.RECORD_AUDIO)
|
||||
}) { Text(if (testing) "Stop Test" else "Begin Test") }
|
||||
if (testing) {
|
||||
Text(if (transmitting) "Transmitting" else "Not transmitting", Modifier.padding(start = 16.dp),
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
if (testing) {
|
||||
Toggle("Hear myself", loopback, sessions::setMicLoopback)
|
||||
if (loopback) Hint("Use headphones to avoid feedback.")
|
||||
}
|
||||
if (failed) Hint("Capture device unavailable")
|
||||
if (denied) Hint("Allow the microphone to test it.")
|
||||
}
|
||||
|
||||
/** The push-to-talk button over other apps, which first needs the user to allow the app to draw there. */
|
||||
@Composable
|
||||
private fun FloatingButtonToggle(audio: AudioSetup) {
|
||||
|
||||
+17
-26
@@ -1,26 +1,24 @@
|
||||
package eu.lixko.ts3j.client.ui.options;
|
||||
package eu.lixko.ts3j.client.audio;
|
||||
|
||||
import eu.lixko.ts3j.client.audio.AudioBackend;
|
||||
import eu.lixko.ts3j.client.audio.AudioPlayback;
|
||||
import eu.lixko.ts3j.client.audio.VoiceInput;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Drives the settings dialog's microphone test from a real capture chain.
|
||||
* The settings' microphone test, driven from a real capture chain.
|
||||
*
|
||||
* <p>The dialog runs its own {@link VoiceInput} rather than borrowing the connected
|
||||
* one, whose listeners belong to the connection. Because it is the same class that feeds
|
||||
* <p>It runs its own {@link VoiceInput} rather than borrowing the connected one, whose
|
||||
* listeners belong to the connection. Because it is the same class that feeds
|
||||
* the server, the level and the gate shown here are exactly what would be transmitted —
|
||||
* pre-processing, voice detection, hangover and pre-roll included.
|
||||
*
|
||||
* <p>Loopback is optional: when enabled, the frames that would be sent are played back
|
||||
* locally so you can hear precisely what the other side would.
|
||||
*
|
||||
* <p>Level and transmit changes are reported on the capture thread.
|
||||
*/
|
||||
final class MicrophoneTest {
|
||||
public final class MicrophoneTest {
|
||||
|
||||
/** Frames buffered for loopback before the oldest is dropped (~100 ms). */
|
||||
private static final int LOOPBACK_QUEUE_FRAMES = 5;
|
||||
@@ -38,14 +36,14 @@ final class MicrophoneTest {
|
||||
private Thread loopbackThread;
|
||||
private String outputDevice = "";
|
||||
|
||||
MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> onTransmitting) {
|
||||
public MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> onTransmitting) {
|
||||
this.audio = audio;
|
||||
this.onLevel = onLevel;
|
||||
this.onTransmitting = onTransmitting;
|
||||
}
|
||||
|
||||
/** Whether a capture chain is currently running. */
|
||||
boolean isRunning() {
|
||||
public boolean isRunning() {
|
||||
return mic != null;
|
||||
}
|
||||
|
||||
@@ -54,11 +52,11 @@ final class MicrophoneTest {
|
||||
*
|
||||
* @return false if the capture device could not be opened
|
||||
*/
|
||||
boolean start(Settings settings) {
|
||||
public boolean start(Settings settings) {
|
||||
stop();
|
||||
VoiceInput input = audio.createInput(settings);
|
||||
input.setLevelListener(db -> SwingUtilities.invokeLater(() -> onLevel.accept(db)));
|
||||
input.setTalkListener(talking -> SwingUtilities.invokeLater(() -> onTransmitting.accept(talking)));
|
||||
input.setLevelListener(onLevel);
|
||||
input.setTalkListener(onTransmitting);
|
||||
input.setMonitorListener(this::enqueueForLoopback);
|
||||
this.outputDevice = settings.outputDevice.get();
|
||||
try {
|
||||
@@ -74,7 +72,7 @@ final class MicrophoneTest {
|
||||
* Stops the capture chain. The caller owns the UI reset: doing it here would race with
|
||||
* a restart, whose own state has already been put on screen.
|
||||
*/
|
||||
void stop() {
|
||||
public void stop() {
|
||||
setLoopback(false);
|
||||
if (mic != null) {
|
||||
mic.stop();
|
||||
@@ -83,17 +81,17 @@ final class MicrophoneTest {
|
||||
}
|
||||
|
||||
/** Applies a change to the test chain, if it is running. */
|
||||
void configure(Consumer<VoiceInput> change) {
|
||||
public void configure(Consumer<VoiceInput> change) {
|
||||
VoiceInput m = mic;
|
||||
if (m != null) change.accept(m);
|
||||
}
|
||||
|
||||
/** The playback device to loop back through; takes effect on the next enable. */
|
||||
void setOutputDevice(String deviceId) {
|
||||
public void setOutputDevice(String deviceId) {
|
||||
this.outputDevice = deviceId == null ? "" : deviceId;
|
||||
}
|
||||
|
||||
void setLoopback(boolean enabled) {
|
||||
public void setLoopback(boolean enabled) {
|
||||
if (enabled == loopbackEnabled) {
|
||||
return;
|
||||
}
|
||||
@@ -157,14 +155,7 @@ final class MicrophoneTest {
|
||||
/** Converts float samples in [-1, 1] to 16-bit little-endian PCM. */
|
||||
static byte[] toPcm16(float[] samples) {
|
||||
byte[] pcm = new byte[samples.length * 2];
|
||||
for (int i = 0; i < samples.length; i++) {
|
||||
float f = samples[i];
|
||||
if (f > 1f) f = 1f;
|
||||
else if (f < -1f) f = -1f;
|
||||
int s = Math.round(f * 32767f);
|
||||
pcm[2 * i] = (byte) (s & 0xFF);
|
||||
pcm[2 * i + 1] = (byte) ((s >> 8) & 0xFF);
|
||||
}
|
||||
Pcm16.encode(samples, samples.length, pcm);
|
||||
return pcm;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.lixko.ts3j.client.ui.options;
|
||||
package eu.lixko.ts3j.client.audio;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
+5
-1
@@ -4,6 +4,7 @@ import com.formdev.flatlaf.util.ScaledEmptyBorder;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import eu.lixko.ts3j.client.audio.AudioBackend;
|
||||
import eu.lixko.ts3j.client.audio.InputLevel;
|
||||
import eu.lixko.ts3j.client.audio.MicrophoneTest;
|
||||
import eu.lixko.ts3j.client.audio.VoiceInput;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
import eu.lixko.ts3j.client.hotkey.Hotkey;
|
||||
@@ -24,6 +25,7 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JRadioButton;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.JToggleButton;
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
@@ -67,7 +69,9 @@ final class VoiceActivationSection {
|
||||
VoiceActivationSection(Settings settings, AudioBackend audio, VoiceInput liveMic, HotkeyService hotkeys,
|
||||
HotkeysPanel hotkeysPanel, Supplier<Settings> audioSnapshot) {
|
||||
this.liveMic = liveMic;
|
||||
this.micTest = new MicrophoneTest(audio, this::onTestLevel, this::onTestTalking);
|
||||
this.micTest = new MicrophoneTest(audio,
|
||||
db -> SwingUtilities.invokeLater(() -> onTestLevel(db)),
|
||||
talking -> SwingUtilities.invokeLater(() -> onTestTalking(talking)));
|
||||
this.hotkeys = hotkeys;
|
||||
this.hotkeysPanel = hotkeysPanel;
|
||||
this.audioSnapshot = audioSnapshot;
|
||||
|
||||
Reference in New Issue
Block a user