-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
110 lines (100 loc) · 3.19 KB
/
Copy pathmain.js
File metadata and controls
110 lines (100 loc) · 3.19 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
import * as EmblaCore from "@mideind/embla-core";
const apiKeyInput = document.getElementById("server_api_key");
const button = document.getElementById("start_button");
const urlInput = document.getElementById("server_url");
// Helper function for easier viewing of logs
const logArea = document.getElementById("log");
logArea.value = "";
function log(msg) {
logArea.value += msg + "\n";
console.log(msg);
// Auto scroll log textarea
logArea.scrollTop = logArea.scrollHeight;
}
/**
* Function which fetches authentication tokens from proxy endpoint,
* and returns instances of EmblaCore.AuthenticationToken.
*/
async function proxyFetchToken() {
// Note: here we simply use the same Ratatoskur server,
// but this could be an endpoint in your application which acts as a proxy for the Ratatoskur token endpoint.
let r = await fetch(`${urlInput.value}/rat/v1/token`, {
headers: { "X-API-Key": apiKeyInput.value },
});
return EmblaCore.AuthenticationToken.fromJson(await r.text());
}
// Create config (this should be reused if possible)
var config = new EmblaCore.EmblaSessionConfig(proxyFetchToken, urlInput.value);
/** Set handler functions and API key for the current config. */
function setupConfig() {
config.onStartStreaming = () => {
log("[ASR] Started streaming audio...");
};
config.onSpeechTextReceived = (transcript, isFinal, data) => {
log(
`[ASR] Transcript: '${transcript}', isFinal: ${isFinal}, alternatives: ${data.alternatives}`
);
};
config.onStartQuerying = () => {
log("[ASR] Stopped streaming audio.");
log("[QUERY] Sending query...");
};
config.onQueryAnswerReceived = (qanswer) => {
log(`[QUERY] Got query answer: ${qanswer.answer}`);
};
config.onStartAnswering = () => {
log("[TTS] Reading answer...");
};
config.onDone = () => {
log("Session finished.");
updateButton();
};
config.onError = (error) => {
log(`ERROR: ${error}`);
updateButton();
};
console.log(config);
}
var session;
function sessionIsActive() {
return (
session === undefined ||
session.state === EmblaCore.EmblaSessionState.idle ||
session.state === EmblaCore.EmblaSessionState.done
);
}
function updateButton() {
if (sessionIsActive()) {
button.value = "Stream microphone";
} else {
button.value = "Stop session";
}
}
async function toggle_start() {
if (sessionIsActive()) {
log("Starting session...");
session = new EmblaCore.EmblaSession(config);
await session.start();
} else {
log("Cancelling session...");
await session.cancel();
session = undefined;
}
updateButton();
}
// Add change event listener to url input
urlInput.addEventListener("change", (event) => {
config = new EmblaCore.EmblaSessionConfig(
proxyFetchToken,
event.target.value
);
setupConfig();
});
// Add click event listener to the start button
button.addEventListener("click", (_) => {
toggle_start();
});
// Set up callback handlers in config instance
setupConfig();
// Prepare audio assets
await EmblaCore.EmblaSession.prepare();