From c7499219d6d5ee3914a1338f3c7c37507775ef52 Mon Sep 17 00:00:00 2001 From: Pranjal Pandey Date: Sat, 18 Jan 2025 02:40:07 +0530 Subject: [PATCH] added speed control option for tts --- resources/i18n/en.json | 5 +++- resources/i18n/qqq.json | 5 +++- src/kiwixapp.cpp | 12 +++++++++ src/kiwixapp.h | 4 +++ src/mainmenu.cpp | 2 ++ src/texttospeechbar.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ src/texttospeechbar.h | 5 ++++ src/texttospeechbar.ui | 14 ++++++++++ 8 files changed, 104 insertions(+), 2 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 9a4fe05e..29d9ca1c 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -184,5 +184,8 @@ "select-read-voice": "Select reading voice", "select-read-language": "Select reading language", "save-or-open": "Save or Open file", - "save-or-open-text": "What should Kiwix do with this file?" + "save-or-open-text": "What should Kiwix do with this file?", + "speed": "Speed", + "increase-tts-speed": "Represents the action of increasing the speed of the text-to-speech.", + "decrease-tts-speed": "Represents the action of decreasing the speed of the text-to-speech." } diff --git a/resources/i18n/qqq.json b/resources/i18n/qqq.json index 0bd8c772..d23d6c80 100644 --- a/resources/i18n/qqq.json +++ b/resources/i18n/qqq.json @@ -192,5 +192,8 @@ "select-read-voice": "Represents the action of opening the voice selection for text-to-speech.", "select-read-language": "Represents the action of opening the language selection for text-to-speech.", "save-or-open": "Title of the message box allowing to choose whether a remote resource should be saved to disk or opened with a respective application", - "save-or-open-text": "Text of the message box allowing to choose whether a remote resource should be saved to disk or opened with a respective application" + "save-or-open-text": "Text of the message box allowing to choose whether a remote resource should be saved to disk or opened with a respective application", + "speed": "Label for text-to-speech speed adjustment control.", + "increase-tts-speed": "Represents the action of increasing the speed of the text-to-speech.", + "decrease-tts-speed": "Represents the action of decreasing the speed of the text-to-speech." } diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index c11dca15..282c5aeb 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -398,6 +398,8 @@ void KiwixApp::createActions() CREATE_ACTION_SHORTCUT(ReadStopAction, gt("read-stop"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_X)); CREATE_ACTION_SHORTCUT(ToggleTTSLanguageAction, gt("select-read-language"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_L)); CREATE_ACTION_SHORTCUT(ToggleTTSVoiceAction, gt("select-read-voice"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_V)); + CREATE_ACTION_SHORTCUT(IncreaseTTSSpeedAction, gt("increase-tts-speed"), QKeySequence(Qt::SHIFT | Qt::Key_Period)); + CREATE_ACTION_SHORTCUT(DecreaseTTSSpeedAction, gt("decrease-tts-speed"), QKeySequence(Qt::SHIFT | Qt::Key_Comma)); mpa_actions[ToggleTTSLanguageAction]->setCheckable(true); mpa_actions[ToggleTTSVoiceAction]->setCheckable(true); @@ -558,6 +560,11 @@ void KiwixApp::saveVoiceName(const QString& langName, const QString& voiceName) mp_session->setValue("voice/" + langName, voiceName); } +void KiwixApp::saveTtsSpeed(const QString& langName, double speed) +{ + mp_session->setValue("speed/" + langName, speed); +} + void KiwixApp::restoreWindowState() { getMainWindow()->restoreGeometry(mp_session->value("geometry").toByteArray()); @@ -579,6 +586,11 @@ QString KiwixApp::getSavedVoiceName(const QString& langName) const return mp_session->value("voice/" + langName, "").toString(); } +double KiwixApp::getSavedTtsSpeed(const QString& langName) const +{ + return mp_session->value("speed/" + langName, 1.0).toDouble(); // Default: 1.0 (normal speed) +} + QString KiwixApp::getPrevSaveDir() const { QString prevSaveDir = mp_session->value("prevSaveDir", DEFAULT_SAVE_DIR).toString(); diff --git a/src/kiwixapp.h b/src/kiwixapp.h index d5eaa71d..36ad170e 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -50,6 +50,8 @@ class KiwixApp : public QtSingleApplication ToggleAddBookmarkAction, ToggleTTSLanguageAction, ToggleTTSVoiceAction, + IncreaseTTSSpeedAction, + DecreaseTTSSpeedAction, ZoomInAction, ZoomOutAction, ZoomResetAction, @@ -101,10 +103,12 @@ class KiwixApp : public QtSingleApplication void saveListOfOpenTabs(); void saveWindowState(); void saveVoiceName(const QString& langName, const QString& voiceName); + void saveTtsSpeed(const QString& langName, double speed); void restoreWindowState(); void saveCurrentTabIndex(); void savePrevSaveDir(const QString& prevSaveDir); QString getSavedVoiceName(const QString& langName) const; + double getSavedTtsSpeed(const QString& langName) const; QString getPrevSaveDir() const; void restoreTabs(); void setupDirectoryMonitoring(); diff --git a/src/mainmenu.cpp b/src/mainmenu.cpp index 7e7a1a71..7528f83d 100644 --- a/src/mainmenu.cpp +++ b/src/mainmenu.cpp @@ -49,6 +49,8 @@ MainMenu::MainMenu(QWidget *parent) : m_viewMenu.ADD_ACTION(ToggleReadingListAction); m_viewMenu.ADD_ACTION(ToggleTTSLanguageAction); m_viewMenu.ADD_ACTION(ToggleTTSVoiceAction); + m_viewMenu.ADD_ACTION(IncreaseTTSSpeedAction); + m_viewMenu.ADD_ACTION(DecreaseTTSSpeedAction); m_viewMenu.ADD_ACTION(ZoomInAction); m_viewMenu.ADD_ACTION(ZoomOutAction); m_viewMenu.ADD_ACTION(ZoomResetAction); diff --git a/src/texttospeechbar.cpp b/src/texttospeechbar.cpp index a100f8d0..fe60bd23 100644 --- a/src/texttospeechbar.cpp +++ b/src/texttospeechbar.cpp @@ -21,6 +21,7 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent) connect(ui->closeButton, &QPushButton::pressed, this, &TextToSpeechBar::speechClose); + setupSpeedOptionsComboBox(); setupVoiceComboBox(); setupLanguageComboBox(); languageSelected(ui->langComboBox->currentIndex()); @@ -28,6 +29,28 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent) this, &TextToSpeechBar::toggleLanguage); connect(app->getAction(KiwixApp::ToggleTTSVoiceAction), &QAction::triggered, this, &TextToSpeechBar::toggleVoice); + connect(app->getAction(KiwixApp::IncreaseTTSSpeedAction), &QAction::triggered, + this, &TextToSpeechBar::increaseSpeed); + connect(app->getAction(KiwixApp::DecreaseTTSSpeedAction), &QAction::triggered, + this, &TextToSpeechBar::decreaseSpeed); + connect(ui->speedComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &TextToSpeechBar::onSpeedChanged); +} + +void TextToSpeechBar::increaseSpeed() +{ + int currentIndex = ui->speedComboBox->currentIndex(); + if (currentIndex < ui->speedComboBox->count() - 1) { + ui->speedComboBox->setCurrentIndex(currentIndex + 1); + } +} + +void TextToSpeechBar::decreaseSpeed() +{ + int currentIndex = ui->speedComboBox->currentIndex(); + if (currentIndex > 0) { + ui->speedComboBox->setCurrentIndex(currentIndex - 1); + } } void TextToSpeechBar::speak(const QString &text) @@ -54,6 +77,16 @@ void TextToSpeechBar::setLocale(const QLocale& locale) } } +void TextToSpeechBar::setupSpeedOptionsComboBox() +{ + ui->speedLabel->setText(gt("speed")); + ui->speedComboBox->setMaxVisibleItems(10); + ui->speedComboBox->setLineEdit(new ComboBoxLineEdit(ui->speedComboBox)); + + QStringList speedOptions = {"0.25","0.50","0.75","1.00","1.25","1.50","1.75","2.00"}; + ui->speedComboBox->addItems(speedOptions); +} + void TextToSpeechBar::setupLanguageComboBox() { ui->langLabel->setText(gt("language")); @@ -107,6 +140,13 @@ void TextToSpeechBar::resetVoiceComboBox() connect(ui->voiceComboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &TextToSpeechBar::voiceSelected); } +void TextToSpeechBar::resetSpeedComboBox() +{ + double savedSpeed = KiwixApp::instance()->getSavedTtsSpeed(m_speech.locale().name()); + int index = (savedSpeed - 0.25) * 4; + ui->speedComboBox->setCurrentIndex(index); +} + int TextToSpeechBar::getVoiceIndex() { int voiceIndex = 0; @@ -174,6 +214,7 @@ void TextToSpeechBar::languageSelected(int index) const QLocale locale = ui->langComboBox->itemData(index).toLocale(); m_speech.setLocale(locale); resetVoiceComboBox(); + resetSpeedComboBox(); } void TextToSpeechBar::voiceSelected(int index) @@ -203,6 +244,24 @@ void TextToSpeechBar::onStateChanged(QTextToSpeech::State state) ui->stopButton->setEnabled(state != QTextToSpeech::Ready); } +void TextToSpeechBar::onSpeedChanged(int index) +{ + QString speedText = ui->speedComboBox->itemText(index); + double speed = speedText.toDouble(); + m_speech.setRate(speed - 1); // range:-1,1 + + // Save tts speed for current lang + const auto currentLang = ui->langComboBox->currentData().toLocale().name(); + KiwixApp::instance()->saveTtsSpeed(currentLang, speed); + + // Restarting the speech with new speed set above + if (m_speech.state() == QTextToSpeech::Speaking) + { + m_speech.stop(); + m_speech.say(m_text); + } +} + ComboBoxLineEdit::ComboBoxLineEdit(QWidget *parent) : QLineEdit(parent) { setFrame(false); diff --git a/src/texttospeechbar.h b/src/texttospeechbar.h index 989850bf..225d15e4 100644 --- a/src/texttospeechbar.h +++ b/src/texttospeechbar.h @@ -35,6 +35,8 @@ class TextToSpeechBar : public QFrame void setupLanguageComboBox(); void setupVoiceComboBox(); void resetVoiceComboBox(); + void setupSpeedOptionsComboBox(); + void resetSpeedComboBox(); int getVoiceIndex(); @@ -46,6 +48,9 @@ public slots: void toggleLanguage(); void languageSelected(int index); void voiceSelected(int index); + void onSpeedChanged(int index); + void increaseSpeed(); + void decreaseSpeed(); protected: void keyPressEvent(QKeyEvent *event); diff --git a/src/texttospeechbar.ui b/src/texttospeechbar.ui index afcd84ae..fbaf7a23 100644 --- a/src/texttospeechbar.ui +++ b/src/texttospeechbar.ui @@ -51,6 +51,20 @@ + + + + Speed + + + + + + + false + + +