8#include "voskspeechtotextdevice.h"
9#include "libvoskspeechtotext_debug.h"
10#if HAVE_VOSK_API_SUPPORT
13#include <QJsonDocument>
15VoskSpeechToTextDevice::VoskSpeechToTextDevice(
QObject *parent)
19 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) <<
"Impossible to open VoskSpeechToTextDevice";
20#if HAVE_VOSK_API_SUPPORT
21 vosk_set_log_level(-1);
26VoskSpeechToTextDevice::~VoskSpeechToTextDevice()
28#if HAVE_VOSK_API_SUPPORT
29 vosk_recognizer_free(mRecognizer);
30 vosk_model_free(mModel);
34bool VoskSpeechToTextDevice::available()
const
36#if HAVE_VOSK_API_SUPPORT
43bool VoskSpeechToTextDevice::isAsking()
const
48void VoskSpeechToTextDevice::setAsking(
bool asking)
50 if (mIsAsking != asking) {
56bool VoskSpeechToTextDevice::initialize(VoskSpeechToTextDeviceInfo &&info)
58#if HAVE_VOSK_API_SUPPORT
61 mRecognizer = vosk_recognizer_new(mModel, info.sampleRate);
64 if (!mModel || !mRecognizer) {
71void VoskSpeechToTextDevice::clear()
73#if HAVE_VOSK_API_SUPPORT
75 vosk_recognizer_reset(mRecognizer);
80qint64 VoskSpeechToTextDevice::readData(
char *data, qint64 maxlen)
86qint64 VoskSpeechToTextDevice::writeData(
const char *data, qint64 len)
88#if HAVE_VOSK_API_SUPPORT
89 if (vosk_recognizer_accept_waveform(mRecognizer, data, (
int)len)) {
90 parseText(vosk_recognizer_result(mRecognizer));
92 parsePartial(vosk_recognizer_partial_result(mRecognizer));
100void VoskSpeechToTextDevice::parseText(
const char *json)
103 QString text = obj[QStringLiteral(
"text")].toString();
107 else if (mIsAsking) {
115 if (!mIsListiningBecauseOfWakeWord)
118 Q_EMIT falsePositiveWakeWord();
119 mIsListiningBecauseOfWakeWord =
false;
127 qDebug() <<
"[debug] Text:" << text;
131void VoskSpeechToTextDevice::parsePartial(
const char *json)
134 QString text = obj[QStringLiteral(
"partial")].toString();
140 Q_EMIT wakeWordDetected();
142 mIsListiningBecauseOfWakeWord =
true;
143 }
else if (mIsListiningBecauseOfWakeWord) {
144 Q_EMIT falsePositiveWakeWord();
145 mIsListiningBecauseOfWakeWord =
false;
147 }
else if (!mIsAsking)
155 d.
space() <<
"sampleRate" << t.sampleRate;
156 d.
space() <<
"modelDir" << t.modelDir;
157 d.
space() <<
"formattedLang" << t.formattedLang;
161#include "moc_voskspeechtotextdevice.cpp"
const QList< QKeySequence > & open()
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
const char * constData() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString & append(QChar ch)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
qsizetype size() const const
QByteArray toUtf8() const const
QString trimmed() const const