Skip to content
Snippets Groups Projects
GAudioOutput.cc 2.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • /****************************************************************************
     *
     *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
     *
     * QGroundControl is licensed according to the terms in the file
     * COPYING.md in the root of the source code directory.
     *
     ****************************************************************************/
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    
    
    /**
     * @file
     *   @brief Implementation of audio output
     *
     *   @author Lorenz Meier <mavteam@student.ethz.ch>
    
    Thomas Gubler's avatar
    Thomas Gubler committed
     *   @author Thomas Gubler <thomasgubler@gmail.com>
    
    Lorenz Meier's avatar
    Lorenz Meier committed
     *
     */
    
    #include <QApplication>
    #include <QDebug>
    
    
    #include "GAudioOutput.h"
    #include "QGCApplication.h"
    #include "QGC.h"
    
    #include "SettingsManager.h"
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    
    
    dogmaphobic's avatar
    dogmaphobic committed
    #if defined __android__
    #include <QtAndroidExtras/QtAndroidExtras>
    #include <QtAndroidExtras/QAndroidJniObject>
    #endif
    
    
    GAudioOutput::GAudioOutput(QGCApplication* app)
        : QGCTool(app)
    
    dogmaphobic's avatar
    dogmaphobic committed
    #ifndef __android__
        , thread(new QThread())
        , worker(new QGCAudioWorker())
    #endif
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    {
    
    dogmaphobic's avatar
    dogmaphobic committed
    #ifndef __android__
    
        worker->moveToThread(thread);
    
        connect(this, &GAudioOutput::textToSpeak, worker, &QGCAudioWorker::say);
    
        connect(thread, &QThread::finished, thread, &QObject::deleteLater);
        connect(thread, &QThread::finished, worker, &QObject::deleteLater);
    
        thread->start();
    
    dogmaphobic's avatar
    dogmaphobic committed
    #endif
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    }
    
    GAudioOutput::~GAudioOutput()
    {
    
    dogmaphobic's avatar
    dogmaphobic committed
    #ifndef __android__
    
        thread->quit();
    
    dogmaphobic's avatar
    dogmaphobic committed
    #endif
    
    Don Gagne's avatar
    Don Gagne committed
    bool GAudioOutput::say(const QString& inText)
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    {
    
        bool muted = qgcApp()->toolbox()->settingsManager()->appSettings()->audioMuted()->rawValue().toBool();
    
        muted |= qgcApp()->runningUnitTests();
    
    Don Gagne's avatar
    Don Gagne committed
        if (!muted && !qgcApp()->runningUnitTests()) {
    
    dogmaphobic's avatar
    dogmaphobic committed
    #if defined __android__
    #if defined QGC_SPEECH_ENABLED
    
            static const char V_jniClassName[] {"org/mavlink/qgroundcontrol/QGCActivity"};
    
    dogmaphobic's avatar
    dogmaphobic committed
            QAndroidJniEnvironment env;
            if (env->ExceptionCheck()) {
                env->ExceptionDescribe();
                env->ExceptionClear();
            }
            QString text = QGCAudioWorker::fixTextMessageForAudio(inText);
            QAndroidJniObject javaMessage = QAndroidJniObject::fromString(text);
            QAndroidJniObject::callStaticMethod<void>(V_jniClassName, "say", "(Ljava/lang/String;)V", javaMessage.object<jstring>());
    #endif
    #else
    
    Don Gagne's avatar
    Don Gagne committed
            emit textToSpeak(inText);
    
    dogmaphobic's avatar
    dogmaphobic committed
    #endif
    
        return true;
    
    Lorenz Meier's avatar
    Lorenz Meier committed
    }