Skip to content

Commit a01ed1e

Browse files
committed
added speed control option for tts
1 parent a2ad69d commit a01ed1e

File tree

8 files changed

+104
-2
lines changed

8 files changed

+104
-2
lines changed

resources/i18n/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,8 @@
184184
"select-read-voice": "Select reading voice",
185185
"select-read-language": "Select reading language",
186186
"save-or-open": "Save or Open file",
187-
"save-or-open-text": "What should Kiwix do with this file?"
187+
"save-or-open-text": "What should Kiwix do with this file?",
188+
"speed": "Speed",
189+
"increase-tts-speed": "Increase TTS speed.",
190+
"decrease-tts-speed": "Decrease TTS speed."
188191
}

resources/i18n/qqq.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,8 @@
192192
"select-read-voice": "Represents the action of opening the voice selection for text-to-speech.",
193193
"select-read-language": "Represents the action of opening the language selection for text-to-speech.",
194194
"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",
195-
"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"
195+
"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",
196+
"speed": "Label for text-to-speech speed adjustment control.",
197+
"increase-tts-speed": "Represents the action of increasing the speed of the text-to-speech.",
198+
"decrease-tts-speed": "Represents the action of decreasing the speed of the text-to-speech."
196199
}

src/kiwixapp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ void KiwixApp::createActions()
398398
CREATE_ACTION_SHORTCUT(ReadStopAction, gt("read-stop"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_X));
399399
CREATE_ACTION_SHORTCUT(ToggleTTSLanguageAction, gt("select-read-language"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_L));
400400
CREATE_ACTION_SHORTCUT(ToggleTTSVoiceAction, gt("select-read-voice"), QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_V));
401+
CREATE_ACTION_SHORTCUT(IncreaseTTSSpeedAction, gt("increase-tts-speed"), QKeySequence(Qt::Key_Greater));
402+
CREATE_ACTION_SHORTCUT(DecreaseTTSSpeedAction, gt("decrease-tts-speed"), QKeySequence(Qt::Key_Less));
401403
mpa_actions[ToggleTTSLanguageAction]->setCheckable(true);
402404
mpa_actions[ToggleTTSVoiceAction]->setCheckable(true);
403405

@@ -558,6 +560,11 @@ void KiwixApp::saveVoiceName(const QString& langName, const QString& voiceName)
558560
mp_session->setValue("voice/" + langName, voiceName);
559561
}
560562

563+
void KiwixApp::saveTtsSpeed(const QString& langName, double speed)
564+
{
565+
mp_session->setValue("speed/" + langName, speed);
566+
}
567+
561568
void KiwixApp::restoreWindowState()
562569
{
563570
getMainWindow()->restoreGeometry(mp_session->value("geometry").toByteArray());
@@ -579,6 +586,11 @@ QString KiwixApp::getSavedVoiceName(const QString& langName) const
579586
return mp_session->value("voice/" + langName, "").toString();
580587
}
581588

589+
double KiwixApp::getSavedTtsSpeed(const QString& langName) const
590+
{
591+
return mp_session->value("speed/" + langName, 1.0).toDouble(); // Default: 1.0 (normal speed)
592+
}
593+
582594
QString KiwixApp::getPrevSaveDir() const
583595
{
584596
QString prevSaveDir = mp_session->value("prevSaveDir", DEFAULT_SAVE_DIR).toString();

src/kiwixapp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class KiwixApp : public QtSingleApplication
5050
ToggleAddBookmarkAction,
5151
ToggleTTSLanguageAction,
5252
ToggleTTSVoiceAction,
53+
IncreaseTTSSpeedAction,
54+
DecreaseTTSSpeedAction,
5355
ZoomInAction,
5456
ZoomOutAction,
5557
ZoomResetAction,
@@ -101,10 +103,12 @@ class KiwixApp : public QtSingleApplication
101103
void saveListOfOpenTabs();
102104
void saveWindowState();
103105
void saveVoiceName(const QString& langName, const QString& voiceName);
106+
void saveTtsSpeed(const QString& langName, double speed);
104107
void restoreWindowState();
105108
void saveCurrentTabIndex();
106109
void savePrevSaveDir(const QString& prevSaveDir);
107110
QString getSavedVoiceName(const QString& langName) const;
111+
double getSavedTtsSpeed(const QString& langName) const;
108112
QString getPrevSaveDir() const;
109113
void restoreTabs();
110114
void setupDirectoryMonitoring();

src/mainmenu.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ MainMenu::MainMenu(QWidget *parent) :
4949
m_viewMenu.ADD_ACTION(ToggleReadingListAction);
5050
m_viewMenu.ADD_ACTION(ToggleTTSLanguageAction);
5151
m_viewMenu.ADD_ACTION(ToggleTTSVoiceAction);
52+
m_viewMenu.ADD_ACTION(IncreaseTTSSpeedAction);
53+
m_viewMenu.ADD_ACTION(DecreaseTTSSpeedAction);
5254
m_viewMenu.ADD_ACTION(ZoomInAction);
5355
m_viewMenu.ADD_ACTION(ZoomOutAction);
5456
m_viewMenu.ADD_ACTION(ZoomResetAction);

src/texttospeechbar.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,36 @@ TextToSpeechBar::TextToSpeechBar(QWidget *parent)
2121
connect(ui->closeButton, &QPushButton::pressed,
2222
this, &TextToSpeechBar::speechClose);
2323

24+
setupSpeedOptionsComboBox();
2425
setupVoiceComboBox();
2526
setupLanguageComboBox();
2627
languageSelected(ui->langComboBox->currentIndex());
2728
connect(app->getAction(KiwixApp::ToggleTTSLanguageAction), &QAction::triggered,
2829
this, &TextToSpeechBar::toggleLanguage);
2930
connect(app->getAction(KiwixApp::ToggleTTSVoiceAction), &QAction::triggered,
3031
this, &TextToSpeechBar::toggleVoice);
32+
connect(app->getAction(KiwixApp::IncreaseTTSSpeedAction), &QAction::triggered,
33+
this, &TextToSpeechBar::increaseSpeed);
34+
connect(app->getAction(KiwixApp::DecreaseTTSSpeedAction), &QAction::triggered,
35+
this, &TextToSpeechBar::decreaseSpeed);
36+
connect(ui->speedComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
37+
this, &TextToSpeechBar::onSpeedChanged);
38+
}
39+
40+
void TextToSpeechBar::increaseSpeed()
41+
{
42+
int currentIndex = ui->speedComboBox->currentIndex();
43+
if (currentIndex < ui->speedComboBox->count() - 1) {
44+
ui->speedComboBox->setCurrentIndex(currentIndex + 1);
45+
}
46+
}
47+
48+
void TextToSpeechBar::decreaseSpeed()
49+
{
50+
int currentIndex = ui->speedComboBox->currentIndex();
51+
if (currentIndex > 0) {
52+
ui->speedComboBox->setCurrentIndex(currentIndex - 1);
53+
}
3154
}
3255

