GAudioOutput.cc 6.7 KB
Newer Older
pixhawk's avatar
pixhawk 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK 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.

    PIXHAWK 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 PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

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

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

#include <QApplication>
#include "GAudioOutput.h"
#include "MG.h"
#include <flite.h>

#include <Phonon>
#include <QTemporaryFile>

#include <QDebug>

extern "C" {
#include <cmu_us_awb/voxdefs.h>
    //#include <cmu_us_slt/voxdefs.h>
    //cst_voice *REGISTER_VOX(const char *voxdir);
    //void UNREGISTER_VOX(cst_voice *vox);
    cst_voice *register_cmu_us_awb(const char *voxdir);
    void unregister_cmu_us_awb(cst_voice *vox);
    cst_voice *register_cmu_us_slt(const char *voxdir);
    void unregister_cmu_us_slt(cst_voice *vox);
    cst_voice *register_cmu_us_rms(const char *voxdir);
    void unregister_cmu_us_rms(cst_voice *vox);
};

/**
 * 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),
voice(NULL),
voiceIndex(0),
emergency(false)
{
#ifndef _WIN32
    flite_init();
    m_media = new Phonon::MediaObject(this);
    Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    createPath(m_media, audioOutput);
    emergencyTimer = new QTimer();
    connect(emergencyTimer, SIGNAL(timeout()), this, SLOT(beep()));
#endif

    switch (voiceIndex)
    {
    case 0:
        selectFemaleVoice();
        break;
    case 1:
        selectMaleVoice();
        break;
    default:
        selectNeutralVoice();
        break;
    }

    /*
    // List available voices
    QStringList voices = listVoices();
    foreach (QString s, voices)
    {
        qDebug() << "VOICE: " << s;
    }

    if (voice.length() > 0)
    {
        //this->voice = flite_voice_select(voice.toStdString().c_str());
    }
    else
    {
        // Take default voice
        //this->voice = flite_voice_select("awb");
    }
    */


}

bool GAudioOutput::say(QString text, int severity)
{
    bool res = false;
    if (!emergency)
    {
        // Only give speech output on Linux and MacOS
#ifndef _WIN32
        QTemporaryFile file;
        file.setFileTemplate("XXXXXX.wav");
        if (file.open())
        {
            cst_wave* wav = flite_text_to_wave(text.toStdString().c_str(), this->voice);
            // 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()));
            qDebug() << "TYPE:" << m_media->currentSource().type();
            m_media->play();
            qDebug() << "Synthesized: " << text << ", tmp file:" << file.fileName().toStdString().c_str();
            res = true;
        }
#else
        qDebug() << "Synthesized: " << text << ", NO OUTPUT SUPPORT ON WINDOWS!";
#endif
    }
    return res;
}

/**
 * @param text This message will be played after the alert beep
 */
bool GAudioOutput::alert(QString text)
{
    if (!emergency)
    {
        // Play alert sound
        m_media->setCurrentSource(Phonon::MediaSource(QString("alert.wav").toStdString().c_str()));
        qDebug() << "FILENAME:" << m_media->currentSource().fileName();
        qDebug() << "TYPE:" << m_media->currentSource().type();
        qDebug() << QString("alert.wav").toStdString().c_str();
        m_media->play();
        m_media->setCurrentSource(Phonon::MediaSource(QString("alert.wav").toStdString().c_str()));
        m_media->play();
        // Say alert message
        return true;//say(text, 2);
    }
    else
    {
        return false;
    }
}

/**
 * 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;
        emergencyTimer->start(1600);
    }
    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()
{
    m_media->setCurrentSource(Phonon::MediaSource(QString("alert.wav").toStdString().c_str()));
    m_media->play();
}

void GAudioOutput::selectFemaleVoice()
{
#ifndef _WIN32
    this->voice = register_cmu_us_slt(NULL);
#endif
}

void GAudioOutput::selectMaleVoice()
{
#ifndef _WIN32
    this->voice = register_cmu_us_rms(NULL);
#endif
}


void GAudioOutput::selectNeutralVoice()
{
#ifndef _WIN32
    this->voice = register_cmu_us_awb(NULL);
#endif
}

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
    QStringList GAudioOutput::listVoices(void)
    {


        cst_voice *voice;
        const cst_val *v;
        QStringList l;

#ifndef _WIN32
        /*
        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);
        }
        printf("\n");
*/
#endif
        return l;

    }
#ifdef __cplusplus
}
#endif /* __cplusplus */