Example usage for com.amazonaws.services.polly.model OutputFormat Pcm

List of usage examples for com.amazonaws.services.polly.model OutputFormat Pcm

Introduction

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

Prototype

OutputFormat Pcm

To view the source code for com.amazonaws.services.polly.model OutputFormat Pcm.

Click Source Link

Usage

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   w ww  .j  a  v a2  s  .c om*/
        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();
}