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): build transcription batches cutting only at speech gaps

Luis Ortiz6715733a45a5
diff --git a/src/core/capture/voice/__tests__/batching.test.ts b/src/core/capture/voice/__tests__/batching.test.ts
index 17cfbdb..65b24d1 100644
--- a/src/core/capture/voice/__tests__/batching.test.ts
+++ b/src/core/capture/voice/__tests__/batching.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { mergeGaps } from '../batching';
+import { buildBatches, MAX_BATCH_S, mergeGaps } from '../batching';
import { absoluteSeconds, type SpeechSegment } from '../types';
const seg = (start: number, end: number): SpeechSegment => ({
@@ -28,3 +28,34 @@ describe('mergeGaps', () => {
expect(mergeGaps([])).toEqual([]);
});
});
+
+describe('buildBatches', () => {
+ it('groups consecutive segments into one batch when they fit', () => {
+ const { batches } = buildBatches([seg(0, 5), seg(6, 10)]);
+ expect(batches).toHaveLength(1);
+ expect(batches[0].segments).toHaveLength(2);
+ });
+
+ it('starts a new batch when the span would exceed the cap', () => {
+ const { batches } = buildBatches([seg(0, 10), seg(11, 20), seg(21, 40)]);
+ expect(batches.length).toBeGreaterThan(1);
+ for (const b of batches) expect(b.end - b.start).toBeLessThanOrEqual(MAX_BATCH_S);
+ });
+
+ it('never splits an individual segment', () => {
+ const input = [seg(0, 4), seg(5, 9)];
+ for (const b of buildBatches(input).batches) {
+ for (const s of b.segments) expect(input).toContainEqual(s);
+ }
+ });
+
+ it('drops batches with less than one second of speech and counts them', () => {
+ const { batches, dropped } = buildBatches([seg(0, 0.4)]);
+ expect(batches).toEqual([]);
+ expect(dropped).toBe(1);
+ });
+
+ it('reports zero dropped when everything qualifies', () => {
+ expect(buildBatches([seg(0, 5)]).dropped).toBe(0);
+ });
+});
diff --git a/src/core/capture/voice/batching.ts b/src/core/capture/voice/batching.ts
index 882e444..4f3335d 100644
--- a/src/core/capture/voice/batching.ts
+++ b/src/core/capture/voice/batching.ts
@@ -1,4 +1,4 @@
-import { absoluteSeconds, type SpeechSegment } from './types';
+import { absoluteSeconds, type Batch, type SpeechSegment } from './types';
export const MERGE_GAP_S = 0.5;
export const MAX_BATCH_S = 25;
@@ -16,3 +16,26 @@ export function mergeGaps(segments: SpeechSegment[], gap = MERGE_GAP_S): SpeechS
}
return out;
}
+
+export interface BatchPlan {
+ batches: Batch[];
+ dropped: number;
+}
+
+const speechSeconds = (segments: SpeechSegment[]): number =>
+ segments.reduce((total, s) => total + (s.end - s.start), 0);
+
+export function buildBatches(segments: SpeechSegment[]): BatchPlan {
+ const grouped: Batch[] = [];
+ for (const s of segments) {
+ const last = grouped[grouped.length - 1];
+ if (last && s.end - last.start <= MAX_BATCH_S) {
+ last.end = s.end;
+ last.segments.push(s);
+ } else {
+ grouped.push({ start: s.start, end: s.end, segments: [s] });
+ }
+ }
+ const batches = grouped.filter((b) => speechSeconds(b.segments) >= MIN_SPEECH_S);
+ return { batches, dropped: grouped.length - batches.length };
+}