forked from collabora/WhisperLive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
197 lines (166 loc) · 5.57 KB
/
options.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
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
/**
* Captures audio from the active tab in Google Chrome.
* @returns {Promise<MediaStream>} A promise that resolves with the captured audio stream.
*/
function captureTabAudio() {
return new Promise((resolve) => {
chrome.tabCapture.capture(
{
audio: true,
video: false,
},
(stream) => {
resolve(stream);
}
);
});
}
/**
* Sends a message to a specific tab in Google Chrome.
* @param {number} tabId - The ID of the tab to send the message to.
* @param {any} data - The data to be sent as the message.
* @returns {Promise<any>} A promise that resolves with the response from the tab.
*/
function sendMessageToTab(tabId, data) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, data, (response) => {
resolve(response);
});
});
}
/**
* Resamples the audio data to a target sample rate of 16kHz.
* @param {Array|ArrayBuffer|TypedArray} audioData - The input audio data.
* @param {number} [origSampleRate=44100] - The original sample rate of the audio data.
* @returns {Float32Array} The resampled audio data at 16kHz.
*/
function resampleTo16kHZ(audioData, origSampleRate = 44100) {
// Convert the audio data to a Float32Array
const data = new Float32Array(audioData);
// Calculate the desired length of the resampled data
const targetLength = Math.round(data.length * (16000 / origSampleRate));
// Create a new Float32Array for the resampled data
const resampledData = new Float32Array(targetLength);
// Calculate the spring factor and initialize the first and last values
const springFactor = (data.length - 1) / (targetLength - 1);
resampledData[0] = data[0];
resampledData[targetLength - 1] = data[data.length - 1];
// Resample the audio data
for (let i = 1; i < targetLength - 1; i++) {
const index = i * springFactor;
const leftIndex = Math.floor(index).toFixed();
const rightIndex = Math.ceil(index).toFixed();
const fraction = index - leftIndex;
resampledData[i] = data[leftIndex] + (data[rightIndex] - data[leftIndex]) * fraction;
}
// Return the resampled data
return resampledData;
}
function generateUUID() {
let dt = new Date().getTime();
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
/**
* Starts recording audio from the captured tab.
* @param {Object} option - The options object containing the currentTabId.
*/
async function startRecord(option) {
const stream = await captureTabAudio();
const uuid = generateUUID();
if (stream) {
// call when the stream inactive
stream.oninactive = () => {
window.close();
};
const socket = new WebSocket(`ws://${option.host}:${option.port}/`);
let isServerReady = false;
let language = option.language;
socket.onopen = function(e) {
socket.send(
JSON.stringify({
uid: uuid,
language: option.language,
task: option.task,
model: option.modelSize
})
);
};
socket.onmessage = async (event) => {
const data = JSON.parse(event.data);
if (data["uid"] !== uuid)
return;
if (data["status"] === "WAIT"){
await sendMessageToTab(option.currentTabId, {
type: "showWaitPopup",
data: data["message"],
});
chrome.runtime.sendMessage({ action: "toggleCaptureButtons", data: false })
chrome.runtime.sendMessage({ action: "stopCapture" })
return;
}
if (isServerReady === false){
isServerReady = true;
return;
}
if (language === null) {
language = data["language"];
// send message to popup.js to update dropdown
// console.log(language);
chrome.runtime.sendMessage({
action: "updateSelectedLanguage",
detectedLanguage: language,
});
return;
}
if (data["message"] === "DISCONNECT"){
chrome.runtime.sendMessage({ action: "toggleCaptureButtons", data: false })
return;
}
res = await sendMessageToTab(option.currentTabId, {
type: "transcript",
data: event.data,
});
};
const audioDataCache = [];
const context = new AudioContext();
const mediaStream = context.createMediaStreamSource(stream);
const recorder = context.createScriptProcessor(4096, 1, 1);
recorder.onaudioprocess = async (event) => {
if (!context || !isServerReady) return;
const inputData = event.inputBuffer.getChannelData(0);
const audioData16kHz = resampleTo16kHZ(inputData, context.sampleRate);
audioDataCache.push(inputData);
socket.send(audioData16kHz);
};
// Prevent page mute
mediaStream.connect(recorder);
recorder.connect(context.destination);
mediaStream.connect(context.destination);
// }
} else {
window.close();
}
}
/**
* Listener for incoming messages from the extension's background script.
* @param {Object} request - The message request object.
* @param {Object} sender - The sender object containing information about the message sender.
* @param {Function} sendResponse - The function to send a response back to the message sender.
*/
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const { type, data } = request;
switch (type) {
case "start_capture":
startRecord(data);
break;
default:
break;
}
sendResponse({});
return true;
});