GAudioOutput.cc 4.6 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 "GAudioOutput.h"
#include <QDebug>
37
#include <QGC.h>
Lorenz Meier's avatar
Lorenz Meier committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/**
 * This class follows the singleton design pattern
 * @see http://en.wikipedia.org/wiki/Singleton_pattern
 * A call to this function thus returns the only instance of this object
 * the call can occur at any place in the code, no reference to the
 * GAudioOutput object has to be passed.
 */
GAudioOutput *GAudioOutput::instance()
{
    static GAudioOutput *_instance = 0;

    if (_instance == 0)
    {
        _instance = new GAudioOutput();
        // Set the application as parent to ensure that this object
        // will be destroyed when the main application exits
        _instance->setParent(qApp);
    }

    return _instance;
}

GAudioOutput::GAudioOutput(QObject *parent) : QObject(parent),
62 63 64
    muted(false),
    thread(new QThread()),
    worker(new QGCAudioWorker())
Lorenz Meier's avatar
Lorenz Meier committed
65
{
66
    worker->moveToThread(thread);
67 68
    // Initialize within right thread context
    worker->init();
69
    connect(this, SIGNAL(textToSpeak(QString,int)), worker, SLOT(say(QString,int)));
70
    connect(this, SIGNAL(beepOnce()), worker, SLOT(beep()));
71
    thread->start();
Lorenz Meier's avatar
Lorenz Meier committed
72 73 74 75
}

GAudioOutput::~GAudioOutput()
{
76 77 78 79 80 81 82 83
    thread->quit();
    while (thread->isRunning()) {
        QGC::SLEEP::usleep(100);
    }
    worker->deleteLater();
    thread->deleteLater();
    worker = NULL;
    thread = NULL;
Lorenz Meier's avatar
Lorenz Meier committed
84 85 86 87 88
}


void GAudioOutput::mute(bool mute)
{
89
    // XXX handle muting
90
    Q_UNUSED(mute);
Lorenz Meier's avatar
Lorenz Meier committed
91 92 93 94
}

bool GAudioOutput::isMuted()
{
95 96
    // XXX return right stuff
    return false;
Lorenz Meier's avatar
Lorenz Meier committed
97 98 99 100
}

bool GAudioOutput::say(QString text, int severity)
{
101 102
    emit textToSpeak(text, severity);
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
103 104 105 106 107 108 109
}

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
110
    emit textToSpeak(text, 1);
111
    return true;
Lorenz Meier's avatar
Lorenz Meier committed
112 113 114 115 116 117 118
}

void GAudioOutput::notifyPositive()
{
    if (!muted)
    {
        // Use QFile to transform path for all OS
119 120
        // FIXME: Get working with Qt5's QtMultimedia module
        //QFile f(QCoreApplication::applicationDirPath() + QString("/files/audio/double_notify.wav"));
Lorenz Meier's avatar
Lorenz Meier committed
121 122 123 124 125 126 127 128 129 130
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
    }
}

void GAudioOutput::notifyNegative()
{
    if (!muted)
    {
        // Use QFile to transform path for all OS
131 132
        // FIXME: Get working with Qt5's QtMultimedia module
        //QFile f(QCoreApplication::applicationDirPath() + QString("/files/audio/flat_notify.wav"));
Lorenz Meier's avatar
Lorenz Meier committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
    }
}

/**
 * The emergency sound will be played continously during the emergency.
 * call stopEmergency() to disable it again. No speech synthesis or other
 * audio output is available during the emergency.
 *
 * @return true if the emergency could be started, false else
 */
bool GAudioOutput::startEmergency()
{
147 148 149
//    if (!emergency)
//    {
//        emergency = true;
Lorenz Meier's avatar
Lorenz Meier committed
150

151
//        // Beep immediately and then start timer
Lorenz Meier's avatar
Lorenz Meier committed
152

153 154 155
//        emergencyTimer->start(1500);
//        QTimer::singleShot(5000, this, SLOT(stopEmergency()));
//    }
Lorenz Meier's avatar
Lorenz Meier committed
156 157 158 159 160 161 162 163 164 165 166 167

    return true;
}

/**
 * Stops the continous emergency sound. Use startEmergency() to start
 * the emergency sound.
 *
 * @return true if the emergency could be stopped, false else
 */
bool GAudioOutput::stopEmergency()
{
168 169 170 171 172
//    if (emergency)
//    {
//        emergency = false;
//        emergencyTimer->stop();
//    }
Lorenz Meier's avatar
Lorenz Meier committed
173 174 175 176 177 178

    return true;
}

void GAudioOutput::beep()
{
179
    emit beepOnce();
Lorenz Meier's avatar
Lorenz Meier committed
180
}