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 #include "voskengineutils.h"
0008 #include "libvoskspeechtotext_debug.h"
0009 #include <QDir>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QStandardPaths>
0013 
0014 QString VoskEngineUtils::defaultVoskRepository()
0015 {
0016     return QStringLiteral("https://alphacephei.com/vosk/models/model-list.json");
0017 }
0018 
0019 QString VoskEngineUtils::storageLanguagePath()
0020 {
0021     return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/vosk-texttospeech");
0022 }
0023 
0024 VoskEngineUtils::LanguageInstalled VoskEngineUtils::loadInstalledLanguageInfo(const QString &modelInfoPath)
0025 {
0026     VoskEngineUtils::LanguageInstalled info;
0027     QFile file(modelInfoPath + QStringLiteral("/model_info.json"));
0028     if (!file.open(QFile::ReadOnly)) {
0029         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open model_info.json in " << modelInfoPath;
0030         return info;
0031     }
0032     const QByteArray ba = file.readAll();
0033     const QJsonDocument loadDoc(QJsonDocument::fromJson(ba));
0034     const QJsonObject o = loadDoc.object();
0035     info.absoluteLanguageModelPath = o[QLatin1String("absolutePath")].toString();
0036     info.name = o[QLatin1String("name")].toString();
0037     info.url = o[QLatin1String("url")].toString();
0038     info.versionStr = o[QLatin1String("version")].toString();
0039     return info;
0040 }
0041 
0042 bool VoskEngineUtils::createInstalledLanguageInfo(const QString &modelInfoPath, const LanguageInstalled &installed)
0043 {
0044     QFile file(modelInfoPath + QStringLiteral("/model_info.json"));
0045     if (!file.open(QFile::WriteOnly)) {
0046         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to save model_info.json in :" << modelInfoPath;
0047         return false;
0048     }
0049     QJsonDocument d;
0050     QJsonObject o;
0051     o[QLatin1String("absolutePath")] = installed.absoluteLanguageModelPath;
0052     o[QLatin1String("name")] = installed.name;
0053     o[QLatin1String("url")] = installed.url;
0054     o[QLatin1String("version")] = installed.versionStr;
0055     d.setObject(o);
0056     const QByteArray ba = d.toJson();
0057     file.write(d.toJson());
0058     file.close();
0059     return true;
0060 }
0061 
0062 QVector<VoskEngineUtils::LanguageInstalled> VoskEngineUtils::languageLocallyStored(const QString &path)
0063 {
0064     QString newPath = path;
0065     if (newPath.isEmpty()) {
0066         newPath = VoskEngineUtils::storageLanguagePath();
0067     }
0068     const QDir dir(newPath);
0069     QVector<VoskEngineUtils::LanguageInstalled> languages;
0070     const QStringList list = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
0071     // qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " list " << list;
0072     for (const auto &name : list) {
0073         // qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << " name " << dir;
0074         const QString modelLanguagePath{dir.absolutePath() + QLatin1Char('/') + name};
0075         const VoskEngineUtils::LanguageInstalled info = loadInstalledLanguageInfo(modelLanguagePath);
0076         if (info.isValid()) {
0077             languages.append(info);
0078         }
0079     }
0080     return languages;
0081 }
0082 
0083 QDebug operator<<(QDebug d, const VoskEngineUtils::LanguageInstalled &t)
0084 {
0085     d << " shortName " << t.name;
0086     d << " absoluteLanguageModelPath " << t.absoluteLanguageModelPath;
0087     d << " version " << t.versionStr;
0088     d << " url " << t.url;
0089     return d;
0090 }
0091 
0092 bool VoskEngineUtils::LanguageInstalled::operator==(const LanguageInstalled &other) const
0093 {
0094     return name == other.name && absoluteLanguageModelPath == other.absoluteLanguageModelPath && versionStr == other.versionStr && url == other.url;
0095 }
0096 
0097 bool VoskEngineUtils::LanguageInstalled::isValid() const
0098 {
0099     return !absoluteLanguageModelPath.isEmpty();
0100 }
0101 
0102 void VoskEngineUtils::saveSettings()
0103 {
0104     // TODO
0105 }
0106 
0107 void VoskEngineUtils::loadSettings()
0108 {
0109     // TODO
0110 }
0111 
0112 QString VoskEngineUtils::defaultLanguage()
0113 {
0114     // TODO
0115     return {};
0116 }