December 6, 2024
O. Wolfson
This article will guide you through the process of setting up a simple TTS web app using Google Cloud Text-to-Speech (TTS) and Next.js. We'll include server-side integration, client-side rendering, and Google Cloud API configuration.
Node.js and npm installed on your system.
Google Cloud Project:
Next.js Project:
Initialize a Next.js project:
bashnpx create-next-app@latest my-tts-app
cd my-tts-app
Install necessary dependencies:
bashnpm install @google-cloud/text-to-speech
Service Account Key:
Place the downloaded service account key file (e.g., tts-key.json) in a secure location in your project directory, such as ./keys.
Environment Variables: Securely reference your key file using environment variables if deploying:
Add the path to .env.local:
envGOOGLE_APPLICATION_CREDENTIALS=./keys/tts-key.json
Access the variable in the code:
javascriptprocess.env.GOOGLE_APPLICATION_CREDENTIALS;
We’ll use a server action in Next.js to handle API requests.
Create a file at app/api/tts/route.js:
javascriptimport { TextToSpeechClient } from "@google-cloud/text-to-speech";
const client = new TextToSpeechClient();
export async function POST(request) {
try {
const { text } = await request.json();
if (!text) {
return new Response(JSON.stringify({ error: "Text is required" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
const requestPayload = {
input: { text },
voice: {
languageCode: "en-US",
name: "en-US-Neural2-D",
},
audioConfig: { audioEncoding: "MP3" },
};
const [response] = await client.synthesizeSpeech(requestPayload);
return new Response(response.audioContent, {
status: 200,
headers: {
"Content-Type": "audio/mpeg",
"Content-Disposition": ,
},
});
} (error) {
.(, error);
(
.({ : }),
{ : , : { : } }
);
}
}
Create a React component for interacting with the API.
In app/page.js:
javascript"use client";
import { useState } from "react";
export default function TextToSpeechPage() {
const [text, setText] = useState("This is a test sentence.");
const [isLoading, setIsLoading] = useState(false);
const [audioUrl, setAudioUrl] = useState(null);
const handleGenerateSpeech = async () => {
setIsLoading(true);
setAudioUrl(null);
try {
const response = await fetch("/api/tts", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text }),
});
if (!response.ok) {
throw new Error("Failed to generate speech.");
}
const audioBlob = await response.blob();
const audioUrl = URL.createObjectURL(audioBlob);
setAudioUrl(audioUrl);
} (error) {
.(, error);
} {
();
}
};
(
);
}
tts-key.json file securely or replace it with an environment variable setup.With this foundation, you can extend the application by:
Test the API locally: Start the Next.js development server:
bashnpm run dev
Send a POST request to /api/tts using a tool like Postman or curl with a JSON body:
json{ "text": "Hello, world!" }