-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
94 lines (81 loc) · 2.88 KB
/
app.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
import dotenv from 'dotenv';
import express from 'express';
import { Readable } from 'stream';
import path from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import EventEmitter from 'events';
import axios from 'axios';
import { streamChatGptText } from './chatGptStream.js';
import { convertTextToSpeech } from './playhtTTS.js';
import { streamAudioToA2F } from './grpcClient.js';
dotenv.config();
EventEmitter.defaultMaxListeners = 100;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const port = process.env.PORT || 3000;
// Function to load USD Scene
async function loadUsdScene(serverUrl, usdScenePath) {
try {
const payload = { file_name: usdScenePath };
const response = await axios.post(`${serverUrl}/A2F/USD/Load`, payload);
console.log(`USD scene loaded: ${response.data}`);
return response.data;
} catch (error) {
console.error(`Error loading USD scene: ${error.message}`);
return null;
}
}
const loadUsdSceneAtStart = true;
if (loadUsdSceneAtStart) {
// Load USD scene at the start
const serverUrl = 'http://localhost:8011';
const usdScenePath = 'D:/Code/text-to-audio2face/stream-livelink.usd'; // Replace with your USD scene path
loadUsdScene(serverUrl, usdScenePath);
}
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/public')));
// Endpoint to start the streaming process
app.post('/startStreaming', async (req, res) => {
const { prompt, manualMode, sendToA2F } = req.body;
try {
let chatGptTTFB = 0;
let playHTTTFB = 0;
let chatGptStream;
if (!manualMode) {
// Start the TTFB measurement for ChatGPT
const gptStartTime = Date.now();
chatGptStream = await streamChatGptText(prompt);
chatGptTTFB = Date.now() - gptStartTime;
} else {
// Manual mode - Directly use prompt as text for TTS
chatGptStream = Readable.from([prompt]);
}
// Start the TTFB measurement for PlayHT
const playHTStartTime = Date.now();
const { stream: ttsStream } = await convertTextToSpeech(chatGptStream);
playHTTTFB = Date.now() - playHTStartTime;
if (sendToA2F) {
// Stream audio to Audio2Face if the toggle is enabled
streamAudioToA2F(ttsStream);
res.json({
message: 'Audio streaming initiated to Audio2Face',
chatGptTTFB,
playHTTTFB
});
} else {
// Pipe TTS audio data directly to the browser if the toggle is disabled
res.setHeader('Content-Type', 'audio/mpeg');
// Send TTFB data as custom headers
res.setHeader('X-ChatGpt-TTFB', chatGptTTFB);
res.setHeader('X-PlayHT-TTFB', playHTTTFB);
ttsStream.pipe(res);
}
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});