GAudioOutput.cc 2.2 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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
9 10 11 12 13 14 15


/**
 * @file
 *   @brief Implementation of audio output
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
Thomas Gubler's avatar
Thomas Gubler committed
16
 *   @author Thomas Gubler <thomasgubler@gmail.com>
Lorenz Meier's avatar
Lorenz Meier committed
17 18 19 20 21 22
 *
 */

#include <QApplication>
#include <QDebug>

23 24 25
#include "GAudioOutput.h"
#include "QGCApplication.h"
#include "QGC.h"
26
#include "SettingsManager.h"
Lorenz Meier's avatar
Lorenz Meier committed
27

dogmaphobic's avatar
dogmaphobic committed
28 29 30 31 32
#if defined __android__
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>
#endif

33 34
GAudioOutput::GAudioOutput(QGCApplication* app)
    : QGCTool(app)
dogmaphobic's avatar
dogmaphobic committed
35 36 37 38
#ifndef __android__
    , thread(new QThread())
    , worker(new QGCAudioWorker())
#endif
Lorenz Meier's avatar
Lorenz Meier committed
39
{
dogmaphobic's avatar
dogmaphobic committed
40
#ifndef __android__
41
    worker->moveToThread(thread);
42
    connect(this, &GAudioOutput::textToSpeak, worker, &QGCAudioWorker::say);
43 44
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    connect(thread, &QThread::finished, worker, &QObject::deleteLater);
45
    thread->start();
dogmaphobic's avatar
dogmaphobic committed
46
#endif
Lorenz Meier's avatar
Lorenz Meier committed
47 48 49 50
}

GAudioOutput::~GAudioOutput()
{
dogmaphobic's avatar
dogmaphobic committed
51
#ifndef __android__
52
    thread->quit();
dogmaphobic's avatar
dogmaphobic committed
53
#endif
Lorenz Meier's avatar
Lorenz Meier committed
54 55 56
}


Don Gagne's avatar
Don Gagne committed
57
bool GAudioOutput::say(const QString& inText)
Lorenz Meier's avatar
Lorenz Meier committed
58
{
59
    bool muted = qgcApp()->toolbox()->settingsManager()->appSettings()->audioMuted()->rawValue().toBool();
60
    muted |= qgcApp()->runningUnitTests();
Don Gagne's avatar
Don Gagne committed
61
    if (!muted && !qgcApp()->runningUnitTests()) {
dogmaphobic's avatar
dogmaphobic committed
62 63
#if defined __android__
#if defined QGC_SPEECH_ENABLED
64
        static const char V_jniClassName[] {"org/mavlink/qgroundcontrol/QGCActivity"};
dogmaphobic's avatar
dogmaphobic committed
65 66 67 68 69 70 71 72 73 74
        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
75
        emit textToSpeak(inText);
dogmaphobic's avatar
dogmaphobic committed
76
#endif
77
    }
78
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
79
}