Skip to content

Commit

Permalink
Add dict_dir arg to c api to support Chinese TTS models using jieba (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Apr 25, 2024
1 parent 83cd533 commit 6686c7d
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(sherpa-onnx)

set(SHERPA_ONNX_VERSION "1.9.22")
set(SHERPA_ONNX_VERSION "1.9.23")

# Disable warning about
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ void CNonStreamingTextToSpeechDlg::Init() {
} else if (Exists("./lexicon.txt")) {
config.model.vits.lexicon = "./lexicon.txt";
}

if (Exists("./dict/jieba.dict.utf8")) {
config.model.vits.dict_dir = "./dict";
}

if (Exists("./phone.fst") && Exists("./date.fst") && Exists("./number.fst")) {
config.rule_fsts = "./phone.fst,./date.fst,number.fst";
}

if (Exists("./rule.far")) {
config.rule_fars = "./rule.far";
}

config.model.vits.tokens = "./tokens.txt";

tts_ = SherpaOnnxCreateOfflineTts(&config);
Expand Down
1 change: 1 addition & 0 deletions nodejs-examples/test-offline-tts-en.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function createOfflineTts() {
lexicon: '',
tokens: './vits-piper-en_US-amy-low/tokens.txt',
dataDir: './vits-piper-en_US-amy-low/espeak-ng-data',
dictDir: '',
noiseScale: 0.667,
noiseScaleW: 0.8,
lengthScale: 1.0,
Expand Down
1 change: 1 addition & 0 deletions nodejs-examples/test-offline-tts-zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function createOfflineTts() {
lexicon: './vits-icefall-zh-aishell3/lexicon.txt',
tokens: './vits-icefall-zh-aishell3/tokens.txt',
dataDir: '',
dictDir: '',
noiseScale: 0.667,
noiseScaleW: 0.8,
lengthScale: 1.0,
Expand Down
5 changes: 5 additions & 0 deletions scripts/dotnet/offline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public OfflineTtsVitsModelConfig()
NoiseScale = 0.667F;
NoiseScaleW = 0.8F;
LengthScale = 1.0F;

DictDir = "";
}
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
Expand All @@ -39,6 +41,9 @@ public OfflineTtsVitsModelConfig()
public float NoiseScale;
public float NoiseScaleW;
public float LengthScale;

[MarshalAs(UnmanagedType.LPStr)]
public string DictDir;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
6 changes: 5 additions & 1 deletion scripts/go/sherpa_onnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,11 @@ type OfflineTtsVitsModelConfig struct {
Model string // Path to the VITS onnx model
Lexicon string // Path to lexicon.txt
Tokens string // Path to tokens.txt
DataDir string // Path to tokens.txt
DataDir string // Path to espeak-ng-data directory
NoiseScale float32 // noise scale for vits models. Please use 0.667 in general
NoiseScaleW float32 // noise scale for vits models. Please use 0.8 in general
LengthScale float32 // Please use 1.0 in general. Smaller -> Faster speech speed. Larger -> Slower speech speed
DictDir string // Path to dict directory for jieba (used only in Chinese tts)
}

type OfflineTtsModelConfig struct {
Expand Down Expand Up @@ -605,6 +606,9 @@ func NewOfflineTts(config *OfflineTtsConfig) *OfflineTts {
c.model.vits.noise_scale_w = C.float(config.Model.Vits.NoiseScaleW)
c.model.vits.length_scale = C.float(config.Model.Vits.LengthScale)

c.model.vits.dict_dir = C.CString(config.Model.Vits.DictDir)
defer C.free(unsafe.Pointer(c.model.vits.dict_dir))

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

Expand Down
2 changes: 2 additions & 0 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts(
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.vits.dict_dir =
SHERPA_ONNX_OR(config->model.vits.dict_dir, "");

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 @@ -772,6 +772,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineTtsVitsModelConfig {
float noise_scale;
float noise_scale_w;
float length_scale; // < 1, faster in speed; > 1, slower in speed
const char *dict_dir;
} SherpaOnnxOfflineTtsVitsModelConfig;

SHERPA_ONNX_API typedef struct SherpaOnnxOfflineTtsModelConfig {
Expand Down
6 changes: 4 additions & 2 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ func sherpaOnnxOfflineTtsVitsModelConfig(
dataDir: String = "",
noiseScale: Float = 0.667,
noiseScaleW: Float = 0.8,
lengthScale: Float = 1.0
lengthScale: Float = 1.0,
dictDir: String = ""
) -> SherpaOnnxOfflineTtsVitsModelConfig {
return SherpaOnnxOfflineTtsVitsModelConfig(
model: toCPointer(model),
Expand All @@ -632,7 +633,8 @@ func sherpaOnnxOfflineTtsVitsModelConfig(
data_dir: toCPointer(dataDir),
noise_scale: noiseScale,
noise_scale_w: noiseScaleW,
length_scale: lengthScale)
length_scale: lengthScale,
dict_dir: toCPointer(dictDir))
}

func sherpaOnnxOfflineTtsModelConfig(
Expand Down
1 change: 1 addition & 0 deletions wasm/nodejs/sherpa-onnx-wasm-nodejs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void PrintOfflineTtsConfig(SherpaOnnxOfflineTtsConfig *tts_config) {
fprintf(stdout, "noise scale: %.3f\n", vits_model_config->noise_scale);
fprintf(stdout, "noise scale w: %.3f\n", vits_model_config->noise_scale_w);
fprintf(stdout, "length scale: %.3f\n", vits_model_config->length_scale);
fprintf(stdout, "dict_dir: %s\n", vits_model_config->dict_dir);

fprintf(stdout, "----------tts model config----------\n");
fprintf(stdout, "num threads: %d\n", tts_model_config->num_threads);
Expand Down
13 changes: 12 additions & 1 deletion wasm/tts/sherpa-onnx-tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ function initSherpaOnnxOfflineTtsVitsModelConfig(config, Module) {
const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
const dataDirLen = Module.lengthBytesUTF8(config.dataDir) + 1;

const n = modelLen + lexiconLen + tokensLen + dataDirLen;
if (!('dictDir' in config)) {
config.dictDir = ''
}
const dictDirLen = Module.lengthBytesUTF8(config.dictDir) + 1;

const n = modelLen + lexiconLen + tokensLen + dataDirLen + dictDirLen;

const buffer = Module._malloc(n);

Expand All @@ -38,6 +43,9 @@ function initSherpaOnnxOfflineTtsVitsModelConfig(config, Module) {
Module.stringToUTF8(config.dataDir, buffer + offset, dataDirLen);
offset += dataDirLen;

Module.stringToUTF8(config.dictDir, buffer + offset, dictDirLen);
offset += dictDirLen;

offset = 0;
Module.setValue(ptr, buffer + offset, 'i8*');
offset += modelLen;
Expand All @@ -54,6 +62,8 @@ function initSherpaOnnxOfflineTtsVitsModelConfig(config, Module) {
Module.setValue(ptr + 16, config.noiseScale, 'float');
Module.setValue(ptr + 20, config.noiseScaleW, 'float');
Module.setValue(ptr + 24, config.lengthScale, 'float');
Module.setValue(ptr + 28, buffer + offset, 'i8*');
offset += dictDirLen;

return {
buffer: buffer, ptr: ptr, len: len,
Expand Down Expand Up @@ -184,6 +194,7 @@ function createOfflineTts(Module, myConfig) {
lexicon: '',
tokens: './tokens.txt',
dataDir: './espeak-ng-data',
dictDir: '',
noiseScale: 0.667,
noiseScaleW: 0.8,
lengthScale: 1.0,
Expand Down
3 changes: 2 additions & 1 deletion wasm/tts/sherpa-onnx-wasm-main-tts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

extern "C" {

static_assert(sizeof(SherpaOnnxOfflineTtsVitsModelConfig) == 7 * 4, "");
static_assert(sizeof(SherpaOnnxOfflineTtsVitsModelConfig) == 8 * 4, "");
static_assert(sizeof(SherpaOnnxOfflineTtsModelConfig) ==
sizeof(SherpaOnnxOfflineTtsVitsModelConfig) + 3 * 4,
"");
Expand All @@ -32,6 +32,7 @@ void MyPrint(SherpaOnnxOfflineTtsConfig *tts_config) {
fprintf(stdout, "noise scale: %.3f\n", vits_model_config->noise_scale);
fprintf(stdout, "noise scale w: %.3f\n", vits_model_config->noise_scale_w);
fprintf(stdout, "length scale: %.3f\n", vits_model_config->length_scale);
fprintf(stdout, "dict_dir: %s\n", vits_model_config->dict_dir);

fprintf(stdout, "----------tts model config----------\n");
fprintf(stdout, "num threads: %d\n", tts_model_config->num_threads);
Expand Down

0 comments on commit 6686c7d

Please sign in to comment.