forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice-endpoint-voicer.js
152 lines (137 loc) · 4.05 KB
/
voice-endpoint-voicer.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
/* this module is responsible for mapping a remote TTS endpoint to the character. */
import audioManager from '../audio-manager.js';
import {makePromise} from '../util.js';
import {voiceEndpointBaseUrl} from '../constants.js';
class VoiceEndpoint {
constructor(url) {
this.url = new URL(url, import.meta.url);
}
}
class PreloadMessage {
constructor(voiceEndpointUrl, text) {
this.voiceEndpointUrl = voiceEndpointUrl;
this.text = text;
this.isPreloadMessage = true;
this.loadPromise = VoiceEndpointVoicer.loadAudioBuffer(this.voiceEndpointUrl, this.text);
}
waitForLoad() {
return this.loadPromise;
}
}
class VoiceEndpointVoicer {
constructor(voiceEndpoint, player) {
this.voiceEndpoint = voiceEndpoint;
this.player = player;
// this.live = true;
this.running = false;
this.queue = [];
this.cancel = null;
this.endPromise = null;
}
static preloadMessage(voiceEndpointUrl, text) {
return new PreloadMessage(voiceEndpointUrl, text);
}
preloadMessage(text) {
return VoiceEndpointVoicer.preloadMessage(this.voiceEndpoint.url.toString(), text);
}
static async loadAudioBuffer(voiceEndpointUrl, text) {
// console.log('load audio buffer', voiceEndpointUrl, text);
try {
const u = new URL(voiceEndpointUrl);
u.searchParams.set('s', text);
const res = await fetch(u/*, {
mode: 'cors',
} */);
const arrayBuffer = await res.arrayBuffer();
const audioContext = audioManager.getAudioContext();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
return audioBuffer;
} catch(err) {
console.warn('epic fail', err);
debugger;
}
}
/* async loadAudioBuffer(text) {
return VoiceEndpointVoicer.loadAudioBuffer(this.voiceEndpoint.url, text);
} */
start(text) {
if (!this.endPromise) {
this.endPromise = makePromise();
}
if (!this.running) {
this.running = true;
if (!this.player.avatar.isAudioEnabled()) {
this.player.avatar.setAudioEnabled(true);
}
const _next = () => {
const {endPromise} = this;
if (endPromise) {
this.endPromise = null;
endPromise.accept();
}
};
let live = true;
const cancelFns = [
() => {
live = false;
_next();
},
];
this.cancel = () => {
for (const cancelFn of cancelFns) {
cancelFn();
}
};
(async () => {
const audioBuffer = await (text.isPreloadMessage ? text.waitForLoad() : this.loadAudioBuffer(text));
if (!live) {
console.log('bail on audio buffer');
return;
}
const audioContext = audioManager.getAudioContext();
const audioBufferSourceNode = audioContext.createBufferSource();
audioBufferSourceNode.buffer = audioBuffer;
const ended = () => {
this.cancel = null;
this.running = false;
if (this.queue.length > 0) {
const text = this.queue.shift();
this.start(text);
} else {
_next();
}
};
audioBufferSourceNode.addEventListener('ended', ended, {once: true});
if (!this.player.avatar.microphoneWorker) {
this.player.avatar.setAudioEnabled(true);
}
audioBufferSourceNode.connect(this.player.avatar.getAudioInput());
audioBufferSourceNode.start();
cancelFns.push(() => {
audioBufferSourceNode.removeEventListener('ended', ended);
audioBufferSourceNode.stop();
audioBufferSourceNode.disconnect();
});
})();
} else {
this.queue.push(text);
}
return this.endPromise;
}
stop() {
// this.live = false;
this.queue.length = 0;
if (this.cancel) {
this.cancel();
this.cancel = null;
}
this.running = false;
}
}
const getVoiceEndpointUrl = (voiceId) => `${voiceEndpointBaseUrl}?voice=${encodeURIComponent(voiceId)}`;
export {
VoiceEndpoint,
PreloadMessage,
VoiceEndpointVoicer,
getVoiceEndpointUrl,
};