#!/usr/bin/env node /** * Example: Streaming Text-to-Speech * * This example demonstrates real-time streaming TTS for progressive playback. * Useful for real-time applications where you want audio to start immediately. * āš ļø This consumes API credits! */ import { Supertone } from "@supertone/supertone"; import * as models from "@supertone/supertone/models"; import * as fs from "fs"; import * as dotenv from "dotenv"; // Load environment variables dotenv.config(); const API_KEY = process.env.SUPERTONE_API_KEY; const VOICE_ID = process.env.VOICE_ID || "your-voice-id-here"; async function main() { if (!API_KEY) { console.error("āŒ SUPERTONE_API_KEY not found in .env file"); process.exit(1); } if (VOICE_ID === "your-voice-id-here") { console.error("āŒ Please set VOICE_ID in .env or run list_voices.ts first"); process.exit(1); } try { // Initialize the client const client = new Supertone({ apiKey: API_KEY }); const text = "This is a streaming test. Audio chunks arrive progressively for real-time playback."; console.log("šŸ“” Starting streaming TTS..."); console.log(` Text: "${text}"`); console.log(` Voice ID: ${VOICE_ID}`); console.log(" āš ļø This will consume credits!"); // Stream speech const response = await client.textToSpeech.streamSpeech({ voiceId: VOICE_ID, apiConvertTextToSpeechUsingCharacterRequest: { text: text, language: models.APIConvertTextToSpeechUsingCharacterRequestLanguage.En, outputFormat: models.APIConvertTextToSpeechUsingCharacterRequestOutputFormat.Wav, style: "neutral", model: "sona_speech_1", }, }); console.log("\nāœ… Stream started!"); console.log(" šŸ“¦ Receiving audio chunks...\n"); // Process streaming response if ( response.result && typeof response.result === "object" && "getReader" in response.result ) { const reader = ( response.result as ReadableStream ).getReader(); const chunks: Uint8Array[] = []; let chunkCount = 0; let totalBytes = 0; while (true) { const { done, value } = await reader.read(); if (done) break; if (value) { chunks.push(value); chunkCount++; totalBytes += value.length; // Show progress (every 5 chunks or last chunk) if (chunkCount % 5 === 0 || done) { console.log( ` šŸ“¦ Chunk ${chunkCount}: ${value.length} bytes (total: ${totalBytes} bytes)` ); } } } // Save complete audio const audioData = Buffer.concat(chunks); const outputFile = "output_streamed_speech.wav"; fs.writeFileSync(outputFile, audioData); console.log(`\nāœ… Streaming complete!`); console.log(` šŸ’¾ Saved to: ${outputFile}`); console.log(` šŸ“Š Total chunks: ${chunkCount}`); console.log(` šŸ“Š Total size: ${totalBytes} bytes`); console.log( `\nšŸ’” Tip: In a real application, play chunks as they arrive for real-time audio!` ); } } catch (error: any) { console.error("āŒ Error:", error.message); process.exit(1); } } main();