GAudioOutput.cc 9.05 KB
Newer Older
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 29 30 31 32
/*=====================================================================

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

#include <QApplication>
33
#include <QSettings>
34 35 36 37 38 39 40 41 42 43 44
#include <QTemporaryFile>
#include "GAudioOutput.h"
#include "MG.h"

#include <QDebug>

#ifdef Q_OS_MAC
#include <ApplicationServices/ApplicationServices.h>
#endif

// Speech synthesis is only supported with MSVC compiler
pixhawk's avatar
pixhawk committed
45
#if _MSC_VER2
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
// Documentation: http://msdn.microsoft.com/en-us/library/ee125082%28v=VS.85%29.aspx
#define _ATL_APARTMENT_THREADED

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#include <sapi.h>

//using System;
//using System.Speech.Synthesis;
#endif

#ifdef Q_OS_LINUX
extern "C" {
#include <flite/flite.h>
    cst_voice* register_cmu_us_kal(const char* voxdir);
};
#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;
LM's avatar
LM committed
80 81
    if(_instance == 0)
    {
82 83 84 85 86 87 88 89
        _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;
}

90 91
#define QGC_GAUDIOOUTPUT_KEY QString("QGC_AUDIOOUTPUT_")

92
GAudioOutput::GAudioOutput(QObject* parent) : QObject(parent),
93 94 95
    voiceIndex(0),
    emergency(false),
    muted(false)
96
{
97 98 99 100 101 102
    // Load settings
    QSettings settings;
    settings.sync();
    muted = settings.value(QGC_GAUDIOOUTPUT_KEY+"muted", muted).toBool();


103 104 105 106
#ifdef Q_OS_LINUX
    flite_init();
#endif

107 108 109
#if _MSC_VER2

    ISpVoice * pVoice = NULL;
LM's avatar
LM committed
110 111
    if (FAILED(::CoInitialize(NULL)))
    {
112
        qDebug("Creating COM object for audio output failed!");
LM's avatar
LM committed
113 114 115
    }
    else
    {
116 117

        HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);
LM's avatar
LM committed
118 119
        if( SUCCEEDED( hr ) )
        {
120 121 122 123
            hr = pVoice->Speak(L"Hello world", 0, NULL);
            pVoice->Release();
            pVoice = NULL;
        }
124 125 126
    }
#endif
    // Initialize audio output
127 128 129
    //m_media = new Phonon::MediaObject(this);
    //Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    //createPath(m_media, audioOutput);
130 131 132 133 134

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

135
    switch (voiceIndex) {
136 137 138 139 140 141 142 143 144
    case 0:
        selectFemaleVoice();
        break;
    default:
        selectMaleVoice();
        break;
    }
}

pixhawk's avatar
pixhawk committed
145 146 147 148 149 150
//GAudioOutput::~GAudioOutput()
//{
//#ifdef _MSC_VER2
//    ::CoUninitialize();
//#endif
//}
151

152 153
void GAudioOutput::mute(bool mute)
{
LM's avatar
LM committed
154 155
    if (mute != muted)
    {
156 157 158 159 160 161
        this->muted = mute;
        QSettings settings;
        settings.setValue(QGC_GAUDIOOUTPUT_KEY+"muted", this->muted);
        settings.sync();
        emit mutedChanged(muted);
    }
162 163 164 165 166 167 168
}

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

169 170
bool GAudioOutput::say(QString text, int severity)
{
LM's avatar
LM committed
171 172
    if (!muted)
    {
173 174 175
        // TODO Add severity filter
        Q_UNUSED(severity);
        bool res = false;
LM's avatar
LM committed
176 177
        if (!emergency)
        {
178 179

            // Speech synthesis is only supported with MSVC compiler
180
#ifdef _MSC_VER2
181 182 183 184
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.SelectVoice("Microsoft Anna");
            synth.SpeakText(text.toStdString().c_str());
            res = true;
185 186 187
#endif

#ifdef Q_OS_LINUX
188 189 190 191 192 193 194
            QTemporaryFile file;
            file.setFileTemplate("XXXXXX.wav");
            if (file.open()) {
                cst_voice* v = register_cmu_us_kal(NULL);
                cst_wave* wav = flite_text_to_wave(text.toStdString().c_str(), v);
                // file.fileName() returns the unique file name
                cst_wave_save(wav, file.fileName().toStdString().c_str(), "riff");
195 196
                //m_media->setCurrentSource(Phonon::MediaSource(file.fileName().toStdString().c_str()));
                //m_media->play();
197 198
                res = true;
            }
199 200 201
#endif

#ifdef Q_OS_MAC
202 203 204 205 206 207 208 209
            // 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);
            res = true;
210
#endif
211 212
        }
        return res;
LM's avatar
LM committed
213 214 215
    }
    else
    {
216 217 218
        return false;
    }
}
219 220 221 222 223 224

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
LM's avatar
LM committed
225 226
    if (!emergency || !muted)
    {
227 228 229 230 231
        // Play alert sound
        beep();
        // Say alert message
        say(text, 2);
        return true;
LM's avatar
LM committed
232 233 234
    }
    else
    {
235 236 237 238 239 240
        return false;
    }
}

void GAudioOutput::notifyPositive()
{
LM's avatar
LM committed
241 242
    if (!muted)
    {
243
        // Use QFile to transform path for all OS
LM's avatar
LM committed
244
        QFile f(QCoreApplication::applicationDirPath()+QString("/files/audio/double_notify.wav"));
245 246
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
247
    }
248
}
249 250 251

void GAudioOutput::notifyNegative()
{
LM's avatar
LM committed
252 253
    if (!muted)
    {
254
        // Use QFile to transform path for all OS
LM's avatar
LM committed
255
        QFile f(QCoreApplication::applicationDirPath()+QString("/files/audio/flat_notify.wav"));
256 257
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
258
    }
259
}
260 261 262 263 264 265 266 267 268 269

/**
 * 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()
{
LM's avatar
LM committed
270 271
    if (!emergency)
    {
272 273
        emergency = true;
        // Beep immediately and then start timer
274
        if (!muted) beep();
275 276 277 278 279 280 281 282 283 284 285 286 287 288
        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()
{
289
    if (emergency) {
290 291 292 293 294 295 296 297
        emergency = false;
        emergencyTimer->stop();
    }
    return true;
}

void GAudioOutput::beep()
{
LM's avatar
LM committed
298 299
    if (!muted)
    {
300
        // Use QFile to transform path for all OS
LM's avatar
LM committed
301
        QFile f(QCoreApplication::applicationDirPath()+QString("/files/audio/alert.wav"));
302
        qDebug() << "FILE:" << f.fileName();
303 304
        //m_media->setCurrentSource(Phonon::MediaSource(f.fileName().toStdString().c_str()));
        //m_media->play();
305
    }
306
}
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

void GAudioOutput::selectFemaleVoice()
{
#ifdef Q_OS_LINUX
    //this->voice = register_cmu_us_slt(NULL);
#endif
}

void GAudioOutput::selectMaleVoice()
{
#ifdef Q_OS_LINUX
    //this->voice = register_cmu_us_rms(NULL);
#endif
}

/*
void GAudioOutput::selectNeutralVoice()
{
#ifdef Q_OS_LINUX
    this->voice = register_cmu_us_awb(NULL);
#endif
}*/

QStringList GAudioOutput::listVoices(void)
{
    QStringList l;
#ifdef Q_OS_LINUX2
    cst_voice *voice;
    const cst_val *v;



    printf("Voices available: ");
340
    for (v=flite_voice_list; v; v=val_cdr(v)) {
341 342 343 344 345 346 347 348 349 350 351 352
        voice = val_voice(val_car(v));
        QString s;
        s.sprintf("%s",voice->name);
        printf("%s",voice->name);
        l.append(s);
    }
    printf("\n");

#endif
    return l;

}