forked from abhaybuch/node-mac-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mic-monitor.js
More file actions
114 lines (96 loc) · 2.97 KB
/
test-mic-monitor.js
File metadata and controls
114 lines (96 loc) · 2.97 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
/**
* Test file for microphone monitoring functionality
*
* This file demonstrates how to:
* 1. Request microphone permission with callback
* 2. Start monitoring microphone usage
* 3. Get a list of processes using the microphone
*/
const { getRunningInputAudioProcesses, INFO_ERROR_CODE, ERROR_DOMAIN } = require('./index');
const EventEmitter = require('events');
// Only import mic monitoring functions on Darwin (macOS) systems
const { startMonitoringMic, stopMonitoringMic } = process.platform === 'darwin'
? require('./index')
: {
startMonitoringMic: () => {
throw new Error('Microphone monitoring is only supported on macOS');
},
stopMonitoringMic: () => {
// No-op for non-Darwin systems
}
};
class MicrophoneStatusEmitter extends EventEmitter {
start() {
startMonitoringMic((microphoneActive, error) => {
if (error) {
if (error.code === INFO_ERROR_CODE && error.domain === ERROR_DOMAIN) {
this.emit('info', error.message);
} else {
this.emit('error', error.message);
}
} else {
this.emit('status', microphoneActive);
}
});
}
stop() {
stopMonitoringMic();
}
}
// Function to display microphone processes
async function displayMicProcesses() {
try {
console.log('\n--- Current Microphone Processes ---');
const processes = await getRunningInputAudioProcesses();
if (processes.length === 0) {
console.log('No processes are currently using the microphone');
} else {
console.log('Processes using the microphone:');
processes.forEach((process, index) => {
console.log(`${index + 1}. ${process}`);
});
}
} catch (error) {
console.error('Error getting microphone processes:', error.message);
}
}
function startMicrophoneStatusEmitter() {
const emitter = new MicrophoneStatusEmitter();
emitter.on('status', (isActive) => {
console.log('🎤 Microphone Status: ' + isActive);
});
emitter.on('info', (info) => {
console.log('⚠️ Microphone Info:', info);
});
emitter.start();
}
function startMicMonitor() {
try {
startMonitoringMic((microphoneActive, error) => {
const timestamp = new Date().toISOString();
if (error) {
console.error('Node - error');
console.error('Error starting microphone monitor:', error.message);
} else {
console.log(`Node: [${timestamp}] Microphone active:`, microphoneActive);
}
});
} catch (error) {
console.error('Node - error');
console.error('Error starting microphone monitor:', error.message);
}
}
// Handle process termination
process.on('SIGINT', () => {
console.log('\nStopping microphone monitoring...');
stopMonitoringMic();
process.exit(0);
});
console.log('Starting microphone monitoring...');
console.log('Press Ctrl+C to stop.\n');
displayMicProcesses();
if (process.platform === 'darwin') {
startMicrophoneStatusEmitter();
// Keep the process running
process.stdin.resume();
}