summaryrefslogtreecommitdiffstats
path: root/src/plugins/tts/common/qtexttospeechprocessor_p.h
blob: 67b850e184f7bc99cb9abc0df8f9f111dff3f33b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Speech module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QTEXTTOSPEECHPROCESSOR_P_H
#define QTEXTTOSPEECHPROCESSOR_P_H

#include "qvoice.h"

#include <QtCore/QString>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QSemaphore>
#include <QtCore/QIODevice>
#include <QtMultimedia/QAudioOutput>

QT_BEGIN_NAMESPACE

// A common base class for text-to-speech engine integrations
// that require audio output implementation and thread handling.
//
// QAudioOutput is used for audio, and each call to say() cancels
// any previous processing. The public interface is thread-safe.
class QTextToSpeechProcessor : public QThread {
    Q_OBJECT

public:
    struct VoiceInfo
    {
        int id;
        QString name;
        QString locale;
        QVoice::Gender gender;
        QVoice::Age age;
    };
    QTextToSpeechProcessor();
    ~QTextToSpeechProcessor() override;
    void say(const QString &text, int voiceId);
    void stop();
    void pause();
    void resume();
    bool isIdle() const;
    bool setRate(double rate);
    bool setPitch(double pitch);
    bool setVolume(double volume);
    double rate() const;
    double pitch() const;
    double volume() const;
    virtual const QVector<VoiceInfo> &voices() const = 0;

protected:
    // These are re-implemented QThread methods.
    // exit() waits until the processor thread finishes or the wait times out.
    void start(QThread::Priority = QThread::InheritPriority);
    void exit(int retcode = 0);

    // These methods can be used for audio output.
    // audioOutput() blocks until all the audio has been written or processing
    // is interrupted.
    bool audioStart(int sampleRate, int channelCount, QString *errorString = nullptr);
    bool audioOutput(const char* data, qint64 dataSize, QString *errorString = nullptr);
    void audioStop(bool abort = false);

    // These methods should be re-implemented if the parameters need
    // to be changed while TTS is speaking. By default, updateVolume() just
    // changes the QAudioOutput volume. The other methods do nothing by default.
    virtual bool updateRate(double rate);
    virtual bool updatePitch(double pitch);
    virtual bool updateVolume(double volume);

    // This method is called from the internal processor thread, and should block
    // until the given text has been processed or processing is interrupted.
    virtual int processText(const QString &text, int voiceId) = 0;

signals:
    // This signal is emitted when the processor goes to idle state, i.e. when no
    // new text is set to be spoken. The parameter is the latest return value of
    // processText(). As the signal is emitted from the internal thread, the recipient
    // should call isIdle() to get updated state.
    void notSpeaking(int statusCode);

private:
    void run() override;
    mutable QMutex m_lock;
    volatile bool m_stop;
    volatile bool m_idle;
    volatile bool m_paused;
    double m_rate;
    double m_pitch;
    double m_volume;
    QSemaphore m_speakSem;
    QString m_nextText;
    int m_nextVoice;
    QAudioOutput *m_audio;
    QIODevice *m_audioBuffer;
};

QT_END_NAMESPACE

#endif