-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemoASRV2.js
More file actions
191 lines (175 loc) · 5.34 KB
/
Copy pathemoASRV2.js
File metadata and controls
191 lines (175 loc) · 5.34 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
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
import Recorder from "../recorder.js";
import Utils from "../utils.js";
//language: "ar-AE" or "en-US"
class EmoASR {
constructor({ auth, language, whisper_config }) {
if (!auth) {
throw new Error("No API key provided!");
}
this.apiKey = auth;
this.wsToken = "";
// To use the London deployment, uncomment the lines below:
this.requestUrl = "https://chatda.api.emotechlab.com";
this.wsUrl = "wss://chatda.api.emotechlab.com/asr/ws/";
// To use the UAE deployment, uncomment the lines below:
// this.requestUrl = "https://emoda-uae.api.emotechlab.com";
// this.wsUrl = "wss://emoda-uae.api.emotechlab.com/asr/ws/";
this.mediaAcquired = false;
this.recording = false;
this.language = language || "en-US";
this.sample_rate = 16000;
this.asrStartSend = false;
this.whisper_config = whisper_config || null;
this.recorder = new Recorder();
}
async mount() {
this.wsToken = await this.getApiToken();
console.log("get ws token for asr: ", this.wsToken);
}
/**
* Sends an HTTP request.
*
* @param {string} method - The HTTP method (e.g., GET, POST, PUT, DELETE).
* @param {string} url - The URL to send the request to.
* @param {Object} data - The data to send with the request.
* @returns {Promise} A promise that resolves with the response data if the request is successful, or rejects with an error if the request fails.
*/
request(method, url, data) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + this.wsToken);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(new Error(xhr.statusText));
}
}
};
xhr.send(JSON.stringify(data));
});
}
/**
* Retrieves the API token.
* @returns {Promise<string>} A promise that resolves with the API token.
*/
getApiToken() {
return new Promise((resolve, reject) => {
this.request("get", `${this.requestUrl}/token/init?apikey=${this.apiKey}`)
.then((res) => {
let tokenExpire = Number(res.data.timeOut);
let refrshTime = tokenExpire * 60 * 1000 - 2 * 60 * 1000;
setInterval(() => {
this.refreshToken();
}, refrshTime);
resolve(res.data.token);
})
.catch((err) => {
console.error(err);
reject(err);
});
});
}
/**
* This function are use to refreshToken.
*
*/
refreshToken() {
return new Promise((resolve, reject) => {
this.request("get", `${this.requestUrl}/da/user/token`)
.then((res) => {
console.log("request send", res.data);
this.wsToken = res.data;
})
.catch((err) => {
console.error("speak request err:", err);
});
});
}
async startRecording(onData) {
if (this.recording) {
return;
}
if (this.language === "en-US") {
this.socket = new WebSocket(`${this.wsUrl}en?token=${this.wsToken}`);
} else if (this.language === "ar-AE") {
this.socket = new WebSocket(`${this.wsUrl}ar?token=${this.wsToken}`);
}
this.socket.addEventListener("message", (event) => {
const msg = JSON.parse(event.data);
if (onData) {
onData(msg);
}
});
this.handshake = {
request: "start",
channel_index: 0,
params: {
encoding: "s16",
sample_rate: this.sample_rate,
channel_count: 1,
},
config: {
single_utterance: true,
keep_connection: false,
partial_interval: 500,
reuse_tolerance: 100,
"silence-threshold": 1000,
},
};
// There is a chance when we call this, socket already opened. so eventListneer will be skiped.
let wsConnected = new Promise((resolve) => {
if (this.socket.readyState == this.socket.OPEN) {
this.socket.send(JSON.stringify(this.handshake));
this.asrStartSend = true;
resolve();
} else {
this.socket.addEventListener("open", (_) => {
this.socket.send(JSON.stringify(this.handshake));
this.asrStartSend = true;
resolve();
});
}
});
await wsConnected;
this.recording = true;
this.recorder.record();
this.recorder.onMessage = (wavBlob) => {
this._sendAudio(wavBlob);
};
console.log("Finished connecting");
}
_sendAudio(buf) {
if (
this.socket &&
this.socket.readyState == this.socket.OPEN &&
this.asrStartSend
) {
const data = Utils.arrayBufferToBase64({ buffer: buf });
data.length > 0 &&
this.socket.send(JSON.stringify({ request: "audio", data }));
}
}
async stopRecording() {
if (!this.recording) {
return;
}
await new Promise((resolve) => {
this.socket.send(
JSON.stringify({
request: "stop",
})
);
this.asrStartSend = false;
this.socket.onclose = resolve;
});
this.recording = false;
this.recorder.stopRecord();
this.recorder.currentBufferClear();
console.log("Stopped recording");
}
}
export default EmoASR;