GAudioOutput.cc 8.86 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 37 38 39 40
 *
 */

#include <QApplication>
#include <QSettings>
#include <QTemporaryFile>
#include "GAudioOutput.h"
#include "MG.h"

#include <QDebug>

41
#if defined Q_OS_MAC && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
42 43 44 45
#include <ApplicationServices/ApplicationServices.h>
#endif

// Speech synthesis is only supported with MSVC compiler
46
#if defined _MSC_VER && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
47 48 49 50
// Documentation: http://msdn.microsoft.com/en-us/library/ee125082%28v=VS.85%29.aspx
#include <sapi.h>
#endif

51
#if defined Q_OS_LINUX && defined QGC_SPEECH_ENABLED
52 53
// Using eSpeak for speech synthesis: following https://github.com/mondhs/espeak-sample/blob/master/sampleSpeak.cpp
#include <espeak/speak_lib.h>
Lorenz Meier's avatar
Lorenz Meier committed
54 55
#endif

56
#if defined _MSC_VER && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
ISpVoice *GAudioOutput::pVoice = NULL;
#endif

/**
 * 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;
}

#define QGC_GAUDIOOUTPUT_KEY QString("QGC_AUDIOOUTPUT_")

GAudioOutput::GAudioOutput(QObject *parent) : QObject(parent),
    voiceIndex(0),
    emergency(false),
    muted(false)
{
    // Load settings
    QSettings settings;
    settings.sync();
    muted = settings.value(QGC_GAUDIOOUTPUT_KEY + "muted", muted).toBool();


95
#if defined Q_OS_LINUX && defined QGC_SPEECH_ENABLED
Thomas Gubler's avatar
Thomas Gubler committed
96
    espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 500, NULL, 0); // initialize for playback with 500ms buffer and no options (see speak_lib.h)
97 98 99 100 101 102 103
    espeak_VOICE *espeak_voice = espeak_GetCurrentVoice();
    espeak_voice->languages = "en-uk"; // Default to British English
    espeak_voice->identifier = NULL; // no specific voice file specified
    espeak_voice->name = "klatt"; // espeak voice name
    espeak_voice->gender = 2; // Female
    espeak_voice->age = 0; // age not specified
    espeak_SetVoiceByProperties(espeak_voice);
Lorenz Meier's avatar
Lorenz Meier committed
104 105
#endif

106
#if defined _MSC_VER && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
107 108 109 110
    pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
    {
111
        qDebug() << "ERROR: Creating COM object for audio output failed!";
Lorenz Meier's avatar
Lorenz Meier committed
112 113 114 115 116 117 118
    }

    else
    {

        HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);

119
        if (FAILED(hr))
Lorenz Meier's avatar
Lorenz Meier committed
120
        {
121
            qDebug() << "ERROR: Initializing voice for audio output failed!";
Lorenz Meier's avatar
Lorenz Meier committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        }
    }

#endif

    // Prepare regular emergency signal, will be fired off on calling startEmergency()
    emergencyTimer = new QTimer();
    connect(emergencyTimer, SIGNAL(timeout()), this, SLOT(beep()));

    switch (voiceIndex)
    {
    case 0:
        selectFemaleVoice();
        break;

    default:
        selectMaleVoice();
        break;
    }
}

GAudioOutput::~GAudioOutput()
{
145
#if defined _MSC_VER && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    pVoice->Release();
    pVoice = NULL;
    ::CoUninitialize();
#endif
}


void GAudioOutput::mute(bool mute)
{
    if (mute != muted)
    {
        this->muted = mute;
        QSettings settings;
        settings.setValue(QGC_GAUDIOOUTPUT_KEY + "muted", this->muted);
        settings.sync();
        emit mutedChanged(muted);
    }
}

bool GAudioOutput::isMuted()
{
    return this->muted;
}

bool GAudioOutput::say(QString text, int severity)
{
    if (!muted)
    {
        // TODO Add severity filter
        Q_UNUSED(severity);
        bool res = false;

        if (!emergency)
        {

181 182 183 184
#if defined _MSC_VER && defined QGC_SPEECH_ENABLED
            pVoice->Speak(text.toStdWString().c_str(), SPF_ASYNC, NULL);

#elif defined Q_OS_LINUX && defined QGC_SPEECH_ENABLED
185 186
            // Set size of string for espeak: +1 for the null-character
            unsigned int espeak_size = strlen(text.toStdString().c_str()) + 1;
Thomas Gubler's avatar
Thomas Gubler committed
187
            espeak_Synth(text.toStdString().c_str(), espeak_size, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL);
Lorenz Meier's avatar
Lorenz Meier committed
188

189
#elif defined Q_OS_MAC && defined QGC_SPEECH_ENABLED
Lorenz Meier's avatar
Lorenz Meier committed
190 191 192
            // Slashes necessary to have the right start to the sentence
            // copying data prevents SpeakString from reading additional chars
            text = "\\" + text;
Lorenz Meier's avatar
Lorenz Meier committed
193
            std::wstring str = text.toStdWString();
Lorenz Meier's avatar
Lorenz Meier committed
194
            unsigned char str2[1024] = {};
195
            memcpy(str2, text.toLatin1().data(), str.length());
Lorenz Meier's avatar
Lorenz Meier committed
196 197
            SpeakString(str2);
            res = true;
198 199

#else
200
            // Make sure there isn't an unused variable warning when speech output is disabled
201
            Q_UNUSED(text);
202
#endif
Lorenz Meier's avatar
Lorenz Meier committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        }

        return res;
    }

    else
    {
        return false;
    }
}

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
    if (!emergency || !muted)
    {
        // Play alert sound
        beep();
        // Say alert message
        say(text, 2);
        return true;
    }

    else
    {
        return false;
    }
}

void GAudioOutput::notifyPositive()
{
    if (!muted)
    {
        // Use QFile to transform path for all OS
239 240
        // 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
241 242 243 244 245 246 247 248 249 250
        //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
251 252
        // 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
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        //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()
{
    if (!emergency)
    {
        emergency = true;

        // Beep immediately and then start timer
        if (!muted) beep();

        emergencyTimer->start(1500);
        QTimer::singleShot(5000, this, SLOT(stopEmergency()));
    }

    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()
{
    if (emergency)
    {
        emergency = false;
        emergencyTimer->stop();
    }

    return true;
}

void GAudioOutput::beep()
{
    if (!muted)
    {
302
        // FIXME: Re-enable audio beeps
Lorenz Meier's avatar
Lorenz Meier committed
303
        // Use QFile to transform path for all OS
304 305
        //QFile f(QCoreApplication::applicationDirPath() + QString("/files/audio/alert.wav"));
        //qDebug() << "FILE:" << f.fileName();
Lorenz Meier's avatar
Lorenz Meier committed
306 307 308 309 310 311 312
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
    }
}

void GAudioOutput::selectFemaleVoice()
{
313
#if defined Q_OS_LINUX && defined QGC_SPEECH_ENABLED
314
    // FIXME: Enable selecting a female voice on all platforms
Lorenz Meier's avatar
Lorenz Meier committed
315 316 317 318 319 320
    //this->voice = register_cmu_us_slt(NULL);
#endif
}

void GAudioOutput::selectMaleVoice()
{
321
#if defined Q_OS_LINUX && defined QGC_SPEECH_ENABLED
322
    // FIXME: Enable selecting a male voice on all platforms
Lorenz Meier's avatar
Lorenz Meier committed
323 324 325 326 327 328
    //this->voice = register_cmu_us_rms(NULL);
#endif
}

QStringList GAudioOutput::listVoices(void)
{
329
    // No voice selection is currently supported, so just return an empty list
Lorenz Meier's avatar
Lorenz Meier committed
330 331 332
    QStringList l;
    return l;
}