Skip to content

Commit 81daf0d

Browse files
committed
Add clamping speed parameter
1 parent 065c264 commit 81daf0d

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ inline constexpr size_t kMinDurationTicks =
2020
inline constexpr size_t kMaxDurationTicks =
2121
296; // Corresponds to DurationPredictor output and one of Synthesizer's
2222
// input shapes
23+
inline constexpr float kMinValidSpeed = 0.1F;
24+
inline constexpr float kMaxValidSpeed = 3.0F;
2325

2426
// Model input sizes - voice reference vector
2527
inline constexpr int32_t kVoiceRefSize =

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Kokoro.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ std::vector<float> Kokoro::generate(std::u32string input, float speed,
9696
"Kokoro: maximum input text size exceeded");
9797
}
9898

99+
if (speed < constants::kMinValidSpeed) {
100+
throw RnExecutorchError(RnExecutorchErrorCode::InvalidUserInput,
101+
"Kokoro: speed value too low (min " +
102+
std::to_string(constants::kMinValidSpeed) +
103+
")");
104+
}
105+
106+
if (speed > constants::kMaxValidSpeed) {
107+
throw RnExecutorchError(RnExecutorchErrorCode::InvalidUserInput,
108+
"Kokoro: speed value too high (max " +
109+
std::to_string(constants::kMaxValidSpeed) +
110+
")");
111+
}
112+
99113
if (input.empty()) {
100114
return {};
101115
}
@@ -133,6 +147,20 @@ std::vector<float> Kokoro::generate(std::u32string input, float speed,
133147

134148
void Kokoro::stream(std::shared_ptr<jsi::Function> callback, float speed,
135149
bool phonemize, bool stopOnEmptyBuffer) {
150+
if (speed < constants::kMinValidSpeed) {
151+
throw RnExecutorchError(RnExecutorchErrorCode::InvalidUserInput,
152+
"Kokoro: speed value too low (min " +
153+
std::to_string(constants::kMinValidSpeed) +
154+
")");
155+
}
156+
157+
if (speed > constants::kMaxValidSpeed) {
158+
throw RnExecutorchError(RnExecutorchErrorCode::InvalidUserInput,
159+
"Kokoro: speed value too high (max " +
160+
std::to_string(constants::kMaxValidSpeed) +
161+
")");
162+
}
163+
136164
// Create a callback
137165
auto nativeCallback = [this, callback](const std::vector<float> &audioVec) {
138166
if (this->isStreaming_) {

packages/react-native-executorch/src/modules/natural_language_processing/TextToSpeechModule.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,28 @@ export class TextToSpeechModule {
7575
...sources
7676
);
7777

78-
if (paths === null || paths.length !== sources.length) {
78+
// Required fields
79+
const [duration, synth, voice] = paths;
80+
if (!duration || !synth || !voice) {
7981
throw new RnExecutorchError(
8082
RnExecutorchErrorCode.DownloadInterrupted,
81-
'Download interrupted or missing resource.'
83+
'Kokoro: missing required model paths.'
8284
);
8385
}
8486

87+
// Optional fields
88+
const tagger = paths[taggerIdx] ?? '';
89+
const lexicon = paths[lexiconIdx] ?? '';
90+
const neural = paths[neuralModelIdx] ?? '';
91+
8592
return await global.loadTextToSpeechKokoro(
8693
phonemizerConfig.lang,
87-
taggerIdx >= 0 ? (paths[taggerIdx] as string) : '',
88-
lexiconIdx >= 0 ? (paths[lexiconIdx] as string) : '',
89-
neuralModelIdx >= 0 ? (paths[neuralModelIdx] as string) : '',
90-
paths[0] as string, // DurationPredictor source
91-
paths[1] as string, // Synthesizer source
92-
paths[2] as string // Voice source
94+
tagger,
95+
lexicon,
96+
neural,
97+
duration,
98+
synth,
99+
voice
93100
);
94101
}
95102

0 commit comments

Comments
 (0)