Skip to content

Commit

Permalink
fix(client): use processed samples (if available) in settings screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Feb 20, 2023
1 parent 553e864 commit 2711053
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package su.plo.voice.api.client.event.audio.capture;

import lombok.Getter;
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;
import su.plo.voice.api.client.audio.capture.AudioCapture;
import su.plo.voice.api.client.audio.device.InputDevice;
import su.plo.voice.api.event.Event;

/**
* This event is fired once a samples was processed by {@link AudioCapture}
*/
public final class AudioCaptureProcessedEvent implements Event {

@Getter
private final AudioCapture capture;
@Getter
private final InputDevice device;
@Getter
private final short[] samples;
@Getter
private final short[] samplesProcessed;

public AudioCaptureProcessedEvent(@NonNull AudioCapture capture,
@NonNull InputDevice device,
short[] samples,
short[] samplesProcessed) {
this.capture = capture;
this.device = device;
this.samples = samples;
this.samplesProcessed = samplesProcessed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import su.plo.voice.api.client.audio.device.InputDevice;
import su.plo.voice.api.client.connection.ServerConnection;
import su.plo.voice.api.client.connection.ServerInfo;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureEvent;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureInitializeEvent;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureStartEvent;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureStopEvent;
import su.plo.voice.api.client.event.audio.capture.*;
import su.plo.voice.api.client.socket.UdpClient;
import su.plo.voice.api.encryption.Encryption;
import su.plo.voice.api.encryption.EncryptionException;
Expand Down Expand Up @@ -213,8 +210,7 @@ private void run() {
}

AudioCaptureEvent captureEvent = new AudioCaptureEvent(this, device.get(), samples);
voiceClient.getEventBus().call(captureEvent);
if (captureEvent.isCancelled()) continue;
if (!voiceClient.getEventBus().call(captureEvent)) continue;

ClientActivation parentActivation = activations.getParentActivation().get();

Expand All @@ -234,6 +230,13 @@ private void run() {
sendVoiceEndPacket(activation);
}
});

voiceClient.getEventBus().call(new AudioCaptureProcessedEvent(
this,
device.get(),
samples,
null
));
continue;
}

Expand Down Expand Up @@ -274,6 +277,13 @@ private void run() {
processActivation(device.get(), parentActivation, ClientActivation.Result.END, null, encoded);
}
}

voiceClient.getEventBus().call(new AudioCaptureProcessedEvent(
this,
device.get(),
samples,
encoded.monoProcessed
));
} catch (InterruptedException ignored) {
break;
}
Expand Down Expand Up @@ -313,12 +323,14 @@ private void processActivation(@NotNull InputDevice device,
(filter) -> (filter instanceof StereoToMonoFilter) ||
(filter instanceof NoiseSuppressionFilter)
);
encoded.stereoProcessed = processedSamples;
encoded.stereo = encode(stereoEncoder, processedSamples);
} else if (!isStereo && encoded.mono == null) {
short[] processedSamples = new short[samples.length];
System.arraycopy(samples, 0, processedSamples, 0, samples.length);

processedSamples = device.processFilters(processedSamples);
encoded.monoProcessed = processedSamples;
encoded.mono = encode(monoEncoder, processedSamples);
}
}
Expand Down Expand Up @@ -402,6 +414,8 @@ private long getSequenceNumber(@NotNull ClientActivation activation) {
static class EncodedCapture {

private byte[] mono;
private short[] monoProcessed;
private byte[] stereo;
private short[] stereoProcessed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import su.plo.voice.api.client.audio.device.DeviceException;
import su.plo.voice.api.client.audio.source.LoopbackSource;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureEvent;
import su.plo.voice.api.client.event.audio.capture.AudioCaptureProcessedEvent;
import su.plo.voice.api.event.EventSubscribe;
import su.plo.voice.api.util.AudioUtil;
import su.plo.voice.client.audio.filter.NoiseSuppressionFilter;
Expand All @@ -17,6 +18,9 @@
import su.plo.voice.client.event.gui.MicrophoneTestStartedEvent;
import su.plo.voice.client.event.gui.MicrophoneTestStoppedEvent;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@RequiredArgsConstructor
public final class MicrophoneTestController {

Expand Down Expand Up @@ -68,13 +72,26 @@ public void stop() {

@EventSubscribe
public void onAudioCapture(@NotNull AudioCaptureEvent event) {
short[] samples = new short[event.getSamples().length];
System.arraycopy(event.getSamples(), 0, samples, 0, event.getSamples().length);
samples = event.getDevice().processFilters(
samples,
(filter) -> isStereo() &&
((filter instanceof StereoToMonoFilter) || (filter instanceof NoiseSuppressionFilter))
);
if (source != null) {
event.setSendEnd(true);
}
}

@EventSubscribe
public void onAudioCaptureProcessed(@NotNull AudioCaptureProcessedEvent event) {
short[] samples;
if (event.getSamplesProcessed() != null) {
samples = event.getSamplesProcessed();
} else {
samples = new short[event.getSamples().length];
System.arraycopy(event.getSamples(), 0, samples, 0, event.getSamples().length);

samples = event.getDevice().processFilters(
samples,
(filter) -> isStereo() &&
((filter instanceof StereoToMonoFilter) || (filter instanceof NoiseSuppressionFilter))
);
}

this.microphoneDB = AudioUtil.calculateHighestAudioLevel(samples);
if (microphoneDB > highestDB) {
Expand All @@ -93,7 +110,6 @@ public void onAudioCapture(@NotNull AudioCaptureEvent event) {
}

if (source != null) {
event.setSendEnd(true);
source.write(samples);
}
}
Expand Down

0 comments on commit 2711053

Please sign in to comment.