GAudioOutput.cc 2.64 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

41
IMPLEMENT_QGC_SINGLETON(GAudioOutput, GAudioOutput)
Lorenz Meier's avatar
Lorenz Meier committed
42

43 44
const char* GAudioOutput::_mutedKey = "AudioMuted";

45 46
GAudioOutput::GAudioOutput(QObject *parent) :
    QGCSingleton(parent),
47 48 49
    muted(false),
    thread(new QThread()),
    worker(new QGCAudioWorker())
Lorenz Meier's avatar
Lorenz Meier committed
50
{
51 52 53 54
    QSettings settings;
    
    muted = settings.value(_mutedKey, false).toBool();
    muted |= qgcApp()->runningUnitTests();
55
    
56
    worker->moveToThread(thread);
57 58
    connect(this, &GAudioOutput::textToSpeak, worker, &QGCAudioWorker::say);
    connect(this, &GAudioOutput::beepOnce, worker, &QGCAudioWorker::beep);
59 60
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    connect(thread, &QThread::finished, worker, &QObject::deleteLater);
61
    thread->start();
Lorenz Meier's avatar
Lorenz Meier committed
62 63 64 65
}

GAudioOutput::~GAudioOutput()
{
66
    thread->quit();
Lorenz Meier's avatar
Lorenz Meier committed
67 68 69 70 71
}


void GAudioOutput::mute(bool mute)
{
72 73
    QSettings settings;
    
74
    muted = mute;
75 76 77
    settings.setValue(_mutedKey, mute);
    
    emit mutedChanged(mute);
Lorenz Meier's avatar
Lorenz Meier committed
78 79 80 81
}

bool GAudioOutput::isMuted()
{
82
    return muted;
Lorenz Meier's avatar
Lorenz Meier committed
83 84 85 86
}

bool GAudioOutput::say(QString text, int severity)
{
87 88 89
    if (!muted) {
        emit textToSpeak(text, severity);
    }
90
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
91 92 93 94 95 96 97
}

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
98 99 100
    if (!muted) {
        emit textToSpeak(text, 1);
    }
101
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
102 103 104 105
}

void GAudioOutput::beep()
{
106 107 108
    if (!muted) {
        emit beepOnce();
    }
Lorenz Meier's avatar
Lorenz Meier committed
109
}