-
Notifications
You must be signed in to change notification settings - Fork 554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CMake option to disable/enable TTS support #500
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
include_directories(${CMAKE_SOURCE_DIR}) | ||
add_library(sherpa-onnx-c-api c-api.cc) | ||
|
||
if(SHERPA_ONNX_ENABLE_TTS) | ||
target_sources(sherpa-onnx-c-api PRIVATE c-api-tts.cc) | ||
endif() | ||
|
||
target_link_libraries(sherpa-onnx-c-api sherpa-onnx-core) | ||
|
||
if(BUILD_SHARED_LIBS) | ||
target_compile_definitions(sherpa-onnx-c-api PRIVATE SHERPA_ONNX_BUILD_SHARED_LIBS=1) | ||
target_compile_definitions(sherpa-onnx-c-api PRIVATE SHERPA_ONNX_BUILD_MAIN_LIB=1) | ||
endif() | ||
|
||
if(SHERPA_ONNX_ENABLE_TTS) | ||
target_compile_definitions(sherpa-onnx-c-api PUBLIC SHERPA_ONNX_ENABLE_TTS=1) | ||
endif() | ||
|
||
install(TARGETS sherpa-onnx-c-api DESTINATION lib) | ||
|
||
install(FILES c-api.h | ||
DESTINATION include/sherpa-onnx/c-api | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// sherpa-onnx/c-api/c-api-tts.cc | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
#include "sherpa-onnx/c-api/c-api.h" | ||
|
||
#include <cstdio> | ||
#include <memory> | ||
|
||
#include "sherpa-onnx/csrc/offline-tts.h" | ||
#include "sherpa-onnx/csrc/wave-writer.h" | ||
|
||
#define SHERPA_ONNX_OR(x, y) (x ? x : y) | ||
|
||
struct SherpaOnnxOfflineTts { | ||
std::unique_ptr<sherpa_onnx::OfflineTts> impl; | ||
}; | ||
|
||
SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts( | ||
const SherpaOnnxOfflineTtsConfig *config) { | ||
sherpa_onnx::OfflineTtsConfig tts_config; | ||
|
||
tts_config.model.vits.model = SHERPA_ONNX_OR(config->model.vits.model, ""); | ||
tts_config.model.vits.lexicon = | ||
SHERPA_ONNX_OR(config->model.vits.lexicon, ""); | ||
tts_config.model.vits.tokens = SHERPA_ONNX_OR(config->model.vits.tokens, ""); | ||
tts_config.model.vits.data_dir = | ||
SHERPA_ONNX_OR(config->model.vits.data_dir, ""); | ||
tts_config.model.vits.noise_scale = | ||
SHERPA_ONNX_OR(config->model.vits.noise_scale, 0.667); | ||
tts_config.model.vits.noise_scale_w = | ||
SHERPA_ONNX_OR(config->model.vits.noise_scale_w, 0.8); | ||
tts_config.model.vits.length_scale = | ||
SHERPA_ONNX_OR(config->model.vits.length_scale, 1.0); | ||
|
||
tts_config.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1); | ||
tts_config.model.debug = config->model.debug; | ||
tts_config.model.provider = SHERPA_ONNX_OR(config->model.provider, "cpu"); | ||
tts_config.rule_fsts = SHERPA_ONNX_OR(config->rule_fsts, ""); | ||
tts_config.max_num_sentences = SHERPA_ONNX_OR(config->max_num_sentences, 2); | ||
|
||
if (tts_config.model.debug) { | ||
fprintf(stderr, "%s\n", tts_config.ToString().c_str()); | ||
} | ||
|
||
SherpaOnnxOfflineTts *tts = new SherpaOnnxOfflineTts; | ||
|
||
tts->impl = std::make_unique<sherpa_onnx::OfflineTts>(tts_config); | ||
|
||
return tts; | ||
} | ||
|
||
void SherpaOnnxDestroyOfflineTts(SherpaOnnxOfflineTts *tts) { delete tts; } | ||
|
||
int32_t SherpaOnnxOfflineTtsSampleRate(const SherpaOnnxOfflineTts *tts) { | ||
return tts->impl->SampleRate(); | ||
} | ||
|
||
const SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerate( | ||
const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid, | ||
float speed) { | ||
return SherpaOnnxOfflineTtsGenerateWithCallback(tts, text, sid, speed, | ||
nullptr); | ||
} | ||
|
||
const SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerateWithCallback( | ||
const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid, float speed, | ||
SherpaOnnxGeneratedAudioCallback callback) { | ||
sherpa_onnx::GeneratedAudio audio = | ||
tts->impl->Generate(text, sid, speed, callback); | ||
|
||
if (audio.samples.empty()) { | ||
return nullptr; | ||
} | ||
|
||
SherpaOnnxGeneratedAudio *ans = new SherpaOnnxGeneratedAudio; | ||
|
||
float *samples = new float[audio.samples.size()]; | ||
std::copy(audio.samples.begin(), audio.samples.end(), samples); | ||
|
||
ans->samples = samples; | ||
ans->n = audio.samples.size(); | ||
ans->sample_rate = audio.sample_rate; | ||
|
||
return ans; | ||
} | ||
|
||
void SherpaOnnxDestroyOfflineTtsGeneratedAudio( | ||
const SherpaOnnxGeneratedAudio *p) { | ||
if (p) { | ||
delete[] p->samples; | ||
delete p; | ||
} | ||
} | ||
|
||
int32_t SherpaOnnxWriteWave(const float *samples, int32_t n, | ||
int32_t sample_rate, const char *filename) { | ||
return sherpa_onnx::WriteWave(filename, sample_rate, samples, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should probably invert this, so as to not break the examples.