markupr 2.1.8 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) hide show
  1. package/README.md +292 -15
  2. package/dist/cli/index.mjs +3593 -0
  3. package/dist/main/index.mjs +743 -220
  4. package/dist/mcp/index.mjs +4053 -0
  5. package/package.json +32 -7
  6. package/.claude/commands/review-feedback.md +0 -47
  7. package/.eslintrc.json +0 -35
  8. package/.github/CODEOWNERS +0 -16
  9. package/.github/FUNDING.yml +0 -1
  10. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -56
  11. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -54
  12. package/.github/PULL_REQUEST_TEMPLATE.md +0 -89
  13. package/.github/dependabot.yml +0 -70
  14. package/.github/workflows/ci.yml +0 -184
  15. package/.github/workflows/deploy-landing.yml +0 -134
  16. package/.github/workflows/nightly.yml +0 -288
  17. package/.github/workflows/release.yml +0 -318
  18. package/CHANGELOG.md +0 -127
  19. package/CLAUDE.md +0 -137
  20. package/CODE_OF_CONDUCT.md +0 -9
  21. package/CONTRIBUTING.md +0 -390
  22. package/PRODUCT_VISION.md +0 -277
  23. package/SECURITY.md +0 -51
  24. package/SIGNING_INSTRUCTIONS.md +0 -284
  25. package/assets/DMG_BACKGROUND_INSTRUCTIONS.md +0 -130
  26. package/assets/svg-source/dmg-background.svg +0 -70
  27. package/assets/svg-source/icon.svg +0 -20
  28. package/assets/svg-source/tray-icon-processing.svg +0 -7
  29. package/assets/svg-source/tray-icon-recording.svg +0 -7
  30. package/assets/svg-source/tray-icon.svg +0 -6
  31. package/assets/tray-complete.png +0 -0
  32. package/assets/tray-complete@2x.png +0 -0
  33. package/assets/tray-completeTemplate.png +0 -0
  34. package/assets/tray-completeTemplate@2x.png +0 -0
  35. package/assets/tray-error.png +0 -0
  36. package/assets/tray-error@2x.png +0 -0
  37. package/assets/tray-errorTemplate.png +0 -0
  38. package/assets/tray-errorTemplate@2x.png +0 -0
  39. package/assets/tray-icon-processing.png +0 -0
  40. package/assets/tray-icon-processing@2x.png +0 -0
  41. package/assets/tray-icon-processingTemplate.png +0 -0
  42. package/assets/tray-icon-processingTemplate@2x.png +0 -0
  43. package/assets/tray-icon-recording.png +0 -0
  44. package/assets/tray-icon-recording@2x.png +0 -0
  45. package/assets/tray-icon-recordingTemplate.png +0 -0
  46. package/assets/tray-icon-recordingTemplate@2x.png +0 -0
  47. package/assets/tray-icon.png +0 -0
  48. package/assets/tray-icon@2x.png +0 -0
  49. package/assets/tray-iconTemplate.png +0 -0
  50. package/assets/tray-iconTemplate@2x.png +0 -0
  51. package/assets/tray-idle.png +0 -0
  52. package/assets/tray-idle@2x.png +0 -0
  53. package/assets/tray-idleTemplate.png +0 -0
  54. package/assets/tray-idleTemplate@2x.png +0 -0
  55. package/assets/tray-processing-0.png +0 -0
  56. package/assets/tray-processing-0@2x.png +0 -0
  57. package/assets/tray-processing-0Template.png +0 -0
  58. package/assets/tray-processing-0Template@2x.png +0 -0
  59. package/assets/tray-processing-1.png +0 -0
  60. package/assets/tray-processing-1@2x.png +0 -0
  61. package/assets/tray-processing-1Template.png +0 -0
  62. package/assets/tray-processing-1Template@2x.png +0 -0
  63. package/assets/tray-processing-2.png +0 -0
  64. package/assets/tray-processing-2@2x.png +0 -0
  65. package/assets/tray-processing-2Template.png +0 -0
  66. package/assets/tray-processing-2Template@2x.png +0 -0
  67. package/assets/tray-processing-3.png +0 -0
  68. package/assets/tray-processing-3@2x.png +0 -0
  69. package/assets/tray-processing-3Template.png +0 -0
  70. package/assets/tray-processing-3Template@2x.png +0 -0
  71. package/assets/tray-processing.png +0 -0
  72. package/assets/tray-processing@2x.png +0 -0
  73. package/assets/tray-processingTemplate.png +0 -0
  74. package/assets/tray-processingTemplate@2x.png +0 -0
  75. package/assets/tray-recording.png +0 -0
  76. package/assets/tray-recording@2x.png +0 -0
  77. package/assets/tray-recordingTemplate.png +0 -0
  78. package/assets/tray-recordingTemplate@2x.png +0 -0
  79. package/build/DMG_BACKGROUND_SPEC.md +0 -50
  80. package/build/dmg-background.png +0 -0
  81. package/build/dmg-background@2x.png +0 -0
  82. package/build/entitlements.mac.inherit.plist +0 -27
  83. package/build/entitlements.mac.plist +0 -41
  84. package/build/favicon-16.png +0 -0
  85. package/build/favicon-180.png +0 -0
  86. package/build/favicon-192.png +0 -0
  87. package/build/favicon-32.png +0 -0
  88. package/build/favicon-48.png +0 -0
  89. package/build/favicon-512.png +0 -0
  90. package/build/favicon-64.png +0 -0
  91. package/build/icon-128.png +0 -0
  92. package/build/icon-16.png +0 -0
  93. package/build/icon-24.png +0 -0
  94. package/build/icon-256.png +0 -0
  95. package/build/icon-32.png +0 -0
  96. package/build/icon-48.png +0 -0
  97. package/build/icon-64.png +0 -0
  98. package/build/icon.icns +0 -0
  99. package/build/icon.ico +0 -0
  100. package/build/icon.iconset/icon_128x128.png +0 -0
  101. package/build/icon.iconset/icon_128x128@2x.png +0 -0
  102. package/build/icon.iconset/icon_16x16.png +0 -0
  103. package/build/icon.iconset/icon_16x16@2x.png +0 -0
  104. package/build/icon.iconset/icon_256x256.png +0 -0
  105. package/build/icon.iconset/icon_256x256@2x.png +0 -0
  106. package/build/icon.iconset/icon_32x32.png +0 -0
  107. package/build/icon.iconset/icon_32x32@2x.png +0 -0
  108. package/build/icon.iconset/icon_512x512.png +0 -0
  109. package/build/icon.iconset/icon_512x512@2x.png +0 -0
  110. package/build/icon.png +0 -0
  111. package/build/installer-header.bmp +0 -0
  112. package/build/installer-header.png +0 -0
  113. package/build/installer-sidebar.bmp +0 -0
  114. package/build/installer-sidebar.png +0 -0
  115. package/build/installer.nsh +0 -45
  116. package/build/overlay-processing.png +0 -0
  117. package/build/overlay-recording.png +0 -0
  118. package/build/toolbar-record.png +0 -0
  119. package/build/toolbar-screenshot.png +0 -0
  120. package/build/toolbar-settings.png +0 -0
  121. package/build/toolbar-stop.png +0 -0
  122. package/dist/preload/index.mjs +0 -907
  123. package/dist/renderer/assets/index-CCmUjl9K.js +0 -19495
  124. package/dist/renderer/assets/index-CUqz_Gs6.css +0 -2270
  125. package/dist/renderer/index.html +0 -27
  126. package/docs/AI_AGENT_QUICKSTART.md +0 -42
  127. package/docs/AI_PIPELINE_DESIGN.md +0 -595
  128. package/docs/API.md +0 -514
  129. package/docs/ARCHITECTURE.md +0 -460
  130. package/docs/CONFIGURATION.md +0 -336
  131. package/docs/DEVELOPMENT.md +0 -508
  132. package/docs/EXPORT_FORMATS.md +0 -451
  133. package/docs/GETTING_STARTED.md +0 -236
  134. package/docs/KEYBOARD_SHORTCUTS.md +0 -334
  135. package/docs/TROUBLESHOOTING.md +0 -418
  136. package/docs/landing/index.html +0 -672
  137. package/docs/landing/script.js +0 -342
  138. package/docs/landing/styles.css +0 -1543
  139. package/electron-builder.yml +0 -140
  140. package/electron.vite.config.ts +0 -63
  141. package/railway.json +0 -12
  142. package/scripts/build.mjs +0 -51
  143. package/scripts/generate-icons.mjs +0 -314
  144. package/scripts/generate-installer-images.cjs +0 -253
  145. package/scripts/generate-tray-icons.mjs +0 -258
  146. package/scripts/notarize.cjs +0 -180
  147. package/scripts/one-click-clean-test.sh +0 -147
  148. package/scripts/postinstall.mjs +0 -36
  149. package/scripts/setup-markupr.sh +0 -55
  150. package/setup +0 -17
  151. package/site/index.html +0 -1835
  152. package/site/package.json +0 -11
  153. package/site/railway.json +0 -12
  154. package/site/server.js +0 -31
  155. package/src/main/AutoUpdater.ts +0 -392
  156. package/src/main/CrashRecovery.ts +0 -655
  157. package/src/main/ErrorHandler.ts +0 -703
  158. package/src/main/HotkeyManager.ts +0 -399
  159. package/src/main/MenuManager.ts +0 -529
  160. package/src/main/PermissionManager.ts +0 -420
  161. package/src/main/SessionController.ts +0 -1465
  162. package/src/main/TrayManager.ts +0 -540
  163. package/src/main/ai/AIPipelineManager.ts +0 -199
  164. package/src/main/ai/ClaudeAnalyzer.ts +0 -339
  165. package/src/main/ai/ImageOptimizer.ts +0 -176
  166. package/src/main/ai/StructuredMarkdownBuilder.ts +0 -379
  167. package/src/main/ai/index.ts +0 -16
  168. package/src/main/ai/types.ts +0 -258
  169. package/src/main/analysis/ClarificationGenerator.ts +0 -385
  170. package/src/main/analysis/FeedbackAnalyzer.ts +0 -531
  171. package/src/main/analysis/index.ts +0 -19
  172. package/src/main/audio/AudioCapture.ts +0 -978
  173. package/src/main/audio/audioUtils.ts +0 -100
  174. package/src/main/audio/index.ts +0 -20
  175. package/src/main/capture/index.ts +0 -1
  176. package/src/main/index.ts +0 -1693
  177. package/src/main/ipc/captureHandlers.ts +0 -272
  178. package/src/main/ipc/index.ts +0 -45
  179. package/src/main/ipc/outputHandlers.ts +0 -302
  180. package/src/main/ipc/sessionHandlers.ts +0 -56
  181. package/src/main/ipc/settingsHandlers.ts +0 -471
  182. package/src/main/ipc/types.ts +0 -56
  183. package/src/main/ipc/windowHandlers.ts +0 -277
  184. package/src/main/output/ClipboardService.ts +0 -369
  185. package/src/main/output/ExportService.ts +0 -539
  186. package/src/main/output/FileManager.ts +0 -416
  187. package/src/main/output/MarkdownGenerator.ts +0 -791
  188. package/src/main/output/MarkdownPatcher.ts +0 -299
  189. package/src/main/output/index.ts +0 -186
  190. package/src/main/output/sessionAdapter.ts +0 -207
  191. package/src/main/output/templates/html-template.ts +0 -553
  192. package/src/main/pipeline/FrameExtractor.ts +0 -330
  193. package/src/main/pipeline/PostProcessor.ts +0 -399
  194. package/src/main/pipeline/TranscriptAnalyzer.ts +0 -226
  195. package/src/main/pipeline/index.ts +0 -36
  196. package/src/main/platform/WindowsTaskbar.ts +0 -600
  197. package/src/main/platform/index.ts +0 -16
  198. package/src/main/settings/SettingsManager.ts +0 -730
  199. package/src/main/settings/index.ts +0 -19
  200. package/src/main/transcription/ModelDownloadManager.ts +0 -494
  201. package/src/main/transcription/TierManager.ts +0 -219
  202. package/src/main/transcription/TranscriptionRecoveryService.ts +0 -340
  203. package/src/main/transcription/WhisperService.ts +0 -748
  204. package/src/main/transcription/index.ts +0 -56
  205. package/src/main/transcription/types.ts +0 -135
  206. package/src/main/windows/PopoverManager.ts +0 -284
  207. package/src/main/windows/TaskbarIntegration.ts +0 -452
  208. package/src/main/windows/index.ts +0 -23
  209. package/src/preload/index.ts +0 -1047
  210. package/src/renderer/App.tsx +0 -515
  211. package/src/renderer/AppWrapper.tsx +0 -28
  212. package/src/renderer/assets/logo-dark.svg +0 -7
  213. package/src/renderer/assets/logo.svg +0 -7
  214. package/src/renderer/audio/AudioCaptureRenderer.ts +0 -454
  215. package/src/renderer/capture/ScreenRecordingRenderer.ts +0 -492
  216. package/src/renderer/components/AnnotationOverlay.tsx +0 -836
  217. package/src/renderer/components/AudioWaveform.tsx +0 -811
  218. package/src/renderer/components/ClarificationQuestions.tsx +0 -656
  219. package/src/renderer/components/CountdownTimer.tsx +0 -495
  220. package/src/renderer/components/CrashRecoveryDialog.tsx +0 -632
  221. package/src/renderer/components/DonateButton.tsx +0 -127
  222. package/src/renderer/components/ErrorBoundary.tsx +0 -308
  223. package/src/renderer/components/ExportDialog.tsx +0 -872
  224. package/src/renderer/components/HotkeyHint.tsx +0 -261
  225. package/src/renderer/components/KeyboardShortcuts.tsx +0 -787
  226. package/src/renderer/components/ModelDownloadDialog.tsx +0 -844
  227. package/src/renderer/components/Onboarding.tsx +0 -1830
  228. package/src/renderer/components/ProcessingOverlay.tsx +0 -157
  229. package/src/renderer/components/RecordingOverlay.tsx +0 -423
  230. package/src/renderer/components/SessionHistory.tsx +0 -1746
  231. package/src/renderer/components/SessionReview.tsx +0 -1321
  232. package/src/renderer/components/SettingsPanel.tsx +0 -217
  233. package/src/renderer/components/Skeleton.tsx +0 -347
  234. package/src/renderer/components/StatusIndicator.tsx +0 -86
  235. package/src/renderer/components/ThemeProvider.tsx +0 -429
  236. package/src/renderer/components/Tooltip.tsx +0 -370
  237. package/src/renderer/components/TranscriptionPreview.tsx +0 -183
  238. package/src/renderer/components/TranscriptionTierSelector.tsx +0 -640
  239. package/src/renderer/components/UpdateNotification.tsx +0 -377
  240. package/src/renderer/components/WindowSelector.tsx +0 -947
  241. package/src/renderer/components/index.ts +0 -99
  242. package/src/renderer/components/primitives/ApiKeyInput.tsx +0 -98
  243. package/src/renderer/components/primitives/ColorPicker.tsx +0 -65
  244. package/src/renderer/components/primitives/DangerButton.tsx +0 -45
  245. package/src/renderer/components/primitives/DirectoryPicker.tsx +0 -41
  246. package/src/renderer/components/primitives/Dropdown.tsx +0 -34
  247. package/src/renderer/components/primitives/KeyRecorder.tsx +0 -117
  248. package/src/renderer/components/primitives/SettingsSection.tsx +0 -32
  249. package/src/renderer/components/primitives/Slider.tsx +0 -43
  250. package/src/renderer/components/primitives/Toggle.tsx +0 -36
  251. package/src/renderer/components/primitives/index.ts +0 -10
  252. package/src/renderer/components/settings/AdvancedTab.tsx +0 -174
  253. package/src/renderer/components/settings/AppearanceTab.tsx +0 -77
  254. package/src/renderer/components/settings/GeneralTab.tsx +0 -40
  255. package/src/renderer/components/settings/HotkeysTab.tsx +0 -79
  256. package/src/renderer/components/settings/RecordingTab.tsx +0 -84
  257. package/src/renderer/components/settings/index.ts +0 -9
  258. package/src/renderer/components/settings/settingsStyles.ts +0 -673
  259. package/src/renderer/components/settings/tabConfig.tsx +0 -85
  260. package/src/renderer/components/settings/useSettingsPanel.ts +0 -447
  261. package/src/renderer/contexts/ProcessingContext.tsx +0 -227
  262. package/src/renderer/contexts/RecordingContext.tsx +0 -683
  263. package/src/renderer/contexts/UIContext.tsx +0 -326
  264. package/src/renderer/contexts/index.ts +0 -24
  265. package/src/renderer/donateMessages.ts +0 -69
  266. package/src/renderer/hooks/index.ts +0 -75
  267. package/src/renderer/hooks/useAnimation.tsx +0 -544
  268. package/src/renderer/hooks/useTheme.ts +0 -313
  269. package/src/renderer/index.html +0 -26
  270. package/src/renderer/main.tsx +0 -52
  271. package/src/renderer/styles/animations.css +0 -1093
  272. package/src/renderer/styles/app-shell.css +0 -662
  273. package/src/renderer/styles/globals.css +0 -515
  274. package/src/renderer/styles/theme.ts +0 -578
  275. package/src/renderer/types/electron.d.ts +0 -385
  276. package/src/shared/hotkeys.ts +0 -283
  277. package/src/shared/types.ts +0 -809
  278. package/tests/clipboard.test.ts +0 -228
  279. package/tests/e2e/criticalPaths.test.ts +0 -594
  280. package/tests/feedbackAnalyzer.test.ts +0 -303
  281. package/tests/integration/sessionFlow.test.ts +0 -583
  282. package/tests/markdownGenerator.test.ts +0 -418
  283. package/tests/output.test.ts +0 -96
  284. package/tests/setup.ts +0 -486
  285. package/tests/unit/appIntegration.test.ts +0 -676
  286. package/tests/unit/appViewState.test.ts +0 -281
  287. package/tests/unit/audioIpcChannels.test.ts +0 -17
  288. package/tests/unit/exportService.test.ts +0 -492
  289. package/tests/unit/hotkeys.test.ts +0 -92
  290. package/tests/unit/navigationPreload.test.ts +0 -94
  291. package/tests/unit/onboardingFlow.test.ts +0 -345
  292. package/tests/unit/permissionManager.test.ts +0 -175
  293. package/tests/unit/permissionManagerExpanded.test.ts +0 -296
  294. package/tests/unit/screenRecordingRenderer.test.ts +0 -368
  295. package/tests/unit/sessionController.test.ts +0 -515
  296. package/tests/unit/tierManager.test.ts +0 -61
  297. package/tests/unit/tierManagerExpanded.test.ts +0 -142
  298. package/tests/unit/transcriptAnalyzer.test.ts +0 -64
  299. package/tsconfig.json +0 -25
  300. package/vitest.config.ts +0 -46
