| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "audio_output.h" | 8 #include "audio_output.h" |
| 9 #include "log.h" | 9 #include "log.h" |
| 10 #include "resampler.h" | 10 #include "resampler.h" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 } | 185 } |
| 186 | 186 |
| 187 utterance_running_ = true; | 187 utterance_running_ = true; |
| 188 | 188 |
| 189 mutex_->Unlock(); | 189 mutex_->Unlock(); |
| 190 | 190 |
| 191 if (!current_utterance_) { | 191 if (!current_utterance_) { |
| 192 continue; | 192 continue; |
| 193 } | 193 } |
| 194 | 194 |
| 195 engine_->SetRate(current_utterance_->options->rate); | 195 if (current_utterance_->options) { |
| 196 engine_->SetPitch(current_utterance_->options->pitch); | 196 engine_->SetRate(current_utterance_->options->rate); |
| 197 engine_->SetVolume(current_utterance_->options->volume); | 197 engine_->SetPitch(current_utterance_->options->pitch); |
| 198 engine_->SetVolume(current_utterance_->options->volume); |
| 199 } |
| 198 | 200 |
| 199 // Synthesize the current utterance. The TTS engine will call our | 201 // Synthesize the current utterance. The TTS engine will call our |
| 200 // callback method, Receive, repeatedly while it performs synthesis. | 202 // callback method, Receive, repeatedly while it performs synthesis. |
| 201 // During that callback, we check if Stop was called and can cause | 203 // During that callback, we check if Stop was called and can cause |
| 202 // this method to exit prematurely. Otherwise this method won't exit | 204 // this method to exit prematurely. Otherwise this method won't exit |
| 203 // until this utterance is done synthesizing, and then current_utterance_ | 205 // until this utterance is done synthesizing, and then current_utterance_ |
| 204 // will be set to NULL. | 206 // will be set to NULL. |
| 205 int samples_output = 0; | 207 int samples_output = 0; |
| 206 engine_->SetVoice(current_utterance_->voice_index); | 208 engine_->SetVoice(current_utterance_->voice_index); |
| 207 | 209 |
| 208 resampler_ = NULL; | 210 resampler_ = NULL; |
| 209 if (audio_output_->GetSampleRate() != engine_->GetSampleRate()) { | 211 if (audio_output_->GetSampleRate() != engine_->GetSampleRate()) { |
| 210 resampler_ = new Resampler(this, | 212 resampler_ = new Resampler(this, |
| 211 engine_->GetSampleRate(), | 213 engine_->GetSampleRate(), |
| 212 audio_output_->GetSampleRate(), | 214 audio_output_->GetSampleRate(), |
| 213 audio_buffer_size_); | 215 audio_buffer_size_); |
| 214 engine_->SetReceiver(resampler_); | 216 engine_->SetReceiver(resampler_); |
| 215 } else { | 217 } else { |
| 216 engine_->SetReceiver(this); | 218 engine_->SetReceiver(this); |
| 217 } | 219 } |
| 218 | 220 |
| 221 // Save the utterance text because current_utterance_ is deleted |
| 222 // by the Done() callback before the call to SynthesizeText exits. |
| 223 string utterance_text = current_utterance_->text; |
| 224 |
| 219 engine_->SynthesizeText( | 225 engine_->SynthesizeText( |
| 220 current_utterance_->text.c_str(), | 226 utterance_text.c_str(), |
| 221 audio_buffer_, | 227 audio_buffer_, |
| 222 audio_buffer_size_, | 228 audio_buffer_size_, |
| 223 &samples_output); | 229 &samples_output); |
| 224 | 230 |
| 225 // TODO(chaitanyag): Make the completion callback here. | 231 // TODO(chaitanyag): Make the completion callback here. |
| 226 LOG(INFO) << "Done: " << current_utterance_->text.c_str(); | 232 LOG(INFO) << "Done: " << utterance_text.c_str(); |
| 227 | 233 |
| 228 mutex_->Lock(); | 234 mutex_->Lock(); |
| 229 if (utterance_running_ == false) { | 235 if (utterance_running_ == false) { |
| 230 // The utterance was interrupted | 236 // The utterance was interrupted |
| 231 engine_->Stop(); | 237 engine_->Stop(); |
| 232 } | 238 } |
| 233 utterance_running_ = false; | 239 utterance_running_ = false; |
| 234 cond_var_->Signal(); | 240 cond_var_->Signal(); |
| 235 mutex_->Unlock(); | 241 mutex_->Unlock(); |
| 236 | 242 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 cond_var_->WaitWithTimeout(mutex_, ms_to_sleep); | 280 cond_var_->WaitWithTimeout(mutex_, ms_to_sleep); |
| 275 if (service_running_ == false || utterance_running_ == false) { | 281 if (service_running_ == false || utterance_running_ == false) { |
| 276 mutex_->Unlock(); | 282 mutex_->Unlock(); |
| 277 return TTS_CALLBACK_HALT; | 283 return TTS_CALLBACK_HALT; |
| 278 } | 284 } |
| 279 mutex_->Unlock(); | 285 mutex_->Unlock(); |
| 280 } | 286 } |
| 281 | 287 |
| 282 bool success = ring_buffer_->Write(data, num_samples); | 288 bool success = ring_buffer_->Write(data, num_samples); |
| 283 if (!success) { | 289 if (!success) { |
| 290 LOG(INFO) << "Unable to write to ring buffer"; |
| 284 exit(0); | 291 exit(0); |
| 285 } | 292 } |
| 286 | 293 |
| 287 if (status == TTS_SYNTH_DONE) { | 294 if (status == TTS_SYNTH_DONE) { |
| 288 ring_buffer_->MarkFinished(); | 295 ring_buffer_->MarkFinished(); |
| 289 } | 296 } |
| 290 | 297 |
| 291 return TTS_CALLBACK_CONTINUE; | 298 return TTS_CALLBACK_CONTINUE; |
| 292 } | 299 } |
| 293 | 300 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 306 ring_buffer_->Read(samples, copy_len); | 313 ring_buffer_->Read(samples, copy_len); |
| 307 for (int i = copy_len; i < size; i++) { | 314 for (int i = copy_len; i < size; i++) { |
| 308 samples[i] = 0; | 315 samples[i] = 0; |
| 309 } | 316 } |
| 310 | 317 |
| 311 return !ring_buffer_->IsFinished(); | 318 return !ring_buffer_->IsFinished(); |
| 312 } | 319 } |
| 313 | 320 |
| 314 } // namespace speech_synthesis | 321 } // namespace speech_synthesis |
| 315 | 322 |
| OLD | NEW |