File size: 3,241 Bytes
28b5ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.sound.sampled.*;
import org.json.JSONObject;

public class GoogleTTSClient {
    private final String apiKey;
    private final String voiceName = "en-US-Neural2-F"; // Female voice
    private final String languageCode = "en-US";

    public GoogleTTSClient(String apiKey) {
        this.apiKey = apiKey;
    }

    public void speakText(String text) throws IOException {
        String inputJson = "{\n" +
                "  \"input\": {\n" +
                "    \"text\": \"" + text.replace("\"", "\\\"") + "\"\n" +
                "  },\n" +
                "  \"voice\": {\n" +
                "    \"languageCode\": \"" + languageCode + "\",\n" +
                "    \"name\": \"" + voiceName + "\"\n" +
                "  },\n" +
                "  \"audioConfig\": {\n" +
                "    \"audioEncoding\": \"LINEAR16\",\n" +
                "    \"speakingRate\": 1.0,\n" +
                "    \"pitch\": 0.0\n" +
                "  }\n" +
                "}";

        URL url = new URL("https://texttospeech.googleapis.com/v1/text:synthesize?key=" + apiKey);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        try (OutputStream os = conn.getOutputStream()) {
            os.write(inputJson.getBytes("utf-8"));
        }

        int status = conn.getResponseCode();
        if (status == 200) {
            try (InputStream is = conn.getInputStream()) {
                // Parse the response
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
                
                JSONObject jsonResponse = new JSONObject(response.toString());
                String audioContent = jsonResponse.getString("audioContent");
                
                // Decode base64 audio content
                byte[] audioBytes = java.util.Base64.getDecoder().decode(audioContent);
                
                // Save to temporary file
                File tempFile = File.createTempFile("speech", ".wav");
                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                    fos.write(audioBytes);
                }

                // Play the audio
                try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(tempFile)) {
                    Clip clip = AudioSystem.getClip();
                    clip.open(audioIn);
                    clip.start();
                    clip.drain();
                    clip.close();
                } catch (UnsupportedAudioFileException | LineUnavailableException e) {
                    throw new IOException("Error playing audio: " + e.getMessage());
                }

                // Clean up
                tempFile.delete();
            }
        } else {
            throw new IOException("Error: " + status);
        }
    }
}