Example usage for android.speech.tts SynthesisRequest getCountry

List of usage examples for android.speech.tts SynthesisRequest getCountry

Introduction

In this page you can find the example usage for android.speech.tts SynthesisRequest getCountry.

Prototype

public String getCountry() 

Source Link

Document

Gets the ISO 3-letter country code for the language to use.

Usage

From source file:com.github.olga_yakovleva.rhvoice.android.RHVoiceService.java

@Override
protected void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) {
    if (BuildConfig.DEBUG) {
        Log.v(TAG, "onSynthesize called");
        logLanguage(request.getLanguage(), request.getCountry(), request.getVariant());
    }/*from  w  ww  .j  a v  a  2s . c o m*/
    Tts tts = ttsManager.acquireForSynthesis();
    if (tts == null) {
        if (BuildConfig.DEBUG)
            Log.w(TAG, "Not initialized yet");
        callback.error();
        return;
    }
    try {
        speaking = true;
        Map<String, LanguageSettings> languageSettings = getLanguageSettings(tts);
        String voiceName = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            voiceName = request.getVoiceName();
        final Candidate bestMatch = findBestVoice(tts, request.getLanguage(), request.getCountry(),
                request.getVariant(), voiceName, languageSettings);
        if (bestMatch.voice == null) {
            if (BuildConfig.DEBUG)
                Log.e(TAG, "Unsupported language");
            callback.error();
            return;
        }
        if (BuildConfig.DEBUG)
            Log.v(TAG, "Selected voice: " + bestMatch.voice.getSource().getName());
        currentVoice = bestMatch.voice;
        StringBuilder voiceProfileSpecBuilder = new StringBuilder();
        voiceProfileSpecBuilder.append(bestMatch.voice.getSource().getName());
        for (Map.Entry<String, LanguageSettings> entry : languageSettings.entrySet()) {
            if (entry.getKey().equals(bestMatch.voice.getLanguage()))
                continue;
            if (entry.getValue().detect) {
                String name = entry.getValue().voice.getSource().getName();
                voiceProfileSpecBuilder.append("+").append(name);
            }
        }
        String profileSpec = voiceProfileSpecBuilder.toString();
        if (BuildConfig.DEBUG)
            Log.v(TAG, "Synthesizing the following text: " + request.getText());
        int rate = request.getSpeechRate();
        if (BuildConfig.DEBUG)
            Log.v(TAG, "rate=" + rate);
        int pitch = request.getPitch();
        if (BuildConfig.DEBUG)
            Log.v(TAG, "pitch=" + pitch);
        if (BuildConfig.DEBUG)
            Log.v(TAG, "Profile: " + profileSpec);
        final SynthesisParameters params = new SynthesisParameters();
        params.setVoiceProfile(profileSpec);
        params.setRate(((double) rate) / 100.0);
        params.setPitch(((double) pitch) / 100.0);
        final Player player = new Player(callback);
        callback.start(24000, AudioFormat.ENCODING_PCM_16BIT, 1);
        tts.engine.speak(request.getText(), params, player);
        callback.done();
    } catch (RHVoiceException e) {
        if (BuildConfig.DEBUG)
            Log.e(TAG, "Synthesis error", e);
        callback.error();
    } finally {
        speaking = false;
        ttsManager.release(tts);
    }
}