-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
text-to-speech.service.ts
43 lines (39 loc) · 1.69 KB
/
text-to-speech.service.ts
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
import { Inject, Injectable } from '@angular/core';
import { SynthesisVoice } from '../state/chat-box.actions';
import { BrowserFeatureKey, FeatureService, WINDOW } from '@ngx-starter-kit/core';
@Injectable()
export class TextToSpeechService {
public canUseSpeechSynthesis = false;
private speechSynthesis: SpeechSynthesis;
constructor(private readonly featureService: FeatureService, @Inject(WINDOW) private window: Window) {
this.canUseSpeechSynthesis = this.featureService.detectFeature(BrowserFeatureKey.SpeechSynthesis).supported;
if (this.canUseSpeechSynthesis) {
this.speechSynthesis = (window as any).speechSynthesis;
}
}
public synthesisVoice(text: string, voice: SynthesisVoice): void {
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = voice.voice;
utterance.text = text;
utterance.volume = voice.volume; // 0 to 1
utterance.rate = voice.rate; // 0.1 to 10
utterance.pitch = voice.pitch; // 0 to 2
this.speechSynthesis.speak(utterance);
}
public async getVoiceList(): Promise<SpeechSynthesisVoice[]> {
await new Promise((resolve, reject) => {
this.speechSynthesis.addEventListener('voiceschanged', resolve);
});
return this.speechSynthesis.getVoices();
// return new Promise((resolve: any, reject: any): void => {
// // With Chrome, we have to wait for onvoiceschanged event to fire before calling getVoices
// if (this.speechSynthesis.onvoiceschanged !== undefined) {
// this.speechSynthesis.onvoiceschanged = (): void => {
// resolve(this.speechSynthesis.getVoices());
// };
// } else {
// resolve(this.speechSynthesis.getVoices());
// }
// });
}
}