GAudioOutput.cc 6.89 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

pixhawk's avatar
pixhawk committed
3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

pixhawk's avatar
pixhawk committed
5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

pixhawk's avatar
pixhawk committed
7
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
8

pixhawk's avatar
pixhawk committed
9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    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.

pixhawk's avatar
pixhawk committed
14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    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
pixhawk's avatar
pixhawk committed
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25 26 27 28 29 30 31 32

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

/**
 * @file
 *   @brief Implementation of audio output
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QApplication>
pixhawk's avatar
pixhawk committed
33
#include <QTemporaryFile>
pixhawk's avatar
pixhawk committed
34 35 36
#include "GAudioOutput.h"
#include "MG.h"

pixhawk's avatar
pixhawk committed
37 38
#include <QDebug>

39 40 41
#ifdef Q_OS_MAC
#include <ApplicationServices/ApplicationServices.h>
#endif
42

43 44
// Speech synthesis is only supported with MSVC compiler
#if _MSC_VER
pixhawk's avatar
pixhawk committed
45
#include <sapi.h>
pixhawk's avatar
pixhawk committed
46 47 48
using System;
using System.Speech.Synthesis;
#endif
pixhawk's avatar
pixhawk committed
49

50 51 52 53 54 55
#ifdef Q_OS_LINUX
extern "C" {
#include <flite/flite.h>
    cst_voice* register_cmu_us_kal(const char* voxdir);
};
#endif
lm's avatar
lm committed
56 57


pixhawk's avatar
pixhawk committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

/**
 * 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),
voiceIndex(0),
emergency(false)
{
82
#ifdef Q_OS_LINUX
pixhawk's avatar
pixhawk committed
83
    flite_init();
84
#endif
85
    // Initialize audio output
pixhawk's avatar
pixhawk committed
86 87 88
    m_media = new Phonon::MediaObject(this);
    Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    createPath(m_media, audioOutput);
89 90

    // Prepare regular emergency signal, will be fired off on calling startEmergency()
pixhawk's avatar
pixhawk committed
91 92 93 94 95 96 97 98 99
    emergencyTimer = new QTimer();
    connect(emergencyTimer, SIGNAL(timeout()), this, SLOT(beep()));

    switch (voiceIndex)
    {
    case 0:
        selectFemaleVoice();
        break;
    default:
100
        selectMaleVoice();
pixhawk's avatar
pixhawk committed
101 102 103 104 105 106
        break;
    }
}

bool GAudioOutput::say(QString text, int severity)
{
107 108
    // TODO Add severity filter
    Q_UNUSED(severity);
pixhawk's avatar
pixhawk committed
109 110 111
    bool res = false;
    if (!emergency)
    {
pixhawk's avatar
pixhawk committed
112

113
        // Speech synthesis is only supported with MSVC compiler
pixhawk's avatar
pixhawk committed
114
#ifdef _MSC_VER
pixhawk's avatar
pixhawk committed
115 116
        SpeechSynthesizer synth = new SpeechSynthesizer();
        synth.SelectVoice("Microsoft Anna");
117 118
        synth.SpeakText(text.toStdString().c_str());
        res = true;
pixhawk's avatar
pixhawk committed
119 120
#endif

121
#ifdef Q_OS_LINUX
pixhawk's avatar
pixhawk committed
122 123 124 125
        QTemporaryFile file;
        file.setFileTemplate("XXXXXX.wav");
        if (file.open())
        {
126
            cst_voice* v = register_cmu_us_kal(NULL);
lm's avatar
lm committed
127
            cst_wave* wav = flite_text_to_wave(text.toStdString().c_str(), v);
pixhawk's avatar
pixhawk committed
128 129 130 131 132 133
            // file.fileName() returns the unique file name
            cst_wave_save(wav, file.fileName().toStdString().c_str(), "riff");
            m_media->setCurrentSource(Phonon::MediaSource(file.fileName().toStdString().c_str()));
            m_media->play();
            res = true;
        }
134 135 136 137 138 139 140 141 142 143
#endif

#ifdef Q_OS_MAC
        // Slashes necessary to have the right start to the sentence
        // copying data prevents SpeakString from reading additional chars
        text = "\\" + text;
        QStdWString str = text.toStdWString();
        unsigned char str2[1024] = {};
        memcpy(str2, text.toAscii().data(), str.length());
        SpeakString(str2);
144
        res = true;
145
#endif
pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152 153 154 155 156 157
    }
    return res;
}

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
    if (!emergency)
    {
        // Play alert sound
158
        beep();
pixhawk's avatar
pixhawk committed
159
        // Say alert message
160 161
        say(text, 2);
        return true;
pixhawk's avatar
pixhawk committed
162 163 164 165 166 167 168
    }
    else
    {
        return false;
    }
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
void GAudioOutput::notifyPositive()
{
    // Use QFile to transform path for all OS
    QFile f(QCoreApplication::applicationDirPath()+QString("/audio/double_notify.wav"));
    m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
    m_media->play();
}

void GAudioOutput::notifyNegative()
{
    // Use QFile to transform path for all OS
    QFile f(QCoreApplication::applicationDirPath()+QString("/audio/flat_notify.wav"));
    m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
    m_media->play();
}

pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192 193 194 195 196
/**
 * 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;
pixhawk's avatar
pixhawk committed
197 198 199
        // Beep immediately and then start timer
        beep();
        emergencyTimer->start(1500);
lm's avatar
lm committed
200
        QTimer::singleShot(5000, this, SLOT(stopEmergency()));
pixhawk's avatar
pixhawk committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    }
    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()
{
pixhawk's avatar
pixhawk committed
223
    // Use QFile to transform path for all OS
224
    QFile f(QCoreApplication::applicationDirPath()+QString("/audio/alert.wav"));
pixhawk's avatar
pixhawk committed
225
    m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
pixhawk's avatar
pixhawk committed
226 227 228 229 230
    m_media->play();
}

void GAudioOutput::selectFemaleVoice()
{
231
#ifdef Q_OS_LINUX
lm's avatar
lm committed
232
    //this->voice = register_cmu_us_slt(NULL);
pixhawk's avatar
pixhawk committed
233 234 235 236 237
#endif
}

void GAudioOutput::selectMaleVoice()
{
238
#ifdef Q_OS_LINUX
lm's avatar
lm committed
239
    //this->voice = register_cmu_us_rms(NULL);
pixhawk's avatar
pixhawk committed
240 241 242
#endif
}

243
/*
pixhawk's avatar
pixhawk committed
244 245
void GAudioOutput::selectNeutralVoice()
{
246
#ifdef Q_OS_LINUX
247
    this->voice = register_cmu_us_awb(NULL);
pixhawk's avatar
pixhawk committed
248
#endif
249
}*/
pixhawk's avatar
pixhawk committed
250

251 252 253
QStringList GAudioOutput::listVoices(void)
{
    QStringList l;
lm's avatar
lm committed
254
#ifdef Q_OS_LINUX2
255 256
    cst_voice *voice;
    const cst_val *v;
pixhawk's avatar
pixhawk committed
257

258

pixhawk's avatar
pixhawk committed
259

260 261 262 263 264 265 266 267
    printf("Voices available: ");
    for (v=flite_voice_list; v; v=val_cdr(v))
    {
        voice = val_voice(val_car(v));
        QString s;
        s.sprintf("%s",voice->name);
        printf("%s",voice->name);
        l.append(s);
pixhawk's avatar
pixhawk committed
268
    }
269 270 271 272 273
    printf("\n");

#endif
    return l;

pixhawk's avatar
pixhawk committed
274
}