binding_to_external_libraries.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, a speech synthesis (text-to-speech)
  9. library written in C++.
  10. To bind to an external library, set up a module directory similar to the Summator example:
  11. ::
  12. godot/modules/tts/
  13. Next, you will create a header file with a simple TTS class:
  14. .. code:: cpp
  15. /* tts.h */
  16. #ifndef GODOT_TTS_H
  17. #define GODOT_TTS_H
  18. #include "core/reference.h"
  19. class TTS : public Reference {
  20. GDCLASS(TTS, Reference);
  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:: 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. ::
  46. register_types.h
  47. register_types.cpp
  48. With the following contents:
  49. .. code:: cpp
  50. /* register_types.h */
  51. void register_tts_types();
  52. void unregister_tts_types();
  53. /* yes, the word in the middle must be the same as the module folder name */
  54. .. code:: cpp
  55. /* register_types.cpp */
  56. #include "register_types.h"
  57. #include "core/class_db.h"
  58. #include "tts.h"
  59. void register_tts_types() {
  60. ClassDB::register_class<TTS>();
  61. }
  62. void unregister_tts_types() {
  63. // Nothing to do here in this example.
  64. }
  65. Next, you need to create a ``SCsub`` file so the build system compiles
  66. this module:
  67. .. code:: python
  68. # SCsub
  69. Import('env')
  70. env_tts = env.Clone()
  71. env_tts.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
  72. You'll need to install the external library on your machine to get the .a library files. See the library's official
  73. documentation for specific instructions on how to do this for your operation system. We've included the
  74. installation commands for Linux below, for reference.
  75. ::
  76. sudo apt-get install festival festival-dev <-- Installs festival and speech_tools libraries
  77. apt-cache search festvox-* <-- Displays list of voice packages
  78. sudo apt-get install festvox-don festvox-rablpc16k festvox-kallpc16k festvox-kdlpc16k <-- Installs voices
  79. .. note::
  80. **Important:** The voices that Festival uses (and any other potential external/3rd-party
  81. resource) all have varying licenses and terms of use; some (if not most) of them may be
  82. be problematic with Godot, even if the Festival Library itself is MIT License compatible.
  83. Please be sure to check the licenses and terms of use.
  84. The external library will also need to be installed inside your module to make the source
  85. files accessible to the compiler, while also keeping the module code self-contained. The
  86. festival and speech_tools libraries can be installed from the modules/tts/ directory via
  87. git using the following commands:
  88. ::
  89. git clone https://github.com/festvox/festival
  90. git clone https://github.com/festvox/speech_tools
  91. If you don't want the external repository source files committed to your repository, you
  92. can link to them instead by adding them as submodules (from within the modules/tts/ directory), as seen below:
  93. ::
  94. git submodule add https://github.com/festvox/festival
  95. git submodule add https://github.com/festvox/speech_tools
  96. .. note::
  97. **Important:** Please note that Git submodules are not used in the Godot repository. If
  98. you are developing a module to be merged into the main Godot repository, you should not
  99. use submodules. If your module doesn't get merged in, you can always try to implement
  100. the external library as a GDNative C++ plugin.
  101. To add include directories for the compiler to look at you can append it to the
  102. environment's paths:
  103. .. code:: python
  104. env_tts.Append(CPPPATH=["speech_tools/include", "festival/src/include"]) # this is a path relative to /modules/tts/
  105. # http://www.cstr.ed.ac.uk/projects/festival/manual/festival_28.html#SEC132 <-- Festival library documentation
  106. env_tts.Append(LIBPATH=['libpath']) # this is a path relative to /modules/tts/ where your .a library files reside
  107. # You should check with the documentation of the external library to see which library files should be included/linked
  108. env_tts.Append(LIBS=['Festival', 'estools', 'estbase', 'eststring'])
  109. If you want to add custom compiler flags when building your module, you need to clone
  110. `env` first, so it won't add those flags to whole Godot build (which can cause errors).
  111. Example `SCsub` with custom flags:
  112. .. code:: python
  113. # SCsub
  114. Import('env')
  115. env_tts = env.Clone()
  116. env_tts.add_source_files(env.modules_sources, "*.cpp")
  117. env_tts.Append(CCFLAGS=['-O2']) # Flags for C and C++ code
  118. env_tts.Append(CXXFLAGS=['-std=c++11']) # Flags for C++ code only
  119. The final module should look like this:
  120. ::
  121. godot/modules/tts/festival/
  122. godot/modules/tts/libpath/libestbase.a
  123. godot/modules/tts/libpath/libestools.a
  124. godot/modules/tts/libpath/libeststring.a
  125. godot/modules/tts/libpath/libFestival.a
  126. godot/modules/tts/speech_tools/
  127. godot/modules/tts/config.py
  128. godot/modules/tts/tts.h
  129. godot/modules/tts/tts.cpp
  130. godot/modules/tts/register_types.h
  131. godot/modules/tts/register_types.cpp
  132. godot/modules/tts/SCsub
  133. Using the module
  134. ----------------
  135. You can now use your newly created module from any script:
  136. ::
  137. var t = TTS.new()
  138. var script = "Hello world. This is a test!"
  139. var is_spoken = t.say_text(script)
  140. print('is_spoken: ', is_spoken)
  141. And the output will be ``is_spoken: True`` if the text is spoken.