Verknüpfung mit externen Bibliotheken¶
Module¶
Das Summator-Beispiel in doc_custom_modules_in_c ++ eignet sich hervorragend für kleine, benutzerdefinierte Module. Was ist jedoch, wenn Sie eine größere externe Bibliothek verwenden möchten? Schauen wir uns ein Beispiel mit Festival an, einer in C ++ geschriebenen Sprachsynthesebibliothek (Text-to-Speech).
Richten Sie zum Binden an eine externe Bibliothek ein Modulverzeichnis ein, das dem Summator-Beispiel ähnelt:
godot/modules/tts/
Als nächstes erstellen Sie eine Header-Datei mit einer einfachen TTS-Klasse:
/* tts.h */
#ifndef GODOT_TTS_H
#define GODOT_TTS_H
#include "core/reference.h"
class TTS : public Reference {
GDCLASS(TTS, Reference);
protected:
static void _bind_methods();
public:
bool say_text(String p_txt);
TTS();
};
#endif // GODOT_TTS_H
Und dann fügen Sie die cpp-Datei hinzu.
/* tts.cpp */
#include "tts.h"
#include <festival.h>
bool TTS::say_text(String p_txt) {
//convert Godot String to Godot CharString to C string
return festival_say_text(p_txt.ascii().get_data());
}
void TTS::_bind_methods() {
ClassDB::bind_method(D_METHOD("say_text", "txt"), &TTS::say_text);
}
TTS::TTS() {
festival_initialize(true, 210000); //not the best way to do it as this should only ever be called once.
}
Nach wie vor muss die neue Klasse irgendwie registriert werden, sodass zwei weitere Dateien erstellt werden müssen:
register_types.h
register_types.cpp
Wichtig
Diese Dateien müssen sich im obersten Ordner Ihres Moduls befinden (neben Ihren Dateien SCsub
und config.py
), damit das Modul ordnungsgemäß registriert werden kann.
Diese Dateien sollten Folgendes enthalten:
/* register_types.h */
void register_tts_types();
void unregister_tts_types();
/* yes, the word in the middle must be the same as the module folder name */
/* register_types.cpp */
#include "register_types.h"
#include "core/class_db.h"
#include "tts.h"
void register_tts_types() {
ClassDB::register_class<TTS>();
}
void unregister_tts_types() {
// Nothing to do here in this example.
}
Als nächstes muss eine SCsub
Datei erstellt werden, damit das Build-System dieses Modul kompiliert:
# SCsub
Import('env')
env_tts = env.Clone()
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.
sudo apt-get install festival festival-dev <-- Installs festival and speech_tools libraries
apt-cache search festvox-* <-- Displays list of voice packages
sudo apt-get install festvox-don festvox-rablpc16k festvox-kallpc16k festvox-kdlpc16k <-- Installs voices
Wichtig
Die Stimmen, die Festival verwendet (und alle anderen potenziellen externen Ressourcen/Ressourcen von Drittanbietern), haben unterschiedliche Lizenzen und Nutzungsbedingungen. Einige (wenn nicht die meisten) von ihnen können mit Godot problematisch sein, selbst wenn die Festivalbibliothek selbst mit der MIT-Lizenz kompatibel ist. Bitte überprüfen Sie unbedingt die Lizenzen und Nutzungsbedingungen.
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:
git clone https://github.com/festvox/festival
git clone https://github.com/festvox/speech_tools
Wenn Sie nicht möchten, dass die externen Repository-Quelldateien in Ihr Repository übernommen werden, können Sie stattdessen eine Verknüpfung zu ihnen herstellen, indem Sie sie als Submodule (aus dem Verzeichnis modules/tts/) hinzufügen (siehe unten):
git submodule add https://github.com/festvox/festival
git submodule add https://github.com/festvox/speech_tools
Wichtig
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 GDNative C++ plugin.
Um Include-Verzeichnisse hinzuzufügen, die der Compiler anzeigen kann, können Sie sie an die Pfade der Umgebung anhängen:
# These paths are relative to /modules/tts/
env_tts.Append(CPPPATH=["speech_tools/include", "festival/src/include"])
# LIBPATH and LIBS need to be set on the real "env" (not the clone)
# to link the specified libraries to the Godot executable.
# This is a path relative to /modules/tts/ where your .a libraries reside.
# If you are compiling the module externally (not in the godot source tree),
# these will need to be full paths.
env.Append(LIBPATH=['libpath'])
# Check with the documentation of the external library to see which library
# files should be included/linked.
env.Append(LIBS=['Festival', 'estools', 'estbase', 'eststring'])
Wenn Sie beim Erstellen Ihres Moduls benutzerdefinierte Compiler-Flags hinzufügen möchten, müssen Sie zuerst env klonen, damit diese Flags nicht zum gesamten Godot-Build hinzugefügt werden (was zu Fehlern führen kann). Beispiel SCsub mit benutzerdefinierten Flags:
# SCsub
Import('env')
env_tts = env.Clone()
env_tts.add_source_files(env.modules_sources, "*.cpp")
# Append CCFLAGS flags for both C and C++ code.
env_tts.Append(CCFLAGS=['-O2'])
# If you need to, you can:
# - Append CFLAGS for C code only.
# - Append CXXFLAGS for C++ code only.
Des endgültige Modul sollte wie folgt aussehen:
godot/modules/tts/festival/
godot/modules/tts/libpath/libestbase.a
godot/modules/tts/libpath/libestools.a
godot/modules/tts/libpath/libeststring.a
godot/modules/tts/libpath/libFestival.a
godot/modules/tts/speech_tools/
godot/modules/tts/config.py
godot/modules/tts/tts.h
godot/modules/tts/tts.cpp
godot/modules/tts/register_types.h
godot/modules/tts/register_types.cpp
godot/modules/tts/SCsub
Modul verwenden¶
Sie können Ihr neu erstelltes Modul jetzt in jedem Skript verwenden:
var t = TTS.new()
var script = "Hello world. This is a test!"
var is_spoken = t.say_text(script)
print('is_spoken: ', is_spoken)
Und die Ausgabe wird is_spoken: True
sein, wenn der Text gesprochen wird.