Move recording setting from Developer menu to Transcribe app; add prominent indicator

This commit is contained in:
James Babcock committed 2026-11-11 15:55:52 -08:00
1 parent e158452ba4
commit 30de2c4dd7
24 files changed
+482 -153

No files matched your search

@@ -40,6 +40,10 @@ constructor(
private var file: RandomAccessFile?
private var dataBytes = 0L
// The PCM bytes the header on disk accounts for. It is rewritten about once a second of
// audio, so a process death mid-recording still leaves a playable file.
private var headerDataBytes = 0L
init {
val parent = File(path).parentFile
if (parent != null && !parent.exists() && !parent.mkdirs())
@@ -58,11 +62,26 @@ constructor(
try {
current.write(pcm16le)
dataBytes += pcm16le.size
if (dataBytes - headerDataBytes >= sampleRate * channels * 2L) writeHeader(current)
} catch (e: IOException) {
Log.w("FaceclawWavRecorder", "append failed", e)
}
}
/** Rewrite the header to cover the PCM written so far, leaving the position at the end. */
private fun writeHeader(target: RandomAccessFile) {
val header =
BinaryEncoding.wavHeader(
minOf(dataBytes, Int.MAX_VALUE.toLong()).toInt(),
sampleRate,
channels,
)
target.seek(0)
target.write(header)
target.seek(header.size + dataBytes)
headerDataBytes = dataBytes
}
@Synchronized fun getDurationMs(): Long = dataBytes * 1000L / (sampleRate * channels * 2L)
fun getPath(): String = path
@@ -72,16 +91,7 @@ constructor(
val current = file ?: return dataBytes
file = null
try {
current.use {
it.seek(0)
it.write(
BinaryEncoding.wavHeader(
minOf(dataBytes, Int.MAX_VALUE.toLong()).toInt(),
sampleRate,
channels,
)
)
}
current.use { writeHeader(it) }
} catch (e: IOException) {
Log.w("FaceclawWavRecorder", "finish failed", e)
}
@@ -35,14 +35,11 @@ interface VoiceCaptureHost {
/** The speaker-embedding model at [modelPath] (may be shared/cached across sessions). */
fun speakerEmbedder(modelPath: String): SpeakerEmbedder
/** Where a saved recording goes, or null when recordings cannot be stored. */
fun recordingSink(): RecordingSink?
}
/**
* Voice-input capture pipeline shared by both platforms: LC3 packet queue with drop/late
* accounting, firmware-DoA beam gate, per-chunk processing (suppression, recording, speaker
* accounting, firmware-DoA beam gate, per-chunk processing (suppression, speaker
* verification buffer, endpointing, PCM emit), the segmented on-device transcript machine with
* REPLACE-semantics partials, and speaker verification at session end. The session owns its
* worker thread (started through the shared [startThread]); listener callbacks go through the
@@ -158,11 +155,8 @@ class VoiceCaptureSession(
private var lastTranscriptDecodeAtMs: Long = 0
private var lastTranscript = ""
@Volatile
private var saveRecordings = false
@Volatile
private var endpointing = false
private val endpointDetector = VoiceEndpointDetector()
private var recordingPcm: ByteSink? = null
@Volatile
private var verifySpeakerModelPath: String? = null
@Volatile
@@ -199,10 +193,6 @@ class VoiceCaptureSession(
this.usePhoneMic = usePhoneMic
}
fun setSaveRecordings(saveRecordings: Boolean) {
this.saveRecordings = saveRecordings
}
fun setOnboardModelKind(kind: String?) {
this.onboardModelKind = parseModelKind(kind)
}
@@ -363,7 +353,6 @@ class VoiceCaptureSession(
}
endpointDetector.reset()
suppressor?.reset()
recordingPcm = if (saveRecordings) ByteSink(SAMPLE_RATE * 2 * 4) else null
val verifying = verifySpeakerModelPath != null && verifyWearerEmbedding != null
verifyBuffer = if (verifying) ShortArray(VERIFY_MAX_SAMPLES) else null
verifyCount = 0
@@ -404,7 +393,6 @@ class VoiceCaptureSession(
emitStatus("Voice control failed: " + error.message)
} finally {
stopG2Audio()
writeRecordingIfAny()
releaseRecognizer()
releaseDecoder()
lock.withLock {
@@ -418,25 +406,6 @@ class VoiceCaptureSession(
}
}
private fun appendRecording(pcm: ShortArray, count: Int) {
val out = recordingPcm ?: return
out.write(AudioSegmentation.pcm16ToLittleEndian(pcm, 0, count))
}
private fun writeRecordingIfAny() {
val out = recordingPcm
recordingPcm = null
if (out == null || out.size() == 0) {
return
}
val sink = host.recordingSink() ?: return
try {
sink.save(out.toByteArray(), SAMPLE_RATE)
} catch (t: Throwable) {
logWarn("failed to save voice recording (${t.message})")
}
}
private fun startG2Audio(): Boolean {
val source = host.g2AudioSource() ?: return false
resetAudioStats()
@@ -489,9 +458,6 @@ class VoiceCaptureSession(
if (suppressionEnabled) {
applySuppression(pcm, count)
}
if (recordingPcm != null) {
appendRecording(pcm, count)
}
val verifyBuffer = this.verifyBuffer
if (verifyBuffer != null && verifyCount < VERIFY_MAX_SAMPLES) {
val copied = min(count, VERIFY_MAX_SAMPLES - verifyCount)
@@ -34,11 +34,6 @@ fun interface CallbackDispatcher {
fun post(action: () -> Unit)
}
/** Receives the session's decoded PCM once capture ends (Android saves a WAV file). */
fun interface RecordingSink {
fun save(pcm16le: ByteArray, sampleRate: Int)
}
/**
* A glasses microphone packet decoder to 16 kHz mono PCM: the G2's LC3 (the shared
* [Lc3PacketFramer] over a platform LC3 codec) or the RayNeo iO's Opus.