-
Notifications
You must be signed in to change notification settings - Fork 10
/
audio.service.ts
166 lines (144 loc) · 4.49 KB
/
audio.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
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
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment.development';
@Injectable()
export class AudioService {
ELEVEN_LABS_VOICE_ID = environment.ELEVEN_LABS_VOICE_ID;
ELEVEN_LABS_API_KEY = environment.ELEVEN_LABS_API_KEY;
public audio!: HTMLAudioElement;
public source!: AudioBufferSourceNode;
private audioContext!: AudioContext;
private streamPlaying: boolean = false;
constructor(private http: HttpClient) {
this.audio = new Audio();
}
public setAudioSourceAndPlay(source: string): void {
if (this.isAudioPlaying()) return;
this.audio.src = source;
this.audio.play();
}
public setAudioAndPlay(data: ArrayBuffer): void {
if (this.isAudioPlaying()) return;
const blob = new Blob([data], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob);
this.audio.src = url;
console.log('Started playing: ' + Date.now());
this.audio.play();
console.log('Ended playing: ' + Date.now());
}
public async playTextToSpeech(text: string) {
if (this.isAudioPlaying()) return;
await this.getAudio(text);
}
private async getAudio(text: string) {
const ttsURL = `https://api.elevenlabs.io/v1/text-to-speech/${this.ELEVEN_LABS_VOICE_ID}`;
const headers = {
accept: 'audio/mpeg',
'content-type': 'application/json',
'xi-api-key': this.ELEVEN_LABS_API_KEY,
};
const request = {
text,
"model_id": "eleven_multilingual_v2",
"voice_settings": { //defaults specific to voiceId
"stability": 0.5,
"similarity_boost": 0.75,
"style": 0,
"use_speaker_boost": true
}
};
console.log('Call made: ' + Date.now());
this.http
.post(ttsURL, request, { headers, responseType: 'arraybuffer' })
.subscribe({
next: (response: ArrayBuffer) => {
this.playAudio(response);
},
error: (error) => {
console.error('Error:', error);
}
});
}
private playAudio(data: ArrayBuffer) {
this.setAudioAndPlay(data);
}
public async playStreamAudio(text: string) {
await this.getStreamAudio(text);
}
private async getStreamAudio(text: string) {
if (this.isAudioStreamingPlaying()) return;
const streamingURL = `https://api.elevenlabs.io/v1/text-to-speech/${this.ELEVEN_LABS_VOICE_ID}/stream?optimize_streaming_latency=3`;
const headers = {
'content-type': 'application/json',
'xi-api-key': this.ELEVEN_LABS_API_KEY,
};
console.log('Call made: ' + Date.now());
const request = {
text,
"model_id": "eleven_multilingual_v2",
"voice_settings": { //defaults specific to voiceId
"stability": 0.5,
"similarity_boost": 0.75,
"style": 0,
"use_speaker_boost": true
}
};
this.http
.post(streamingURL, request, { headers, responseType: 'arraybuffer' })
.subscribe({
next: (response: ArrayBuffer) => {
this.playAudioStream(response);
console.log('stream chunk');
},
error: (error) => {
console.error('Error:', error);
}
});
}
private async playAudioStream(audioData: ArrayBuffer) {
//todo: add error handling
if (this.isAudioStreamingPlaying()) {
return;
}
else {
this.audioContext = new AudioContext();
this.source = this.audioContext.createBufferSource();
}
const audioBuffer = await this.audioContext.decodeAudioData(audioData);
this.source.buffer = audioBuffer;
this.source.connect(this.audioContext.destination);
this.source.onended = () => {
console.log('Ended playing: ' + Date.now());
};
let startTime = this.audioContext.currentTime + 0.1;
console.log('Started playing: ' + startTime);
this.streamPlaying = true;
this.source.start(startTime);
}
pause() {
if (this.isAudioPlaying()) {
this.audio.pause();
this.audio.currentTime = 0;
}
if (this.isAudioStreamingPlaying()) {
this.source.stop();
this.source.disconnect();
this.audioContext.close();
this.streamPlaying = false;
}
}
public isAudioPlaying(){
return (this.audio
&& this.audio.currentTime > 0
&& !this.audio.paused
&& !this.audio.ended
&& this.audio.readyState > 2);
}
public isAudioStreamingPlaying() {
return (this.source
&& this.audioContext
&& this.audioContext.state == 'running'
&& this.streamPlaying
);
}
}