-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtts.js
70 lines (59 loc) · 1.85 KB
/
tts.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
// 구글 TTS로 텍스트를 음성으로 변환
const {TextToSpeechClient} = require('@google-cloud/text-to-speech');
const {PassThrough} = require('stream');
const client = new TextToSpeechClient({
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
});
async function sendTTSResponse(ws, streamSid, gptResponse){
const speechStream = await synthesizeSpeechToStream(gptResponse);
speechStream.on('data', chunk => {
const payloadWithoutHeader = removeHeaderBytes(chunk);
const base64Payload = payloadWithoutHeader.toString('base64');
// console.log("Base64 인코딩된 데이터:", base64Payload.slice(0, 100));
ws.send(
JSON.stringify({
event: 'media',
streamSid: streamSid,
media: {
payload: base64Payload,
}
})
);
});
speechStream.on('error', console.error);
speechStream.on('end', () => {
// 음성 출력이 완료되면 종료를 알리는 mark 메시지 전송
ws.send(
JSON.stringify({
event: 'mark',
streamSid: streamSid,
mark: {
name: 'TTS-end'
}
})
);
});
}
/**
* GPT로부터 받은 텍스트를 음성으로 변환하고, 음성 데이터를 스트림으로 전송하는 함수
* @param {string} gptResponse - 변환할 텍스트
*/
async function synthesizeSpeechToStream(gptResponse) {
const request = {
input: { text: gptResponse },
voice: { languageCode: 'ko-KR', ssmlGender: 'NEUTRAL' },
audioConfig: {
audioEncoding: 'MULAW',
sampleRateHertz: 8000,
},
};
const [response] = await client.synthesizeSpeech(request);
const speechStream = new PassThrough();
speechStream.end(response.audioContent);
return speechStream;
}
function removeHeaderBytes(chunk) {
const headerSize = 44; //62
return chunk.slice(headerSize);
}
module.exports = {sendTTSResponse};