File indexing completed on 2024-12-22 04:28:23

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "config-vosk-plugin.h"
0010 #include "libvoskspeechtotext_export.h"
0011 #include <QIODevice>
0012 
0013 #if HAVE_VOSK_API_SUPPORT
0014 class VoskModel;
0015 class VoskRecognizer;
0016 #endif
0017 
0018 class LIBVOSKSPEECHTOTEXT_EXPORT VoskSpeechToTextDevice : public QIODevice
0019 {
0020     Q_OBJECT
0021 public:
0022     struct LIBVOSKSPEECHTOTEXT_EXPORT VoskSpeechToTextDeviceInfo {
0023         // TODO add language and co
0024         QString modelDir;
0025         QString formattedLang;
0026         int sampleRate = 0;
0027     };
0028 
0029     explicit VoskSpeechToTextDevice(QObject *parent = nullptr);
0030     ~VoskSpeechToTextDevice() override;
0031 
0032     void clear();
0033     [[nodiscard]] bool initialize(VoskSpeechToTextDeviceInfo &&info);
0034 
0035     [[nodiscard]] bool available() const;
0036 
0037     [[nodiscard]] bool isAsking() const;
0038 
0039     void setAsking(bool asking);
0040 
0041 Q_SIGNALS:
0042     void result(const QString &str);
0043     void askingChanged();
0044     void doneListening();
0045     void falsePositiveWakeWord();
0046     void wakeWordDetected();
0047 
0048 protected:
0049     qint64 readData(char *data, qint64 maxlen) override;
0050     qint64 writeData(const char *data, qint64 len) override;
0051 
0052 private:
0053     LIBVOSKSPEECHTOTEXT_NO_EXPORT void parseText(const char *json);
0054     LIBVOSKSPEECHTOTEXT_NO_EXPORT void parsePartial(const char *json);
0055     QString mWakeWord;
0056     bool mIsAsking = false;
0057     bool mIsListiningBecauseOfWakeWord = false;
0058 #if HAVE_VOSK_API_SUPPORT
0059     VoskModel *mModel = nullptr;
0060     VoskRecognizer *mRecognizer = nullptr;
0061 #endif
0062 };
0063 Q_DECLARE_TYPEINFO(VoskSpeechToTextDevice::VoskSpeechToTextDeviceInfo, Q_MOVABLE_TYPE);
0064 LIBVOSKSPEECHTOTEXT_EXPORT QDebug operator<<(QDebug d, const VoskSpeechToTextDevice::VoskSpeechToTextDeviceInfo &t);