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