Search code examples
c++qttext-to-speech

How to use RHVoice via Speech Dispatcher in Qt


I am trying to use RHVoice in my Qt C++ project on a Linux.

I installed RHVoice, and I struggled with its library RHVoice.h, which is provided by librhvoice-dev package, and so far, it seems like a dead end.

Then I found out that Speech-Dispatcher supports RHVoice module, and Qt Text To Speech supports Speech-Dispatcher, so it seemed like the proper way to use RHVoice in Qt.

But the Qt documentation says:

The speech-dispatcher engine does not support any engine specific parameters.

So I cannot make Speech-Dispatcher use RHVoice through Qt, and my question is basically how to work around this limitation?


Solution

  • One solution to this is to set RHVoice as the default synthesizer for Speech-Dispatcher, which can be done as follows:

    Modify Speech-Dispatcher configuration by modifying the following files:

    /usr/share/speech-dispatcher/conf/speechd.conf
    
    /etc/speech-dispatcher/speechd.conf
    

    If you want to change the configuration per-user only, editing the following file is enough:

    ~/.config/speech-dispatcher/speechd.conf
    

    Change the DefaultModule to rhvoice as follows:

    ...
    # -----OUTPUT MODULES CONFIGURATION-----
    ...
    # The DefaultModule selects which output module is the default.  You
    # must use one of the names of the modules loaded with AddModule.
    
    DefaultModule rhvoice
    

    save and close, and check if it works in your Qt Project, here's an example:

    CMake:

    cmake_minimum_required(VERSION 3.14)
    
    project(RHVoiceSpeechD LANGUAGES CXX)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core TextToSpeech)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core TextToSpeech)
    
    add_executable(RHVoiceSpeechD
      main.cpp
    )
    target_link_libraries(RHVoiceSpeechD Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::TextToSpeech)
    
    install(TARGETS RHVoiceSpeechD
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
    

    main.cpp:

    #include <QCoreApplication>
    #include <QTextToSpeech>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QTextToSpeech *tts = new QTextToSpeech();
    
        //set speechd as the engine if it is not the default engine
        tts->setEngine("speechd");
    
        //test it
        tts->say("I'm using RHVoice as a synthesizer");
    
        return a.exec();
    }
    

    Run this command in a terminal:

    spd-say -o rhvoice "I'm using RHVoice as a synthesizer"
    

    and compare the audio output with the one from your Qt project, they should sound the same if the previous steps worked, the default voice for RHVoice is "Alan".

    Sources: