Search in sources :

Example 1 with AmazonPollyClient

use of com.amazonaws.services.polly.AmazonPollyClient in project myrobotlab by MyRobotLab.

the class Polly method getPolly.

private AmazonPollyClient getPolly() {
    if (polly == null) {
        try {
            // BasicAWSCredentials("AKIAJGL6AEN37LDO3N7A", "secret-access-key");
            if (credentials == null) {
                // try credential chain - in case they have set env vars
                credentials = new BasicAWSCredentials(keyId, keyIdSecret);
            }
            // polly = (AmazonPollyClient)
            // AmazonPollyClientBuilder.standard().withCredentials(new
            // AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
            polly = (AmazonPollyClient) AmazonPollyClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_WEST_2).build();
            processVoicesRequest();
        } catch (Exception e) {
            try {
                log.error("could not get client with keys supplied - trying default chain", e);
                polly = new AmazonPollyClient(new DefaultAWSCredentialsProviderChain(), new ClientConfiguration());
                // polly.setRegion(Region.getRegion(Regions.US_EAST_1));
                polly.setRegion(Region.getRegion(Regions.US_WEST_2));
                processVoicesRequest();
            } catch (Exception e2) {
                credentialsError = true;
                error("could not get Polly client - did you setKeys ?");
                error("Environment variables – AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY or");
                error("check http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html");
                log.error("giving up", e2);
            }
        }
    }
    return polly;
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientConfiguration(com.amazonaws.ClientConfiguration) AmazonPollyClient(com.amazonaws.services.polly.AmazonPollyClient)

Example 2 with AmazonPollyClient

use of com.amazonaws.services.polly.AmazonPollyClient in project myrobotlab by MyRobotLab.

the class Polly method cacheFile.

/*
   * Proxy works in 3 modes
   *    client - consumer of mrl services
   *    relay - proxy (running as cloud service)
   *    direct - goes direct to service
   * In Polly's case - 
   *    client - would be an end user using a client key
   *    relay - is the mrl proxy service
   *    direct would be from a users, by-passing mrl and going directly to Amazon with amazon keys
   * cache file - caches file locally (both client or relay) 
   */
public byte[] cacheFile(String toSpeak, OutputFormat format) throws IOException {
    byte[] mp3File = null;
    // cache it begin -----
    String localFileName = getLocalFileName(this, toSpeak, "mp3");
    // localFileName;
    if (!audioFile.cacheContains(localFileName)) {
        log.info("retrieving speech from Amazon - {}", localFileName);
        AmazonPollyClient polly = getPolly();
        SynthesizeSpeechRequest synthReq = new SynthesizeSpeechRequest().withText(toSpeak).withVoiceId(awsVoice.getId()).withOutputFormat(format);
        SynthesizeSpeechResult synthRes = polly.synthesizeSpeech(synthReq);
        InputStream data = synthRes.getAudioStream();
        mp3File = FileIO.toByteArray(data);
        audioFile.cache(localFileName, mp3File, toSpeak);
    } else {
        log.info("using local cached file");
        mp3File = FileIO.toByteArray(new File(AudioFile.globalFileCacheDir + File.separator + getLocalFileName(this, toSpeak, "mp3")));
    }
    // log.info("Finished waiting for completion.");
    return mp3File;
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest) InputStream(java.io.InputStream) File(java.io.File) AmazonPollyClient(com.amazonaws.services.polly.AmazonPollyClient) SynthesizeSpeechResult(com.amazonaws.services.polly.model.SynthesizeSpeechResult)

Aggregations

AmazonPollyClient (com.amazonaws.services.polly.AmazonPollyClient)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 SynthesizeSpeechRequest (com.amazonaws.services.polly.model.SynthesizeSpeechRequest)1 SynthesizeSpeechResult (com.amazonaws.services.polly.model.SynthesizeSpeechResult)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1