binding_to_external_libraries.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. .. _doc_binding_to_external_libraries:
  2. Binding to external libraries
  3. =============================
  4. Modules
  5. -------
  6. The Summator example in :ref:`doc_custom_modules_in_c++` is great for small,
  7. custom modules, but what if you want to use a larger, external library?
  8. Let's look at an example using `Festival <http://www.cstr.ed.ac.uk/projects/festival/>`_,
  9. a speech synthesis (text-to-speech) library written in C++.
  10. To bind to an external library, set up a module directory similar to the Summator example:
  11. .. code-block:: none
  12. godot/modules/tts/
  13. Next, you will create a header file with a TTS class:
  14. .. code-block:: cpp
  15. /* tts.h */
  16. #ifndef GODOT_TTS_H
  17. #define GODOT_TTS_H
  18. #include "core/object/ref_counted.h"
  19. class TTS : public RefCounted {
  20. GDCLASS(TTS, RefCounted);
  21. protected:
  22. static void _bind_methods();
  23. public:
  24. bool say_text(String p_txt);
  25. TTS();
  26. };
  27. #endif // GODOT_TTS_H
  28. And then you'll add the cpp file.
  29. .. code-block:: cpp
  30. /* tts.cpp */
  31. #include "tts.h"
  32. #include <festival.h>
  33. bool TTS::say_text(String p_txt) {
  34. //convert Godot String to Godot CharString to C string
  35. return festival_say_text(p_txt.ascii().get_data());
  36. }
  37. void TTS::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("say_text", "txt"), &TTS::say_text);
  39. }
  40. TTS::TTS() {
  41. festival_initialize(true, 210000); //not the best way to do it as this should only ever be called once.
  42. }
  43. Just as before, the new class needs to be registered somehow, so two more files
  44. need to be created:
  45. .. code-block:: none
  46. register_types.h
  47. register_types.cpp
  48. .. important::
  49. These files must be in the top-level folder of your module (next to your
  50. ``SCsub`` and ``config.py`` files) for the module to be registered properly.
  51. These files should contain the following:
  52. .. code-block:: cpp
  53. /* register_types.h */
  54. void register_tts_types();
  55. void unregister_tts_types();
  56. /* yes, the word in the middle must be the same as the module folder name */
  57. .. code-block:: cpp
  58. /* register_types.cpp */
  59. #include "register_types.h"
  60. #include "core/object/class_db.h"
  61. #include "tts.h"
  62. void register_tts_types() {
  63. ClassDB::register_class<TTS>();
  64. }
  65. void unregister_tts_types() {
  66. // Nothing to do here in this example.
  67. }
  68. Next, you need to create a ``SCsub`` file so the build system compiles
  69. this module:
  70. .. code-block:: python
  71. # SCsub
  72. Import('env')
  73. env_tts = env.Clone()
  74. env_tts.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
  75. You'll need to install the external library on your machine to get the .a library files. See the library's official
  76. documentation for specific instructions on how to do this for your operation system. We've included the
  77. installation commands for Linux below, for reference.
  78. .. code-block:: shell
  79. sudo apt-get install festival festival-dev <-- Installs festival and speech_tools libraries
  80. apt-cache search festvox-* <-- Displays list of voice packages
  81. sudo apt-get install festvox-don festvox-rablpc16k festvox-kallpc16k festvox-kdlpc16k <-- Installs voices
  82. .. important::
  83. The voices that Festival uses (and any other potential external/3rd-party
  84. resource) all have varying licenses and terms of use; some (if not most) of them may be
  85. be problematic with Godot, even if the Festival Library itself is MIT License compatible.
  86. Please be sure to check the licenses and terms of use.
  87. The external library will also need to be installed inside your module to make the source
  88. files accessible to the compiler, while also keeping the module code self-contained. The
  89. festival and speech_tools libraries can be installed from the modules/tts/ directory via
  90. git using the following commands:
  91. .. code-block:: shell
  92. git clone https://github.com/festvox/festival
  93. git clone https://github.com/festvox/speech_tools
  94. If you don't want the external repository source files committed to your repository, you
  95. can link to them instead by adding them as submodules (from within the modules/tts/ directory), as seen below:
  96. .. code-block:: shell
  97. git submodule add https://github.com/festvox/festival
  98. git submodule add https://github.com/festvox/speech_tools
  99. .. important::
  100. Please note that Git submodules are not used in the Godot repository. If
  101. you are developing a module to be merged into the main Godot repository, you should not
  102. use submodules. If your module doesn't get merged in, you can always try to implement
  103. the external library as a GDNative C++ plugin.
  104. To add include directories for the compiler to look at you can append it to the
  105. environment's paths:
  106. .. code-block:: python
  107. # These paths are relative to /modules/tts/
  108. env_tts.Append(CPPPATH=["speech_tools/include", "festival/src/include"])
  109. # LIBPATH and LIBS need to be set on the real "env" (not the clone)
  110. # to link the specified libraries to the Godot executable.
  111. # This is a path relative to /modules/tts/ where your .a libraries reside.
  112. # If you are compiling the module externally (not in the godot source tree),
  113. # these will need to be full paths.
  114. env.Append(LIBPATH=['libpath'])
  115. # Check with the documentation of the external library to see which library
  116. # files should be included/linked.
  117. env.Append(LIBS=['Festival', 'estools', 'estbase', 'eststring'])
  118. If you want to add custom compiler flags when building your module, you need to clone
  119. `env` first, so it won't add those flags to whole Godot build (which can cause errors).
  120. Example `SCsub` with custom flags:
  121. .. code-block:: python
  122. # SCsub
  123. Import('env')
  124. env_tts = env.Clone()
  125. env_tts.add_source_files(env.modules_sources, "*.cpp")
  126. # Append CCFLAGS flags for both C and C++ code.
  127. env_tts.Append(CCFLAGS=['-O2'])
  128. # If you need to, you can:
  129. # - Append CFLAGS for C code only.
  130. # - Append CXXFLAGS for C++ code only.
  131. The final module should look like this:
  132. .. code-block:: none
  133. godot/modules/tts/festival/
  134. godot/modules/tts/libpath/libestbase.a
  135. godot/modules/tts/libpath/libestools.a
  136. godot/modules/tts/libpath/libeststring.a
  137. godot/modules/tts/libpath/libFestival.a
  138. godot/modules/tts/speech_tools/
  139. godot/modules/tts/config.py
  140. godot/modules/tts/tts.h
  141. godot/modules/tts/tts.cpp
  142. godot/modules/tts/register_types.h
  143. godot/modules/tts/register_types.cpp
  144. godot/modules/tts/SCsub
  145. Using the module
  146. ----------------
  147. You can now use your newly created module from any script:
  148. ::
  149. var t = TTS.new()
  150. var script = "Hello world. This is a test!"
  151. var is_spoken = t.say_text(script)
  152. print('is_spoken: ', is_spoken)
  153. And the output will be ``is_spoken: True`` if the text is spoken.