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.

Level meter: read its colour once, skip frames that draw the same pixels, and stop its loop at silence and while hidden

christianmat5db62e637c99
diff --git a/app/e2e/meterSleep.spec.ts b/app/e2e/meterSleep.spec.ts
new file mode 100644
index 00000000..15f91705
--- /dev/null
+++ b/app/e2e/meterSleep.spec.ts
@@ -0,0 +1,39 @@
+import { _electron as electron, expect, test } from '@playwright/test'
+import { mkdtempSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+
+// The meter's frame loop is most of the app's own work while recording: it runs only while it can be seen and something moves.
+test('the level meter stops drawing while its window is hidden and once the room goes quiet', async () => {
+ const app = await electron.launch({ args: [join(import.meta.dirname, '../out/main/index.js')], env: {
+ ...process.env, YOGURT_BACKGROUND: '1', YOGURT_FAKE: '1', YOGURT_USER_DATA: mkdtempSync(join(tmpdir(), 'yogurt-meter-')),
+ } })
+ app.context().setDefaultTimeout(15_000)
+ try {
+ const page = await app.firstWindow()
+ await expect(page.getByTestId('home')).toBeVisible()
+ await page.getByRole('button', { name: 'Start recording', exact: true }).click()
+ const wave = page.getByTestId('note-wave')
+ // the fake mic talks for its first 16.5 s
+ await expect(wave).toHaveAttribute('data-live', '1')
+ await expect.poll(async () => Number(await wave.getAttribute('data-level'))).toBeGreaterThan(0.05)
+ // hidden (minimised, or covered by another window): the loop stops, and new levels don't wake it
+ const visibility = (state: 'hidden' | null) => page.evaluate((s) => {
+ if (s) Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => s })
+ else delete (document as { visibilityState?: string }).visibilityState
+ document.dispatchEvent(new Event('visibilitychange'))
+ }, state)
+ await visibility('hidden')
+ await expect(wave).toHaveAttribute('data-live', '0')
+ await page.waitForTimeout(500)
+ await expect(wave).toHaveAttribute('data-live', '0')
+ await visibility(null)
+ await expect(wave).toHaveAttribute('data-live', '1')
+ // the fake mic falls silent: the meter settles at rest, still drawn, and the loop sleeps
+ await expect(wave).toHaveAttribute('data-live', '0', { timeout: 30_000 })
+ await expect(wave).toHaveAttribute('data-level', '0.00')
+ expect(await wave.evaluate((c: HTMLCanvasElement) => c.getContext('2d')!.getImageData(0, 0, c.width, c.height).data.some((v, i) => i % 4 === 3 && v > 0))).toBe(true)
+ await page.waitForTimeout(500)
+ await expect(wave).toHaveAttribute('data-live', '0')
+ } finally { await app.close() }
+})
diff --git a/app/src/renderer/src/components/LevelBars.tsx b/app/src/renderer/src/components/LevelBars.tsx
index afe8346a..602ac570 100644
--- a/app/src/renderer/src/components/LevelBars.tsx
+++ b/app/src/renderer/src/components/LevelBars.tsx
@@ -1,5 +1,5 @@
import { useEffect, useRef } from 'react'
-import { barPlan, micLevel, Wave } from '../../../shared/wave'
+import { barPlan, frameKey, micLevel, Wave } from '../../../shared/wave'
const BAR_W = 3, GAP = 3
@@ -17,13 +17,20 @@ export function LevelBars({ height, className, testId, paused = false, bars = 5
const dpr = window.devicePixelRatio || 1
let reduced = mq.matches
c.width = width * dpr; c.height = height * dpr
- const draw = () => {
+ // read once (and again when the theme changes), not every frame: getComputedStyle forces a style pass
+ let color = getComputedStyle(c).color
+ let drawn = ''
+ const draw = (force = false) => {
const ctx = c.getContext('2d')
if (!ctx) return
const plan = barPlan({ reduced, paused, level: wave.level, bars })
+ // the same pixels as the last frame: nothing new for the compositor
+ const key = frameKey(plan, height - BAR_W)
+ if (!force && key === drawn) return
+ drawn = key
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
ctx.clearRect(0, 0, width, height)
- ctx.fillStyle = getComputedStyle(c).color
+ ctx.fillStyle = color
plan.heights.forEach((v, i) => {
const h = BAR_W + Math.round(v * (height - BAR_W))
ctx.globalAlpha = plan.alpha[i]
@@ -38,28 +45,38 @@ export function LevelBars({ height, className, testId, paused = false, bars = 5
let levelAt = performance.now()
const off = window.yogurt.widget.onLevel((lv) => {
wave.target = micLevel(lv); levelAt = performance.now()
- if (reduced) { wave.level = wave.target; draw() }
+ if (reduced) { wave.level = wave.target; draw() } else if (wave.target > 0) wake()
})
let raf = 0, last = performance.now()
+ // tests read whether the loop runs; set only when that changes
+ const live = (on: boolean) => { if (!on) { cancelAnimationFrame(raf); raf = 0 } c.dataset.live = on ? '1' : '0' }
const frame = (now: number) => {
const dt = Math.min(100, now - last); last = now
if (now - levelAt > 300) wave.target = 0
wave.step(dt)
+ // at rest in silence the loop sleeps until the next level wakes it
+ if (wave.settled()) { draw(true); live(false); return }
draw()
raf = requestAnimationFrame(frame)
}
- // the loop runs only without reduced motion; either way something is drawn right away
+ const start = () => { last = performance.now(); raf = requestAnimationFrame(frame); live(true) }
+ // nothing runs where it can't be seen: a hidden or covered window, reduced motion, a pause
+ const wake = () => { if (!raf && !reduced && !paused && document.visibilityState === 'visible') start() }
+ // either way something is drawn right away
const sync = () => {
- cancelAnimationFrame(raf); raf = 0
- if (!reduced && !paused) { last = performance.now(); raf = requestAnimationFrame(frame) }
- draw()
+ live(false)
+ if (!reduced && !paused && document.visibilityState === 'visible') start()
+ draw(true)
}
const onReduced = () => { reduced = mq.matches; sync() }
+ const onVisible = () => { if (document.visibilityState === 'visible') wake(); else live(false) }
+ const onScheme = () => { color = getComputedStyle(c).color; draw(true) }
const scheme = window.matchMedia('(prefers-color-scheme: dark)')
mq.addEventListener('change', onReduced)
- scheme.addEventListener('change', draw)
+ scheme.addEventListener('change', onScheme)
+ document.addEventListener('visibilitychange', onVisible)
sync()
- return () => { off(); cancelAnimationFrame(raf); mq.removeEventListener('change', onReduced); scheme.removeEventListener('change', draw) }
+ return () => { off(); cancelAnimationFrame(raf); mq.removeEventListener('change', onReduced); scheme.removeEventListener('change', onScheme); document.removeEventListener('visibilitychange', onVisible) }
}, [height, width, paused, bars])
return <canvas ref={canvas} data-testid={testId} aria-hidden="true" className={className} style={{ width, height }} />
}
diff --git a/app/src/shared/wave.ts b/app/src/shared/wave.ts
index 544ce262..6651ddde 100644
--- a/app/src/shared/wave.ts
+++ b/app/src/shared/wave.ts
@@ -16,6 +16,12 @@ export class Wave {
const tau = this.target > this.level ? ATTACK_MS : RELEASE_MS
this.level += (this.target - this.level) * (1 - Math.exp(-dtMs / tau))
}
+ /** back at rest in silence (snapped to 0): nothing moves until the next level */
+ settled(): boolean {
+ if (this.target !== 0 || this.level >= 0.002) return false
+ this.level = 0
+ return true
+ }
}
/** Five-bar silhouette; pause and reduced motion are three still dots. */
@@ -25,5 +31,9 @@ export function barPlan(p: { reduced: boolean; paused: boolean; level: number; b
return { heights: weights.map(v => v * p.level), alpha: weights.map(() => .45 + .55 * p.level) }
}
+/** What a frame puts on screen, in whole pixels of growth and 1% steps of alpha: the same key draws the same pixels. */
+export const frameKey = (plan: { heights: number[]; alpha: number[] }, span: number): string =>
+ plan.heights.map((v) => Math.round(v * span)).join() + '|' + Math.round(plan.alpha[0] * 100)
+
/** Capture maps -60..0 dBFS to 0..1; show -51..-12 dBFS across the meter's range. */
export const micLevel = (lv: { me: number; them: number }): number => Number.isFinite(lv.me) ? Math.max(0, Math.min(1, (lv.me - .15) / .65)) : 0
diff --git a/app/test/wave.test.ts b/app/test/wave.test.ts
index 299e7268..4f89ea26 100644
--- a/app/test/wave.test.ts
+++ b/app/test/wave.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { barPlan, micLevel, Wave } from '../src/shared/wave'
+import { barPlan, frameKey, micLevel, Wave } from '../src/shared/wave'
describe('microphone meter', () => {
it('keeps silence quiet and makes ordinary speech visible without using call audio', () => {
@@ -41,4 +41,23 @@ describe('microphone meter', () => {
expect(barPlan({ level: 1, reduced, paused })).toEqual(rest)
}
})
+ it('a frame that would draw the same pixels is the same frame, so it is not drawn again', () => {
+ const at = (level: number) => frameKey(barPlan({ level, reduced: false, paused: false }), 16)
+ expect(at(.5)).toBe(at(.5001))
+ expect(at(0)).toBe(at(.0004))
+ expect(at(.5)).not.toBe(at(.6))
+ // the bars fade in with the level even before they grow a pixel
+ expect(at(0)).not.toBe(at(.02))
+ })
+ it('the meter settles at silence, and only then may its loop sleep', () => {
+ const w = new Wave()
+ expect(w.settled()).toBe(true)
+ w.target = 1; w.step(60)
+ expect(w.settled()).toBe(false)
+ w.target = 0; w.step(60)
+ expect(w.settled()).toBe(false)
+ w.step(2000)
+ expect(w.settled()).toBe(true)
+ expect(w.level).toBe(0)
+ })
})