-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-microphone.js
More file actions
589 lines (490 loc) · 16.3 KB
/
debug-microphone.js
File metadata and controls
589 lines (490 loc) · 16.3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env node
/**
* Microphone Debug Tool for Otto Assistant
* Tests and configures microphone access on Mac
*/
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
async function debugMicrophone() {
console.log("🎤 === MIKROFON DEBUG TOOL ===\n");
console.log("📋 System Information:");
console.log(`Platform: ${process.platform}`);
console.log(`Architecture: ${process.arch}`);
console.log(`Node.js: ${process.version}\n`);
// 1. Check SoX installation
console.log("1. 🔧 SoX Installation Check");
console.log("─".repeat(40));
const soxAvailable = await checkCommand('sox', ['--version']);
if (soxAvailable) {
console.log("✅ SoX ist installiert");
await showSoxVersion();
} else {
console.log("❌ SoX ist NICHT installiert");
console.log("💡 Installation: brew install sox");
return false;
}
// 2. Check Core Audio support
console.log("\n2. 🍎 Core Audio Support Check");
console.log("─".repeat(40));
const coreAudioSupport = await checkCoreAudioSupport();
if (coreAudioSupport) {
console.log("✅ Core Audio Support verfügbar");
} else {
console.log("❌ Core Audio Support NICHT verfügbar");
console.log("💡 Neuinstallation nötig: brew reinstall sox");
}
// 3. List available audio devices
console.log("\n3. 🎧 Verfügbare Audio-Geräte");
console.log("─".repeat(40));
await listAudioDevices();
// 4. Test microphone access
console.log("\n4. 🎤 Mikrofon-Zugriff Test");
console.log("─".repeat(40));
const micAccess = await testMicrophoneAccess();
if (!micAccess) {
console.log("❌ Mikrofon-Zugriff fehlgeschlagen");
await showMicrophoneTroubleshooting();
return false;
}
// 5. Test audio recording
console.log("\n5. 📹 Audio-Aufnahme Test");
console.log("─".repeat(40));
const recordingWorks = await testAudioRecording();
if (!recordingWorks) {
console.log("❌ Audio-Aufnahme fehlgeschlagen");
return false;
}
// 6. Test Whisper integration
console.log("\n6. 🤖 Whisper Integration Test");
console.log("─".repeat(40));
const whisperWorks = await testWhisperIntegration();
if (!whisperWorks) {
console.log("❌ Whisper Integration fehlgeschlagen");
await showWhisperTroubleshooting();
return false;
}
console.log("\n🎉 === ALLE TESTS BESTANDEN ===");
console.log("✅ Mikrofon ist einsatzbereit für Otto Live Mode!");
return true;
}
/**
* Check if command is available
*/
function checkCommand(command, args = []) {
return new Promise((resolve) => {
const process = spawn(command, args, { stdio: 'pipe' });
process.on('close', (code) => {
resolve(code === 0);
});
process.on('error', () => {
resolve(false);
});
setTimeout(() => {
process.kill();
resolve(false);
}, 5000);
});
}
/**
* Show SoX version info
*/
async function showSoxVersion() {
return new Promise((resolve) => {
const process = spawn('sox', ['--version'], { stdio: 'pipe' });
let output = '';
process.stdout.on('data', (data) => {
output += data.toString();
});
process.on('close', () => {
console.log(`📦 ${output.trim()}`);
resolve();
});
setTimeout(() => {
process.kill();
resolve();
}, 3000);
});
}
/**
* Check Core Audio support in SoX
*/
async function checkCoreAudioSupport() {
return new Promise((resolve) => {
const process = spawn('sox', ['--help-device'], { stdio: 'pipe' });
let output = '';
process.stdout.on('data', (data) => {
output += data.toString();
});
process.stderr.on('data', (data) => {
output += data.toString();
});
process.on('close', () => {
const hasCoreAudio = output.toLowerCase().includes('coreaudio');
if (hasCoreAudio) {
console.log("🍎 Core Audio Driver gefunden");
} else {
console.log("⚠️ Core Audio Driver nicht gefunden");
console.log("Verfügbare Formate:", output.split('\n').slice(0, 5).join('\n'));
}
resolve(hasCoreAudio);
});
setTimeout(() => {
process.kill();
resolve(false);
}, 5000);
});
}
/**
* List available audio devices
*/
async function listAudioDevices() {
console.log("🔍 Suche nach Audio-Geräten...");
// Try system_profiler for detailed device info
try {
await new Promise((resolve) => {
const process = spawn('system_profiler', ['SPAudioDataType'], { stdio: 'pipe' });
let output = '';
process.stdout.on('data', (data) => {
output += data.toString();
});
process.on('close', () => {
const lines = output.split('\n');
const devices = lines.filter(line =>
line.includes('Built-in') ||
line.includes('Microphone') ||
line.includes('Input')
);
if (devices.length > 0) {
console.log("🎤 Gefundene Audio-Eingänge:");
devices.forEach(device => {
console.log(` • ${device.trim()}`);
});
} else {
console.log("⚠️ Keine spezifischen Audio-Eingänge gefunden");
}
resolve();
});
setTimeout(() => {
process.kill();
resolve();
}, 5000);
});
} catch (error) {
console.log("⚠️ Konnte Audio-Geräte nicht auflisten");
}
// Test default device
console.log("\n🎯 Test Standard-Mikrofon...");
const defaultWorks = await testDefaultDevice();
if (defaultWorks) {
console.log("✅ Standard-Mikrofon funktioniert");
} else {
console.log("❌ Standard-Mikrofon nicht verfügbar");
}
}
/**
* Test default audio device
*/
async function testDefaultDevice() {
return new Promise((resolve) => {
console.log("🔊 Test läuft 2 Sekunden...");
const process = spawn('sox', [
'-t', 'coreaudio', '-d', // Input: default coreaudio device
'-n', // Output: null (no output file)
'trim', '0', '2' // Record for 2 seconds
], { stdio: 'pipe' });
let hasOutput = false;
process.stderr.on('data', (data) => {
const message = data.toString();
if (message.includes('Input File') || message.includes('Progress')) {
hasOutput = true;
}
// Don't log normal SoX progress messages
if (!message.includes('Progress') && !message.includes('%')) {
console.log(`📊 ${message.trim()}`);
}
});
process.on('close', (code) => {
const success = code === 0 || hasOutput;
if (success) {
console.log("✅ 2-Sekunden-Test erfolgreich");
} else {
console.log(`❌ Test fehlgeschlagen (Exit Code: ${code})`);
}
resolve(success);
});
process.on('error', (error) => {
console.log(`❌ Fehler: ${error.message}`);
resolve(false);
});
setTimeout(() => {
process.kill();
resolve(false);
}, 5000);
});
}
/**
* Test microphone access permissions
*/
async function testMicrophoneAccess() {
console.log("🔐 Prüfe Mikrofon-Berechtigungen...");
// On macOS, try to access microphone
if (process.platform === 'darwin') {
return new Promise((resolve) => {
const process = spawn('sox', [
'-t', 'coreaudio', '-d',
'-n',
'trim', '0', '0.1' // Very short test
], { stdio: 'pipe' });
let permissionDenied = false;
process.stderr.on('data', (data) => {
const message = data.toString().toLowerCase();
if (message.includes('permission') || message.includes('denied') || message.includes('not permitted')) {
permissionDenied = true;
console.log("❌ Mikrofon-Berechtigung verweigert");
}
});
process.on('close', (code) => {
if (permissionDenied) {
console.log("💡 Lösung:");
console.log(" 1. Systemeinstellungen > Sicherheit > Datenschutz");
console.log(" 2. Mikrofon > Terminal aktivieren");
console.log(" 3. Terminal neu starten");
resolve(false);
} else if (code === 0) {
console.log("✅ Mikrofon-Berechtigung OK");
resolve(true);
} else {
console.log(`⚠️ Unbekannter Fehler (Code: ${code})`);
resolve(false);
}
});
setTimeout(() => {
process.kill();
resolve(false);
}, 3000);
});
}
return true; // Assume OK on non-macOS
}
/**
* Test actual audio recording
*/
async function testAudioRecording() {
console.log("📹 Teste 5-Sekunden Audio-Aufnahme...");
console.log("🗣️ Bitte sprechen Sie jetzt...");
const testFile = path.join(__dirname, 'temp', 'mic-test.wav');
// Ensure temp directory exists
const tempDir = path.dirname(testFile);
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
return new Promise((resolve) => {
const process = spawn('sox', [
'-t', 'coreaudio', '-d', // Input: default device
'-t', 'wav', // Output format
'-r', '16000', // Sample rate
'-c', '1', // Mono
'-b', '16', // 16-bit
testFile, // Output file
'trim', '0', '5' // 5 seconds
], { stdio: 'pipe' });
let recordingStarted = false;
process.stderr.on('data', (data) => {
const message = data.toString();
if (message.includes('Input File')) {
recordingStarted = true;
console.log("🔴 Aufnahme gestartet...");
}
if (message.includes('Progress') && message.includes('%')) {
// Show progress without flooding console
const progress = message.match(/(\d+)%/);
if (progress && parseInt(progress[1]) % 20 === 0) {
console.log(`📊 ${progress[1]}% abgeschlossen`);
}
}
});
process.on('close', (code) => {
if (code === 0 && fs.existsSync(testFile)) {
const stats = fs.statSync(testFile);
console.log(`✅ Aufnahme erfolgreich: ${stats.size} Bytes`);
// Analyze the file
analyzeAudioFile(testFile).then((analysis) => {
console.log(`📊 Audio-Analyse: ${analysis}`);
// Cleanup
fs.unlinkSync(testFile);
resolve(true);
});
} else {
console.log(`❌ Aufnahme fehlgeschlagen (Code: ${code})`);
resolve(false);
}
});
process.on('error', (error) => {
console.log(`❌ Aufnahme-Fehler: ${error.message}`);
resolve(false);
});
setTimeout(() => {
if (!recordingStarted) {
console.log("❌ Aufnahme nicht gestartet - Timeout");
process.kill();
resolve(false);
}
}, 2000);
});
}
/**
* Analyze audio file
*/
async function analyzeAudioFile(filePath) {
return new Promise((resolve) => {
const process = spawn('sox', [filePath, '-n', 'stat'], { stdio: 'pipe' });
let output = '';
process.stderr.on('data', (data) => {
output += data.toString();
});
process.on('close', () => {
// Extract key statistics
const lines = output.split('\n');
const duration = lines.find(line => line.includes('Length'));
const level = lines.find(line => line.includes('Maximum amplitude'));
let analysis = '';
if (duration) analysis += duration.trim();
if (level) analysis += `, ${level.trim()}`;
resolve(analysis || 'Analyse nicht verfügbar');
});
setTimeout(() => {
process.kill();
resolve('Timeout bei Analyse');
}, 3000);
});
}
/**
* Test Whisper integration
*/
async function testWhisperIntegration() {
console.log("🤖 Prüfe Whisper Installation...");
const whisperAvailable = await checkCommand('whisper', ['--help']);
if (!whisperAvailable) {
console.log("❌ Whisper ist nicht installiert");
console.log("💡 Installation: pip install openai-whisper");
return false;
}
console.log("✅ Whisper ist verfügbar");
// Test with a short audio file
console.log("🗣️ Teste Whisper mit 3-Sekunden Aufnahme...");
const testFile = path.join(__dirname, 'temp', 'whisper-test.wav');
// Record 3 seconds
const recordingSuccess = await new Promise((resolve) => {
const process = spawn('sox', [
'-t', 'coreaudio', '-d',
testFile,
'trim', '0', '3'
], { stdio: 'pipe' });
process.on('close', (code) => {
resolve(code === 0 && fs.existsSync(testFile));
});
setTimeout(() => {
process.kill();
resolve(false);
}, 5000);
});
if (!recordingSuccess) {
console.log("❌ Aufnahme für Whisper-Test fehlgeschlagen");
return false;
}
// Test Whisper transcription
return new Promise((resolve) => {
console.log("🔄 Whisper Transkription läuft...");
const process = spawn('whisper', [
testFile,
'--model', 'base',
'--language', 'German',
'--output_format', 'txt',
'--output_dir', path.dirname(testFile)
], { stdio: 'pipe' });
process.on('close', (code) => {
const txtFile = testFile.replace('.wav', '.txt');
if (code === 0 && fs.existsSync(txtFile)) {
const transcription = fs.readFileSync(txtFile, 'utf8').trim();
console.log(`✅ Whisper Transkription: "${transcription}"`);
// Cleanup
fs.unlinkSync(testFile);
fs.unlinkSync(txtFile);
resolve(true);
} else {
console.log(`❌ Whisper fehlgeschlagen (Code: ${code})`);
if (fs.existsSync(testFile)) fs.unlinkSync(testFile);
resolve(false);
}
});
setTimeout(() => {
process.kill();
resolve(false);
}, 30000); // Whisper can take a while
});
}
/**
* Show microphone troubleshooting steps
*/
async function showMicrophoneTroubleshooting() {
console.log("\n🔧 === MIKROFON FEHLERBEHEBUNG ===");
console.log("─".repeat(40));
console.log("1. 📱 Systemeinstellungen überprüfen:");
console.log(" • Systemeinstellungen > Sicherheit & Datenschutz");
console.log(" • Datenschutz > Mikrofon");
console.log(" • Terminal/iTerm aktivieren");
console.log("\n2. 🔧 SoX neu installieren:");
console.log(" brew uninstall sox");
console.log(" brew install sox");
console.log("\n3. 🎤 Audio-Gerät prüfen:");
console.log(" • Systemeinstellungen > Ton > Eingabe");
console.log(" • Eingangspegel testen");
console.log(" • Anderes Mikrofon versuchen");
console.log("\n4. 🔄 Terminal neu starten:");
console.log(" • Komplett schließen und neu öffnen");
console.log(" • sudo-Rechte neu laden");
}
/**
* Show Whisper troubleshooting steps
*/
async function showWhisperTroubleshooting() {
console.log("\n🤖 === WHISPER FEHLERBEHEBUNG ===");
console.log("─".repeat(40));
console.log("1. 🐍 Python/pip überprüfen:");
console.log(" python3 --version");
console.log(" pip3 --version");
console.log("\n2. 📦 Whisper installieren:");
console.log(" pip3 install openai-whisper");
console.log(" # oder mit conda:");
console.log(" conda install openai-whisper");
console.log("\n3. 🔄 PATH überprüfen:");
console.log(" which whisper");
console.log(" echo $PATH");
console.log("\n4. 🚀 Alternative Installation:");
console.log(" pip3 install --upgrade openai-whisper");
console.log(" # Model vorab herunterladen:");
console.log(" whisper --model base dummy.wav");
}
// Main execution
if (require.main === module) {
(async () => {
try {
const success = await debugMicrophone();
if (success) {
console.log("\n🚀 === NÄCHSTE SCHRITTE ===");
console.log("✅ Mikrofon ist bereit für Otto Live Mode!");
console.log("💡 Starten Sie Otto mit: npm run live");
console.log("🎤 Oder testen Sie direkt: node live-mode.js");
} else {
console.log("\n⚠️ === MIKROFON NICHT BEREIT ===");
console.log("🔧 Folgen Sie den Fehlerbehebungsschritten oben");
console.log("🆘 Bei weiteren Problemen: GitHub Issues erstellen");
}
} catch (error) {
console.error("💥 Debug-Tool Fehler:", error);
process.exit(1);
}
})();
}
module.exports = { debugMicrophone };