Skip to content
3 changes: 3 additions & 0 deletions scripts/go/sherpa_onnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ type OfflineTtsPocketModelConfig struct {
TextConditioner string // text_conditioner
VocabJson string // vocab_json
TokenScoresJson string // token_scores_json
VoiceEmbeddingCacheCapacity int // voice_embedding_cache_capacity
}

type OfflineTtsZipvoiceModelConfig struct {
Expand Down Expand Up @@ -1170,6 +1171,8 @@ func NewOfflineTts(config *OfflineTtsConfig) *OfflineTts {
c.model.pocket.token_scores_json = C.CString(config.Model.Pocket.TokenScoresJson)
defer C.free(unsafe.Pointer(c.model.pocket.token_scores_json))

c.model.pocket.voice_embedding_cache_capacity = C.int(config.Model.Pocket.VoiceEmbeddingCacheCapacity)

c.model.num_threads = C.int(config.Model.NumThreads)
c.model.debug = C.int(config.Model.Debug)

Expand Down
13 changes: 9 additions & 4 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,15 +762,14 @@ const SherpaOnnxOfflineRecognizerResult *SherpaOnnxGetOfflineStreamResult(

// Copy segment-level timestamps (from Whisper with segment timestamps)
auto segment_count = result.segment_texts.size();
if (segment_count > 0 &&
result.segment_timestamps.size() == segment_count &&
if (segment_count > 0 && result.segment_timestamps.size() == segment_count &&
result.segment_durations.size() == segment_count) {
r->segment_count = segment_count;

// Copy segment timestamps
float *timestamps = new float[segment_count];
std::copy(result.segment_timestamps.begin(), result.segment_timestamps.end(),
timestamps);
std::copy(result.segment_timestamps.begin(),
result.segment_timestamps.end(), timestamps);
r->segment_timestamps = timestamps;

// Copy segment durations
Expand Down Expand Up @@ -1363,6 +1362,12 @@ static sherpa_onnx::OfflineTtsConfig GetOfflineTtsConfig(
SHERPA_ONNX_OR(config->model.pocket.vocab_json, "");
tts_config.model.pocket.token_scores_json =
SHERPA_ONNX_OR(config->model.pocket.token_scores_json, "");
if (config->model.pocket.voice_embedding_cache_capacity >= 0) {
tts_config.model.pocket.voice_embedding_cache_capacity =
config->model.pocket.voice_embedding_cache_capacity;
} else {
tts_config.model.pocket.voice_embedding_cache_capacity = 50;
}

tts_config.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1);
tts_config.model.debug = config->model.debug;
Expand Down
1 change: 1 addition & 0 deletions sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineTtsPocketModelConfig {
const char *text_conditioner;
const char *vocab_json;
const char *token_scores_json;
int32_t voice_embedding_cache_capacity;
} SherpaOnnxOfflineTtsPocketModelConfig;

SHERPA_ONNX_API typedef struct SherpaOnnxOfflineTtsModelConfig {
Expand Down
3 changes: 3 additions & 0 deletions sherpa-onnx/c-api/cxx-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ OfflineTts OfflineTts::Create(const OfflineTtsConfig &config) {
c.model.pocket.token_scores_json =
config.model.pocket.token_scores_json.c_str();

c.model.pocket.voice_embedding_cache_capacity =
config.model.pocket.voice_embedding_cache_capacity;

c.model.num_threads = config.model.num_threads;
c.model.debug = config.model.debug;
c.model.provider = config.model.provider.c_str();
Expand Down
1 change: 1 addition & 0 deletions sherpa-onnx/c-api/cxx-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ struct OfflineTtsPocketModelConfig {

std::string vocab_json;
std::string token_scores_json;
int32_t voice_embedding_cache_capacity = 50;
};

struct OfflineTtsModelConfig {
Expand Down
39 changes: 27 additions & 12 deletions sherpa-onnx/csrc/normal-data-generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace sherpa_onnx {

// Helper type hidden in translation unit
namespace {
struct RNGHolder {
std::mt19937 rng;
std::normal_distribution<float> dist;
Expand All @@ -24,20 +25,34 @@ struct RNGHolder {
}()),
dist() {}
};

NormalDataGenerator::NormalDataGenerator(float mean /* = 0.0f */,
float stddev /* = 1.0f */)
: mean_(mean), stddev_(stddev) {}
} // namespace

NormalDataGenerator::NormalDataGenerator(float mean /* = 0.0f*/,
float stddev /* = 1.0f*/,
int32_t seed /* = -1*/)
: mean_(mean), stddev_(stddev), seed_(seed) {
if (seed_ >= 0) {
rng_.seed(static_cast<unsigned>(seed_));
}
}

void NormalDataGenerator::Fill(float *data, std::size_t size) const {
// One RNGHolder per thread
static thread_local RNGHolder holder;

holder.dist.param(
std::normal_distribution<float>::param_type(mean_, stddev_));

for (std::size_t i = 0; i < size; ++i) {
data[i] = holder.dist(holder.rng);
if (seed_ >= 0) {
// Deterministic mode: use instance-level RNG
std::normal_distribution<float> dist(mean_, stddev_);
for (std::size_t i = 0; i < size; ++i) {
data[i] = dist(rng_);
}
} else {
// Original behavior: thread-local random device
static thread_local RNGHolder holder;

holder.dist.param(
std::normal_distribution<float>::param_type(mean_, stddev_));

for (std::size_t i = 0; i < size; ++i) {
data[i] = holder.dist(holder.rng);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

Expand Down
7 changes: 6 additions & 1 deletion sherpa-onnx/csrc/normal-data-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
#define SHERPA_ONNX_CSRC_NORMAL_DATA_GENERATOR_H_

#include <cstddef>
#include <cstdint>
#include <random>

namespace sherpa_onnx {

class NormalDataGenerator {
public:
explicit NormalDataGenerator(float mean = 0.0f, float stddev = 1.0f);
explicit NormalDataGenerator(float mean = 0.0f, float stddev = 1.0f,
int32_t seed = -1);

// Fill pre-allocated memory
void Fill(float *data, std::size_t size) const;

private:
float mean_;
float stddev_;
int32_t seed_ = -1; // -1 = use thread-local random device (default)
mutable std::mt19937 rng_; // used if seed_ >= 0
};

} // namespace sherpa_onnx
Expand Down
Loading