| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 1 | /* |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009-2010 Google Inc. |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #define LOG_TAG "SynthProxy" |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | #include <nativehelper/jni.h> |
| 24 | #include <nativehelper/JNIHelp.h> |
| 25 | #include <android_runtime/AndroidRuntime.h> |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 26 | #include <android/tts.h> |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 27 | #include <media/AudioTrack.h> |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 28 | #include <math.h> |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 29 | |
| 30 | #include <dlfcn.h> |
| 31 | |
| 32 | #define DEFAULT_TTS_RATE 16000 |
| 33 | #define DEFAULT_TTS_FORMAT AudioSystem::PCM_16_BIT |
| 34 | #define DEFAULT_TTS_NB_CHANNELS 1 |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 35 | #define DEFAULT_TTS_BUFFERSIZE 2048 |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 36 | // TODO use the TTS stream type when available |
| 37 | #define DEFAULT_TTS_STREAM_TYPE AudioSystem::MUSIC |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 38 | |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 39 | // EQ + BOOST parameters |
| 40 | #define FILTER_LOWSHELF_ATTENUATION -18.0f // in dB |
| 41 | #define FILTER_TRANSITION_FREQ 1100.0f // in Hz |
| 42 | #define FILTER_SHELF_SLOPE 1.0f // Q |
| Jean-Michel Trivi | 3218471 | 2009-09-01 10:22:51 -0700 | [diff] [blame] | 43 | #define FILTER_GAIN 5.5f // linear gain |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 44 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 45 | #define USAGEMODE_PLAY_IMMEDIATELY 0 |
| 46 | #define USAGEMODE_WRITE_TO_FILE 1 |
| 47 | |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 48 | #define SYNTHPLAYSTATE_IS_STOPPED 0 |
| 49 | #define SYNTHPLAYSTATE_IS_PLAYING 1 |
| 50 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 51 | using namespace android; |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | struct fields_t { |
| 55 | jfieldID synthProxyFieldJniData; |
| 56 | jclass synthProxyClass; |
| 57 | jmethodID synthProxyMethodPost; |
| 58 | }; |
| 59 | |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 60 | // structure to hold the data that is used each time the TTS engine has synthesized more data |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 61 | struct afterSynthData_t { |
| 62 | jint jniStorage; |
| 63 | int usageMode; |
| 64 | FILE* outputFile; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 65 | AudioSystem::stream_type streamType; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // ---------------------------------------------------------------------------- |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 69 | // EQ data |
| 70 | double amp; |
| 71 | double w; |
| 72 | double sinw; |
| 73 | double cosw; |
| 74 | double beta; |
| 75 | double a0, a1, a2, b0, b1, b2; |
| 76 | double m_fa, m_fb, m_fc, m_fd, m_fe; |
| 77 | double x0; // x[n] |
| 78 | double x1; // x[n-1] |
| 79 | double x2; // x[n-2] |
| 80 | double out0;// y[n] |
| 81 | double out1;// y[n-1] |
| 82 | double out2;// y[n-2] |
| 83 | |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 84 | static float fFilterLowshelfAttenuation = FILTER_LOWSHELF_ATTENUATION; |
| 85 | static float fFilterTransitionFreq = FILTER_TRANSITION_FREQ; |
| 86 | static float fFilterShelfSlope = FILTER_SHELF_SLOPE; |
| 87 | static float fFilterGain = FILTER_GAIN; |
| 88 | static bool bUseFilter = false; |
| 89 | |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 90 | void initializeEQ() { |
| 91 | |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 92 | amp = float(pow(10.0, fFilterLowshelfAttenuation / 40.0)); |
| 93 | w = 2.0 * M_PI * (fFilterTransitionFreq / DEFAULT_TTS_RATE); |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 94 | sinw = float(sin(w)); |
| 95 | cosw = float(cos(w)); |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 96 | beta = float(sqrt(amp)/fFilterShelfSlope); |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 97 | |
| 98 | // initialize low-shelf parameters |
| 99 | b0 = amp * ((amp+1.0F) - ((amp-1.0F)*cosw) + (beta*sinw)); |
| 100 | b1 = 2.0F * amp * ((amp-1.0F) - ((amp+1.0F)*cosw)); |
| 101 | b2 = amp * ((amp+1.0F) - ((amp-1.0F)*cosw) - (beta*sinw)); |
| 102 | a0 = (amp+1.0F) + ((amp-1.0F)*cosw) + (beta*sinw); |
| 103 | a1 = 2.0F * ((amp-1.0F) + ((amp+1.0F)*cosw)); |
| 104 | a2 = -((amp+1.0F) + ((amp-1.0F)*cosw) - (beta*sinw)); |
| 105 | |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 106 | m_fa = fFilterGain * b0/a0; |
| 107 | m_fb = fFilterGain * b1/a0; |
| 108 | m_fc = fFilterGain * b2/a0; |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 109 | m_fd = a1/a0; |
| 110 | m_fe = a2/a0; |
| 111 | } |
| 112 | |
| 113 | void initializeFilter() { |
| 114 | x0 = 0.0f; |
| 115 | x1 = 0.0f; |
| 116 | x2 = 0.0f; |
| 117 | out0 = 0.0f; |
| 118 | out1 = 0.0f; |
| 119 | out2 = 0.0f; |
| 120 | } |
| 121 | |
| 122 | void applyFilter(int16_t* buffer, size_t sampleCount) { |
| 123 | |
| 124 | for (size_t i=0 ; i<sampleCount ; i++) { |
| 125 | |
| 126 | x0 = (double) buffer[i]; |
| 127 | |
| 128 | out0 = (m_fa*x0) + (m_fb*x1) + (m_fc*x2) + (m_fd*out1) + (m_fe*out2); |
| 129 | |
| 130 | x2 = x1; |
| 131 | x1 = x0; |
| 132 | |
| 133 | out2 = out1; |
| 134 | out1 = out0; |
| 135 | |
| 136 | if (out0 > 32767.0f) { |
| 137 | buffer[i] = 32767; |
| 138 | } else if (out0 < -32768.0f) { |
| 139 | buffer[i] = -32768; |
| 140 | } else { |
| 141 | buffer[i] = (int16_t) out0; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | // ---------------------------------------------------------------------------- |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 148 | static fields_t javaTTSFields; |
| 149 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 150 | // TODO move to synth member once we have multiple simultaneous engines running |
| 151 | static Mutex engineMutex; |
| 152 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 153 | // ---------------------------------------------------------------------------- |
| 154 | class SynthProxyJniStorage { |
| 155 | public : |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 156 | jobject tts_ref; |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 157 | android_tts_engine_t* mEngine; |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 158 | void* mEngineLibHandle; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 159 | AudioTrack* mAudioOut; |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 160 | int8_t mPlayState; |
| 161 | Mutex mPlayLock; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 162 | AudioSystem::stream_type mStreamType; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 163 | uint32_t mSampleRate; |
| Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 164 | uint32_t mAudFormat; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 165 | int mNbChannels; |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 166 | int8_t * mBuffer; |
| 167 | size_t mBufferSize; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 168 | |
| 169 | SynthProxyJniStorage() { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 170 | tts_ref = NULL; |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 171 | mEngine = NULL; |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 172 | mEngineLibHandle = NULL; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 173 | mAudioOut = NULL; |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 174 | mPlayState = SYNTHPLAYSTATE_IS_STOPPED; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 175 | mStreamType = DEFAULT_TTS_STREAM_TYPE; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 176 | mSampleRate = DEFAULT_TTS_RATE; |
| 177 | mAudFormat = DEFAULT_TTS_FORMAT; |
| 178 | mNbChannels = DEFAULT_TTS_NB_CHANNELS; |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 179 | mBufferSize = DEFAULT_TTS_BUFFERSIZE; |
| 180 | mBuffer = new int8_t[mBufferSize]; |
| Charles Chen | 4a3368f | 2009-07-14 17:11:44 -0700 | [diff] [blame] | 181 | memset(mBuffer, 0, mBufferSize); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ~SynthProxyJniStorage() { |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 185 | //LOGV("entering ~SynthProxyJniStorage()"); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 186 | killAudio(); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 187 | if (mEngine) { |
| 188 | mEngine->funcs->shutdown(mEngine); |
| 189 | mEngine = NULL; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 190 | } |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 191 | if (mEngineLibHandle) { |
| 192 | //LOGE("~SynthProxyJniStorage(): before close library"); |
| 193 | int res = dlclose(mEngineLibHandle); |
| 194 | LOGE_IF( res != 0, "~SynthProxyJniStorage(): dlclose returned %d", res); |
| 195 | } |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 196 | delete mBuffer; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void killAudio() { |
| 200 | if (mAudioOut) { |
| 201 | mAudioOut->stop(); |
| 202 | delete mAudioOut; |
| 203 | mAudioOut = NULL; |
| 204 | } |
| 205 | } |
| 206 | |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 207 | void createAudioOut(AudioSystem::stream_type streamType, uint32_t rate, |
| 208 | AudioSystem::audio_format format, int channel) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 209 | mSampleRate = rate; |
| 210 | mAudFormat = format; |
| 211 | mNbChannels = channel; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 212 | mStreamType = streamType; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 213 | |
| 214 | // retrieve system properties to ensure successful creation of the |
| 215 | // AudioTrack object for playback |
| 216 | int afSampleRate; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 217 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 218 | afSampleRate = 44100; |
| 219 | } |
| 220 | int afFrameCount; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 221 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 222 | afFrameCount = 2048; |
| 223 | } |
| 224 | uint32_t afLatency; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 225 | if (AudioSystem::getOutputLatency(&afLatency, mStreamType) != NO_ERROR) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 226 | afLatency = 500; |
| 227 | } |
| 228 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 229 | if (minBufCount < 2) minBufCount = 2; |
| 230 | int minFrameCount = (afFrameCount * rate * minBufCount)/afSampleRate; |
| 231 | |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 232 | mPlayLock.lock(); |
| Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 233 | mAudioOut = new AudioTrack(mStreamType, rate, format, |
| 234 | (channel == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 235 | minFrameCount > 4096 ? minFrameCount : 4096, |
| 236 | 0, 0, 0, 0); // not using an AudioTrack callback |
| 237 | |
| 238 | if (mAudioOut->initCheck() != NO_ERROR) { |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 239 | LOGE("createAudioOut(): AudioTrack error"); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 240 | delete mAudioOut; |
| 241 | mAudioOut = NULL; |
| 242 | } else { |
| Charles Chen | b02ced7 | 2009-07-07 14:33:52 -0700 | [diff] [blame] | 243 | //LOGI("AudioTrack OK"); |
| Jean-Michel Trivi | ddc63ad | 2010-01-08 14:06:21 -0800 | [diff] [blame] | 244 | mAudioOut->setVolume(1.0f, 1.0f); |
| 245 | LOGV("AudioTrack ready"); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 246 | } |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 247 | mPlayLock.unlock(); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 248 | } |
| 249 | }; |
| 250 | |
| 251 | |
| 252 | // ---------------------------------------------------------------------------- |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 253 | void prepAudioTrack(SynthProxyJniStorage* pJniData, AudioSystem::stream_type streamType, |
| 254 | uint32_t rate, AudioSystem::audio_format format, int channel) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 255 | // Don't bother creating a new audiotrack object if the current |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 256 | // object is already initialized with the same audio parameters. |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 257 | if ( pJniData->mAudioOut && |
| 258 | (rate == pJniData->mSampleRate) && |
| 259 | (format == pJniData->mAudFormat) && |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 260 | (channel == pJniData->mNbChannels) && |
| 261 | (streamType == pJniData->mStreamType) ){ |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | if (pJniData->mAudioOut){ |
| 265 | pJniData->killAudio(); |
| 266 | } |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 267 | pJniData->createAudioOut(streamType, rate, format, channel); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | // ---------------------------------------------------------------------------- |
| 272 | /* |
| 273 | * Callback from TTS engine. |
| 274 | * Directly speaks using AudioTrack or write to file |
| 275 | */ |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 276 | extern "C" android_tts_callback_status_t |
| 277 | __ttsSynthDoneCB(void ** pUserdata, uint32_t rate, |
| 278 | android_tts_audio_format_t format, int channel, |
| 279 | int8_t **pWav, size_t *pBufferSize, |
| 280 | android_tts_synth_status_t status) |
| 281 | { |
| Charles Chen | 2a8a2d7 | 2009-07-07 16:24:02 -0700 | [diff] [blame] | 282 | //LOGV("ttsSynthDoneCallback: %d bytes", bufferSize); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 283 | AudioSystem::audio_format encoding; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 284 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 285 | if (*pUserdata == NULL){ |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 286 | LOGE("userdata == NULL"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 287 | return ANDROID_TTS_CALLBACK_HALT; |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 288 | } |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 289 | switch (format) { |
| 290 | case ANDROID_TTS_AUDIO_FORMAT_PCM_8_BIT: |
| 291 | encoding = AudioSystem::PCM_8_BIT; |
| 292 | break; |
| 293 | case ANDROID_TTS_AUDIO_FORMAT_PCM_16_BIT: |
| 294 | encoding = AudioSystem::PCM_16_BIT; |
| 295 | break; |
| 296 | default: |
| 297 | LOGE("Can't play, bad format"); |
| 298 | return ANDROID_TTS_CALLBACK_HALT; |
| 299 | } |
| 300 | afterSynthData_t* pForAfter = (afterSynthData_t*) *pUserdata; |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 301 | SynthProxyJniStorage* pJniData = (SynthProxyJniStorage*)(pForAfter->jniStorage); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 302 | |
| 303 | if (pForAfter->usageMode == USAGEMODE_PLAY_IMMEDIATELY){ |
| Charles Chen | b02ced7 | 2009-07-07 14:33:52 -0700 | [diff] [blame] | 304 | //LOGV("Direct speech"); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 305 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 306 | if (*pWav == NULL) { |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 307 | delete pForAfter; |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 308 | pForAfter = NULL; |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 309 | LOGV("Null: speech has completed"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 310 | return ANDROID_TTS_CALLBACK_HALT; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 313 | if (*pBufferSize > 0) { |
| 314 | prepAudioTrack(pJniData, pForAfter->streamType, rate, encoding, channel); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 315 | if (pJniData->mAudioOut) { |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 316 | pJniData->mPlayLock.lock(); |
| 317 | if(pJniData->mAudioOut->stopped() |
| 318 | && (pJniData->mPlayState == SYNTHPLAYSTATE_IS_PLAYING)) { |
| 319 | pJniData->mAudioOut->start(); |
| 320 | } |
| 321 | pJniData->mPlayLock.unlock(); |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 322 | if (bUseFilter) { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 323 | applyFilter((int16_t*)*pWav, *pBufferSize/2); |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 324 | } |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 325 | pJniData->mAudioOut->write(*pWav, *pBufferSize); |
| 326 | memset(*pWav, 0, *pBufferSize); |
| Jean-Michel Trivi | 6a0e293 | 2009-06-24 11:32:06 -0700 | [diff] [blame] | 327 | //LOGV("AudioTrack wrote: %d bytes", bufferSize); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 328 | } else { |
| Jean-Michel Trivi | 6a0e293 | 2009-06-24 11:32:06 -0700 | [diff] [blame] | 329 | LOGE("Can't play, null audiotrack"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 330 | delete pForAfter; |
| 331 | pForAfter = NULL; |
| 332 | return ANDROID_TTS_CALLBACK_HALT; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } else if (pForAfter->usageMode == USAGEMODE_WRITE_TO_FILE) { |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 336 | //LOGV("Save to file"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 337 | if (*pWav == NULL) { |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 338 | delete pForAfter; |
| Jean-Michel Trivi | 6a0e293 | 2009-06-24 11:32:06 -0700 | [diff] [blame] | 339 | LOGV("Null: speech has completed"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 340 | return ANDROID_TTS_CALLBACK_HALT; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 341 | } |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 342 | if (*pBufferSize > 0){ |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 343 | if (bUseFilter) { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 344 | applyFilter((int16_t*)*pWav, *pBufferSize/2); |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 345 | } |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 346 | fwrite(*pWav, 1, *pBufferSize, pForAfter->outputFile); |
| 347 | memset(*pWav, 0, *pBufferSize); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| Jean-Michel Trivi | 6c24f24 | 2009-06-25 17:03:51 -0700 | [diff] [blame] | 350 | // Future update: |
| 351 | // For sync points in the speech, call back into the SynthProxy class through the |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 352 | // javaTTSFields.synthProxyMethodPost methode to notify |
| Jean-Michel Trivi | 6c24f24 | 2009-06-25 17:03:51 -0700 | [diff] [blame] | 353 | // playback has completed if the synthesis is done or if a marker has been reached. |
| 354 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 355 | if (status == ANDROID_TTS_SYNTH_DONE) { |
| Jean-Michel Trivi | 6c24f24 | 2009-06-25 17:03:51 -0700 | [diff] [blame] | 356 | // this struct was allocated in the original android_tts_SynthProxy_speak call, |
| 357 | // all processing matching this call is now done. |
| 358 | LOGV("Speech synthesis done."); |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 359 | if (pForAfter->usageMode == USAGEMODE_PLAY_IMMEDIATELY) { |
| 360 | // only delete for direct playback. When writing to a file, we still have work to do |
| 361 | // in android_tts_SynthProxy_synthesizeToFile. The struct will be deleted there. |
| 362 | delete pForAfter; |
| 363 | pForAfter = NULL; |
| 364 | } |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 365 | return ANDROID_TTS_CALLBACK_HALT; |
| Jean-Michel Trivi | 6c24f24 | 2009-06-25 17:03:51 -0700 | [diff] [blame] | 366 | } |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 367 | |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 368 | // we don't update the wav (output) parameter as we'll let the next callback |
| 369 | // write at the same location, we've consumed the data already, but we need |
| 370 | // to update bufferSize to let the TTS engine know how much it can write the |
| 371 | // next time it calls this function. |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 372 | *pBufferSize = pJniData->mBufferSize; |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 373 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 374 | return ANDROID_TTS_CALLBACK_CONTINUE; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| 378 | // ---------------------------------------------------------------------------- |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 379 | static int |
| 380 | android_tts_SynthProxy_setLowShelf(JNIEnv *env, jobject thiz, jboolean applyFilter, |
| 381 | jfloat filterGain, jfloat attenuationInDb, jfloat freqInHz, jfloat slope) |
| 382 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 383 | int result = ANDROID_TTS_SUCCESS; |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 384 | |
| 385 | bUseFilter = applyFilter; |
| 386 | if (applyFilter) { |
| 387 | fFilterLowshelfAttenuation = attenuationInDb; |
| 388 | fFilterTransitionFreq = freqInHz; |
| 389 | fFilterShelfSlope = slope; |
| 390 | fFilterGain = filterGain; |
| 391 | |
| 392 | if (fFilterShelfSlope != 0.0f) { |
| 393 | initializeEQ(); |
| 394 | } else { |
| 395 | LOGE("Invalid slope, can't be null"); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 396 | result = ANDROID_TTS_FAILURE; |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | return result; |
| 401 | } |
| 402 | |
| 403 | // ---------------------------------------------------------------------------- |
| 404 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 405 | android_tts_SynthProxy_native_setup(JNIEnv *env, jobject thiz, |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 406 | jobject weak_this, jstring nativeSoLib, jstring engConfig) |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 407 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 408 | int result = ANDROID_TTS_FAILURE; |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 409 | |
| 410 | bUseFilter = false; |
| 411 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 412 | SynthProxyJniStorage* pJniStorage = new SynthProxyJniStorage(); |
| 413 | |
| 414 | prepAudioTrack(pJniStorage, |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 415 | DEFAULT_TTS_STREAM_TYPE, DEFAULT_TTS_RATE, DEFAULT_TTS_FORMAT, DEFAULT_TTS_NB_CHANNELS); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 416 | |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 417 | const char *nativeSoLibNativeString = env->GetStringUTFChars(nativeSoLib, 0); |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 418 | const char *engConfigString = env->GetStringUTFChars(engConfig, 0); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 419 | |
| 420 | void *engine_lib_handle = dlopen(nativeSoLibNativeString, |
| 421 | RTLD_NOW | RTLD_LOCAL); |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 422 | if (engine_lib_handle == NULL) { |
| 423 | LOGE("android_tts_SynthProxy_native_setup(): engine_lib_handle == NULL"); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 424 | } else { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 425 | android_tts_engine_t * (*get_TtsEngine)() = |
| 426 | reinterpret_cast<android_tts_engine_t* (*)()>(dlsym(engine_lib_handle, "android_getTtsEngine")); |
| Charles Chen | 83e712a | 2009-06-05 13:58:33 -0700 | [diff] [blame] | 427 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 428 | // Support obsolete/legacy binary modules |
| 429 | if (get_TtsEngine == NULL) { |
| 430 | get_TtsEngine = |
| 431 | reinterpret_cast<android_tts_engine_t* (*)()>(dlsym(engine_lib_handle, "getTtsEngine")); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 432 | } |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 433 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 434 | pJniStorage->mEngine = (*get_TtsEngine)(); |
| 435 | pJniStorage->mEngineLibHandle = engine_lib_handle; |
| 436 | |
| 437 | android_tts_engine_t *engine = pJniStorage->mEngine; |
| 438 | if (engine) { |
| 439 | Mutex::Autolock l(engineMutex); |
| 440 | engine->funcs->init( |
| 441 | engine, |
| 442 | __ttsSynthDoneCB, |
| 443 | engConfigString); |
| 444 | } |
| 445 | |
| 446 | result = ANDROID_TTS_SUCCESS; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // we use a weak reference so the SynthProxy object can be garbage collected. |
| 450 | pJniStorage->tts_ref = env->NewGlobalRef(weak_this); |
| 451 | |
| 452 | // save the JNI resources so we can use them (and free them) later |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 453 | env->SetIntField(thiz, javaTTSFields.synthProxyFieldJniData, (int)pJniStorage); |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 454 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 455 | env->ReleaseStringUTFChars(nativeSoLib, nativeSoLibNativeString); |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 456 | env->ReleaseStringUTFChars(engConfig, engConfigString); |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 457 | |
| 458 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | static void |
| 463 | android_tts_SynthProxy_native_finalize(JNIEnv *env, jobject thiz, jint jniData) |
| 464 | { |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 465 | //LOGV("entering android_tts_SynthProxy_finalize()"); |
| 466 | if (jniData == 0) { |
| 467 | //LOGE("android_tts_SynthProxy_native_finalize(): invalid JNI data"); |
| 468 | return; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 469 | } |
| Jean-Michel Trivi | cee3bd4 | 2009-07-21 14:12:47 -0700 | [diff] [blame] | 470 | |
| 471 | Mutex::Autolock l(engineMutex); |
| 472 | |
| 473 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 474 | env->DeleteGlobalRef(pSynthData->tts_ref); |
| 475 | delete pSynthData; |
| 476 | |
| 477 | env->SetIntField(thiz, javaTTSFields.synthProxyFieldJniData, 0); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | static void |
| 482 | android_tts_SynthProxy_shutdown(JNIEnv *env, jobject thiz, jint jniData) |
| 483 | { |
| 484 | //LOGV("entering android_tts_SynthProxy_shutdown()"); |
| 485 | |
| 486 | // do everything a call to finalize would |
| 487 | android_tts_SynthProxy_native_finalize(env, thiz, jniData); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 490 | |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 491 | static int |
| 492 | android_tts_SynthProxy_isLanguageAvailable(JNIEnv *env, jobject thiz, jint jniData, |
| 493 | jstring language, jstring country, jstring variant) |
| 494 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 495 | int result = ANDROID_TTS_LANG_NOT_SUPPORTED; |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 496 | |
| 497 | if (jniData == 0) { |
| 498 | LOGE("android_tts_SynthProxy_isLanguageAvailable(): invalid JNI data"); |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 503 | const char *langNativeString = env->GetStringUTFChars(language, 0); |
| 504 | const char *countryNativeString = env->GetStringUTFChars(country, 0); |
| 505 | const char *variantNativeString = env->GetStringUTFChars(variant, 0); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 506 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 507 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 508 | |
| 509 | if (engine) { |
| 510 | result = engine->funcs->isLanguageAvailable(engine,langNativeString, |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 511 | countryNativeString, variantNativeString); |
| 512 | } |
| 513 | env->ReleaseStringUTFChars(language, langNativeString); |
| 514 | env->ReleaseStringUTFChars(country, countryNativeString); |
| 515 | env->ReleaseStringUTFChars(variant, variantNativeString); |
| 516 | return result; |
| 517 | } |
| 518 | |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 519 | static int |
| 520 | android_tts_SynthProxy_setConfig(JNIEnv *env, jobject thiz, jint jniData, jstring engineConfig) |
| 521 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 522 | int result = ANDROID_TTS_FAILURE; |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 523 | |
| 524 | if (jniData == 0) { |
| 525 | LOGE("android_tts_SynthProxy_setConfig(): invalid JNI data"); |
| 526 | return result; |
| 527 | } |
| 528 | |
| 529 | Mutex::Autolock l(engineMutex); |
| 530 | |
| 531 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 532 | const char *engineConfigNativeString = env->GetStringUTFChars(engineConfig, 0); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 533 | android_tts_engine_t *engine = pSynthData->mEngine; |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 534 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 535 | if (engine) { |
| 536 | result = engine->funcs->setProperty(engine,ANDROID_TTS_ENGINE_PROPERTY_CONFIG, |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 537 | engineConfigNativeString, strlen(engineConfigNativeString)); |
| 538 | } |
| 539 | env->ReleaseStringUTFChars(engineConfig, engineConfigNativeString); |
| 540 | |
| 541 | return result; |
| 542 | } |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 543 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 544 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 545 | android_tts_SynthProxy_setLanguage(JNIEnv *env, jobject thiz, jint jniData, |
| Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 546 | jstring language, jstring country, jstring variant) |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 547 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 548 | int result = ANDROID_TTS_LANG_NOT_SUPPORTED; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 549 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 550 | if (jniData == 0) { |
| 551 | LOGE("android_tts_SynthProxy_setLanguage(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 552 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 555 | Mutex::Autolock l(engineMutex); |
| 556 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 557 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 558 | const char *langNativeString = env->GetStringUTFChars(language, 0); |
| Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 559 | const char *countryNativeString = env->GetStringUTFChars(country, 0); |
| 560 | const char *variantNativeString = env->GetStringUTFChars(variant, 0); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 561 | android_tts_engine_t *engine = pSynthData->mEngine; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 562 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 563 | if (engine) { |
| 564 | result = engine->funcs->setLanguage(engine, langNativeString, |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 565 | countryNativeString, variantNativeString); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 566 | } |
| 567 | env->ReleaseStringUTFChars(language, langNativeString); |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 568 | env->ReleaseStringUTFChars(country, countryNativeString); |
| 569 | env->ReleaseStringUTFChars(variant, variantNativeString); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 570 | return result; |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 574 | static int |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 575 | android_tts_SynthProxy_loadLanguage(JNIEnv *env, jobject thiz, jint jniData, |
| 576 | jstring language, jstring country, jstring variant) |
| 577 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 578 | int result = ANDROID_TTS_LANG_NOT_SUPPORTED; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 579 | |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 580 | if (jniData == 0) { |
| 581 | LOGE("android_tts_SynthProxy_loadLanguage(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 582 | return result; |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 586 | const char *langNativeString = env->GetStringUTFChars(language, 0); |
| 587 | const char *countryNativeString = env->GetStringUTFChars(country, 0); |
| 588 | const char *variantNativeString = env->GetStringUTFChars(variant, 0); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 589 | android_tts_engine_t *engine = pSynthData->mEngine; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 590 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 591 | if (engine) { |
| 592 | result = engine->funcs->loadLanguage(engine, langNativeString, |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 593 | countryNativeString, variantNativeString); |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 594 | } |
| 595 | env->ReleaseStringUTFChars(language, langNativeString); |
| 596 | env->ReleaseStringUTFChars(country, countryNativeString); |
| 597 | env->ReleaseStringUTFChars(variant, variantNativeString); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 598 | |
| 599 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 603 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 604 | android_tts_SynthProxy_setSpeechRate(JNIEnv *env, jobject thiz, jint jniData, |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 605 | jint speechRate) |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 606 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 607 | int result = ANDROID_TTS_FAILURE; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 608 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 609 | if (jniData == 0) { |
| 610 | LOGE("android_tts_SynthProxy_setSpeechRate(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 611 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| Kenny Root | 1ead4f0 | 2010-02-18 10:35:17 -0800 | [diff] [blame] | 614 | int bufSize = 12; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 615 | char buffer [bufSize]; |
| 616 | sprintf(buffer, "%d", speechRate); |
| 617 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 618 | Mutex::Autolock l(engineMutex); |
| 619 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 620 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 621 | LOGI("setting speech rate to %d", speechRate); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 622 | android_tts_engine_t *engine = pSynthData->mEngine; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 623 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 624 | if (engine) { |
| 625 | result = engine->funcs->setProperty(engine, "rate", buffer, bufSize); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 626 | } |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 627 | |
| 628 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 632 | static int |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 633 | android_tts_SynthProxy_setPitch(JNIEnv *env, jobject thiz, jint jniData, |
| 634 | jint pitch) |
| 635 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 636 | int result = ANDROID_TTS_FAILURE; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 637 | |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 638 | if (jniData == 0) { |
| 639 | LOGE("android_tts_SynthProxy_setPitch(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 640 | return result; |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 643 | Mutex::Autolock l(engineMutex); |
| 644 | |
| Kenny Root | 1ead4f0 | 2010-02-18 10:35:17 -0800 | [diff] [blame] | 645 | int bufSize = 12; |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 646 | char buffer [bufSize]; |
| 647 | sprintf(buffer, "%d", pitch); |
| 648 | |
| 649 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 650 | LOGI("setting pitch to %d", pitch); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 651 | android_tts_engine_t *engine = pSynthData->mEngine; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 652 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 653 | if (engine) { |
| 654 | result = engine->funcs->setProperty(engine, "pitch", buffer, bufSize); |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 655 | } |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 656 | |
| 657 | return result; |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 661 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 662 | android_tts_SynthProxy_synthesizeToFile(JNIEnv *env, jobject thiz, jint jniData, |
| 663 | jstring textJavaString, jstring filenameJavaString) |
| 664 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 665 | int result = ANDROID_TTS_FAILURE; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 666 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 667 | if (jniData == 0) { |
| 668 | LOGE("android_tts_SynthProxy_synthesizeToFile(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 669 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 673 | if (!pSynthData->mEngine) { |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 674 | LOGE("android_tts_SynthProxy_synthesizeToFile(): invalid engine handle"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 675 | return result; |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 678 | initializeFilter(); |
| 679 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 680 | Mutex::Autolock l(engineMutex); |
| 681 | |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 682 | // Retrieve audio parameters before writing the file header |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 683 | AudioSystem::audio_format encoding; |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 684 | uint32_t rate = DEFAULT_TTS_RATE; |
| 685 | int channels = DEFAULT_TTS_NB_CHANNELS; |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 686 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 687 | android_tts_audio_format_t format = ANDROID_TTS_AUDIO_FORMAT_DEFAULT; |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 688 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 689 | engine->funcs->setAudioFormat(engine, &format, &rate, &channels); |
| 690 | |
| 691 | switch (format) { |
| 692 | case ANDROID_TTS_AUDIO_FORMAT_PCM_16_BIT: |
| 693 | encoding = AudioSystem::PCM_16_BIT; |
| 694 | break; |
| 695 | case ANDROID_TTS_AUDIO_FORMAT_PCM_8_BIT: |
| 696 | encoding = AudioSystem::PCM_8_BIT; |
| 697 | break; |
| 698 | default: |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 699 | LOGE("android_tts_SynthProxy_synthesizeToFile(): engine uses invalid format"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 700 | return result; |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 701 | } |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 702 | |
| 703 | const char *filenameNativeString = |
| 704 | env->GetStringUTFChars(filenameJavaString, 0); |
| 705 | const char *textNativeString = env->GetStringUTFChars(textJavaString, 0); |
| 706 | |
| 707 | afterSynthData_t* pForAfter = new (afterSynthData_t); |
| 708 | pForAfter->jniStorage = jniData; |
| 709 | pForAfter->usageMode = USAGEMODE_WRITE_TO_FILE; |
| 710 | |
| 711 | pForAfter->outputFile = fopen(filenameNativeString, "wb"); |
| 712 | |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 713 | if (pForAfter->outputFile == NULL) { |
| 714 | LOGE("android_tts_SynthProxy_synthesizeToFile(): error creating output file"); |
| 715 | delete pForAfter; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 716 | return result; |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 719 | // Write 44 blank bytes for WAV header, then come back and fill them in |
| 720 | // after we've written the audio data |
| 721 | char header[44]; |
| 722 | fwrite(header, 1, 44, pForAfter->outputFile); |
| 723 | |
| 724 | unsigned int unique_identifier; |
| 725 | |
| Charles Chen | 4a3368f | 2009-07-14 17:11:44 -0700 | [diff] [blame] | 726 | memset(pSynthData->mBuffer, 0, pSynthData->mBufferSize); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 727 | |
| 728 | result = engine->funcs->synthesizeText(engine, textNativeString, |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 729 | pSynthData->mBuffer, pSynthData->mBufferSize, (void *)pForAfter); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 730 | |
| 731 | long filelen = ftell(pForAfter->outputFile); |
| 732 | |
| 733 | int samples = (((int)filelen) - 44) / 2; |
| 734 | header[0] = 'R'; |
| 735 | header[1] = 'I'; |
| 736 | header[2] = 'F'; |
| 737 | header[3] = 'F'; |
| 738 | ((uint32_t *)(&header[4]))[0] = filelen - 8; |
| 739 | header[8] = 'W'; |
| 740 | header[9] = 'A'; |
| 741 | header[10] = 'V'; |
| 742 | header[11] = 'E'; |
| 743 | |
| 744 | header[12] = 'f'; |
| 745 | header[13] = 'm'; |
| 746 | header[14] = 't'; |
| 747 | header[15] = ' '; |
| 748 | |
| 749 | ((uint32_t *)(&header[16]))[0] = 16; // size of fmt |
| 750 | |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 751 | int sampleSizeInByte = (encoding == AudioSystem::PCM_16_BIT ? 2 : 1); |
| 752 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 753 | ((unsigned short *)(&header[20]))[0] = 1; // format |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 754 | ((unsigned short *)(&header[22]))[0] = channels; // channels |
| 755 | ((uint32_t *)(&header[24]))[0] = rate; // samplerate |
| 756 | ((uint32_t *)(&header[28]))[0] = rate * sampleSizeInByte * channels;// byterate |
| 757 | ((unsigned short *)(&header[32]))[0] = sampleSizeInByte * channels; // block align |
| 758 | ((unsigned short *)(&header[34]))[0] = sampleSizeInByte * 8; // bits per sample |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 759 | |
| 760 | header[36] = 'd'; |
| 761 | header[37] = 'a'; |
| 762 | header[38] = 't'; |
| 763 | header[39] = 'a'; |
| 764 | |
| 765 | ((uint32_t *)(&header[40]))[0] = samples * 2; // size of data |
| 766 | |
| 767 | // Skip back to the beginning and rewrite the header |
| 768 | fseek(pForAfter->outputFile, 0, SEEK_SET); |
| 769 | fwrite(header, 1, 44, pForAfter->outputFile); |
| 770 | |
| 771 | fflush(pForAfter->outputFile); |
| 772 | fclose(pForAfter->outputFile); |
| 773 | |
| Jean-Michel Trivi | f07d824 | 2009-06-30 09:36:08 -0700 | [diff] [blame] | 774 | delete pForAfter; |
| 775 | pForAfter = NULL; |
| 776 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 777 | env->ReleaseStringUTFChars(textJavaString, textNativeString); |
| 778 | env->ReleaseStringUTFChars(filenameJavaString, filenameNativeString); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 779 | |
| 780 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 784 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 785 | android_tts_SynthProxy_speak(JNIEnv *env, jobject thiz, jint jniData, |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 786 | jstring textJavaString, jint javaStreamType) |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 787 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 788 | int result = ANDROID_TTS_FAILURE; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 789 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 790 | if (jniData == 0) { |
| 791 | LOGE("android_tts_SynthProxy_speak(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 792 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| Jean-Michel Trivi | 4fb7d88 | 2009-08-13 19:05:45 -0700 | [diff] [blame] | 795 | initializeFilter(); |
| 796 | |
| Jean-Michel Trivi | 5e11a6a | 2009-07-20 14:05:33 -0700 | [diff] [blame] | 797 | Mutex::Autolock l(engineMutex); |
| 798 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 799 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 800 | |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 801 | pSynthData->mPlayLock.lock(); |
| 802 | pSynthData->mPlayState = SYNTHPLAYSTATE_IS_PLAYING; |
| 803 | pSynthData->mPlayLock.unlock(); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 804 | |
| 805 | afterSynthData_t* pForAfter = new (afterSynthData_t); |
| 806 | pForAfter->jniStorage = jniData; |
| 807 | pForAfter->usageMode = USAGEMODE_PLAY_IMMEDIATELY; |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 808 | pForAfter->streamType = (AudioSystem::stream_type) javaStreamType; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 809 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 810 | if (pSynthData->mEngine) { |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 811 | const char *textNativeString = env->GetStringUTFChars(textJavaString, 0); |
| Charles Chen | 4a3368f | 2009-07-14 17:11:44 -0700 | [diff] [blame] | 812 | memset(pSynthData->mBuffer, 0, pSynthData->mBufferSize); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 813 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 814 | |
| 815 | result = engine->funcs->synthesizeText(engine, textNativeString, |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 816 | pSynthData->mBuffer, pSynthData->mBufferSize, (void *)pForAfter); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 817 | env->ReleaseStringUTFChars(textJavaString, textNativeString); |
| 818 | } |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 819 | |
| 820 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 824 | static int |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 825 | android_tts_SynthProxy_stop(JNIEnv *env, jobject thiz, jint jniData) |
| 826 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 827 | int result = ANDROID_TTS_FAILURE; |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 828 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 829 | if (jniData == 0) { |
| 830 | LOGE("android_tts_SynthProxy_stop(): invalid JNI data"); |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 831 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 835 | |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 836 | pSynthData->mPlayLock.lock(); |
| 837 | pSynthData->mPlayState = SYNTHPLAYSTATE_IS_STOPPED; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 838 | if (pSynthData->mAudioOut) { |
| 839 | pSynthData->mAudioOut->stop(); |
| 840 | } |
| Jean-Michel Trivi | e3c1890 | 2010-02-24 18:52:39 -0800 | [diff] [blame] | 841 | pSynthData->mPlayLock.unlock(); |
| 842 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 843 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 844 | if (engine) { |
| 845 | result = engine->funcs->stop(engine); |
| Charles Chen | 4a3368f | 2009-07-14 17:11:44 -0700 | [diff] [blame] | 846 | } |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 847 | |
| 848 | return result; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
| Jean-Michel Trivi | 09f8db7 | 2009-08-31 10:41:55 -0700 | [diff] [blame] | 852 | static int |
| 853 | android_tts_SynthProxy_stopSync(JNIEnv *env, jobject thiz, jint jniData) |
| 854 | { |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 855 | int result = ANDROID_TTS_FAILURE; |
| Jean-Michel Trivi | 09f8db7 | 2009-08-31 10:41:55 -0700 | [diff] [blame] | 856 | |
| 857 | if (jniData == 0) { |
| 858 | LOGE("android_tts_SynthProxy_stop(): invalid JNI data"); |
| 859 | return result; |
| 860 | } |
| 861 | |
| 862 | // perform a regular stop |
| 863 | result = android_tts_SynthProxy_stop(env, thiz, jniData); |
| 864 | // but wait on the engine having released the engine mutex which protects |
| 865 | // the synthesizer resources. |
| 866 | engineMutex.lock(); |
| 867 | engineMutex.unlock(); |
| 868 | |
| 869 | return result; |
| 870 | } |
| 871 | |
| 872 | |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 873 | static jobjectArray |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 874 | android_tts_SynthProxy_getLanguage(JNIEnv *env, jobject thiz, jint jniData) |
| 875 | { |
| 876 | if (jniData == 0) { |
| 877 | LOGE("android_tts_SynthProxy_getLanguage(): invalid JNI data"); |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 878 | return NULL; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 882 | |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 883 | if (pSynthData->mEngine) { |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 884 | size_t bufSize = 100; |
| 885 | char lang[bufSize]; |
| 886 | char country[bufSize]; |
| 887 | char variant[bufSize]; |
| 888 | memset(lang, 0, bufSize); |
| 889 | memset(country, 0, bufSize); |
| 890 | memset(variant, 0, bufSize); |
| 891 | jobjectArray retLocale = (jobjectArray)env->NewObjectArray(3, |
| 892 | env->FindClass("java/lang/String"), env->NewStringUTF("")); |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 893 | |
| 894 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 895 | engine->funcs->getLanguage(engine, lang, country, variant); |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 896 | env->SetObjectArrayElement(retLocale, 0, env->NewStringUTF(lang)); |
| 897 | env->SetObjectArrayElement(retLocale, 1, env->NewStringUTF(country)); |
| 898 | env->SetObjectArrayElement(retLocale, 2, env->NewStringUTF(variant)); |
| 899 | return retLocale; |
| 900 | } else { |
| 901 | return NULL; |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 902 | } |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 905 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 906 | JNIEXPORT int JNICALL |
| 907 | android_tts_SynthProxy_getRate(JNIEnv *env, jobject thiz, jint jniData) |
| 908 | { |
| 909 | if (jniData == 0) { |
| 910 | LOGE("android_tts_SynthProxy_getRate(): invalid JNI data"); |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | SynthProxyJniStorage* pSynthData = (SynthProxyJniStorage*)jniData; |
| 915 | size_t bufSize = 100; |
| 916 | |
| 917 | char buf[bufSize]; |
| 918 | memset(buf, 0, bufSize); |
| 919 | // TODO check return codes |
| David 'Digit' Turner | 01f2f96 | 2010-05-20 16:57:34 -0700 | [diff] [blame] | 920 | android_tts_engine_t *engine = pSynthData->mEngine; |
| 921 | if (engine) { |
| 922 | engine->funcs->getProperty(engine,"rate", buf, &bufSize); |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 923 | } |
| 924 | return atoi(buf); |
| 925 | } |
| 926 | |
| 927 | // Dalvik VM type signatures |
| 928 | static JNINativeMethod gMethods[] = { |
| 929 | { "native_stop", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 930 | "(I)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 931 | (void*)android_tts_SynthProxy_stop |
| 932 | }, |
| Jean-Michel Trivi | 09f8db7 | 2009-08-31 10:41:55 -0700 | [diff] [blame] | 933 | { "native_stopSync", |
| 934 | "(I)I", |
| 935 | (void*)android_tts_SynthProxy_stopSync |
| 936 | }, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 937 | { "native_speak", |
| Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 938 | "(ILjava/lang/String;I)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 939 | (void*)android_tts_SynthProxy_speak |
| 940 | }, |
| 941 | { "native_synthesizeToFile", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 942 | "(ILjava/lang/String;Ljava/lang/String;)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 943 | (void*)android_tts_SynthProxy_synthesizeToFile |
| 944 | }, |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 945 | { "native_isLanguageAvailable", |
| 946 | "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", |
| 947 | (void*)android_tts_SynthProxy_isLanguageAvailable |
| 948 | }, |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 949 | { "native_setConfig", |
| 950 | "(ILjava/lang/String;)I", |
| 951 | (void*)android_tts_SynthProxy_setConfig |
| 952 | }, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 953 | { "native_setLanguage", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 954 | "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 955 | (void*)android_tts_SynthProxy_setLanguage |
| 956 | }, |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 957 | { "native_loadLanguage", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 958 | "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", |
| Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 959 | (void*)android_tts_SynthProxy_loadLanguage |
| 960 | }, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 961 | { "native_setSpeechRate", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 962 | "(II)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 963 | (void*)android_tts_SynthProxy_setSpeechRate |
| 964 | }, |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 965 | { "native_setPitch", |
| Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 966 | "(II)I", |
| Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 967 | (void*)android_tts_SynthProxy_setPitch |
| 968 | }, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 969 | { "native_getLanguage", |
| Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 970 | "(I)[Ljava/lang/String;", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 971 | (void*)android_tts_SynthProxy_getLanguage |
| 972 | }, |
| 973 | { "native_getRate", |
| 974 | "(I)I", |
| 975 | (void*)android_tts_SynthProxy_getRate |
| 976 | }, |
| 977 | { "native_shutdown", |
| 978 | "(I)V", |
| 979 | (void*)android_tts_SynthProxy_shutdown |
| 980 | }, |
| 981 | { "native_setup", |
| Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 982 | "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)I", |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 983 | (void*)android_tts_SynthProxy_native_setup |
| 984 | }, |
| Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 985 | { "native_setLowShelf", |
| 986 | "(ZFFFF)I", |
| 987 | (void*)android_tts_SynthProxy_setLowShelf |
| 988 | }, |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 989 | { "native_finalize", |
| 990 | "(I)V", |
| 991 | (void*)android_tts_SynthProxy_native_finalize |
| 992 | } |
| 993 | }; |
| 994 | |
| 995 | #define SP_JNIDATA_FIELD_NAME "mJniData" |
| 996 | #define SP_POSTSPEECHSYNTHESIZED_METHOD_NAME "postNativeSpeechSynthesizedInJava" |
| 997 | |
| Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 998 | static const char* const kClassPathName = "android/tts/SynthProxy"; |
| 999 | |
| 1000 | jint JNI_OnLoad(JavaVM* vm, void* reserved) |
| 1001 | { |
| 1002 | JNIEnv* env = NULL; |
| 1003 | jint result = -1; |
| 1004 | jclass clazz; |
| 1005 | |
| 1006 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 1007 | LOGE("ERROR: GetEnv failed\n"); |
| 1008 | goto bail; |
| 1009 | } |
| 1010 | assert(env != NULL); |
| 1011 | |
| 1012 | clazz = env->FindClass(kClassPathName); |
| 1013 | if (clazz == NULL) { |
| 1014 | LOGE("Can't find %s", kClassPathName); |
| 1015 | goto bail; |
| 1016 | } |
| 1017 | |
| 1018 | javaTTSFields.synthProxyClass = clazz; |
| 1019 | javaTTSFields.synthProxyFieldJniData = NULL; |
| 1020 | javaTTSFields.synthProxyMethodPost = NULL; |
| 1021 | |
| 1022 | javaTTSFields.synthProxyFieldJniData = env->GetFieldID(clazz, |
| 1023 | SP_JNIDATA_FIELD_NAME, "I"); |
| 1024 | if (javaTTSFields.synthProxyFieldJniData == NULL) { |
| 1025 | LOGE("Can't find %s.%s field", kClassPathName, SP_JNIDATA_FIELD_NAME); |
| 1026 | goto bail; |
| 1027 | } |
| 1028 | |
| 1029 | javaTTSFields.synthProxyMethodPost = env->GetStaticMethodID(clazz, |
| 1030 | SP_POSTSPEECHSYNTHESIZED_METHOD_NAME, "(Ljava/lang/Object;II)V"); |
| 1031 | if (javaTTSFields.synthProxyMethodPost == NULL) { |
| 1032 | LOGE("Can't find %s.%s method", kClassPathName, SP_POSTSPEECHSYNTHESIZED_METHOD_NAME); |
| 1033 | goto bail; |
| 1034 | } |
| 1035 | |
| 1036 | if (jniRegisterNativeMethods( |
| 1037 | env, kClassPathName, gMethods, NELEM(gMethods)) < 0) |
| 1038 | goto bail; |
| 1039 | |
| 1040 | /* success -- return valid version number */ |
| 1041 | result = JNI_VERSION_1_4; |
| 1042 | |
| 1043 | bail: |
| 1044 | return result; |
| 1045 | } |