Search in sources :

Example 1 with SynthesizeSpeechRequest

use of com.amazonaws.services.polly.model.SynthesizeSpeechRequest in project openhab-addons by openhab.

the class PollyTTSCloudImpl method getTextToSpeech.

/**
 * This method will return an input stream to an audio stream for the given
 * parameters.
 * Get the given text in specified locale and audio format as input stream.
 *
 * @param text
 *            the text to translate into speech
 * @param label
 *            the voice Label to use
 * @param audioFormat
 *            the audio format to use
 * @return an InputStream to the audio data in specified format
 * @throws IOException
 *             will be raised if the audio data can not be retrieved from
 *             cloud service
 */
public InputStream getTextToSpeech(String text, String label, String audioFormat) {
    String voiceID = labelToID.get(label);
    String format = audioFormat.toLowerCase();
    if ("ogg".equals(format)) {
        format = "ogg_vorbis";
    }
    TextType textType = text.startsWith("<speak>") ? TextType.Ssml : TextType.Text;
    SynthesizeSpeechRequest request = new SynthesizeSpeechRequest().withTextType(textType).withText(text).withVoiceId(voiceID).withOutputFormat(OutputFormat.fromValue(format));
    return client.synthesizeSpeech(request).getAudioStream();
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest) TextType(com.amazonaws.services.polly.model.TextType)

Example 2 with SynthesizeSpeechRequest

use of com.amazonaws.services.polly.model.SynthesizeSpeechRequest in project aws-java-reference-pocs by hardikSinghBehl.

the class AudioGenerationService method generate.

public InputStream generate(final String inputText) {
    final var synthesizeSpeechRequest = new SynthesizeSpeechRequest().withText(inputText).withVoiceId(getDefaultVoice().getId()).withOutputFormat(OutputFormat.Mp3);
    final var synthesizeSpeechResult = amazonPolly.synthesizeSpeech(synthesizeSpeechRequest);
    return synthesizeSpeechResult.getAudioStream();
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest)

Example 3 with SynthesizeSpeechRequest

use of com.amazonaws.services.polly.model.SynthesizeSpeechRequest in project amplify-android by aws-amplify.

the class AWSPollyService method synthesizeSpeech.

private InputStream synthesizeSpeech(String text, AWSVoiceType voiceType) throws PredictionsException {
    final String languageCode;
    final String voiceId;
    if (AWSVoiceType.UNKNOWN.equals(voiceType)) {
        // Obtain voice + language from plugin configuration by default
        SpeechGeneratorConfiguration config = pluginConfiguration.getSpeechGeneratorConfiguration();
        languageCode = config.getLanguage();
        voiceId = config.getVoice();
    } else {
        // Override configuration defaults if explicitly specified in the options
        languageCode = voiceType.getLanguageCode();
        voiceId = voiceType.getName();
    }
    SynthesizeSpeechRequest request = new SynthesizeSpeechRequest().withText(text).withTextType(TextType.Text).withLanguageCode(languageCode).withVoiceId(voiceId).withOutputFormat(OutputFormat.Mp3).withSampleRate(Integer.toString(MP3_SAMPLE_RATE));
    // Synthesize speech from given text via Amazon Polly
    final SynthesizeSpeechResult result;
    try {
        result = polly.synthesizeSpeech(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Polly encountered an error while synthesizing speech.", serviceException, "See attached service exception for more details.");
    }
    return result.getAudioStream();
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) SynthesizeSpeechResult(com.amazonaws.services.polly.model.SynthesizeSpeechResult)

Example 4 with SynthesizeSpeechRequest

use of com.amazonaws.services.polly.model.SynthesizeSpeechRequest 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

SynthesizeSpeechRequest (com.amazonaws.services.polly.model.SynthesizeSpeechRequest)4 SynthesizeSpeechResult (com.amazonaws.services.polly.model.SynthesizeSpeechResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonPollyClient (com.amazonaws.services.polly.AmazonPollyClient)1 TextType (com.amazonaws.services.polly.model.TextType)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 SpeechGeneratorConfiguration (com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration)1 File (java.io.File)1 InputStream (java.io.InputStream)1