summaryrefslogtreecommitdiffstats
path: root/pico/compat/src/com/android
diff options
context:
space:
mode:
Diffstat (limited to 'pico/compat/src/com/android')
-rwxr-xr-xpico/compat/src/com/android/tts/compat/CompatTtsService.java145
-rwxr-xr-xpico/compat/src/com/android/tts/compat/SynthProxy.java183
2 files changed, 328 insertions, 0 deletions
diff --git a/pico/compat/src/com/android/tts/compat/CompatTtsService.java b/pico/compat/src/com/android/tts/compat/CompatTtsService.java
new file mode 100755
index 0000000..1ec7ebf
--- /dev/null
+++ b/pico/compat/src/com/android/tts/compat/CompatTtsService.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.android.tts.compat;
+
+import android.database.Cursor;
+import android.net.Uri;
+import android.speech.tts.SynthesisRequest;
+import android.speech.tts.TextToSpeech;
+import android.speech.tts.TextToSpeechService;
+import android.util.Log;
+
+import java.io.File;
+
+public abstract class CompatTtsService extends TextToSpeechService {
+
+ private static final boolean DBG = false;
+ private static final String TAG = "CompatTtsService";
+
+ private SynthProxy mNativeSynth = null;
+
+ protected abstract String getSoFilename();
+
+ @Override
+ public void onCreate() {
+ if (DBG) Log.d(TAG, "onCreate()");
+ super.onCreate();
+
+ String soFilename = getSoFilename();
+
+ File f = new File(soFilename);
+ if (!f.exists()) {
+ Log.e(TAG, "Invalid TTS Binary: " + soFilename);
+ return;
+ }
+
+ if (mNativeSynth != null) {
+ mNativeSynth.stopSync();
+ mNativeSynth.shutdown();
+ mNativeSynth = null;
+ }
+
+ // Load the engineConfig from the plugin if it has any special configuration
+ // to be loaded. By convention, if an engine wants the TTS framework to pass
+ // in any configuration, it must put it into its content provider which has the URI:
+ // content://<packageName>.providers.SettingsProvider
+ // That content provider must provide a Cursor which returns the String that
+ // is to be passed back to the native .so file for the plugin when getString(0) is
+ // called on it.
+ // Note that the TTS framework does not care what this String data is: it is something
+ // that comes from the engine plugin and is consumed only by the engine plugin itself.
+ String engineConfig = "";
+ Cursor c = getContentResolver().query(Uri.parse("content://" + getPackageName()
+ + ".providers.SettingsProvider"), null, null, null, null);
+ if (c != null){
+ c.moveToFirst();
+ engineConfig = c.getString(0);
+ c.close();
+ }
+ mNativeSynth = new SynthProxy(soFilename, engineConfig);
+ }
+
+ @Override
+ public void onDestroy() {
+ if (DBG) Log.d(TAG, "onDestroy()");
+ super.onDestroy();
+
+ if (mNativeSynth != null) {
+ mNativeSynth.shutdown();
+ }
+ mNativeSynth = null;
+ }
+
+ @Override
+ protected String[] onGetLanguage() {
+ if (mNativeSynth == null) return null;
+ return mNativeSynth.getLanguage();
+ }
+
+ @Override
+ protected int onIsLanguageAvailable(String lang, String country, String variant) {
+ if (DBG) Log.d(TAG, "onIsLanguageAvailable(" + lang + "," + country + "," + variant + ")");
+ if (mNativeSynth == null) return TextToSpeech.ERROR;
+ return mNativeSynth.isLanguageAvailable(lang, country, variant);
+ }
+
+ @Override
+ protected int onLoadLanguage(String lang, String country, String variant) {
+ if (DBG) Log.d(TAG, "onLoadLanguage(" + lang + "," + country + "," + variant + ")");
+ int result = onIsLanguageAvailable(lang, country, variant);
+ // TODO: actually load language
+ return result;
+ }
+
+ @Override
+ protected int onSynthesizeText(SynthesisRequest request) {
+ if (mNativeSynth == null) return TextToSpeech.ERROR;
+
+ // Set language
+ String lang = request.getLanguage();
+ String country = request.getCountry();
+ String variant = request.getVariant();
+ if (mNativeSynth.setLanguage(lang, country, variant) != TextToSpeech.SUCCESS) {
+ Log.e(TAG, "setLanguage(" + lang + "," + country + "," + variant + ") failed");
+ return TextToSpeech.ERROR;
+ }
+
+ // Set speech rate
+ int speechRate = request.getSpeechRate();
+ if (mNativeSynth.setSpeechRate(speechRate) != TextToSpeech.SUCCESS) {
+ Log.e(TAG, "setSpeechRate(" + speechRate + ") failed");
+ return TextToSpeech.ERROR;
+ }
+
+ // Set speech
+ int pitch = request.getPitch();
+ if (mNativeSynth.setPitch(pitch) != TextToSpeech.SUCCESS) {
+ Log.e(TAG, "setPitch(" + pitch + ") failed");
+ return TextToSpeech.ERROR;
+ }
+
+ // Synthesize
+ return mNativeSynth.speak(request);
+ }
+
+ @Override
+ protected void onStop() {
+ if (DBG) Log.d(TAG, "onStop()");
+ if (mNativeSynth == null) return;
+ mNativeSynth.stop();
+ }
+
+}
diff --git a/pico/compat/src/com/android/tts/compat/SynthProxy.java b/pico/compat/src/com/android/tts/compat/SynthProxy.java
new file mode 100755
index 0000000..68d0d92
--- /dev/null
+++ b/pico/compat/src/com/android/tts/compat/SynthProxy.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.android.tts.compat;
+
+import android.speech.tts.SynthesisRequest;
+import android.util.Log;
+
+/**
+ * The SpeechSynthesis class provides a high-level api to create and play
+ * synthesized speech. This class is used internally to talk to a native
+ * TTS library that implements the interface defined in
+ * frameworks/base/include/tts/TtsEngine.h
+ *
+ */
+public class SynthProxy {
+
+ static {
+ System.loadLibrary("ttscompat");
+ }
+
+ private final static String TAG = "SynthProxy";
+
+ // Default parameters of a filter to be applied when using the Pico engine.
+ // Such a huge filter gain is justified by how much energy in the low frequencies is "wasted" at
+ // the output of the synthesis. The low shelving filter removes it, leaving room for
+ // amplification.
+ private final static float PICO_FILTER_GAIN = 5.0f; // linear gain
+ private final static float PICO_FILTER_LOWSHELF_ATTENUATION = -18.0f; // in dB
+ private final static float PICO_FILTER_TRANSITION_FREQ = 1100.0f; // in Hz
+ private final static float PICO_FILTER_SHELF_SLOPE = 1.0f; // Q
+
+ private int mJniData = 0;
+
+ /**
+ * Constructor; pass the location of the native TTS .so to use.
+ */
+ public SynthProxy(String nativeSoLib, String engineConfig) {
+ boolean applyFilter = shouldApplyAudioFilter(nativeSoLib);
+ Log.v(TAG, "About to load "+ nativeSoLib + ", applyFilter=" + applyFilter);
+ mJniData = native_setup(nativeSoLib, engineConfig);
+ if (mJniData == 0) {
+ throw new RuntimeException("Failed to load " + nativeSoLib);
+ }
+ native_setLowShelf(applyFilter, PICO_FILTER_GAIN, PICO_FILTER_LOWSHELF_ATTENUATION,
+ PICO_FILTER_TRANSITION_FREQ, PICO_FILTER_SHELF_SLOPE);
+ }
+
+ // HACK: Apply audio filter if the engine is pico
+ private boolean shouldApplyAudioFilter(String nativeSoLib) {
+ return nativeSoLib.toLowerCase().contains("pico");
+ }
+
+ /**
+ * Stops and clears the AudioTrack.
+ */
+ public int stop() {
+ return native_stop(mJniData);
+ }
+
+ /**
+ * Synchronous stop of the synthesizer. This method returns when the synth
+ * has completed the stop procedure and doesn't use any of the resources it
+ * was using while synthesizing.
+ *
+ * @return {@link android.speech.tts.TextToSpeech#SUCCESS} or
+ * {@link android.speech.tts.TextToSpeech#ERROR}
+ */
+ public int stopSync() {
+ return native_stopSync(mJniData);
+ }
+
+ public int speak(SynthesisRequest request) {
+ return native_speak(mJniData, request.getText(), request);
+ }
+
+ /**
+ * Queries for language support.
+ * Return codes are defined in android.speech.tts.TextToSpeech
+ */
+ public int isLanguageAvailable(String language, String country, String variant) {
+ return native_isLanguageAvailable(mJniData, language, country, variant);
+ }
+
+ /**
+ * Updates the engine configuration.
+ */
+ public int setConfig(String engineConfig) {
+ return native_setProperty(mJniData, "engineConfig", engineConfig);
+ }
+
+ /**
+ * Sets the language.
+ */
+ public int setLanguage(String language, String country, String variant) {
+ return native_setLanguage(mJniData, language, country, variant);
+ }
+
+ /**
+ * Loads the language: it's not set, but prepared for use later.
+ */
+ public int loadLanguage(String language, String country, String variant) {
+ return native_loadLanguage(mJniData, language, country, variant);
+ }
+
+ /**
+ * Sets the speech rate.
+ */
+ public final int setSpeechRate(int speechRate) {
+ return native_setProperty(mJniData, "rate", String.valueOf(speechRate));
+ }
+
+ /**
+ * Sets the pitch of the synthesized voice.
+ */
+ public final int setPitch(int pitch) {
+ return native_setProperty(mJniData, "pitch", String.valueOf(pitch));
+ }
+
+ /**
+ * Returns the currently set language, country and variant information.
+ */
+ public String[] getLanguage() {
+ return native_getLanguage(mJniData);
+ }
+
+ /**
+ * Shuts down the native synthesizer.
+ */
+ public void shutdown() {
+ native_shutdown(mJniData);
+ }
+
+ @Override
+ protected void finalize() {
+ if (mJniData != 0) {
+ Log.w(TAG, "SynthProxy finalized without being shutdown");
+ native_finalize(mJniData);
+ mJniData = 0;
+ }
+ }
+
+ private native final int native_setup(String nativeSoLib, String engineConfig);
+
+ private native final int native_setLowShelf(boolean applyFilter, float filterGain,
+ float attenuationInDb, float freqInHz, float slope);
+
+ private native final void native_finalize(int jniData);
+
+ private native final int native_stop(int jniData);
+
+ private native final int native_stopSync(int jniData);
+
+ private native final int native_speak(int jniData, String text, SynthesisRequest request);
+
+ private native final int native_isLanguageAvailable(int jniData, String language,
+ String country, String variant);
+
+ private native final int native_setLanguage(int jniData, String language, String country,
+ String variant);
+
+ private native final int native_loadLanguage(int jniData, String language, String country,
+ String variant);
+
+ private native final int native_setProperty(int jniData, String name, String value);
+
+ private native final String[] native_getLanguage(int jniData);
+
+ private native final void native_shutdown(int jniData);
+
+}