-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathaudioBridge.js
More file actions
151 lines (130 loc) · 4.05 KB
/
audioBridge.js
File metadata and controls
151 lines (130 loc) · 4.05 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
const { app } = require("electron");
const { spawn } = require("child_process");
const fs = require("fs");
const path = require("path");
function createAudioBridge(sendLevel, onStatusChange = () => {}) {
let helperProcess = null;
let helperStatus = {
mode: "simulated",
reason: "Helper not started yet."
};
function updateStatus(nextStatus) {
helperStatus = nextStatus;
onStatusChange(helperStatus);
}
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) {
updateStatus({
mode: "simulated",
reason: [
"Audio capture helper not found.",
"\n",
"Troubleshooting:",
"\n- The required C# audio helper binary is missing.",
"\n- Please build it with: dotnet build .\\audio-helper\\Paraline.AudioBridge.csproj",
"\n- Or run: npm run build:helper",
"\n- See DEVELOPMENT.md for setup instructions."
].join("")
});
return;
}
helperProcess = spawn(helperBinary, [], {
windowsHide: true,
stdio: ["ignore", "pipe", "pipe"]
});
updateStatus({
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) {
updateStatus({
mode: "simulated",
reason: [
"Audio helper sent invalid data.",
"\n",
"Troubleshooting:",
"\n- The audio capture process returned unexpected output.",
"\n- Try restarting Paraline.",
"\n- If the problem persists, rebuild the helper binary."
].join("")
});
}
}
});
helperProcess.stderr.on("data", (chunk) => {
updateStatus({
mode: "helper-error",
reason: [
"Audio helper error: ",
chunk.toString().trim() || "Helper reported an error.",
"\n",
"Troubleshooting:",
"\n- Check if your audio device is in use by another app.",
"\n- Try restarting Paraline or your computer.",
"\n- If this continues, rebuild the helper binary."
].join("")
});
});
helperProcess.on("exit", (code) => {
helperProcess = null;
updateStatus({
mode: "simulated",
reason: [
`Audio helper stopped (exit code ${code}).`,
"\n",
"Troubleshooting:",
"\n- The audio capture process exited unexpectedly.",
"\n- Try restarting Paraline.",
"\n- If the problem persists, rebuild the helper binary."
].join("")
});
});
}
function stop() {
if (helperProcess) {
helperProcess.kill();
helperProcess = null;
}
updateStatus({
mode: "simulated",
reason: "Helper stopped."
});
}
function getStatus() {
return helperStatus;
}
return {
start,
stop,
getStatus
};
}
module.exports = {
createAudioBridge
};