summaryrefslogtreecommitdiffstats
path: root/src/asr/qspeechrecognitionengine.cpp
blob: 56223aa43cc750efc5ad26e2762d50b125a5de8f (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
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
311
312
313
314
315
/****************************************************************************
**
** 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 "qspeechrecognitionengine_p.h"

#include <QLoggingCategory>

QT_BEGIN_NAMESPACE

/*
    When these conditions are satisfied, QStringLiteral is implemented by
    gcc's statement-expression extension.  However, in this file it will
    not work, because "statement-expressions are not allowed outside functions
    nor in template-argument lists".
    MSVC 2012 produces an internal compiler error on encountering
    QStringLiteral in this context.

    Fall back to the less-performant QLatin1String in this case.
*/
#if defined(Q_CC_GNU) && defined(Q_COMPILER_LAMBDA)
#    define Q_DEFINE_ASR_ENGINE_PARAMETER(key) const QString QSpeechRecognitionEngine::key(QStringLiteral(#key))
#else
#    define Q_DEFINE_ASR_ENGINE_PARAMETER(key) const QString QSpeechRecognitionEngine::key(QLatin1String(#key))
#endif

/*! \variable QSpeechRecognitionEngine::Locale

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(Locale);

/*! \variable QSpeechRecognitionEngine::Dictionary

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(Dictionary);

/*! \variable QSpeechRecognitionEngine::ResourceDirectory

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(ResourceDirectory);

/*! \variable QSpeechRecognitionEngine::DataDirectory

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(DataDirectory);

/*! \variable QSpeechRecognitionEngine::AudioSampleRate

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(AudioSampleRate);

/*! \variable QSpeechRecognitionEngine::AudioInputFile

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(AudioInputFile);

/*! \variable QSpeechRecognitionEngine::AudioInputDevice

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(AudioInputDevice);

/*! \variable QSpeechRecognitionEngine::AudioInputDevices

    This constant is used as the key for a speech recognition engine parameter.
    See supportedParameters().
*/
Q_DEFINE_ASR_ENGINE_PARAMETER(AudioInputDevices);

/*!
  \class QSpeechRecognitionEngine
  \inmodule QtSpeechRecognition
  \brief The QSpeechRecognitionEngine class is a handle to a speech recognition engine.

  A speech recognition engine can be created with \l QSpeechRecognition::createEngine().
*/

/*!
  \property QSpeechRecognitionEngine::name
  The name of the engine.
*/

/*!
  \property QSpeechRecognitionEngine::created
  Whether the speech recognition engine has been successfully initialized.

  The property is \c false if the engine is still initializing (or failed to initialize)
  and will be set \c true after the engine has been constructed and initialized.
*/

/*!
  \fn bool QSpeechRecognitionEngine::setParameter(const QString &key, const QVariant &value)

  Set engine run-time parameter (name given in \a key) to the given \a value.

  The method should only be called after the engine has been \l created. The method immediately
  fails if the parameter is unknown or the value type is inconsistent with the current value.

  The value is set asynchronously. Therefore, the value returned by \l parameter() will reflect
  the updated value only after the engine has accepted it. In case the engine does not accept
  the value, signal QSpeechRecognition::error() will be emitted.

  Returns \c true if the parameter request was successfully sent to the engine.

  \sa supportedParameters()
*/

/*!
  \fn QList<QString> QSpeechRecognitionEngine::supportedParameters() const

  Get names of the supported engine parameters.

  The method should only be called after the engine has been \l created; an empty list will
  be returned before that.

  Some parameters may only be supported during engine initialization (when calling
  QSpeechRecognition::createEngine()), or may contain read-only information. The following table
  lists some of the commonly supported engine parameters:

  \table
  \header
    \li Key
    \li Value type
    \li Description
  \row
    \li \l Locale
    \li QLocale
    \li The locale for speech recognition
  \row
    \li \l Dictionary
    \li QUrl
    \li Location of the speech recognition dictionary (default lexicon).
        If the URL contains a relative file path, the dictionary is loaded
        from the locale-specific sub-directory under the engine resource directory.
        Some engines may also support reading the dictionary from built-in Qt resources.
        The format of the dictionary is engine-specific; some engines or grammars may not need
        a dictionary at all.
  \row
    \li \l ResourceDirectory
    \li QString
    \li Path to the directory where engine-specific resource files are located.
        If not given, the program's working directory is used.
        Some resources may need to be located inside locale-specific sub-directories
        under this root directory, in which case BCP 47 names should be used for
        the sub-directories.
  \row
    \li \l DataDirectory
    \li QString
    \li Path to a persistent directory where any engine-specific data
        can be stored between application restarts. If not given, the program's
        working directory is used.
  \row
    \li \l AudioSampleRate
    \li int
    \li Samples per second in the input audio. Default: 16000.
  \row
    \li \l AudioInputFile
    \li QString
    \li Path to an audio file that should be read instead of an audio input device.
        The given file will be read once for each recognition session.
        If the engine is muted when the session starts, no data is read from the file
        until mute is released. The recognition session will be automatically stopped
        when the entire file has been read.
  \row
    \li \l AudioInputDevices
    \li QStringList
    \li Names of the supported audio input devices (read-only).
  \row
    \li \l AudioInputDevice
    \li QString
    \li Name of the currently selected audio input device.
        If not set, the system default will be used.
  \endtable

  Returns the names of the supported engine parameters.

  \sa setParameter()
  \sa parameter()
*/

/*!
  \fn QVariant QSpeechRecognitionEngine::parameter(const QString &key) const

  Get engine parameter with the given \a key.

  The method should only be called after the engine has been \l created; an empty value will
  be returned before that.

  Returns the latest known value of the parameter with the given \a key. If the value has not
  been set, or if the parameter is unknown, an empty variant is returned.

  \sa supportedParameters()
*/

/*!
  \fn void QSpeechRecognitionEngine::resetAdaptationState()

  Reset engine adaptation state to its initial value.

  If the engine has been adapted to user speech or audio path features,
  the adaptation state is returned where it was after the first initialization,
  before any audio data was fed to it. Any files that are used for storing the
  adaptation data are deleted.
*/

QSpeechRecognitionEngine::QSpeechRecognitionEngine(QObject *parent):
    QObject(parent)
{
}

QSpeechRecognitionEngine::~QSpeechRecognitionEngine()
{
}

QSpeechRecognitionEngineImpl::QSpeechRecognitionEngineImpl(const QString &name, QObject *parent):
    QSpeechRecognitionEngine(parent),
    m_name(name),
    m_created(false)
{
}

QString QSpeechRecognitionEngineImpl::name() const
{
    return m_name;
}

bool QSpeechRecognitionEngineImpl::setParameter(const QString &key, const QVariant &value)
{
    if (m_parameters.contains(key)
    && (!m_parameters.value(key).isValid() || m_parameters.value(key).type() == value.type())) {
        emit requestSetParameter(key, value);
        return true;
    }
    return false;
}

QList<QString> QSpeechRecognitionEngineImpl::supportedParameters() const
{
    return m_parameters.keys();
}

QVariant QSpeechRecognitionEngineImpl::parameter(const QString &key) const
{
    return m_parameters.value(key);
}

void QSpeechRecognitionEngineImpl::resetAdaptationState()
{
    emit requestResetAdaptationState();
}

bool QSpeechRecognitionEngineImpl::isCreated()
{
    return m_created;
}

void QSpeechRecognitionEngineImpl::setCreated(bool created)
{
    if (!m_created)
        QMetaObject::invokeMethod(this, "created", Qt::QueuedConnection);
    m_created = created;
}

void QSpeechRecognitionEngineImpl::saveParameter(const QString &key, const QVariant &value)
{
    qCDebug(lcSpeechAsr) << "QSpeechRecognitionEngine" << m_name << "Parameter" << key << "<--" << value;
    m_parameters.insert(key, value);
}

QT_END_NAMESPACE