Binding to external libraries

Modules

The Summator example in Custom modules in C++ is great for small, custom modules, but what if you want to use a larger, external library? Let’s look at an example using Festival, a speech synthesis (text-to-speech) library written in C++.

To bind to an external library, set up a module directory similar to the Summator example:

  1. godot/modules/tts/

Next, you will create a header file with a TTS class:

  1. /* tts.h */
  2. #ifndef GODOT_TTS_H
  3. #define GODOT_TTS_H
  4. #include "core/object/ref_counted.h"
  5. class TTS : public RefCounted {
  6. GDCLASS(TTS, RefCounted);
  7. protected:
  8. static void _bind_methods();
  9. public:
  10. bool say_text(String p_txt);
  11. TTS();
  12. };
  13. #endif // GODOT_TTS_H

And then you’ll add the cpp file.

  1. /* tts.cpp */
  2. #include "tts.h"
  3. #include <festival.h>
  4. bool TTS::say_text(String p_txt) {
  5. //convert Godot String to Godot CharString to C string
  6. return festival_say_text(p_txt.ascii().get_data());
  7. }
  8. void TTS::_bind_methods() {
  9. ClassDB::bind_method(D_METHOD("say_text", "txt"), &TTS::say_text);
  10. }
  11. TTS::TTS() {
  12. festival_initialize(true, 210000); //not the best way to do it as this should only ever be called once.
  13. }

Just as before, the new class needs to be registered somehow, so two more files need to be created:

  1. register_types.h
  2. register_types.cpp

Important

These files must be in the top-level folder of your module (next to your SCsub and config.py files) for the module to be registered properly.

These files should contain the following:

  1. /* register_types.h */
  2. void initialize_tts_module(ModuleInitializationLevel p_level);
  3. void uninitialize_tts_module(ModuleInitializationLevel p_level);
  4. /* yes, the word in the middle must be the same as the module folder name */
  1. /* register_types.cpp */
  2. #include "register_types.h"
  3. #include "core/object/class_db.h"
  4. #include "tts.h"
  5. void initialize_tts_module(ModuleInitializationLevel p_level) {
  6. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  7. return;
  8. }
  9. ClassDB::register_class<TTS>();
  10. }
  11. void uninitialize_tts_module(ModuleInitializationLevel p_level) {
  12. // Nothing to do here in this example.
  13. }

Next, you need to create an SCsub file so the build system compiles this module:

  1. # SCsub
  2. Import('env')
  3. env_tts = env.Clone()
  4. env_tts.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build

You’ll need to install the external library on your machine to get the .a library files. See the library’s official documentation for specific instructions on how to do this for your operation system. We’ve included the installation commands for Linux below, for reference.

  1. sudo apt-get install festival festival-dev <-- Installs festival and speech_tools libraries
  2. apt-cache search festvox-* <-- Displays list of voice packages
  3. sudo apt-get install festvox-don festvox-rablpc16k festvox-kallpc16k festvox-kdlpc16k <-- Installs voices

Important

The voices that Festival uses (and any other potential external/3rd-party resource) all have varying licenses and terms of use; some (if not most) of them may be be problematic with Godot, even if the Festival Library itself is MIT License compatible. Please be sure to check the licenses and terms of use.

The external library will also need to be installed inside your module to make the source files accessible to the compiler, while also keeping the module code self-contained. The festival and speech_tools libraries can be installed from the modules/tts/ directory via git using the following commands:

  1. git clone https://github.com/festvox/festival
  2. git clone https://github.com/festvox/speech_tools

If you don’t want the external repository source files committed to your repository, you can link to them instead by adding them as submodules (from within the modules/tts/ directory), as seen below:

  1. git submodule add https://github.com/festvox/festival
  2. git submodule add https://github.com/festvox/speech_tools

Important

Please note that Git submodules are not used in the Godot repository. If you are developing a module to be merged into the main Godot repository, you should not use submodules. If your module doesn’t get merged in, you can always try to implement the external library as a GDExtension.

To add include directories for the compiler to look at you can append it to the environment’s paths:

  1. # These paths are relative to /modules/tts/
  2. env_tts.Append(CPPPATH=["speech_tools/include", "festival/src/include"])
  3. # LIBPATH and LIBS need to be set on the real "env" (not the clone)
  4. # to link the specified libraries to the Godot executable.
  5. # This is a path relative to /modules/tts/ where your .a libraries reside.
  6. # If you are compiling the module externally (not in the godot source tree),
  7. # these will need to be full paths.
  8. env.Append(LIBPATH=['libpath'])
  9. # Check with the documentation of the external library to see which library
  10. # files should be included/linked.
  11. env.Append(LIBS=['Festival', 'estools', 'estbase', 'eststring'])

If you want to add custom compiler flags when building your module, you need to clone env first, so it won’t add those flags to whole Godot build (which can cause errors). Example SCsub with custom flags:

  1. # SCsub
  2. Import('env')
  3. env_tts = env.Clone()
  4. env_tts.add_source_files(env.modules_sources, "*.cpp")
  5. # Append CCFLAGS flags for both C and C++ code.
  6. env_tts.Append(CCFLAGS=['-O2'])
  7. # If you need to, you can:
  8. # - Append CFLAGS for C code only.
  9. # - Append CXXFLAGS for C++ code only.

The final module should look like this:

  1. godot/modules/tts/festival/
  2. godot/modules/tts/libpath/libestbase.a
  3. godot/modules/tts/libpath/libestools.a
  4. godot/modules/tts/libpath/libeststring.a
  5. godot/modules/tts/libpath/libFestival.a
  6. godot/modules/tts/speech_tools/
  7. godot/modules/tts/config.py
  8. godot/modules/tts/tts.h
  9. godot/modules/tts/tts.cpp
  10. godot/modules/tts/register_types.h
  11. godot/modules/tts/register_types.cpp
  12. godot/modules/tts/SCsub

Using the module

You can now use your newly created module from any script:

  1. var t = TTS.new()
  2. var script = "Hello world. This is a test!"
  3. var is_spoken = t.say_text(script)
  4. print('is_spoken: ', is_spoken)

And the output will be is_spoken: True if the text is spoken.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next