GAudioOutput.cc 2.46 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 23
 *
 */

#include <QApplication>
#include <QSettings>
#include <QDebug>

24 25 26
#include "GAudioOutput.h"
#include "QGCApplication.h"
#include "QGC.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
const char* GAudioOutput::_mutedKey = "AudioMuted";

35 36
GAudioOutput::GAudioOutput(QGCApplication* app)
    : QGCTool(app)
dogmaphobic's avatar
dogmaphobic committed
37 38 39 40 41
    , muted(false)
#ifndef __android__
    , thread(new QThread())
    , worker(new QGCAudioWorker())
#endif
Lorenz Meier's avatar
Lorenz Meier committed
42
{
43 44
    QSettings settings;
    muted = settings.value(_mutedKey, false).toBool();
45
    muted |= app->runningUnitTests();
dogmaphobic's avatar
dogmaphobic committed
46
#ifndef __android__
47
    worker->moveToThread(thread);
48
    connect(this, &GAudioOutput::textToSpeak, worker, &QGCAudioWorker::say);
49 50
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    connect(thread, &QThread::finished, worker, &QObject::deleteLater);
51
    thread->start();
dogmaphobic's avatar
dogmaphobic committed
52
#endif
Lorenz Meier's avatar
Lorenz Meier committed
53 54 55 56
}

GAudioOutput::~GAudioOutput()
{
dogmaphobic's avatar
dogmaphobic committed
57
#ifndef __android__
58
    thread->quit();
dogmaphobic's avatar
dogmaphobic committed
59
#endif
Lorenz Meier's avatar
Lorenz Meier committed
60 61 62 63 64
}


void GAudioOutput::mute(bool mute)
{
65
    QSettings settings;
66
    muted = mute;
67
    settings.setValue(_mutedKey, mute);
dogmaphobic's avatar
dogmaphobic committed
68
#ifndef __android__
69
    emit mutedChanged(mute);
dogmaphobic's avatar
dogmaphobic committed
70
#endif
Lorenz Meier's avatar
Lorenz Meier committed
71 72 73 74
}

bool GAudioOutput::isMuted()
{
75
    return muted;
Lorenz Meier's avatar
Lorenz Meier committed
76 77
}

Don Gagne's avatar
Don Gagne committed
78
bool GAudioOutput::say(const QString& inText)
Lorenz Meier's avatar
Lorenz Meier committed
79
{
Don Gagne's avatar
Don Gagne committed
80
    if (!muted && !qgcApp()->runningUnitTests()) {
dogmaphobic's avatar
dogmaphobic committed
81 82 83 84 85 86 87 88 89 90 91 92 93
#if defined __android__
#if defined QGC_SPEECH_ENABLED
        static const char V_jniClassName[] {"org/qgroundcontrol/qgchelper/UsbDeviceJNI"};
        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
94
        emit textToSpeak(inText);
dogmaphobic's avatar
dogmaphobic committed
95
#endif
96
    }
97
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
98
}