Example usage for com.amazonaws.services.polly.model SynthesizeSpeechRequest SynthesizeSpeechRequest

List of usage examples for com.amazonaws.services.polly.model SynthesizeSpeechRequest SynthesizeSpeechRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.polly.model SynthesizeSpeechRequest SynthesizeSpeechRequest.

Prototype

SynthesizeSpeechRequest

Source Link

Usage

From source file:com.github.gregwhitaker.awspolly.example.PollyReadHandler.java

License:Apache License

@Override
public void handle(Context ctx) throws Exception {
    String voiceId = ctx.getRequest().getQueryParams().get("voiceId");
    String text = ctx.getRequest().getQueryParams().get("text");
    String outputFormat = ctx.getRequest().getQueryParams().get("outputFormat");

    SynthesizeSpeechRequest ssRequest = new SynthesizeSpeechRequest();
    ssRequest.setVoiceId(voiceId);//from   w  w w.  jav a2  s.co  m
    ssRequest.setOutputFormat(outputFormat);
    ssRequest.setText(text);

    SynthesizeSpeechResult result = polly.synthesizeSpeech(ssRequest);

    ctx.getResponse().contentType(result.getContentType());
    ctx.getResponse().sendStream(s -> s.onSubscribe(new Subscription() {
        @Override
        public void request(long n) {
            try {
                byte[] data = new byte[1024];
                int bytesRead = result.getAudioStream().read(data);

                while (bytesRead != -1) {
                    s.onNext(Unpooled.wrappedBuffer(data));
                    bytesRead = result.getAudioStream().read(data);
                }
            } catch (IOException e) {
                ctx.getResponse().status(500);
                ctx.getResponse().send();
            } finally {
                s.onComplete();
            }
        }

        @Override
        public void cancel() {

        }
    }));
}

From source file:com.waltercedric.tvprogram.plugins.reader.PollyTTSReader.java

License:Open Source License

private SynthesizeSpeechRequest newRequest() {
    SynthesizeSpeechRequest tssRequest = new SynthesizeSpeechRequest();
    tssRequest.setVoiceId(config.getVoiceid());
    tssRequest.setOutputFormat(OutputFormat.Mp3);

    return tssRequest;
}

From source file:org.openhab.voice.pollytts.internal.cloudapi.PollyTTSCloudImpl.java

License:Open Source License

/**
 * This method will return an input stream to an audio stream for the given
 * parameters./*from w ww .  j  a  v a2 s.c om*/
 * 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();
}

From source file:org.restcomm.connect.tts.awspolly.AWSPollySpeechSyntetizer.java

License:Open Source License

private URI synthesize(final Object message) throws IOException, SpeechSynthesizerException {

    //retrieve resquest message
    final org.restcomm.connect.tts.api.SpeechSynthesizerRequest request = (org.restcomm.connect.tts.api.SpeechSynthesizerRequest) message;

    //retrieve gender, language and text message
    final String gender = request.gender();
    final String language = request.language();
    final String text = request.text();

    //generate file hash name
    final String hash = HashGenerator.hashMessage(gender, language, text);

    if (language == null) {
        if (logger.isInfoEnabled()) {
            logger.info("There is no suitable speaker to synthesize " + request.language());
        }/*w w w.j av a  2  s.c o  m*/
        throw new IllegalArgumentException("There is no suitable language to synthesize " + request.language());
    }

    // Create speech synthesis request.
    SynthesizeSpeechRequest pollyRequest = new SynthesizeSpeechRequest().withText(text)
            .withVoiceId(VoiceId.valueOf(this.retrieveSpeaker(gender, language)))
            .withOutputFormat(OutputFormat.Pcm).withSampleRate("8000");
    //retrieve audio result
    SynthesizeSpeechResult result = pollyClient.synthesizeSpeech(pollyRequest);

    //create a temporary file
    File srcFile = new File(System.getProperty("java.io.tmpdir") + File.separator + hash + ".pcm");
    File dstFile = new File(System.getProperty("java.io.tmpdir") + File.separator + hash + ".wav");

    //save temporary pcm file
    Files.copy(result.getAudioStream(), srcFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

    //convert pcm file to wav
    new PcmToWavConverterUtils().rawToWave(srcFile, dstFile);

    //return file URI
    return dstFile.toURI();
}