Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

Mic detection on a Mac: the mic watch also reads again every 3 s (1.5 s while recording) by itself, so a change the Mac never tells is still seen as soon as before (#143)

christianmat3c82c84e81b6
diff --git a/app/native/yogurt-mac/main.swift b/app/native/yogurt-mac/main.swift
index cbc0c8ec..3291a4c5 100644
--- a/app/native/yogurt-mac/main.swift
+++ b/app/native/yogurt-mac/main.swift
@@ -5,7 +5,8 @@
// yogurt-mac calendar-auth → asks once (the system's own prompt), then the status
// yogurt-mac events FROM TO → meetings between two ISO dates (all-day and cancelled ones left out)
// yogurt-mac mic-apps → [{"bundleId","name","pid"}] of processes using audio input right now
-// yogurt-mac mic-watch → the same as mic-apps, one JSON line at start and on each change, until stdin closes:
+// yogurt-mac mic-watch → the same as mic-apps, one JSON line at start and on each change, until stdin closes
+// (stdin lines "recording" / "idle" set how often it reads again: 1.5 s / 3 s):
// one Core Audio client for Yogurt's whole life instead of one per reading (#143)
// yogurt-mac running-apps ID... → the supplied bundle IDs currently present in NSRunningApplication
// yogurt-mac browser-title ID → {"title":string|null}: the front tab's name in Chrome, Arc, Edge, Brave or Safari (only if running)
@@ -522,8 +523,18 @@ func watchCalls(every: Int, ids: [String], pids: [pid_t] = []) {
/// Core Audio client with privacy checks in coreaudiod, and after about an hour of that powerd refused every power
/// assertion and the displays never woke (#143).
func watchMic() {
- // Yogurt closes stdin to stop the watch (and a crashed Yogurt closes it too)
- DispatchQueue.global().async { _ = FileHandle.standardInput.readDataToEndOfFile(); exit(0) }
+ // Yogurt writes "recording" or "idle" to stdin when that changes, and closes it to stop the watch (a crashed Yogurt
+ // closes it too)
+ var every = 3.0
+ DispatchQueue.global().async {
+ while true {
+ let d = FileHandle.standardInput.availableData
+ if d.isEmpty { exit(0) }
+ guard let word = String(data: d, encoding: .utf8)?.split(whereSeparator: \.isNewline).last else { continue }
+ let next = word == "recording" ? 1.5 : 3.0
+ DispatchQueue.main.async { every = next }
+ }
+ }
let sys = AudioObjectID(kAudioObjectSystemObject)
var listAddr = address(kAudioHardwarePropertyProcessObjectList)
var inputAddr = address(kAudioProcessPropertyIsRunningInput)
@@ -553,11 +564,15 @@ func watchMic() {
AudioObjectAddPropertyListenerBlock(sys, &listAddr, .main, listChanged)
follow()
emit()
- // in case a change was never told: read again now and then, and end if Yogurt is gone without closing stdin
- let safety = DispatchSource.makeTimerSource(queue: .main)
- safety.schedule(deadline: .now() + 30, repeating: 30)
- safety.setEventHandler { if getppid() != parent { exit(0) }; follow(); emit() }
- safety.resume()
+ // in case a change is never told: read again as often as Yogurt used to ask (3 s, 1.5 s while recording) inside
+ // this one client, so detection is never slower than before; and end if Yogurt is gone without closing stdin
+ var again: (() -> Void)!
+ again = {
+ if getppid() != parent { exit(0) }
+ follow(); emit()
+ DispatchQueue.main.asyncAfter(deadline: .now() + every) { again() }
+ }
+ DispatchQueue.main.asyncAfter(deadline: .now() + every) { again() }
dispatchMain()
}
diff --git a/app/src/main/micWatch.ts b/app/src/main/micWatch.ts
index 0ac1c25c..5b57a468 100644
--- a/app/src/main/micWatch.ts
+++ b/app/src/main/micWatch.ts
@@ -2,7 +2,7 @@ import { spawn } from 'node:child_process'
import { createInterface } from 'node:readline'
/** one running yogurt-mac mic-watch: its lines, its end, and how to end it */
-export type MicWatchProc = { onLine(f: (line: string) => void): void; onExit(f: () => void): void; stop(): void }
+export type MicWatchProc = { onLine(f: (line: string) => void): void; onExit(f: () => void): void; send(line: string): void; stop(): void }
/** waits before starting the watcher again, longer each time it ends soon after starting */
export const BACKOFF_MS = [1_000, 2_000, 4_000, 8_000, 16_000]
@@ -30,12 +30,20 @@ export function createMicWatch(d: { start: () => MicWatchProc; poll: () => Promi
let heard: (() => void)[] = []
let polled: { at: number; value: unknown } | undefined
let asking: Promise<unknown> | undefined
+ /** what the watcher was last told: it reads again every 1.5 s while recording, else 3 s */
+ let told: boolean | undefined
+ const tell = () => {
+ const r = d.recording()
+ if (proc && r !== told) { told = r; proc.send(r ? 'recording' : 'idle') }
+ }
const run = () => {
if (stopped) return
const startedAt = Date.now()
const p = d.start()
proc = p
+ told = undefined
+ tell()
p.onLine((l) => {
if (proc !== p) return
let v: unknown
@@ -71,6 +79,7 @@ export function createMicWatch(d: { start: () => MicWatchProc; poll: () => Promi
run()
return {
async read(): Promise<unknown> {
+ tell()
if (value) return value
if (proc && !polling) {
let timer: NodeJS.Timeout | undefined
@@ -89,7 +98,8 @@ export function createMicWatch(d: { start: () => MicWatchProc; poll: () => Promi
}
}
-/** yogurt-mac mic-watch: one JSON line (what mic-apps prints) at start and on each change, until its stdin closes */
+/** yogurt-mac mic-watch: one JSON line (what mic-apps prints) at start and on each change, until its stdin closes;
+ * "recording" or "idle" sent to it sets how often it reads again by itself */
export const spawnMicWatch = (binary: () => string) => (): MicWatchProc => {
const child = spawn(binary(), ['mic-watch'], { stdio: ['pipe', 'pipe', 'ignore'] })
const exits: (() => void)[] = []
@@ -103,6 +113,7 @@ export const spawnMicWatch = (binary: () => string) => (): MicWatchProc => {
return {
onLine: (f) => { lines.on('line', f) },
onExit: (f) => { exits.push(f) },
+ send: (l) => { if (!ended && child.stdin.writable) child.stdin.write(l + '\n') },
stop() {
child.stdin.end()
setTimeout(() => { if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL') }, 2000).unref()
diff --git a/app/test/micWatch.test.ts b/app/test/micWatch.test.ts
index 14843e9a..c1bc3982 100644
--- a/app/test/micWatch.test.ts
+++ b/app/test/micWatch.test.ts
@@ -9,12 +9,12 @@ const zoom = { bundleId: 'us.zoom.xos', name: 'zoom.us', pid: 2 }
/** stand-in helper processes: each start is one, lines and exits sent by hand */
function procs() {
- const all: { line(l: string): void; exit(): void; stopped: boolean }[] = []
+ const all: { line(l: string): void; exit(): void; stopped: boolean; sent: string[] }[] = []
const start = (): MicWatchProc => {
let onLine: (l: string) => void = () => {}, onExit: () => void = () => {}
- const p = { line: (l: string) => onLine(l), exit: () => onExit(), stopped: false }
+ const p = { line: (l: string) => onLine(l), exit: () => onExit(), stopped: false, sent: [] as string[] }
all.push(p)
- return { onLine: (f) => { onLine = f }, onExit: (f) => { onExit = f }, stop: () => { p.stopped = true } }
+ return { onLine: (f) => { onLine = f }, onExit: (f) => { onExit = f }, send: (l) => { p.sent.push(l) }, stop: () => { p.stopped = true } }
}
return { all, start, last: () => all[all.length - 1] }
}
@@ -40,6 +40,23 @@ describe('createMicWatch', () => {
expect(x.p.all).toHaveLength(1)
expect(x.polls).toEqual([])
})
+ it('tells the watcher whether a meeting records, only when that changes, so it reads again at the old pace', async () => {
+ const x = setup()
+ x.p.last().line('[]')
+ expect(x.p.last().sent).toEqual(['idle'])
+ await x.w.read(); await x.w.read()
+ x.setRecording(true)
+ await x.w.read(); await x.w.read()
+ x.setRecording(false)
+ await x.w.read()
+ expect(x.p.last().sent).toEqual(['idle', 'recording', 'idle'])
+ })
+ it('a restarted watcher is told again', async () => {
+ const x = setup({ recording: true })
+ x.p.last().exit()
+ vi.advanceTimersByTime(BACKOFF_MS[0])
+ expect(x.p.last().sent).toEqual(['recording'])
+ })
it('skips lines it cannot read and keeps the last good one', async () => {
const x = setup()
x.p.last().line(JSON.stringify([zoom]))
@@ -146,6 +163,7 @@ describe.skipIf(process.platform === 'win32')('spawnMicWatch', () => {
writeFileSync(bin, '#!/bin/sh\n[ "$1" = mic-watch ] || exit 2\necho \'[{"bundleId":"us.zoom.xos","name":"zoom.us","pid":2}]\'\ncat >/dev/null\n')
chmodSync(bin, 0o755)
const p = spawnMicWatch(() => bin)()
+ p.send('recording')
const line = await new Promise<string>((r) => p.onLine(r))
expect(JSON.parse(line)).toEqual([zoom])
const ended = new Promise<void>((r) => p.onExit(r))