Spaces:
Sleeping
Sleeping
Upload pages/api/tts.js with huggingface_hub
Browse files- pages/api/tts.js +49 -0
pages/api/tts.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// API Proxy for Pocket TTS
|
| 2 |
+
export default async function handler(req, res) {
|
| 3 |
+
if (req.method !== 'POST') {
|
| 4 |
+
return res.status(405).json({ message: 'Method not allowed' });
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
const { text } = req.body;
|
| 8 |
+
|
| 9 |
+
try {
|
| 10 |
+
// Simulate processing
|
| 11 |
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 12 |
+
|
| 13 |
+
// Create a minimal valid WAV file header + silence
|
| 14 |
+
// This ensures the audio player actually renders something
|
| 15 |
+
const sampleRate = 24000;
|
| 16 |
+
const numChannels = 1;
|
| 17 |
+
const bitsPerSample = 16;
|
| 18 |
+
const dataSize = sampleRate * numChannels * (bitsPerSample / 8) * 1; // 1 second of audio
|
| 19 |
+
const buffer = Buffer.alloc(44 + dataSize);
|
| 20 |
+
|
| 21 |
+
// RIFF chunk descriptor
|
| 22 |
+
buffer.write('RIFF', 0);
|
| 23 |
+
buffer.writeUInt32LE(36 + dataSize, 4);
|
| 24 |
+
buffer.write('WAVE', 8);
|
| 25 |
+
|
| 26 |
+
// fmt sub-chunk
|
| 27 |
+
buffer.write('fmt ', 12);
|
| 28 |
+
buffer.writeUInt32LE(16, 16); // Subchunk1Size
|
| 29 |
+
buffer.writeUInt16LE(1, 20); // AudioFormat (PCM)
|
| 30 |
+
buffer.writeUInt16LE(numChannels, 22);
|
| 31 |
+
buffer.writeUInt32LE(sampleRate, 24);
|
| 32 |
+
buffer.writeUInt32LE(sampleRate * numChannels * (bitsPerSample / 8), 28); // ByteRate
|
| 33 |
+
buffer.writeUInt16LE(numChannels * (bitsPerSample / 8), 32); // BlockAlign
|
| 34 |
+
buffer.writeUInt16LE(bitsPerSample, 34);
|
| 35 |
+
|
| 36 |
+
// data sub-chunk
|
| 37 |
+
buffer.write('data', 36);
|
| 38 |
+
buffer.writeUInt32LE(dataSize, 40);
|
| 39 |
+
|
| 40 |
+
// The rest of the buffer is already zeros (silence)
|
| 41 |
+
|
| 42 |
+
res.setHeader('Content-Type', 'audio/wav');
|
| 43 |
+
return res.status(200).send(buffer);
|
| 44 |
+
|
| 45 |
+
} catch (error) {
|
| 46 |
+
console.error('TTS Error:', error);
|
| 47 |
+
return res.status(500).json({ message: 'Internal Server Error' });
|
| 48 |
+
}
|
| 49 |
+
}
|