Example usage for com.amazonaws.services.polly.model SynthesizeSpeechResult getAudioStream

List of usage examples for com.amazonaws.services.polly.model SynthesizeSpeechResult getAudioStream

Introduction

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

Prototype


public java.io.InputStream getAudioStream() 

Source Link

Document

Stream containing the synthesized speech.

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  ww w .  ja  va  2 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: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());
        }/*from ww  w. j  a v  a2s .  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();
}