File size: 4,615 Bytes
1265f08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

function freeConfig(config) {
  if ('buffer' in config) {
    _free(config.buffer);
  }

  if ('config' in config) {
    freeConfig(config.config)
  }

  _free(config.ptr);
}

// The user should free the returned pointers
function initSherpaOnnxOfflineTtsVitsModelConfig(config) {
  let modelLen = lengthBytesUTF8(config.model) + 1;
  let lexiconLen = lengthBytesUTF8(config.lexicon) + 1;
  let tokensLen = lengthBytesUTF8(config.tokens) + 1;
  let dataDirLen = lengthBytesUTF8(config.dataDir) + 1;

  let n = modelLen + lexiconLen + tokensLen + dataDirLen;

  let buffer = _malloc(n);

  let len = 7 * 4;
  let ptr = _malloc(len);

  let offset = 0;
  stringToUTF8(config.model, buffer + offset, modelLen);
  offset += modelLen;

  stringToUTF8(config.lexicon, buffer + offset, lexiconLen);
  offset += lexiconLen;

  stringToUTF8(config.tokens, buffer + offset, tokensLen);
  offset += tokensLen;

  stringToUTF8(config.dataDir, buffer + offset, dataDirLen);
  offset += dataDirLen;

  offset = 0;
  setValue(ptr, buffer + offset, 'i8*');
  offset += modelLen;

  setValue(ptr + 4, buffer + offset, 'i8*');
  offset += lexiconLen;

  setValue(ptr + 8, buffer + offset, 'i8*');
  offset += tokensLen;

  setValue(ptr + 12, buffer + offset, 'i8*');
  offset += dataDirLen;

  setValue(ptr + 16, config.noiseScale, 'float');
  setValue(ptr + 20, config.noiseScaleW, 'float');
  setValue(ptr + 24, config.lengthScale, 'float');

  return {
    buffer: buffer, ptr: ptr, len: len,
  }
}

function initSherpaOnnxOfflineTtsModelConfig(config) {
  let vitsModelConfig =
      initSherpaOnnxOfflineTtsVitsModelConfig(config.offlineTtsVitsModelConfig);

  let len = vitsModelConfig.len + 3 * 4;
  let ptr = _malloc(len);

  let offset = 0;
  _CopyHeap(vitsModelConfig.ptr, vitsModelConfig.len, ptr + offset);
  offset += vitsModelConfig.len;

  setValue(ptr + offset, config.numThreads, 'i32');
  offset += 4;

  setValue(ptr + offset, config.debug, 'i32');
  offset += 4;

  let providerLen = lengthBytesUTF8(config.provider) + 1;
  let buffer = _malloc(providerLen);
  stringToUTF8(config.provider, buffer, providerLen);
  setValue(ptr + offset, buffer, 'i8*');

  return {
    buffer: buffer, ptr: ptr, len: len, config: vitsModelConfig,
  }
}

function initSherpaOnnxOfflineTtsConfig(config) {
  let modelConfig =
      initSherpaOnnxOfflineTtsModelConfig(config.offlineTtsModelConfig);
  let len = modelConfig.len + 2 * 4;
  let ptr = _malloc(len);

  let offset = 0;
  _CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset);
  offset += modelConfig.len;

  let ruleFstsLen = lengthBytesUTF8(config.ruleFsts) + 1;
  let buffer = _malloc(ruleFstsLen);
  stringToUTF8(config.ruleFsts, buffer, ruleFstsLen);
  setValue(ptr + offset, buffer, 'i8*');
  offset += 4;

  setValue(ptr + offset, config.maxNumSentences, 'i32');

  return {
    buffer: buffer, ptr: ptr, len: len, config: modelConfig,
  }
}

class OfflineTts {
  constructor(configObj) {
    let config = initSherpaOnnxOfflineTtsConfig(configObj)
    let handle = _SherpaOnnxCreateOfflineTts(config.ptr);

    freeConfig(config);

    this.handle = handle;
    this.sampleRate = _SherpaOnnxOfflineTtsSampleRate(this.handle);
    this.numSpeakers = _SherpaOnnxOfflineTtsNumSpeakers(this.handle);
  }

  free() {
    _SherpaOnnxDestroyOfflineTts(this.handle);
    this.handle = 0
  }

  // {
  //   text: "hello",
  //   sid: 1,
  //   speed: 1.0
  // }
  generate(config) {
    let textLen = lengthBytesUTF8(config.text) + 1;
    let textPtr = _malloc(textLen);
    stringToUTF8(config.text, textPtr, textLen);

    let h = _SherpaOnnxOfflineTtsGenerate(
        this.handle, textPtr, config.sid, config.speed);

    let numSamples = HEAP32[h / 4 + 1];
    let sampleRate = HEAP32[h / 4 + 2];

    let samplesPtr = HEAP32[h / 4] / 4;
    let samples = new Float32Array(numSamples);
    for (let i = 0; i < numSamples; i++) {
      samples[i] = HEAPF32[samplesPtr + i];
    }

    _SherpaOnnxDestroyOfflineTtsGeneratedAudio(h);
    return {samples: samples, sampleRate: sampleRate};
  }
}

function initSherpaOnnxOfflineTts() {
  let offlineTtsVitsModelConfig = {
    model: './model.onnx',
    lexicon: '',
    tokens: './tokens.txt',
    dataDir: './espeak-ng-data',
    noiseScale: 0.667,
    noiseScaleW: 0.8,
    lengthScale: 1.0,
  };
  let offlineTtsModelConfig = {
    offlineTtsVitsModelConfig: offlineTtsVitsModelConfig,
    numThreads: 1,
    debug: 1,
    provider: 'cpu',
  };
  let offlineTtsConfig = {
    offlineTtsModelConfig: offlineTtsModelConfig,
    ruleFsts: '',
    maxNumSentences: 1,
  }

  return new OfflineTts(offlineTtsConfig);
}