3356
void TextToSpeechBar::speak(const QString &text)
@@ -54,6 +77,16 @@ void TextToSpeechBar::setLocale(const QLocale& locale)
5477
}
5578
}
5679

80+
void TextToSpeechBar::setupSpeedOptionsComboBox()
81+
{
82+
ui->speedLabel->setText(gt("speed"));
83+
ui->speedComboBox->setMaxVisibleItems(10);
84+
ui->speedComboBox->setLineEdit(new ComboBoxLineEdit(ui->speedComboBox));
85+
86+
QStringList speedOptions = {"0.25","0.50","0.75","1.00","1.25","1.50","1.75","2.00"};
87+
ui->speedComboBox->addItems(speedOptions);
88+
}
89+
5790
void TextToSpeechBar::setupLanguageComboBox()
5891
{
5992
ui->langLabel->setText(gt("language"));
@@ -107,6 +140,13 @@ void TextToSpeechBar::resetVoiceComboBox()
107140
connect(ui->voiceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TextToSpeechBar::voiceSelected);
108141
}
109142

143+
void TextToSpeechBar::resetSpeedComboBox()
144+
{
145+
double savedSpeed = KiwixApp::instance()->getSavedTtsSpeed(m_speech.locale().name());
146+
int index = (savedSpeed - 0.25) * 4;
147+
ui->speedComboBox->setCurrentIndex(index);
148+
}
149+
110150
int TextToSpeechBar::getVoiceIndex()
111151
{
112152
int voiceIndex = 0;
@@ -174,6 +214,7 @@ void TextToSpeechBar::languageSelected(int index)
174214
const QLocale locale = ui->langComboBox->itemData(index).toLocale();
175215
m_speech.setLocale(locale);
176216
resetVoiceComboBox();
217+
resetSpeedComboBox();
177218
}
178219

179220
void TextToSpeechBar::voiceSelected(int index)
@@ -203,6 +244,24 @@ void TextToSpeechBar::onStateChanged(QTextToSpeech::State state)
203244
ui->stopButton->setEnabled(state != QTextToSpeech::Ready);
204245
}
205246

247+
void TextToSpeechBar::onSpeedChanged(int index)
248+
{
249+
QString speedText = ui->speedComboBox->itemText(index);
250+
double speed = speedText.toDouble();
251+
m_speech.setRate(speed - 1); // range:-1,1
252+
253+
// Save tts speed for current lang
254+
const auto currentLang = ui->langComboBox->currentData().toLocale().name();
255+
KiwixApp::instance()->saveTtsSpeed(currentLang, speed);
256+
257+
// Restarting the speech with new speed set above
258+
if (m_speech.state() == QTextToSpeech::Speaking)
259+
{
260+
m_speech.stop();
261+
m_speech.say(m_text);
262+
}
263+
}
264+
206265
ComboBoxLineEdit::ComboBoxLineEdit(QWidget *parent) : QLineEdit(parent)
207266
{
208267
setFrame(false);

src/texttospeechbar.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class TextToSpeechBar : public QFrame
3535
void setupLanguageComboBox();
3636
void setupVoiceComboBox();
3737
void resetVoiceComboBox();
38+
void setupSpeedOptionsComboBox();
39+
void resetSpeedComboBox();
3840

3941
int getVoiceIndex();
4042

@@ -46,6 +48,9 @@ public slots:
4648
void toggleLanguage();
4749
void languageSelected(int index);
4850
void voiceSelected(int index);
51+
void onSpeedChanged(int index);
52+
void increaseSpeed();
53+
void decreaseSpeed();
4954

5055
protected:
5156
void keyPressEvent(QKeyEvent *event);

src/texttospeechbar.ui

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151
</property>
5252
</widget>
5353
</item>
54+
<item>
55+
<widget class="QLabel" name="speedLabel">
56+
<property name="text">
57+
<string>Speed</string>
58+
</property>
59+
</widget>
60+
</item>
61+
<item>
62+
<widget class="QComboBox" name="speedComboBox">
63+
<property name="editable">
64+
<bool>false</bool>
65+
</property>
66+
</widget>
67+
</item>
5468
<item>
5569
<spacer name="horizontalSpacer_2">
5670
<property name="orientation">

0 commit comments

Comments
 (0)