{
speechRate_ = Singleton<TextToSpeech>::instance().getSpeed();
- Singleton<TextToSpeech>::instance().addSpeedChangedCb([this](auto val) {
- this->speechRate_ = val;
- });
+ Singleton<TextToSpeech>::instance().addSpeedChangedCb(this, [this](auto val) { this->speechRate_ = val; });
speechRateMin_ = Singleton<TextToSpeech>::instance().getSpeedRange().min;
this->soundFeedback_ = value;
});
- speechRate_.attach([](auto val) {
- Singleton<TextToSpeech>::instance().setSpeed(val);
- });
-
-
keyboardFeedback_.attach([](auto val) {
Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_SCREEN_READER_KEYBOARD_FEEDBACK, val);
});
ScreenReaderSettingsModel::~ScreenReaderSettingsModel()
{
- Singleton<TextToSpeech>::instance().clearSpeedChangedCallbacks();
+ Singleton<TextToSpeech>::instance().removeSpeedChangedCb(this);
}
TextToSpeechModel::TextToSpeechModel() {
speechRate_ = Singleton<TextToSpeech>::instance().getSpeed();
-
- Singleton<TextToSpeech>::instance().addSpeedChangedCb([this](auto val) {
- this->speechRate_ = val;
- });
+ Singleton<TextToSpeech>::instance().addSpeedChangedCb(this, [this](auto val) { this->speechRate_ = val; });
speechRate_.attach([](auto val) {
Singleton<TextToSpeech>::instance().setSpeed(val);
TextToSpeechModel::~TextToSpeechModel() {
- Singleton<TextToSpeech>::instance().clearSpeedChangedCallbacks();
-
+ Singleton<TextToSpeech>::instance().removeSpeedChangedCb(this);
}
if (auto ret = tts_setting_initialize())
ERROR("tts_setting_initialize: %s", ttsSettingErrorToString(ret).c_str());
+
+ if (auto ret = tts_setting_set_speed_changed_cb(onSpeedChange, this))
+ ERROR("tts_setting_set_speed_changed_cb: %s", ttsSettingErrorToString(ret).c_str());
}
TextToSpeech::~TextToSpeech()
{
+ if (tts_setting_unset_speed_changed_cb())
+ WARNING("Fail to unset speed changed cb");
+
if (tts_setting_finalize())
WARNING("Fail to setting finalize");
if (tts_destroy(handle_))
WARNING("Fail to tts destroy");
+
+ clearSpeedChangedCallbacks();
}
void TextToSpeech::play(const std::string &text)
return {};
}
-void TextToSpeech::addSpeedChangedCb(std::function<void(size_t)> onChange)
+void TextToSpeech::addSpeedChangedCb(void *key, std::function<void(size_t)> onChange)
{
- auto ret = tts_setting_set_speed_changed_cb(onSpeedChange, this);
- if (ret)
- ERROR("tts_setting_set_speed_changed_cb: %s", ttsSettingErrorToString(ret).c_str());
- speedChangedCallbacks_.push_back(std::move(onChange));
+ speedChangedCallbacks_.insert({key, std::move(onChange)});
+}
+
+void TextToSpeech::removeSpeedChangedCb(void *key)
+{
+ speedChangedCallbacks_.erase(key);
}
void TextToSpeech::clearSpeedChangedCallbacks()
{
auto self = static_cast<TextToSpeech *>(user_data);
for (auto it : self->speedChangedCallbacks_)
- if (it)
- it(speed);
+ if (it.second)
+ it.second(speed);
}
#include <functional>
#include <tts.h>
#include <tts_setting.h>
-#include <vector>
+#include <unordered_map>
class TextToSpeech
{
size_t getSpeed();
void setSpeed(size_t speed);
SpeedRange getSpeedRange();
- void addSpeedChangedCb(std::function<void(size_t)> onChange);
- void clearSpeedChangedCallbacks();
+ void addSpeedChangedCb(void *key, std::function<void(size_t)> onChange);
+ void removeSpeedChangedCb(void *key);
private:
std::string ttsErrorToString(int error);
std::string ttsSettingErrorToString(int error);
static void onSpeedChange(int speed, void *user_data);
+ void clearSpeedChangedCallbacks();
tts_h handle_ = nullptr;
- std::vector<std::function<void(size_t)>> speedChangedCallbacks_;
+ std::unordered_map<void*, std::function<void(size_t)>> speedChangedCallbacks_;
};
#endif