-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioBridge.js
More file actions
111 lines (92 loc) · 2.74 KB
/
audioBridge.js
File metadata and controls
111 lines (92 loc) · 2.74 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
const { app } = require("electron");
const { spawn } = require("child_process");
const fs = require("fs");
const path = require("path");
function createAudioBridge(sendLevel) {
let helperProcess = null;
let helperStatus = {
mode: "simulated",
reason: "Helper not started yet."
};
function findHelperBinary() {
const appPath = app.getAppPath();
const candidates = [
path.join(process.resourcesPath, "audio-helper", "Paraline.AudioBridge.exe"),
path.join(appPath, "build", "audio-helper", "Paraline.AudioBridge.exe"),
path.join(appPath, "audio-helper", "bin", "Release", "net8.0-windows", "win-x64", "publish", "Paraline.AudioBridge.exe"),
path.join(appPath, "audio-helper", "bin", "Debug", "net8.0-windows", "Paraline.AudioBridge.exe"),
path.join(appPath, "audio-helper", "bin", "Release", "net8.0-windows", "Paraline.AudioBridge.exe")
];
return candidates.find((candidatePath) => fs.existsSync(candidatePath)) || null;
}
function start() {
const helperBinary = findHelperBinary();
if (!helperBinary) {
helperStatus = {
mode: "simulated",
reason: "C# helper binary not found. Build or package the helper before enabling loopback capture."
};
return;
}
helperProcess = spawn(helperBinary, [], {
windowsHide: true,
stdio: ["ignore", "pipe", "pipe"]
});
helperStatus = {
mode: "helper",
reason: "C# helper process connected."
};
let stdoutBuffer = "";
helperProcess.stdout.on("data", (chunk) => {
stdoutBuffer += chunk.toString();
const lines = stdoutBuffer.split(/\r?\n/);
stdoutBuffer = lines.pop() || "";
for (const line of lines) {
if (!line.trim()) {
continue;
}
try {
const message = JSON.parse(line);
if (message.type === "level" && typeof message.value === "number") {
sendLevel(message.value);
}
} catch (_error) {
helperStatus = {
mode: "simulated",
reason: "Received invalid JSON from helper."
};
}
}
});
helperProcess.stderr.on("data", (chunk) => {
helperStatus = {
mode: "helper-error",
reason: chunk.toString().trim() || "Helper reported an error."
};
});
helperProcess.on("exit", (code) => {
helperProcess = null;
helperStatus = {
mode: "simulated",
reason: `Helper stopped with exit code ${code}.`
};
});
}
function stop() {
if (helperProcess) {
helperProcess.kill();
helperProcess = null;
}
}
function getStatus() {
return helperStatus;
}
return {
start,
stop,
getStatus
};
}
module.exports = {
createAudioBridge
};