-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron-communication.js
156 lines (140 loc) · 5.15 KB
/
electron-communication.js
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
const Max = require('max-api');
const WebSocket = require('ws');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath('/usr/local/bin/ffmpeg'); // Update with the correct path if different
const ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open() {
Max.post("Connected to Electron WebSocket server");
});
ws.on('close', () => Max.post("Disconnected from Electron WebSocket server"));
// Max handler to forward progress updates to the Electron app
Max.addHandler('progress_update', (progress) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
action: 'progress_update',
data: progress
}));
Max.post(`Sent progress update to WebSocket server: ${progress}`);
}
});
Max.addHandler('number', (value) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ toggle: value }));
Max.post(`Sent to WebSocket server: { toggle: ${value} }`);
}
});
const sendAudioData = (filePath, actionType) => {
fs.readFile(filePath, { encoding: 'base64' }, (err, base64Data) => {
if (err) {
Max.post(`Error reading file: ${err.message}`);
return;
}
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ action: actionType, data: base64Data }));
Max.post(`Sent ${actionType} back to Electron from ${filePath}.`);
}
});
};
// Function to handle the crop_audio action from Electron
const handleCropAudio = (end) => {
const tempFilePath = '/Applications/g4l/tempAudio.wav';
const croppedFilePath = '/Applications/g4l/myOutput.wav';
fs.copyFileSync('/Applications/g4l/myOutput.wav', tempFilePath); // Copy the original file to tempFilePath
ffmpeg(tempFilePath)
.setStartTime(0)
.setDuration(Number(end)) // Ensure end is a number
.output(croppedFilePath)
.on('start', (cmdline) => {
Max.post(`Started ffmpeg with command: ${cmdline}`);
})
.on('end', () => {
Max.post('Audio cropping successful.');
fs.unlinkSync(tempFilePath);
sendAudioData(croppedFilePath, 'audio_data_output'); // Send the updated audio data back to Electron
Max.outlet('crop_audio', 'success'); // Explicitly send a message indicating crop success
})
.on('error', (err) => {
Max.post('Error cropping audio: ' + err.message);
fs.unlinkSync(tempFilePath);
Max.outlet('crop_audio', 'error');
})
.run();
};
// Function to handle 'crop' message from Max
Max.addHandler('crop', (end) => {
if (!isProcessing) {
isProcessing = true;
ws.send(JSON.stringify({ action: 'crop', data: end }));
Max.post(`Sent crop action with end: ${end}`);
} else {
Max.post('Processing already in progress.');
}
});
ws.on('message', function incoming(data) {
try {
const command = JSON.parse(data);
Max.post(`Received command: ${JSON.stringify(command)}`); // Debugging log
switch (command.action) {
case 'write_buffer':
Max.outlet('write_buffer');
setTimeout(() => sendAudioData('/Applications/myBuffer.wav', 'audio_data_buffer'), 1000);
break;
case 'load_output':
Max.outlet('replace_output'); // Outlet that triggers 'replace C:/g4l/myOutput.wav'
sendAudioData('/Applications/g4l/myOutput.wav', 'audio_data_output');
break;
case 'play':
Max.outlet('play'); // Assumes there's a Max outlet configured to handle this
break;
case 'fix_toggle':
Max.outlet('fix_toggle'); // Assumes there's a Max outlet configured to handle this
break;
case 'pause':
Max.outlet('pause'); // Assumes there's a Max outlet configured to handle this
break;
case 'reset':
Max.outlet('reset'); // Assumes there's a Max outlet configured to handle this
break;
case 'bang':
Max.outlet('bang'); // Assumes there's a Max outlet configured to handle this
break;
case 'continue':
Max.outlet('continue'); // Assumes there's a Max outlet configured to handle this
break;
case 'retry':
Max.outlet('retry'); // Assumes there's a Max outlet configured to handle this
break;
case 'update_model_path':
console.log('Updating model path with:', command.data);
Max.outlet('forward_model_path', command.data);
break;
case 'update_prompt_duration':
console.log('Updating prompt_duration with:', command.data);
Max.outlet('update_prompt_duration', command.data);
break;
case 'crop':
const { data } = command;
handleCropAudio(data);
break;
default:
Max.post(`Unhandled action: ${command.action}`);
}
} catch (error) {
console.error('Error parsing incoming data:', error);
Max.post('Error parsing incoming data: ' + error.message);
}
});
function initSocketConnection() {
ws.on('open', function open() {
Max.post('Connected to Electron WebSocket server.');
});
ws.on('close', () => {
Max.post('Disconnected from Electron WebSocket server.');
});
ws.on('error', (error) => {
Max.post('WebSocket error: ' + error.message);
});
}
// Initialize WebSocket connection and setup event listeners
initSocketConnection();