Skip to content

Commit 469d109

Browse files
refactor: migrate onset hold to microseconds with 500us default
Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
1 parent 403c1e0 commit 469d109

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/lib/components/Voice.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
const ONSET_TRIGGER_OPTIONS = {
1616
threshold: 0.1, // 0..1 linear
1717
preRollMs: 1, // include a short lead-in
18-
holdMs: 0, // minimum ms above threshold
18+
holdUs: 500, // minimum microseconds above threshold
1919
timeoutMs: 10000, // abort if no trigger within 10s
2020
highpassHz: 80 // reduce low-frequency rumble
2121
};

src/lib/services/audioRecorder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export interface RecordingOptions {
4444
preRollMs?: number;
4545

4646
/**
47-
* Minimum time in milliseconds the signal must stay above threshold to trigger.
48-
* Defaults to 12ms.
47+
* Minimum time in microseconds the signal must stay above threshold to trigger.
48+
* Defaults to 500 microseconds.
4949
*/
50-
holdMs?: number;
50+
holdUs?: number;
5151

5252
/**
5353
* Timeout in milliseconds while waiting for a trigger before aborting.
@@ -119,7 +119,7 @@ export async function recordAudio(
119119
const { samples, sampleRate } = await captureOnset(stream, {
120120
threshold: options.threshold,
121121
preRollMs,
122-
holdMs: Math.max(1, Math.floor(options.holdMs ?? 12)),
122+
holdUs: Math.max(0, Math.floor(options.holdUs ?? 500)),
123123
timeoutMs: Math.max(1000, Math.floor(options.timeoutMs ?? 10000)),
124124
highpassHz: options.highpassHz ?? 80,
125125
captureMs

src/lib/services/onsetDetector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const logger = createLogger('OnsetDetector');
55
export interface OnsetConfig {
66
threshold: number;
77
preRollMs: number;
8-
holdMs: number;
8+
holdUs: number;
99
timeoutMs: number;
1010
highpassHz?: number;
1111
captureMs: number; // total capture length AFTER preroll; total output = preRollMs + captureMs
@@ -32,7 +32,7 @@ export async function captureOnset(
3232
node.port.postMessage({
3333
threshold: cfg.threshold,
3434
preRollMs: cfg.preRollMs,
35-
holdMs: cfg.holdMs,
35+
holdUs: cfg.holdUs,
3636
captureMs: cfg.captureMs,
3737
highpassHz: cfg.highpassHz ?? 80
3838
});

src/lib/services/worklets/onset-processor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ registerProcessor('onset-detector', class extends AudioWorkletProcessor {
4141
const c = e.data || {};
4242
this.cfg.threshold = typeof c.threshold === 'number' ? c.threshold : this.cfg.threshold;
4343
const pre = Math.max(0, Math.round(((c.preRollMs ?? 0) * sampleRate) / 1000));
44-
const hold = Math.max(1, Math.round(((c.holdMs ?? 1) * sampleRate) / 1000));
44+
const holdUs = (typeof c.holdUs === 'number')
45+
? c.holdUs
46+
: (typeof c.holdMs === 'number' ? c.holdMs * 1000 : 500);
47+
const hold = Math.max(1, Math.round((holdUs * sampleRate) / 1_000_000));
4548
const cap = Math.max(1, Math.round(((c.captureMs ?? 1000) * sampleRate) / 1000));
4649
const hp = typeof c.highpassHz === 'number' ? c.highpassHz : this.cfg.hpHz;
4750

0 commit comments

Comments
 (0)