-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path05-robust-capture.ts
More file actions
102 lines (88 loc) · 3.67 KB
/
05-robust-capture.ts
File metadata and controls
102 lines (88 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { AudioCapture, AudioCaptureError, ErrorCode, type AudioSample, type ApplicationInfo, type CaptureStatus } from '../../src/index';
// Global error handlers for test suite
process.on('uncaughtException', (err) => {
console.error('❌ Uncaught Exception:', err.message);
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
console.error('❌ Unhandled Rejection:', reason);
process.exit(1);
});
class RobustAudioCapture {
private appName: string;
private capture: AudioCapture;
constructor(appName: string) {
this.appName = appName;
this.capture = new AudioCapture();
this.setupHandlers();
}
async start(): Promise<void> {
// Verify permissions first
const perms = AudioCapture.verifyPermissions();
if (!perms.granted) {
throw new Error(`Permissions not granted: ${perms.message}`);
}
// Start with error handling
try {
this.capture.startCapture(this.appName, {
minVolume: 0.01,
format: 'int16',
channels: 1
});
// Verify we're actually capturing
const status: CaptureStatus | null = this.capture.getStatus();
console.log(`Started capturing from: ${status?.app?.applicationName}`);
} catch (err) {
if (AudioCaptureError.isAudioCaptureError(err) && err.code === ErrorCode.APP_NOT_FOUND) {
// Try to find similar app
const apps: ApplicationInfo[] = this.capture.getApplications();
const similar = apps.find((app) =>
app.applicationName.toLowerCase().includes(this.appName.toLowerCase())
);
if (similar) {
console.log(`Trying ${similar.applicationName} instead...`);
this.capture.startCapture(similar.applicationName);
} else {
// Fallback to any audio app for this example to ensure it runs
const audioApps = this.capture.getAudioApps();
if (audioApps.length > 0) {
console.log(`Requested app not found. Fallback to: ${audioApps[0].applicationName}`);
this.capture.startCapture(audioApps[0].applicationName);
} else {
const available = err.details?.availableApps as string[] | undefined;
throw new Error(`App not found. Available: ${available?.join(', ')}`);
}
}
} else {
throw err;
}
}
}
private setupHandlers(): void {
this.capture.on('audio', (sample: AudioSample) => this.handleAudio(sample));
this.capture.on('error', (err: Error) => this.handleError(err));
this.capture.on('start', ({ app }) => console.log(`Started: ${app?.applicationName}`));
this.capture.on('stop', ({ app }) => console.log(`Stopped: ${app?.applicationName}`));
}
private handleAudio(sample: AudioSample): void {
// Your audio processing here
if (Math.random() < 0.01) console.log(`[Robust] Received audio chunk`);
}
private handleError(err: Error): void {
console.error('Capture error:', err.message);
// Implement retry logic, logging, etc.
}
stop(): void {
if (this.capture.isCapturing()) {
this.capture.stopCapture();
}
}
}
// Usage
// Try to capture 'Safari' or fallback
const capture = new RobustAudioCapture('Safari');
capture.start().catch(console.error);
setTimeout(() => {
console.log('Stopping robust capture...');
capture.stop();
}, 5000);