AudioOutput.cc 3.74 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
#include "AudioOutput.h"

Hugo Vincent's avatar
Hugo Vincent committed
3
#ifdef Q_OS_MAC
4
#include <ApplicationServices/ApplicationServices.h>
Hugo Vincent's avatar
Hugo Vincent committed
5
#else
6 7 8
#include <flite.h>
#include <phonon/mediaobject.h>
#include <QTemporaryFile>
9
#endif
pixhawk's avatar
pixhawk committed
10 11 12

#include <QDebug>

13
#ifndef Q_OS_MAC
pixhawk's avatar
pixhawk committed
14 15 16 17 18 19 20 21 22 23 24 25
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);
};
26
#endif
pixhawk's avatar
pixhawk committed
27

28 29 30 31
AudioOutput::AudioOutput(QString voice, QObject* parent)
    : QObject(parent),
      voice(NULL),
      voiceIndex(0)
pixhawk's avatar
pixhawk committed
32
{
33
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
pixhawk's avatar
pixhawk committed
34 35 36
    flite_init();
#endif

37
    switch (voiceIndex) {
pixhawk's avatar
pixhawk 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 62 63 64 65 66 67 68 69 70 71
    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 AudioOutput::say(QString text, int severity)
{
    // Only give speech output on Linux and MacOS
72 73 74
#if defined(Q_OS_WIN32)
    qDebug() << "Synthesized: " << text << ", NO OUTPUT SUPPORT ON WINDOWS!";
#elif defined(Q_OS_MAC)
75 76 77
    // FIXME, copy string, set callback to free the copy
    SpeakString((const unsigned char*)text.toAscii().data());
    qDebug() << "Synthesized: " << text;
78
#else
pixhawk's avatar
pixhawk committed
79 80
    QTemporaryFile file;
    file.setFileTemplate("XXXXXX.wav");
81
    if (file.open()) {
pixhawk's avatar
pixhawk committed
82 83 84 85
        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");
        Phonon::MediaObject *music =
86 87
            Phonon::createPlayer(Phonon::MusicCategory,
                                 Phonon::MediaSource(file.fileName().toStdString().c_str()));
pixhawk's avatar
pixhawk committed
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
        music->play();
        qDebug() << "Synthesized: " << text << ", tmp file:" << file.fileName().toStdString().c_str();
    }
#endif
    return true;
}

bool AudioOutput::alert(QString text)
{
    // Play alert sound

    // Say alert message
    return say(text, 2);
}

bool AudioOutput::startEmergency()
{
    return false;
}

bool AudioOutput::stopEmergency()
{
    return false;
}

void AudioOutput::selectFemaleVoice()
{
115
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
pixhawk's avatar
pixhawk committed
116 117 118 119 120 121
    this->voice = register_cmu_us_slt(NULL);
#endif
}

void AudioOutput::selectMaleVoice()
{
122
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
pixhawk's avatar
pixhawk committed
123 124 125 126 127 128 129
    this->voice = register_cmu_us_rms(NULL);
#endif
}


void AudioOutput::selectNeutralVoice()
{
130
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
pixhawk's avatar
pixhawk committed
131 132 133 134 135 136 137 138 139
    this->voice = register_cmu_us_awb(NULL);
#endif
}

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
    QStringList AudioOutput::listVoices(void)
    {
Hugo Vincent's avatar
Hugo Vincent committed
140 141
        QStringList l;
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
pixhawk's avatar
pixhawk committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
        cst_voice *voice;
        const cst_val *v;

        /*
        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");
156
        */
pixhawk's avatar
pixhawk committed
157
#endif
158
        return l;
pixhawk's avatar
pixhawk committed
159 160 161 162
    }
#ifdef __cplusplus
}
#endif /* __cplusplus */