File: qtexttospeechprocessor.cpp

package info (click to toggle)
qtspeech-opensource-src 5.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,124 kB
  • sloc: cpp: 3,244; java: 186; makefile: 21; ansic: 12; xml: 7
file content (310 lines) | stat: -rw-r--r-- 8,229 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/****************************************************************************
**
** 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$
**
****************************************************************************/

#include "qtexttospeechprocessor_p.h"

#include <QtCore/QDateTime>

QT_BEGIN_NAMESPACE

QTextToSpeechProcessor::QTextToSpeechProcessor():
    m_stop(true),
    m_idle(true),
    m_paused(false),
    m_rate(0),
    m_pitch(0),
    m_volume(1.0),
    m_audio(0),
    m_audioBuffer(0)
{
}

QTextToSpeechProcessor::~QTextToSpeechProcessor()
{
}

void QTextToSpeechProcessor::say(const QString &text, int voiceId)
{
    if (isInterruptionRequested())
        return;
    QMutexLocker lock(&m_lock);
    bool wasPaused = m_paused;
    m_stop = true; // Cancel any previous utterance
    m_idle = false;
    m_paused = false;
    m_nextText = text;
    m_nextVoice = voiceId;
    // If the speech was paused, one signal is needed to release the pause
    // and another to start processing the new text.
    m_speakSem.release(wasPaused ? 2 : 1);
}

void QTextToSpeechProcessor::stop()
{
    QMutexLocker lock(&m_lock);
    m_stop = true;
    m_paused = false;
    m_nextText.clear();
    m_speakSem.release();
}

void QTextToSpeechProcessor::pause()
{
    QMutexLocker lock(&m_lock);
    m_paused = true;
    m_speakSem.release();
}

void QTextToSpeechProcessor::resume()
{
    QMutexLocker lock(&m_lock);
    m_paused = false;
    m_speakSem.release();
}

bool QTextToSpeechProcessor::setRate(double rate)
{
    QMutexLocker lock(&m_lock);
    if (rate >= -1.0 && rate <= 1.0) {
        if (updateRate(rate)) {
            m_rate = rate;
            return true;
        }
    }
    return false;
}

bool QTextToSpeechProcessor::setPitch(double pitch)
{
    QMutexLocker lock(&m_lock);
    if (pitch >= -1.0 && pitch <= 1.0) {
        if (updatePitch(pitch)) {
            m_pitch = pitch;
            return true;
        }
    }
    return false;
}

bool QTextToSpeechProcessor::setVolume(double volume)
{
    QMutexLocker lock(&m_lock);
    if (volume >= 0.0 && volume <= 1.0) {
        if (updateVolume(volume)) {
            m_volume = volume;
            return true;
        }
    }
    return false;
}

bool QTextToSpeechProcessor::isIdle() const
{
    QMutexLocker lock(&m_lock);
    return m_idle;
}

double QTextToSpeechProcessor::rate() const
{
    QMutexLocker lock(&m_lock);
    return m_rate;
}

double QTextToSpeechProcessor::pitch() const
{
    QMutexLocker lock(&m_lock);
    return m_pitch;
}

double QTextToSpeechProcessor::volume() const
{
    QMutexLocker lock(&m_lock);
    return m_volume;
}

void QTextToSpeechProcessor::start(QThread::Priority priority)
{
    QThread::start(priority);
}

void QTextToSpeechProcessor::exit(int retcode)
{
    QThread::exit(retcode);
    QThread::requestInterruption();
    stop();
    if (!QThread::wait(5000)) {
        QThread::terminate();
        QThread::wait();
    }
}

void QTextToSpeechProcessor::run()
{
    int statusCode = 0;
    forever {
        m_lock.lock();
        if (!m_speakSem.tryAcquire()) {
            m_idle = true;
            m_lock.unlock();
            emit notSpeaking(statusCode); // Going idle
            m_speakSem.acquire();
            m_lock.lock();
        }
        if (isInterruptionRequested()) {
            if (m_audio) {
                delete m_audio;
                m_audio = 0;
                m_audioBuffer = 0;
            }
            m_lock.unlock();
            break;
        }
        m_stop = false;
        if (!m_nextText.isEmpty()) {
            QString text = m_nextText;
            int voice = m_nextVoice;
            m_nextText.clear();
            m_lock.unlock();
            statusCode = processText(text, voice);
        } else {
            m_lock.unlock();
        }
    }
}

bool QTextToSpeechProcessor::audioStart(int sampleRate, int channelCount, QString *errorString)
{
    QMutexLocker lock(&m_lock);
    QAudioFormat format;
    format.setSampleRate(sampleRate);
    format.setChannelCount(channelCount);
    format.setSampleSize(16);
    format.setSampleType(QAudioFormat::SignedInt);
    format.setCodec("audio/pcm");
    if (errorString)
        *errorString = QString();
    if (m_audio)
        delete m_audio;
    m_audio = new QAudioOutput(format);
    m_audioBuffer = m_audio->start();
    updateVolume(m_volume);
    if (m_audioBuffer && m_audio->state() == QAudio::IdleState)
        return true;
    if (errorString)
        *errorString = QLatin1String("Failed to start audio output (error ")
            + QString::number(m_audio->error()) + QLatin1Char(')');
    delete m_audio;
    m_audio = 0;
    m_audioBuffer = 0;
    return false;
}

bool QTextToSpeechProcessor::audioOutput(const char *data, qint64 dataSize, QString *errorString)
{
    bool ret = true;
    int bytesWritten = 0;
    QString error;
    forever {
        m_lock.lock();
        if (m_paused) {
            m_audio->suspend();
            do {
                m_lock.unlock();
                m_speakSem.acquire(); // Wait for any command
                m_lock.lock();
            } while (m_paused);
            m_audio->resume();
        }
        if (m_stop || !m_audioBuffer
        || m_audio->state() == QAudio::StoppedState || isInterruptionRequested()) {
            if (m_audio->error() != QAudio::NoError) {
                error = QLatin1String("Audio error (")
                        + QString::number(m_audio->error()) + QLatin1Char(')');
            }
            m_lock.unlock();
            ret = false;
            break;
        }
        bytesWritten += m_audioBuffer->write(data + bytesWritten, dataSize - bytesWritten);
        m_lock.unlock();
        if (bytesWritten >= dataSize)
            break;
        QThread::msleep(50);
    }
    if (errorString)
        *errorString = error;
    return ret;
}

void QTextToSpeechProcessor::audioStop(bool abort)
{
    QMutexLocker lock(&m_lock);
    if (m_audio) {
        if (abort) {
            m_audio->reset(); // Discard buffered audio
        } else {
            // TODO: Find a way to reliably check if all the audio has been written out before stopping
            m_audioBuffer->write(QByteArray(1024, 0));
            QThread::msleep(200);
            m_audio->stop();
        }
        delete m_audio;
        m_audio = 0;
        m_audioBuffer = 0;
    }
}

bool QTextToSpeechProcessor::updateRate(double rate)
{
    Q_UNUSED(rate);
    return true;
}

bool QTextToSpeechProcessor::updatePitch(double pitch)
{
    Q_UNUSED(pitch);
    return true;
}

bool QTextToSpeechProcessor::updateVolume(double volume)
{
    if (m_audio)
        m_audio->setVolume(volume);
    return true;
}


QT_END_NAMESPACE