Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(voice): merge speech segments separated by short gaps

Luis Ortizb3d6a931e101
diff --git a/src/core/capture/voice/__tests__/batching.test.ts b/src/core/capture/voice/__tests__/batching.test.ts
new file mode 100644
index 0000000..17cfbdb
--- /dev/null
+++ b/src/core/capture/voice/__tests__/batching.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+import { mergeGaps } from '../batching';
+import { absoluteSeconds, type SpeechSegment } from '../types';
+
+const seg = (start: number, end: number): SpeechSegment => ({
+ start: absoluteSeconds(start),
+ end: absoluteSeconds(end),
+});
+
+describe('mergeGaps', () => {
+ it('merges segments separated by a gap at or under the threshold', () => {
+ expect(mergeGaps([seg(1, 2), seg(2.4, 3)])).toEqual([seg(1, 3)]);
+ });
+
+ it('keeps segments separated by a gap over the threshold', () => {
+ expect(mergeGaps([seg(1, 2), seg(2.9, 3)])).toHaveLength(2);
+ });
+
+ it('sorts unordered input before merging', () => {
+ expect(mergeGaps([seg(5, 6), seg(1, 2)])[0].start).toBe(1);
+ });
+
+ it('absorbs a segment fully contained in the previous one', () => {
+ expect(mergeGaps([seg(1, 5), seg(2, 3)])).toEqual([seg(1, 5)]);
+ });
+
+ it('returns an empty array for no input', () => {
+ expect(mergeGaps([])).toEqual([]);
+ });
+});
diff --git a/src/core/capture/voice/batching.ts b/src/core/capture/voice/batching.ts
new file mode 100644
index 0000000..882e444
--- /dev/null
+++ b/src/core/capture/voice/batching.ts
@@ -0,0 +1,18 @@
+import { absoluteSeconds, type SpeechSegment } from './types';
+
+export const MERGE_GAP_S = 0.5;
+export const MAX_BATCH_S = 25;
+export const MIN_SPEECH_S = 1;
+
+export function mergeGaps(segments: SpeechSegment[], gap = MERGE_GAP_S): SpeechSegment[] {
+ const out: SpeechSegment[] = [];
+ for (const s of [...segments].sort((a, b) => a.start - b.start)) {
+ const last = out[out.length - 1];
+ if (last && s.start - last.end <= gap) {
+ last.end = absoluteSeconds(Math.max(last.end, s.end));
+ } else {
+ out.push({ ...s });
+ }
+ }
+ return out;
+}