text to mp3, using google api. step by step using java code.

 

Prompt

write java program , read text file providing file location (telugu text). generate audio file based on speed selection as mp3?

Response

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import com.google.cloud.texttospeech.v1.AudioConfig;
import com.google.cloud.texttospeech.v1.AudioEncoding;
import com.google.cloud.texttospeech.v1.SynthesisInput;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechRequest;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams.LanguageCode;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams.SsmlGender;

public class TeluguTextToSpeech {

    public static void main(String[] args) throws Exception {

        // Replace with your Google Cloud project ID
        String projectId = "your-project-id";

        // File location of Telugu text file
        String fileLocation = "path/to/telugu/text.txt";

        // Speed selection (1.0 is normal speed, higher values increase speed)
        double speed = 1.2;

        // Read Telugu text from file
        String text = readFile(fileLocation);

        // Create TextToSpeech client
        try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
            // Set input text
            SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();

            // Select the language and voice
            VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
                    .setLanguageCode(LanguageCode.TELUGU) // Telugu language
                    .setName("en-US-Wavenet-F") // Female voice
                    .setSsmlGender(SsmlGender.FEMALE) // Specify gender for TTS engine
                    .build();

            // Configure the audio output format
            AudioConfig audioConfig = AudioConfig.newBuilder()
                    .setAudioEncoding(AudioEncoding.MP3) // Output audio as MP3
                    .setSpeakingRate(speed) // Adjust speed
                    .build();

            // Construct the request
            SynthesizeSpeechRequest request = SynthesizeSpeechRequest.newBuilder()
                    .setInput(input)
                    .setVoice(voice)
                    .setAudioConfig(audioConfig)
                    .build();

            // Perform the text-to-speech request
            SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(request);

            // Write the response to the output file (you can specify a filename)
            File outputFile = new File("output.mp3");
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(response.getAudioContent().toByteArray());
            outputStream.close();

            System.out.println("Audio file generated successfully: " + outputFile.getAbsolutePath());

        }
    }

    // Method to read text from file
    private static String readFile(String filePath) throws Exception {
        StringBuilder text = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                text.append(line).append("\n");
            }
        }
        return text.toString();
    }
}

Explanation:

  1. Dependencies:

    • Import the Google Cloud Text-to-Speech client library: com.google.cloud.texttospeech.v1
  2. Setup:

    • Replace "your-project-id" with your Google Cloud Project ID.
    • Specify the fileLocation of your Telugu text file.
    • Adjust the speed value (1.0 is normal, higher values increase speed).
  3. Read Text File:

    • The readFile function reads the Telugu text from the specified file.
  4. Google Cloud Text-to-Speech Client:

    • The code uses the TextToSpeechClient to interact with the Google Cloud Text-to-Speech API.
  5. Synthesis Input:

    • Sets the input text.
  6. Voice Selection:

    • VoiceSelectionParams specifies the voice parameters.
      • LanguageCode.TELUGU: Specifies the language as Telugu.
      • You can choose from various voice names. Look at the Google Cloud Text-to-Speech API documentation for available voices.
      • SsmlGender.FEMALE: Sets the voice gender.
  7. Audio Config:

    • AudioConfig defines the audio output settings.
      • AudioEncoding.MP3: Specifies MP3 as the output format.
      • setSpeakingRate(speed): Sets the speed of the speech.
  8. Request and Synthesis:

    • The code creates a SynthesizeSpeechRequest and sends it to the Text-to-Speech API.
    • The API response includes the audio data in MP3 format.
  9. Write Audio File:

    • The audio data is written to an output file named "output.mp3".

Remember to:

  • Set up the necessary Google Cloud project and enable the Text-to-Speech API.
  • Have the Google Cloud Text-to-Speech client library dependency in your project.
  • Replace the placeholders with your actual values.

Additional Notes:

  • You can experiment with different voice names and other voice parameters to get desired results.
  • Google Cloud Text-to-Speech supports various languages, so you can easily adapt this code for other languages.
  • You can find more information about using the Google Cloud Text-to-Speech API at: https://cloud.google.com/text-to-speech/docs/

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024