@@ -1,978 +0,0 @@
1
- /**
2
- * AudioCapture.ts - Production Audio Capture Service for markupr
3
- *
4
- * Architecture:
5
- * - Renderer process captures audio via getUserMedia (browser API)
6
- * - Audio data streams to main process via IPC
7
- * - Main process handles buffering, VAD, and recovery
8
- *
9
- * Why this approach:
10
- * - getUserMedia is only available in renderer (browser context)
11
- * - Main process provides reliability, buffering, and transcription coordination
12
- * - IPC overhead is minimal for 100ms chunks at 16kHz mono
13
- */
14
-
15
- import { ipcMain, systemPreferences, BrowserWindow } from 'electron';
16
- import { EventEmitter } from 'events';
17
- import { writeFile, unlink, mkdir } from 'fs/promises';
18
- import { existsSync } from 'fs';
19
- import { join, dirname } from 'path';
20
- import { app } from 'electron';
21
- import { errorHandler } from '../ErrorHandler';
22
- import { IPC_CHANNELS } from '../../shared/types';
23
- import { extensionFromMimeType, encodeFloat32Wav } from './audioUtils';
24
-
25
- // ============================================================================
26
- // Types and Interfaces
27
- // ============================================================================
28
-
29
- export interface AudioDevice {
30
- id: string;
31
- name: string;
32
- isDefault: boolean;
33
- }
34
-
35
- export interface AudioChunk {
36
- buffer: Buffer;
37
- timestamp: number;
38
- duration: number;
39
- sampleRate: number;
40
- }
41
-
42
- export interface CapturedAudioAsset {
43
- buffer: Buffer;
44
- mimeType: string;
45
- durationMs: number;
46
- }
47
-
48
- export interface AudioCaptureConfig {
49
- sampleRate: number;
50
- channels: number;
51
- chunkDurationMs: number;
52
- vadThreshold: number;
53
- vadSilenceMs: number;
54
- recoveryBufferMinutes: number;
55
- }
56
-
57
- export interface AudioCaptureService {
58
- checkPermission(): Promise<boolean>;
59
- requestPermission(): Promise<boolean>;
60
- getDevices(): Promise<AudioDevice[]>;
61
- setDevice(deviceId: string): void;
62
- setPaused(paused: boolean): void;
63
- start(): Promise<void>;
64
- stop(): Promise<void>;
65
- getAudioLevel(): number;
66
- isCapturing(): boolean;
67
- getCapturedAudioBuffer(): Buffer | null;
68
- getCapturedAudioAsset(): CapturedAudioAsset | null;
69
- exportCapturedAudio(
70
- filePathBase: string
71
- ): Promise<{ path: string; bytesWritten: number; durationMs: number; mimeType: string } | null>;
72
- exportCapturedAudioWav(filePath: string): Promise<{ bytesWritten: number; durationMs: number } | null>;
73
- clearCapturedAudio(): void;
74
-
75
- // Event handlers
76
- onAudioChunk: (callback: (chunk: AudioChunk) => void) => () => void;
77
- onVoiceActivity: (callback: (active: boolean) => void) => () => void;
78
- onError: (callback: (error: Error) => void) => () => void;
79
- onAudioLevel: (callback: (level: number) => void) => () => void;
80
- }
81
-
82
- // ============================================================================
83
- // IPC Channel Constants
84
- // ============================================================================
85
-
86
- export const AUDIO_IPC_CHANNELS = {
87
- // Main -> Renderer requests
88
- REQUEST_DEVICES: IPC_CHANNELS.AUDIO_REQUEST_DEVICES,
89
- START_CAPTURE: IPC_CHANNELS.AUDIO_START_CAPTURE,
90
- STOP_CAPTURE: IPC_CHANNELS.AUDIO_STOP_CAPTURE,
91
- SET_DEVICE: IPC_CHANNELS.AUDIO_SET_DEVICE,
92
-
93
- // Renderer -> Main data
94
- AUDIO_CHUNK: IPC_CHANNELS.AUDIO_CHUNK,
95
- DEVICES_RESPONSE: IPC_CHANNELS.AUDIO_DEVICES_RESPONSE,
96
- CAPTURE_ERROR: IPC_CHANNELS.AUDIO_CAPTURE_ERROR,
97
- CAPTURE_STARTED: IPC_CHANNELS.AUDIO_CAPTURE_STARTED,
98
- CAPTURE_STOPPED: IPC_CHANNELS.AUDIO_CAPTURE_STOPPED,
99
- } as const;
100
-
101
- // ============================================================================
102
- // Default Configuration
103
- // ============================================================================
104
-
105
- const DEFAULT_CONFIG: AudioCaptureConfig = {
106
- sampleRate: 16000,
107
- channels: 1, // Mono
108
- chunkDurationMs: 250,
109
- vadThreshold: 0.01, // RMS threshold for voice detection
110
- vadSilenceMs: 600, // Consecutive silence before marking inactive
111
- recoveryBufferMinutes: 5, // Rotate buffer files every 5 minutes
112
- };
113
-
114
- // ============================================================================
115
- // AudioCaptureService Implementation
116
- // ============================================================================
117
-
118
- class AudioCaptureServiceImpl extends EventEmitter implements AudioCaptureService {
119
- private config: AudioCaptureConfig;
120
- private capturing: boolean = false;
121
- private stopRequested: boolean = false;
122
- private stopFinalizeTimer: NodeJS.Timeout | null = null;
123
- private stopPromise: Promise<void> | null = null;
124
- private resolveStopPromise: (() => void) | null = null;
125
- private currentDeviceId: string | null = null;
126
- private currentAudioLevel: number = 0;
127
- private voiceActive: boolean = false;
128
- private paused: boolean = false;
129
- private silenceStartTime: number = 0;
130
- private mainWindow: BrowserWindow | null = null;
131
-
132
- // Recovery buffer management
133
- private recoveryBufferPath: string;
134
- private currentBufferFile: string | null = null;
135
- private bufferStartTime: number = 0;
136
- private recoveryChunks: Buffer[] = [];
137
- private recoveryInterval: NodeJS.Timeout | null = null;
138
-
139
- // Full-session audio capture (used for post-session transcription + retry workflows)
140
- private sessionAudioChunks: Buffer[] = [];
141
- private sessionAudioBytes: number = 0;
142
- private sessionAudioDurationMs: number = 0;
143
- private sessionAudioMimeType: string = 'audio/wav';
144
- private encodedAudioChunks: Buffer[] = [];
145
- private encodedAudioBytes: number = 0;
146
- private encodedAudioDurationMs: number = 0;
147
- private encodedAudioMimeType: string | null = null;
148
-
149
- constructor(config: Partial<AudioCaptureConfig> = {}) {
150
- super();
151
- this.config = { ...DEFAULT_CONFIG, ...config };
152
- this.recoveryBufferPath = join(app.getPath('temp'), 'markupr-audio');
153
- this.setupIPCHandlers();
154
- }
155
-
156
- /**
157
- * Set the main window reference for IPC communication
158
- */
159
- setMainWindow(window: BrowserWindow): void {
160
- this.mainWindow = window;
161
- }
162
-
163
- // ==========================================================================
164
- // Permission Management
165
- // ==========================================================================
166
-
167
- /**
168
- * Check if microphone permission is granted (macOS only)
169
- */
170
- async checkPermission(): Promise<boolean> {
171
- if (process.platform !== 'darwin') {
172
- // Non-macOS platforms don't have system-level permission checks
173
- return true;
174
- }
175
-
176
- const status = systemPreferences.getMediaAccessStatus('microphone');
177
- const granted = status === 'granted';
178
-
179
- if (!granted) {
180
- errorHandler.log('info', 'Microphone permission not granted', {
181
- component: 'AudioCapture',
182
- operation: 'checkPermission',
183
- data: { status },
184
- });
185
- }
186
-
187
- return granted;
188
- }
189
-
190
- /**
191
- * Request microphone permission (macOS only)
192
- * Returns true if granted, false if denied
193
- */
194
- async requestPermission(): Promise<boolean> {
195
- if (process.platform !== 'darwin') {
196
- return true;
197
- }
198
-
199
- const status = systemPreferences.getMediaAccessStatus('microphone');
200
-
201
- if (status === 'granted') {
202
- return true;
203
- }
204
-
205
- if (status === 'denied') {
206
- // User previously denied, they need to enable in System Preferences
207
- return false;
208
- }
209
-
210
- // Status is 'not-determined' or 'restricted', request permission
211
- try {
212
- const granted = await systemPreferences.askForMediaAccess('microphone');
213
-
214
- if (!granted) {
215
- errorHandler.log('warn', 'Microphone permission denied by user', {
216
- component: 'AudioCapture',
217
- operation: 'requestPermission',
218
- });
219
- }
220
-
221
- return granted;
222
- } catch (error) {
223
- errorHandler.log('error', 'Permission request failed', {
224
- component: 'AudioCapture',
225
- operation: 'requestPermission',
226
- error: (error as Error).message,
227
- });
228
- return false;
229
- }
230
- }
231
-
232
- // ==========================================================================
233
- // Device Management
234
- // ==========================================================================
235
-
236
- /**
237
- * Get list of available audio input devices
238
- * This requests device list from renderer via IPC
239
- */
240
- async getDevices(): Promise<AudioDevice[]> {
241
- return new Promise((resolve, reject) => {
242
- if (!this.mainWindow) {
243
- reject(new Error('Main window not set'));
244
- return;
245
- }
246
-
247
- const timeout = setTimeout(() => {
248
- reject(new Error('Device enumeration timeout'));
249
- }, 5000);
250
-
251
- const handler = (_event: Electron.IpcMainEvent, devices: AudioDevice[]) => {
252
- clearTimeout(timeout);
253
- ipcMain.removeListener(AUDIO_IPC_CHANNELS.DEVICES_RESPONSE, handler);
254
- resolve(devices);
255
- };
256
-
257
- ipcMain.on(AUDIO_IPC_CHANNELS.DEVICES_RESPONSE, handler);
258
- this.mainWindow.webContents.send(AUDIO_IPC_CHANNELS.REQUEST_DEVICES);
259
- });
260
- }
261
-
262
- /**
263
- * Set the audio input device to use
264
- */
265
- setDevice(deviceId: string): void {
266
- this.currentDeviceId = deviceId;
267
- if (this.capturing && this.mainWindow) {
268
- // If already capturing, notify renderer to switch device
269
- this.mainWindow.webContents.send(AUDIO_IPC_CHANNELS.SET_DEVICE, deviceId);
270
- }
271
- }
272
-
273
- // ==========================================================================
274
- // Capture Control
275
- // ==========================================================================
276
-
277
- /**
278
- * Start audio capture
279
- */
280
- async start(): Promise<void> {
281
- if (this.capturing) {
282
- errorHandler.log('info', 'Audio capture already in progress', {
283
- component: 'AudioCapture',
284
- operation: 'start',
285
- });
286
- return;
287
- }
288
-
289
- // Check permission first
290
- const hasPermission = await this.checkPermission();
291
- if (!hasPermission) {
292
- const granted = await this.requestPermission();
293
- if (!granted) {
294
- const permError = new Error('Microphone permission denied');
295
- errorHandler.handleAudioError(permError, {
296
- component: 'AudioCapture',
297
- operation: 'start',
298
- });
299
- throw permError;
300
- }
301
- }
302
-
303
- if (!this.mainWindow) {
304
- const windowError = new Error('Main window not set');
305
- errorHandler.log('error', 'Cannot start audio - no main window', {
306
- component: 'AudioCapture',
307
- operation: 'start',
308
- });
309
- throw windowError;
310
- }
311
-
312
- return new Promise((resolve, reject) => {
313
- const timeout = setTimeout(() => {
314
- reject(new Error('Audio capture start timeout'));
315
- }, 10000);
316
-
317
- const successHandler = () => {
318
- clearTimeout(timeout);
319
- ipcMain.removeListener(AUDIO_IPC_CHANNELS.CAPTURE_STARTED, successHandler);
320
- ipcMain.removeListener(AUDIO_IPC_CHANNELS.CAPTURE_ERROR, errorHandler);
321
-
322
- this.capturing = true;
323
- this.stopRequested = false;
324
- this.settleStopPromise();
325
- if (this.stopFinalizeTimer) {
326
- clearTimeout(this.stopFinalizeTimer);
327
- this.stopFinalizeTimer = null;
328
- }
329
- this.paused = false;
330
- this.sessionAudioChunks = [];
331
- this.sessionAudioBytes = 0;
332
- this.sessionAudioDurationMs = 0;
333
- this.sessionAudioMimeType = 'audio/wav';
334
- this.encodedAudioChunks = [];
335
- this.encodedAudioBytes = 0;
336
- this.encodedAudioDurationMs = 0;
337
- this.encodedAudioMimeType = null;
338
- this.startRecoveryBuffer();
339
- console.log('[AudioCapture] Capture started');
340
- resolve();
341
- };
342
-
343
- const errorHandler = (_event: Electron.IpcMainEvent, error: string) => {
344
- clearTimeout(timeout);
345
- ipcMain.removeListener(AUDIO_IPC_CHANNELS.CAPTURE_STARTED, successHandler);
346
- ipcMain.removeListener(AUDIO_IPC_CHANNELS.CAPTURE_ERROR, errorHandler);
347
- reject(new Error(error));
348
- };
349
-
350
- ipcMain.once(AUDIO_IPC_CHANNELS.CAPTURE_STARTED, successHandler);
351
- ipcMain.once(AUDIO_IPC_CHANNELS.CAPTURE_ERROR, errorHandler);
352
-
353
- // Send start command to renderer with config
354
- this.mainWindow!.webContents.send(AUDIO_IPC_CHANNELS.START_CAPTURE, {
355
- deviceId: this.currentDeviceId,
356
- sampleRate: this.config.sampleRate,
357
- channels: this.config.channels,
358
- chunkDurationMs: this.config.chunkDurationMs,
359
- });
360
- });
361
- }
362
-
363
- /**
364
- * Stop audio capture
365
- */
366
- async stop(): Promise<void> {
367
- if (!this.capturing) {
368
- this.stopRequested = false;
369
- this.settleStopPromise();
370
- return;
371
- }
372
-
373
- if (this.stopRequested && this.stopPromise) {
374
- return this.stopPromise;
375
- }
376
-
377
- const stopPromise = this.ensureStopPromise();
378
- this.stopRequested = true;
379
- this.paused = false;
380
-
381
- if (this.mainWindow) {
382
- this.mainWindow.webContents.send(AUDIO_IPC_CHANNELS.STOP_CAPTURE);
383
- } else {
384
- this.finalizeCaptureStop('timeout');
385
- return stopPromise;
386
- }
387
-
388
- if (this.stopFinalizeTimer) {
389
- clearTimeout(this.stopFinalizeTimer);
390
- }
391
- this.stopFinalizeTimer = setTimeout(() => {
392
- this.finalizeCaptureStop('timeout');
393
- }, 1500);
394
-
395
- console.log('[AudioCapture] Stop requested; awaiting renderer flush');
396
- return stopPromise;
397
- }
398
-
399
- /**
400
- * Check if currently capturing
401
- */
402
- isCapturing(): boolean {
403
- return this.capturing;
404
- }
405
-
406
- setPaused(paused: boolean): void {
407
- this.paused = paused;
408
- if (paused) {
409
- this.voiceActive = false;
410
- this.currentAudioLevel = 0;
411
- this.emit('audioLevel', 0);
412
- this.emit('voiceActivity', false);
413
- }
414
- }
415
-
416
- /**
417
- * Get current audio level (0-1 normalized)
418
- */
419
- getAudioLevel(): number {
420
- return this.currentAudioLevel;
421
- }
422
-
423
- /**
424
- * Export captured audio with the most accurate available source format.
425
- * Encoded MediaRecorder audio is preserved as-is; PCM falls back to WAV.
426
- */
427
- async exportCapturedAudio(
428
- filePathBase: string
429
- ): Promise<{ path: string; bytesWritten: number; durationMs: number; mimeType: string } | null> {
430
- const encodedAsset = this.getCapturedEncodedAudioAsset();
431
- if (encodedAsset) {
432
- const extension = extensionFromMimeType(encodedAsset.mimeType);
433
- const outputPath = `${filePathBase}${extension}`;
434
- await mkdir(dirname(outputPath), { recursive: true });
435
- await writeFile(outputPath, encodedAsset.buffer);
436
- return {
437
- path: outputPath,
438
- bytesWritten: encodedAsset.buffer.byteLength,
439
- durationMs: encodedAsset.durationMs,
440
- mimeType: encodedAsset.mimeType,
441
- };
442
- }
443
-
444
- const pcmAsset = this.getCapturedPcmAudioAsset();
445
- if (!pcmAsset) {
446
- return null;
447
- }
448
-
449
- const outputPath = `${filePathBase}.wav`;
450
- await mkdir(dirname(outputPath), { recursive: true });
451
- await writeFile(outputPath, pcmAsset.buffer);
452
- return {
453
- path: outputPath,
454
- bytesWritten: pcmAsset.buffer.byteLength,
455
- durationMs: pcmAsset.durationMs,
456
- mimeType: 'audio/wav',
457
- };
458
- }
459
-
460
- /**
461
- * Backward-compatible WAV export wrapper.
462
- */
463
- async exportCapturedAudioWav(
464
- filePath: string
465
- ): Promise<{ bytesWritten: number; durationMs: number } | null> {
466
- const rawAudio = this.getCapturedAudioBuffer();
467
- if (!rawAudio) {
468
- return null;
469
- }
470
-
471
- const wavBuffer = encodeFloat32Wav(rawAudio, this.config.sampleRate, this.config.channels);
472
- await mkdir(dirname(filePath), { recursive: true });
473
- await writeFile(filePath, wavBuffer);
474
-
475
- const durationMs =
476
- (rawAudio.byteLength / (this.config.channels * this.config.sampleRate * 4)) * 1000;
477
- return {
478
- bytesWritten: wavBuffer.byteLength,
479
- durationMs,
480
- };
481
- }
482
-
483
- /**
484
- * Clear in-memory session audio data.
485
- */
486
- clearCapturedAudio(): void {
487
- this.sessionAudioChunks = [];
488
- this.sessionAudioBytes = 0;
489
- this.sessionAudioDurationMs = 0;
490
- this.sessionAudioMimeType = 'audio/wav';
491
- this.encodedAudioChunks = [];
492
- this.encodedAudioBytes = 0;
493
- this.encodedAudioDurationMs = 0;
494
- this.encodedAudioMimeType = null;
495
- }
496
-
497
- // ==========================================================================
498
- // Event Subscription
499
- // ==========================================================================
500
-
501
- onAudioChunk(callback: (chunk: AudioChunk) => void): () => void {
502
- this.on('audioChunk', callback);
503
- return () => this.off('audioChunk', callback);
504
- }
505
-
506
- onVoiceActivity(callback: (active: boolean) => void): () => void {
507
- this.on('voiceActivity', callback);
508
- return () => this.off('voiceActivity', callback);
509
- }
510
-
511
- onError(callback: (error: Error) => void): () => void {
512
- this.on('error', callback);
513
- return () => this.off('error', callback);
514
- }
515
-
516
- onAudioLevel(callback: (level: number) => void): () => void {
517
- this.on('audioLevel', callback);
518
- return () => this.off('audioLevel', callback);
519
- }
520
-
521
- // ==========================================================================
522
- // IPC Handlers
523
- // ==========================================================================
524
-
525
- private setupIPCHandlers(): void {
526
- // Handle incoming audio chunks from renderer
527
- ipcMain.on(AUDIO_IPC_CHANNELS.AUDIO_CHUNK, this.handleAudioChunk.bind(this));
528
-
529
- // Handle capture errors from renderer
530
- ipcMain.on(AUDIO_IPC_CHANNELS.CAPTURE_ERROR, (_event, error: string) => {
531
- const captureError = new Error(error);
532
- errorHandler.handleAudioError(captureError, {
533
- component: 'AudioCapture',
534
- operation: 'rendererCapture',
535
- });
536
- this.emit('error', captureError);
537
- });
538
-
539
- // Handle capture stopped (e.g., device disconnected)
540
- ipcMain.on(AUDIO_IPC_CHANNELS.CAPTURE_STOPPED, () => {
541
- if (this.stopRequested) {
542
- this.finalizeCaptureStop('normal');
543
- return;
544
- }
545
-
546
- if (this.capturing) {
547
- this.capturing = false;
548
- this.stopRecoveryBuffer();
549
- const stopError = new Error('Audio capture stopped unexpectedly');
550
- errorHandler.handleAudioError(stopError, {
551
- component: 'AudioCapture',
552
- operation: 'captureStop',
553
- data: { unexpected: true },
554
- });
555
- this.emit('error', stopError);
556
- }
557
- });
558
- }
559
-
560
- private finalizeCaptureStop(reason: 'normal' | 'timeout'): void {
561
- if (!this.capturing && !this.stopRequested) {
562
- this.settleStopPromise();
563
- return;
564
- }
565
-
566
- if (this.stopFinalizeTimer) {
567
- clearTimeout(this.stopFinalizeTimer);
568
- this.stopFinalizeTimer = null;
569
- }
570
-
571
- this.stopRequested = false;
572
- this.capturing = false;
573
- this.stopRecoveryBuffer();
574
-
575
- this.voiceActive = false;
576
- this.currentAudioLevel = 0;
577
- this.emit('audioLevel', 0);
578
- this.emit('voiceActivity', false);
579
-
580
- console.log(`[AudioCapture] Capture stopped (${reason})`);
581
- this.settleStopPromise();
582
- }
583
-
584
- private ensureStopPromise(): Promise<void> {
585
- if (this.stopPromise) {
586
- return this.stopPromise;
587
- }
588
-
589
- this.stopPromise = new Promise<void>((resolve) => {
590
- this.resolveStopPromise = resolve;
591
- });
592
- return this.stopPromise;
593
- }
594
-
595
- private settleStopPromise(): void {
596
- if (this.resolveStopPromise) {
597
- this.resolveStopPromise();
598
- this.resolveStopPromise = null;
599
- }
600
- this.stopPromise = null;
601
- }
602
-
603
- /**
604
- * Process incoming audio chunk from renderer
605
- */
606
- private handleAudioChunk(
607
- _event: Electron.IpcMainEvent,
608
- data: {
609
- samples?: number[];
610
- encodedChunk?: Buffer | Uint8Array | ArrayBuffer;
611
- mimeType?: string;
612
- audioLevel?: number;
613
- rms?: number;
614
- timestamp: number;
615
- duration: number;
616
- }
617
- ): void {
618
- if (!this.capturing) return;
619
-
620
- if (this.paused) {
621
- return;
622
- }
623
-
624
- if (Array.isArray(data.samples) && data.samples.length > 0) {
625
- const float32 = new Float32Array(data.samples);
626
- const buffer = Buffer.from(float32.buffer);
627
-
628
- // Calculate RMS for VAD and level visualization
629
- const rms = this.calculateRMS(float32);
630
- this.currentAudioLevel = Math.min(1, rms * 10);
631
- this.emit('audioLevel', this.currentAudioLevel);
632
-
633
- // Voice Activity Detection
634
- this.updateVAD(rms, data.timestamp);
635
-
636
- // Create chunk object
637
- const chunk: AudioChunk = {
638
- buffer,
639
- timestamp: data.timestamp,
640
- duration: data.duration,
641
- sampleRate: this.config.sampleRate,
642
- };
643
-
644
- // Add to recovery/session buffers
645
- this.recoveryChunks.push(buffer);
646
- this.sessionAudioChunks.push(buffer);
647
- this.sessionAudioBytes += buffer.byteLength;
648
- this.sessionAudioDurationMs += Math.max(0, data.duration || this.config.chunkDurationMs);
649
- this.sessionAudioMimeType = 'audio/wav';
650
-
651
- this.emit('audioChunk', chunk);
652
- return;
653
- }
654
-
655
- const encodedBuffer = this.toBuffer(data.encodedChunk);
656
- if (!encodedBuffer || encodedBuffer.byteLength === 0) {
657
- return;
658
- }
659
-
660
- this.encodedAudioChunks.push(encodedBuffer);
661
- this.encodedAudioBytes += encodedBuffer.byteLength;
662
- this.encodedAudioDurationMs += Math.max(0, data.duration || this.config.chunkDurationMs);
663
- this.encodedAudioMimeType = data.mimeType || this.encodedAudioMimeType || 'audio/webm';
664
- this.recoveryChunks.push(encodedBuffer);
665
-
666
- // Prefer renderer-provided live RMS/level from real audio analysis.
667
- const level =
668
- Number.isFinite(data.audioLevel)
669
- ? Math.max(0, Math.min(1, Number(data.audioLevel)))
670
- : Math.max(0, Math.min(1, encodedBuffer.byteLength / 6000));
671
- this.currentAudioLevel = level;
672
- this.emit('audioLevel', level);
673
-
674
- const rms =
675
- Number.isFinite(data.rms)
676
- ? Math.max(0, Math.min(1, Number(data.rms)))
677
- : Math.max(0, (level - 0.08) * 0.06);
678
- this.updateVAD(rms, data.timestamp);
679
- }
680
-
681
- // ==========================================================================
682
- // Voice Activity Detection
683
- // ==========================================================================
684
-
685
- /**
686
- * Calculate Root Mean Square of audio samples
687
- */
688
- private calculateRMS(samples: Float32Array): number {
689
- let sum = 0;
690
- for (let i = 0; i < samples.length; i++) {
691
- sum += samples[i] * samples[i];
692
- }
693
- return Math.sqrt(sum / samples.length);
694
- }
695
-
696
- /**
697
- * Update voice activity state based on RMS
698
- */
699
- private updateVAD(rms: number, timestamp: number): void {
700
- const isVoice = rms > this.config.vadThreshold;
701
-
702
- if (isVoice) {
703
- // Voice detected
704
- if (!this.voiceActive) {
705
- this.voiceActive = true;
706
- this.emit('voiceActivity', true);
707
- console.log('[AudioCapture] Voice activity started');
708
- }
709
- this.silenceStartTime = 0;
710
- } else {
711
- // Silence detected
712
- if (this.voiceActive) {
713
- if (this.silenceStartTime === 0) {
714
- this.silenceStartTime = timestamp;
715
- } else if (timestamp - this.silenceStartTime > this.config.vadSilenceMs) {
716
- // Enough silence, mark as inactive
717
- this.voiceActive = false;
718
- this.emit('voiceActivity', false);
719
- this.silenceStartTime = 0;
720
- console.log('[AudioCapture] Voice activity ended');
721
- }
722
- }
723
- }
724
- }
725
-
726
- // ==========================================================================
727
- // Recovery Buffer Management
728
- // ==========================================================================
729
-
730
- /**
731
- * Start the recovery buffer system
732
- * Writes audio to temp files for network failure recovery
733
- */
734
- private async startRecoveryBuffer(): Promise<void> {
735
- // Ensure recovery directory exists
736
- if (!existsSync(this.recoveryBufferPath)) {
737
- await mkdir(this.recoveryBufferPath, { recursive: true });
738
- }
739
-
740
- this.bufferStartTime = Date.now();
741
- this.recoveryChunks = [];
742
- this.currentBufferFile = this.generateBufferFilename();
743
-
744
- // Rotate buffer every recoveryBufferMinutes
745
- this.recoveryInterval = setInterval(
746
- () => this.rotateRecoveryBuffer(),
747
- this.config.recoveryBufferMinutes * 60 * 1000
748
- );
749
-
750
- console.log('[AudioCapture] Recovery buffer started');
751
- }
752
-
753
- /**
754
- * Stop the recovery buffer system
755
- */
756
- private stopRecoveryBuffer(): void {
757
- if (this.recoveryInterval) {
758
- clearInterval(this.recoveryInterval);
759
- this.recoveryInterval = null;
760
- }
761
-
762
- // Write remaining chunks
763
- if (this.recoveryChunks.length > 0) {
764
- this.writeRecoveryBuffer().catch((err) => {
765
- console.error('[AudioCapture] Failed to write final recovery buffer:', err);
766
- });
767
- }
768
-
769
- this.recoveryChunks = [];
770
- console.log('[AudioCapture] Recovery buffer stopped');
771
- }
772
-
773
- /**
774
- * Rotate the recovery buffer - write current and start new
775
- */
776
- private async rotateRecoveryBuffer(): Promise<void> {
777
- await this.writeRecoveryBuffer();
778
-
779
- // Clean up old buffer files (keep last 2)
780
- await this.cleanOldBuffers();
781
-
782
- // Start new buffer
783
- this.bufferStartTime = Date.now();
784
- this.recoveryChunks = [];
785
- this.currentBufferFile = this.generateBufferFilename();
786
- }
787
-
788
- /**
789
- * Write current recovery buffer to disk
790
- */
791
- private async writeRecoveryBuffer(): Promise<void> {
792
- if (this.recoveryChunks.length === 0 || !this.currentBufferFile) {
793
- return;
794
- }
795
-
796
- try {
797
- const combined = Buffer.concat(this.recoveryChunks);
798
- await writeFile(this.currentBufferFile, combined);
799
- console.log(
800
- `[AudioCapture] Recovery buffer written: ${this.currentBufferFile} (${combined.length} bytes)`
801
- );
802
- } catch (error) {
803
- console.error('[AudioCapture] Failed to write recovery buffer:', error);
804
- }
805
- }
806
-
807
- /**
808
- * Generate a unique buffer filename
809
- */
810
- private generateBufferFilename(): string {
811
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
812
- return join(this.recoveryBufferPath, `audio-${timestamp}.raw`);
813
- }
814
-
815
- /**
816
- * Clean up old recovery buffer files
817
- */
818
- private async cleanOldBuffers(): Promise<void> {
819
- try {
820
- const { readdir, stat } = await import('fs/promises');
821
- const files = await readdir(this.recoveryBufferPath);
822
-
823
- const bufferFiles = await Promise.all(
824
- files
825
- .filter((f) => f.startsWith('audio-') && f.endsWith('.raw'))
826
- .map(async (f) => {
827
- const path = join(this.recoveryBufferPath, f);
828
- const stats = await stat(path);
829
- return { path, mtime: stats.mtime.getTime() };
830
- })
831
- );
832
-
833
- // Sort by modification time, newest first
834
- bufferFiles.sort((a, b) => b.mtime - a.mtime);
835
-
836
- // Delete all but the 2 most recent
837
- for (let i = 2; i < bufferFiles.length; i++) {
838
- await unlink(bufferFiles[i].path);
839
- console.log(`[AudioCapture] Deleted old buffer: ${bufferFiles[i].path}`);
840
- }
841
- } catch (error) {
842
- console.error('[AudioCapture] Failed to clean old buffers:', error);
843
- }
844
- }
845
-
846
- /**
847
- * Get recovery buffers for replay after network failure
848
- * Returns buffers from the last N minutes
849
- */
850
- async getRecoveryBuffers(lastMinutes: number = 5): Promise<Buffer[]> {
851
- const buffers: Buffer[] = [];
852
- const cutoff = Date.now() - lastMinutes * 60 * 1000;
853
-
854
- try {
855
- const { readdir, stat, readFile } = await import('fs/promises');
856
- const files = await readdir(this.recoveryBufferPath);
857
-
858
- for (const file of files) {
859
- if (!file.startsWith('audio-') || !file.endsWith('.raw')) continue;
860
-
861
- const path = join(this.recoveryBufferPath, file);
862
- const stats = await stat(path);
863
-
864
- if (stats.mtime.getTime() > cutoff) {
865
- const data = await readFile(path);
866
- buffers.push(data);
867
- }
868
- }
869
-
870
- // Include current in-memory chunks
871
- if (this.recoveryChunks.length > 0) {
872
- buffers.push(Buffer.concat(this.recoveryChunks));
873
- }
874
-
875
- console.log(`[AudioCapture] Retrieved ${buffers.length} recovery buffers`);
876
- } catch (error) {
877
- console.error('[AudioCapture] Failed to get recovery buffers:', error);
878
- }
879
-
880
- return buffers;
881
- }
882
-
883
- /**
884
- * Clear all recovery buffers
885
- */
886
- async clearRecoveryBuffers(): Promise<void> {
887
- try {
888
- const { readdir } = await import('fs/promises');
889
- const files = await readdir(this.recoveryBufferPath);
890
-
891
- for (const file of files) {
892
- if (file.startsWith('audio-') && file.endsWith('.raw')) {
893
- await unlink(join(this.recoveryBufferPath, file));
894
- }
895
- }
896
-
897
- this.recoveryChunks = [];
898
- console.log('[AudioCapture] Recovery buffers cleared');
899
- } catch (error) {
900
- console.error('[AudioCapture] Failed to clear recovery buffers:', error);
901
- }
902
- }
903
-
904
- /**
905
- * Build a single buffer from all captured session chunks.
906
- */
907
- getCapturedAudioBuffer(): Buffer | null {
908
- if (this.sessionAudioChunks.length === 0 || this.sessionAudioBytes === 0) {
909
- return null;
910
- }
911
- return Buffer.concat(this.sessionAudioChunks, this.sessionAudioBytes);
912
- }
913
-
914
- getCapturedAudioAsset(): CapturedAudioAsset | null {
915
- const encoded = this.getCapturedEncodedAudioAsset();
916
- if (encoded) {
917
- return encoded;
918
- }
919
-
920
- return this.getCapturedPcmAudioAsset();
921
- }
922
-
923
- private getCapturedPcmAudioAsset(): CapturedAudioAsset | null {
924
- const rawAudio = this.getCapturedAudioBuffer();
925
- if (!rawAudio) {
926
- return null;
927
- }
928
-
929
- const wavBuffer = encodeFloat32Wav(rawAudio, this.config.sampleRate, this.config.channels);
930
- const durationMs =
931
- this.sessionAudioDurationMs > 0
932
- ? this.sessionAudioDurationMs
933
- : (rawAudio.byteLength / (this.config.channels * this.config.sampleRate * 4)) * 1000;
934
-
935
- return {
936
- buffer: wavBuffer,
937
- mimeType: 'audio/wav',
938
- durationMs,
939
- };
940
- }
941
-
942
- private getCapturedEncodedAudioAsset(): CapturedAudioAsset | null {
943
- if (this.encodedAudioChunks.length === 0 || this.encodedAudioBytes === 0) {
944
- return null;
945
- }
946
-
947
- return {
948
- buffer: Buffer.concat(this.encodedAudioChunks, this.encodedAudioBytes),
949
- mimeType: this.encodedAudioMimeType || 'audio/webm',
950
- durationMs: this.encodedAudioDurationMs,
951
- };
952
- }
953
-
954
- private toBuffer(chunk: Buffer | Uint8Array | ArrayBuffer | undefined): Buffer | null {
955
- if (!chunk) {
956
- return null;
957
- }
958
- if (Buffer.isBuffer(chunk)) {
959
- return chunk;
960
- }
961
- if (chunk instanceof ArrayBuffer) {
962
- return Buffer.from(chunk);
963
- }
964
- if (ArrayBuffer.isView(chunk)) {
965
- return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
966
- }
967
- return null;
968
- }
969
-
970
- }
971
-
972
- // ============================================================================
973
- // Singleton Export
974
- // ============================================================================
975
-
976
- export const audioCapture = new AudioCaptureServiceImpl();
977
- export { AudioCaptureServiceImpl };
978
- export default audioCapture;