GAudioOutput.cc 3.07 KB
Newer Older
Lorenz Meier's avatar
Lorenz Meier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of audio output
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
Thomas Gubler's avatar
Thomas Gubler committed
29
 *   @author Thomas Gubler <thomasgubler@gmail.com>
Lorenz Meier's avatar
Lorenz Meier committed
30 31 32 33 34 35 36
 *
 */

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

37 38 39
#include "GAudioOutput.h"
#include "QGCApplication.h"
#include "QGC.h"
Lorenz Meier's avatar
Lorenz Meier committed
40

dogmaphobic's avatar
dogmaphobic committed
41 42 43 44 45
#if defined __android__
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>
#endif

46 47
const char* GAudioOutput::_mutedKey = "AudioMuted";

48 49
GAudioOutput::GAudioOutput(QGCApplication* app)
    : QGCTool(app)
dogmaphobic's avatar
dogmaphobic committed
50 51 52 53 54
    , muted(false)
#ifndef __android__
    , thread(new QThread())
    , worker(new QGCAudioWorker())
#endif
Lorenz Meier's avatar
Lorenz Meier committed
55
{
56 57
    QSettings settings;
    muted = settings.value(_mutedKey, false).toBool();
58
    muted |= app->runningUnitTests();
dogmaphobic's avatar
dogmaphobic committed
59
#ifndef __android__
60
    worker->moveToThread(thread);
61
    connect(this, &GAudioOutput::textToSpeak, worker, &QGCAudioWorker::say);
62 63
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    connect(thread, &QThread::finished, worker, &QObject::deleteLater);
64
    thread->start();
dogmaphobic's avatar
dogmaphobic committed
65
#endif
Lorenz Meier's avatar
Lorenz Meier committed
66 67 68 69
}

GAudioOutput::~GAudioOutput()
{
dogmaphobic's avatar
dogmaphobic committed
70
#ifndef __android__
71
    thread->quit();
dogmaphobic's avatar
dogmaphobic committed
72
#endif
Lorenz Meier's avatar
Lorenz Meier committed
73 74 75 76 77
}


void GAudioOutput::mute(bool mute)
{
78
    QSettings settings;
79
    muted = mute;
80
    settings.setValue(_mutedKey, mute);
dogmaphobic's avatar
dogmaphobic committed
81
#ifndef __android__
82
    emit mutedChanged(mute);
dogmaphobic's avatar
dogmaphobic committed
83
#endif
Lorenz Meier's avatar
Lorenz Meier committed
84 85 86 87
}

bool GAudioOutput::isMuted()
{
88
    return muted;
Lorenz Meier's avatar
Lorenz Meier committed
89 90
}

dogmaphobic's avatar
dogmaphobic committed
91
bool GAudioOutput::say(const QString& inText, int severity)
Lorenz Meier's avatar
Lorenz Meier committed
92
{
93
    if (!muted) {
dogmaphobic's avatar
dogmaphobic committed
94 95
#if defined __android__
#if defined QGC_SPEECH_ENABLED
dogmaphobic's avatar
dogmaphobic committed
96
        Q_UNUSED(severity);
dogmaphobic's avatar
dogmaphobic committed
97 98 99 100 101 102 103 104 105 106 107 108 109
        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
        emit textToSpeak(inText, severity);
#endif
110
    }
111
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
112
}