Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Synthesiser.java « synthesiser « speech « darkprograms « com « src - github.com/ClusterM/java-speech-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bbedb0d0b7f552931f53d8bfc67d4389e59b99a9 (plain)
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
package com.darkprograms.speech.synthesiser;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * Synthesiser class that connects to Google's unoffical API to retreive data
 *
 * @author Luke Kuza
 */
public class Synthesiser {

    /**
     * URL to query for Google synthesiser
     */
    private final static String GOOGLE_SYNTHESISER_URL = "http://translate.google.com/translate_tts?tl=en&q=";

    /**
     * Constructor
     */
    public Synthesiser() {

    }

    /**
     * Gets an input stream to MP3 data for the returned information from a request
     *
     * @param synthText Text you want to be synthesized into MP3 data
     * @return Returns an input stream of the MP3 data that is returned from Google
     * @throws Exception Throws exception if it can not complete the request
     */
    public InputStream getMP3Data(String synthText) throws Exception {
        String encoded = URLEncoder.encode(synthText, "UTF-8"); //Encode

        URL url = new URL(GOOGLE_SYNTHESISER_URL + encoded);  //create url

        // Open New URL connection channel.
        URLConnection urlConn = url.openConnection(); //Open connection


        urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0");  //Adding header for user agent is required

        return urlConn.getInputStream();
    }

}