forked from Blendman974/faceclaw
Debug recording
This commit is contained in:
1 parent
7fcab243a3
commit
2bc53b8c0e
2 files changed
+100
-6
No files matched your search
@@ -25,6 +25,7 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -87,6 +88,8 @@ public class FaceclawVoiceController {
|
|||||||
private int transcriptSampleCount;
|
private int transcriptSampleCount;
|
||||||
private long lastTranscriptDecodeAtMs;
|
private long lastTranscriptDecodeAtMs;
|
||||||
private String lastTranscript = "";
|
private String lastTranscript = "";
|
||||||
|
private volatile boolean saveRecordings;
|
||||||
|
private java.io.ByteArrayOutputStream recordingPcm;
|
||||||
private long queuedPackets;
|
private long queuedPackets;
|
||||||
private long queueDroppedPackets;
|
private long queueDroppedPackets;
|
||||||
private long decodedSamples;
|
private long decodedSamples;
|
||||||
@@ -108,6 +111,11 @@ public class FaceclawVoiceController {
|
|||||||
this.communicator = communicator;
|
this.communicator = communicator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** When true, the decoded mic PCM for each session is saved as a WAV. */
|
||||||
|
public void setSaveRecordings(boolean saveRecordings) {
|
||||||
|
this.saveRecordings = saveRecordings;
|
||||||
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
start("wakeword");
|
start("wakeword");
|
||||||
}
|
}
|
||||||
@@ -184,6 +192,7 @@ public class FaceclawVoiceController {
|
|||||||
stream = keywordSpotter.createStream();
|
stream = keywordSpotter.createStream();
|
||||||
}
|
}
|
||||||
lc3Decoder = new FaceclawLc3Decoder();
|
lc3Decoder = new FaceclawLc3Decoder();
|
||||||
|
recordingPcm = saveRecordings ? new java.io.ByteArrayOutputStream(SAMPLE_RATE * 2 * 4) : null;
|
||||||
if (!startG2Audio()) {
|
if (!startG2Audio()) {
|
||||||
emitStatus("Could not start G2 microphone input.");
|
emitStatus("Could not start G2 microphone input.");
|
||||||
return;
|
return;
|
||||||
@@ -205,6 +214,7 @@ public class FaceclawVoiceController {
|
|||||||
emitStatus("Voice control failed: " + error.getMessage());
|
emitStatus("Voice control failed: " + error.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
stopG2Audio();
|
stopG2Audio();
|
||||||
|
writeRecordingIfAny();
|
||||||
releaseSherpa();
|
releaseSherpa();
|
||||||
releaseLc3();
|
releaseLc3();
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
@@ -214,6 +224,69 @@ public class FaceclawVoiceController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendRecording(short[] pcm, int count) {
|
||||||
|
java.io.ByteArrayOutputStream out = recordingPcm;
|
||||||
|
if (out == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
short s = pcm[i];
|
||||||
|
out.write(s & 0xff);
|
||||||
|
out.write((s >> 8) & 0xff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */
|
||||||
|
private void writeRecordingIfAny() {
|
||||||
|
java.io.ByteArrayOutputStream out = recordingPcm;
|
||||||
|
recordingPcm = null;
|
||||||
|
if (out == null || out.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
byte[] pcmBytes = out.toByteArray();
|
||||||
|
java.io.File dir = new java.io.File(appContext.getExternalFilesDir(null), "voice-recordings");
|
||||||
|
if (!dir.exists() && !dir.mkdirs()) {
|
||||||
|
Log.w(TAG, "could not create voice-recordings dir");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String stamp = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US)
|
||||||
|
.format(new java.util.Date());
|
||||||
|
java.io.File file = new java.io.File(dir, "voice-" + stamp + ".wav");
|
||||||
|
try (java.io.FileOutputStream fos = new java.io.FileOutputStream(file)) {
|
||||||
|
fos.write(buildWavHeader(pcmBytes.length, SAMPLE_RATE, 1, 16));
|
||||||
|
fos.write(pcmBytes);
|
||||||
|
}
|
||||||
|
Log.i(TAG, "saved voice recording " + file.getAbsolutePath()
|
||||||
|
+ " samples=" + (pcmBytes.length / 2)
|
||||||
|
+ " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.length / 2.0 / SAMPLE_RATE));
|
||||||
|
} catch (Throwable t) {
|
||||||
|
Log.w(TAG, "failed to save voice recording", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] buildWavHeader(int pcmBytes, int sampleRate, int channels, int bitsPerSample) {
|
||||||
|
int byteRate = sampleRate * channels * bitsPerSample / 8;
|
||||||
|
int blockAlign = channels * bitsPerSample / 8;
|
||||||
|
int dataSize = pcmBytes;
|
||||||
|
int riffSize = 36 + dataSize;
|
||||||
|
java.nio.ByteBuffer b = java.nio.ByteBuffer.allocate(44).order(java.nio.ByteOrder.LITTLE_ENDIAN);
|
||||||
|
b.put("RIFF".getBytes(StandardCharsets.US_ASCII));
|
||||||
|
b.putInt(riffSize);
|
||||||
|
b.put("WAVE".getBytes(StandardCharsets.US_ASCII));
|
||||||
|
b.put("fmt ".getBytes(StandardCharsets.US_ASCII));
|
||||||
|
b.putInt(16); // PCM fmt chunk size
|
||||||
|
b.putShort((short) 1); // PCM
|
||||||
|
b.putShort((short) channels);
|
||||||
|
b.putInt(sampleRate);
|
||||||
|
b.putInt(byteRate);
|
||||||
|
b.putShort((short) blockAlign);
|
||||||
|
b.putShort((short) bitsPerSample);
|
||||||
|
b.put("data".getBytes(StandardCharsets.US_ASCII));
|
||||||
|
b.putInt(dataSize);
|
||||||
|
return b.array();
|
||||||
|
}
|
||||||
|
|
||||||
private KeywordSpotterConfig buildConfig(File modelDir) {
|
private KeywordSpotterConfig buildConfig(File modelDir) {
|
||||||
return KeywordSpotterConfig.builder()
|
return KeywordSpotterConfig.builder()
|
||||||
.setFeatureConfig(FeatureConfig.builder()
|
.setFeatureConfig(FeatureConfig.builder()
|
||||||
@@ -332,6 +405,9 @@ public class FaceclawVoiceController {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
decodedSamples += count;
|
decodedSamples += count;
|
||||||
|
if (recordingPcm != null) {
|
||||||
|
appendRecording(pcm, count);
|
||||||
|
}
|
||||||
if (mode == VoiceInputMode.CLOUD) {
|
if (mode == VoiceInputMode.CLOUD) {
|
||||||
emitPcm(pcm, count);
|
emitPcm(pcm, count);
|
||||||
} else if (mode == VoiceInputMode.ONBOARD) {
|
} else if (mode == VoiceInputMode.ONBOARD) {
|
||||||
@@ -554,8 +630,9 @@ public class FaceclawVoiceController {
|
|||||||
+ " decodeErrors=" + decodeErrors
|
+ " decodeErrors=" + decodeErrors
|
||||||
+ " wrongArm=" + wrongArmPackets
|
+ " wrongArm=" + wrongArmPackets
|
||||||
+ " audioSec=" + String.format(java.util.Locale.US, "%.1f", decodedSamples / (double) SAMPLE_RATE);
|
+ " audioSec=" + String.format(java.util.Locale.US, "%.1f", decodedSamples / (double) SAMPLE_RATE);
|
||||||
Log.i(TAG, status + " expectedIntervalMs=" + EXPECTED_PACKET_INTERVAL_MS);
|
// Audio-pipeline stats are diagnostic; keep them in logcat only, out of
|
||||||
emitStatus(status);
|
// the on-glasses voice UI.
|
||||||
|
Log.i(TAG, status.replace('\n', ' ') + " expectedIntervalMs=" + EXPECTED_PACKET_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void emitStatus(String status) {
|
private void emitStatus(String status) {
|
||||||
|
|||||||
@@ -25,10 +25,9 @@ public class FaceclawWebSocket {
|
|||||||
private final WebSocket socket;
|
private final WebSocket socket;
|
||||||
private volatile boolean closeRequested;
|
private volatile boolean closeRequested;
|
||||||
|
|
||||||
public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener) {
|
// Single constructor (no overloads) so NativeScript constructor resolution
|
||||||
this(url, listener, null, null);
|
// can never pick a variant that drops the auth header; callers with no
|
||||||
}
|
// header pass null/null.
|
||||||
|
|
||||||
public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener, String headerName, String headerValue) {
|
public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener, String headerName, String headerValue) {
|
||||||
if (url == null || url.trim().isEmpty()) {
|
if (url == null || url.trim().isEmpty()) {
|
||||||
throw new IllegalArgumentException("url is required");
|
throw new IllegalArgumentException("url is required");
|
||||||
@@ -41,6 +40,15 @@ public class FaceclawWebSocket {
|
|||||||
builder.addHeader(headerName, headerValue);
|
builder.addHeader(headerName, headerValue);
|
||||||
}
|
}
|
||||||
Request request = builder.build();
|
Request request = builder.build();
|
||||||
|
// Redacted diagnostics: confirm the auth header is actually present on
|
||||||
|
// the handshake and carries a plausible value (not empty/truncated).
|
||||||
|
StringBuilder headerLog = new StringBuilder();
|
||||||
|
for (String name : request.headers().names()) {
|
||||||
|
String value = request.header(name);
|
||||||
|
headerLog.append(name).append('=').append(redact(value)).append(' ');
|
||||||
|
}
|
||||||
|
Log.i(TAG, "ws connect host=" + request.url().host() + request.url().encodedPath()
|
||||||
|
+ " headers=[" + headerLog.toString().trim() + "]");
|
||||||
socket = getClient().newWebSocket(request, new WebSocketListener() {
|
socket = getClient().newWebSocket(request, new WebSocketListener() {
|
||||||
@Override public void onOpen(WebSocket webSocket, Response response) {
|
@Override public void onOpen(WebSocket webSocket, Response response) {
|
||||||
mainHandler.post(() -> {
|
mainHandler.post(() -> {
|
||||||
@@ -107,6 +115,15 @@ public class FaceclawWebSocket {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String redact(String value) {
|
||||||
|
if (value == null) {
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
int len = value.length();
|
||||||
|
String prefix = value.substring(0, Math.min(4, len));
|
||||||
|
return "len" + len + ":" + prefix + "...";
|
||||||
|
}
|
||||||
|
|
||||||
public boolean sendText(String message) {
|
public boolean sendText(String message) {
|
||||||
return socket.send(message == null ? "" : message);
|
return socket.send(message == null ? "" : message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user