OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/speech/tts_android.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/android/jni_string.h" |
| 10 #include "base/memory/singleton.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/speech/tts_controller.h" |
| 13 #include "jni/TtsPlatformImpl_jni.h" |
| 14 |
| 15 using base::android::AttachCurrentThread; |
| 16 |
| 17 // static |
| 18 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { |
| 19 return TtsPlatformImplAndroid::GetInstance(); |
| 20 } |
| 21 |
| 22 TtsPlatformImplAndroid::TtsPlatformImplAndroid() |
| 23 : utterance_id_(0) { |
| 24 JNIEnv* env = AttachCurrentThread(); |
| 25 java_ref_.Reset( |
| 26 Java_TtsPlatformImpl_create(env, |
| 27 reinterpret_cast<jint>(this), |
| 28 base::android::GetApplicationContext())); |
| 29 } |
| 30 |
| 31 TtsPlatformImplAndroid::~TtsPlatformImplAndroid() { |
| 32 JNIEnv* env = AttachCurrentThread(); |
| 33 Java_TtsPlatformImpl_destroy(env, java_ref_.obj()); |
| 34 } |
| 35 |
| 36 bool TtsPlatformImplAndroid::PlatformImplAvailable() { |
| 37 return true; |
| 38 } |
| 39 |
| 40 bool TtsPlatformImplAndroid::Speak( |
| 41 int utterance_id, |
| 42 const std::string& utterance, |
| 43 const std::string& lang, |
| 44 const VoiceData& voice, |
| 45 const UtteranceContinuousParameters& params) { |
| 46 JNIEnv* env = AttachCurrentThread(); |
| 47 if (!Java_TtsPlatformImpl_isInitialized(env, java_ref_.obj())) |
| 48 return false; |
| 49 |
| 50 jboolean success = Java_TtsPlatformImpl_speak( |
| 51 env, java_ref_.obj(), |
| 52 utterance_id, |
| 53 base::android::ConvertUTF8ToJavaString(env, utterance).Release(), |
| 54 base::android::ConvertUTF8ToJavaString(env, lang).Release(), |
| 55 params.rate, params.pitch, params.volume); |
| 56 if (!success) |
| 57 return false; |
| 58 |
| 59 utterance_ = utterance; |
| 60 utterance_id_ = utterance_id; |
| 61 return true; |
| 62 } |
| 63 |
| 64 bool TtsPlatformImplAndroid::StopSpeaking() { |
| 65 JNIEnv* env = AttachCurrentThread(); |
| 66 Java_TtsPlatformImpl_stop(env, java_ref_.obj()); |
| 67 utterance_id_ = 0; |
| 68 utterance_.clear(); |
| 69 return true; |
| 70 } |
| 71 |
| 72 bool TtsPlatformImplAndroid::IsSpeaking() { |
| 73 return (utterance_id_ != 0); |
| 74 } |
| 75 |
| 76 void TtsPlatformImplAndroid::GetVoices( |
| 77 std::vector<VoiceData>* out_voices) { |
| 78 JNIEnv* env = AttachCurrentThread(); |
| 79 if (!Java_TtsPlatformImpl_isInitialized(env, java_ref_.obj())) |
| 80 return; |
| 81 |
| 82 int count = Java_TtsPlatformImpl_getVoiceCount(env, java_ref_.obj()); |
| 83 for (int i = 0; i < count; ++i) { |
| 84 out_voices->push_back(VoiceData()); |
| 85 VoiceData& data = out_voices->back(); |
| 86 data.native = true; |
| 87 data.name = base::android::ConvertJavaStringToUTF8( |
| 88 Java_TtsPlatformImpl_getVoiceName(env, java_ref_.obj(), i)); |
| 89 data.lang = base::android::ConvertJavaStringToUTF8( |
| 90 Java_TtsPlatformImpl_getVoiceLanguage(env, java_ref_.obj(), i)); |
| 91 data.gender = TTS_GENDER_NONE; |
| 92 data.events.insert(TTS_EVENT_START); |
| 93 data.events.insert(TTS_EVENT_END); |
| 94 data.events.insert(TTS_EVENT_ERROR); |
| 95 } |
| 96 } |
| 97 |
| 98 void TtsPlatformImplAndroid::VoicesChanged(JNIEnv* env, jobject obj) { |
| 99 TtsController::GetInstance()->VoicesChanged(); |
| 100 } |
| 101 |
| 102 void TtsPlatformImplAndroid::OnEndEvent( |
| 103 JNIEnv* env, jobject obj, jint utterance_id) { |
| 104 SendFinalTtsEvent(utterance_id, TTS_EVENT_END, |
| 105 static_cast<int>(utterance_.size())); |
| 106 } |
| 107 |
| 108 void TtsPlatformImplAndroid::OnErrorEvent( |
| 109 JNIEnv* env, jobject obj, jint utterance_id) { |
| 110 SendFinalTtsEvent(utterance_id, TTS_EVENT_ERROR, 0); |
| 111 } |
| 112 |
| 113 void TtsPlatformImplAndroid::OnStartEvent( |
| 114 JNIEnv* env, jobject obj, jint utterance_id) { |
| 115 if (utterance_id != utterance_id_) |
| 116 return; |
| 117 |
| 118 TtsController::GetInstance()->OnTtsEvent( |
| 119 utterance_id_, TTS_EVENT_START, 0, std::string()); |
| 120 } |
| 121 |
| 122 void TtsPlatformImplAndroid::SendFinalTtsEvent( |
| 123 int utterance_id, TtsEventType event_type, int char_index) { |
| 124 if (utterance_id != utterance_id_) |
| 125 return; |
| 126 |
| 127 TtsController::GetInstance()->OnTtsEvent( |
| 128 utterance_id_, event_type, char_index, std::string()); |
| 129 utterance_id_ = 0; |
| 130 utterance_.clear(); |
| 131 } |
| 132 |
| 133 // static |
| 134 TtsPlatformImplAndroid* TtsPlatformImplAndroid::GetInstance() { |
| 135 return Singleton<TtsPlatformImplAndroid>::get(); |
| 136 } |
| 137 |
| 138 // static |
| 139 bool TtsPlatformImplAndroid::Register(JNIEnv* env) { |
| 140 return RegisterNativesImpl(env); |
| 141 } |
OLD | NEW |