Akjava commited on
Commit
fdcb41f
·
verified ·
1 Parent(s): baa1126

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +228 -1
README.md CHANGED
@@ -49,6 +49,233 @@ quantized_model = quantize_dynamic(src_model_path, dst_model_path, weight_type=Q
49
  ```
50
 
51
 
52
- To use onnx need something,I'll add sample code later
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  ### Audio
54
  I cut with VAD tools and denoise with resemble-enhance
 
49
  ```
50
 
51
 
52
+ To use onnx need something,below is old sample
53
+ ```
54
+ const _pad = "_";
55
+ const _punctuation = ";:,.!?¡¿—…\"«»“” ";
56
+ const _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
57
+ const _letters_ipa = "ɑɐɒæɓʙβɔɕçɗɖðʤəɘɚɛɜɝɞɟʄɡɠɢʛɦɧħɥʜɨɪʝɭɬɫɮʟɱɯɰŋɳɲɴøɵɸθœɶʘɹɺɾɻʀʁɽʂʃʈʧʉʊʋⱱʌɣɤʍχʎʏʑʐʒʔʡʕʢǀǁǂǃˈˌːˑʼʴʰʱʲʷˠˤ˞↓↑→↗↘'̩'ᵻ";
58
+
59
+ // below code called Spread syntax
60
+ const Symbols = [_pad, ..._punctuation, ..._letters, ..._letters_ipa];
61
+
62
+ const SpaceId = Symbols.indexOf(' ');
63
+
64
+ const symbolToId = {};
65
+ const idToSymbol = {};
66
+
67
+ // initialize symbolToId and idToSymbol
68
+ for (let i = 0; i < Symbols.length; i++) {
69
+ symbolToId[Symbols[i]] = i;
70
+ idToSymbol[i] = Symbols[i];
71
+ }
72
+
73
+ class MatchaOnnx {
74
+ constructor() {
75
+ }
76
+ async load_model(model_path,options={}){
77
+ this.session = await ort.InferenceSession.create(model_path,options);
78
+ }
79
+
80
+ get_output_names_html(){
81
+ if (typeof this.session=='undefined'){
82
+ return null
83
+ }
84
+ let outputNamesString = '[outputs]<br>';
85
+ const outputNames = this.session.outputNames;
86
+ for (let outputName of outputNames) {
87
+ console.log(outputName)
88
+ outputNamesString+=outputName+"<br>"
89
+ }
90
+ return outputNamesString.trim()
91
+ }
92
+
93
+ get_input_names_html(){
94
+ if (typeof this.session=='undefined'){
95
+ return null
96
+ }
97
+
98
+ let inputNamesString = '[Inputs]<br>';
99
+ const inputNames = this.session.inputNames;
100
+
101
+ for (let inputName of inputNames) {
102
+ console.log(inputName)
103
+ inputNamesString+=inputName+"<br>"
104
+ }
105
+ return inputNamesString.trim()
106
+ }
107
+
108
+
109
+ processText(text) {
110
+ const x = this.intersperse(this.textToSequence(text));
111
+ const x_phones = this.sequenceToText(x);
112
+ const textList = [];
113
+ for (let i = 1; i < x_phones.length; i += 2) {
114
+ textList.push(x_phones[i]);
115
+ }
116
+
117
+ return {
118
+ x: x,
119
+ x_length: x.length,
120
+ x_phones: x_phones,
121
+ x_phones_label: textList.join(""),
122
+ };
123
+ }
124
+
125
+
126
+ basicCleaners2(text, lowercase = false) {
127
+ if (lowercase) {
128
+ text = text.toLowerCase();
129
+ }
130
+ text = text.replace(/\s+/g, " ");
131
+ return text;
132
+ }
133
+
134
+ textToSequence(text) {
135
+ const sequenceList = [];
136
+ const clean_text = this.basicCleaners2(text);
137
+ for (let i = 0; i < clean_text.length; i++) {
138
+ const symbol = clean_text[i];
139
+ sequenceList.push(symbolToId[symbol]);
140
+ }
141
+ return sequenceList;
142
+ }
143
+
144
+ intersperse(sequence, item = 0) {
145
+ const sequenceList = [item];
146
+ for (let i = 0; i < sequence.length; i++) {
147
+ sequenceList.push(sequence[i]);
148
+ sequenceList.push(item);
149
+ }
150
+ return sequenceList;
151
+ }
152
+
153
+ sequenceToText(sequence) {
154
+ const textList = [];
155
+ for (let i = 0; i < sequence.length; i++) {
156
+ const symbol = idToSymbol[sequence[i]];
157
+ textList.push(symbol);
158
+ }
159
+ return textList.join("");
160
+ }
161
+
162
+ async infer(text, temperature, speed) {
163
+ console.log(this.session)
164
+ const dic = this.processText(text);
165
+ console.log(`x:${dic.x.join(", ")}`);
166
+ console.log(`x_length:${dic.x_length}`);
167
+ console.log(`x_phones_label:${dic.x_phones_label}`);
168
+
169
+ // Prepare input tensors (assuming your ONNX Runtime library uses similar syntax)
170
+ //const x_tensor = new this.session.Tensor('long', dic.x, [1, dic.x.length]);
171
+ //const x_length_tensor = new this.session.Tensor('long', [dic.x.length], [1]);
172
+ //const scales_tensor = new this.session.Tensor('float', [temperature, speed], [2]);
173
+
174
+ const dataX = new BigInt64Array(dic.x.length)
175
+ for (let i = 0; i < dic.x.length; i++) {
176
+ //console.log(dic.x[i])
177
+ dataX[i] = BigInt(dic.x[i]); // Convert each number to a BigInt
178
+ }
179
+ const data_x_length = new BigInt64Array(1)
180
+ data_x_length[0] = BigInt(dic.x_length)
181
+
182
+ //const dataX = Int32Array.from([dic.x_length])
183
+ const tensorX = new ort.Tensor('int64', dataX, [1, dic.x.length]);
184
+ // const data_x_length = Int32Array.from([dic.x_length])
185
+ const tensor_x_length = new ort.Tensor('int64', data_x_length, [1]);
186
+ const data_scale = Float32Array.from( [temperature, speed])
187
+ const tensor_scale = new ort.Tensor('float32', data_scale, [2]);
188
+
189
+
190
+ // Run inference
191
+ const output = await this.session.run({
192
+ x: tensorX,
193
+ x_lengths: tensor_x_length,
194
+ scales: tensor_scale,
195
+ });
196
+ console.log(output)
197
+ // Extract output (assuming your ONNX Runtime library uses similar syntax)
198
+ const wav_array = output.wav.data;
199
+ console.log(wav_array[0]);
200
+ console.log(wav_array.length);
201
+
202
+ const x_lengths_array = output.wav_lengths.data;
203
+ console.log(x_lengths_array.join(", "));
204
+
205
+ return wav_array;
206
+ }
207
+
208
+
209
+ }
210
+ ```
211
+ convert to wav
212
+ ```
213
+
214
+
215
+ function webWavPlay(f32array){
216
+ blob = float32ArrayToWav(f32array)
217
+ url = createObjectUrlFromBlob(blob)
218
+ console.log(url)
219
+ playAudioFromUrl(url)
220
+ }
221
+
222
+ function createObjectUrlFromBlob(blob) {
223
+ const url = URL.createObjectURL(blob);
224
+ return url;
225
+ }
226
+
227
+ function playAudioFromUrl(url) {
228
+ const audio = new Audio(url);
229
+ audio.play().catch(error => console.error('Failed to play audio:', error));
230
+ }
231
+
232
+
233
+ //I copied
234
+ //https://huggingface.co/spaces/k2-fsa/web-assembly-tts-sherpa-onnx-de/blob/main/app-tts.js
235
+ // this function is copied/modified from
236
+ // https://gist.github.com/meziantou/edb7217fddfbb70e899e
237
+ function float32ArrayToWav(floatSamples, sampleRate=22050) {
238
+ let samples = new Int16Array(floatSamples.length);
239
+ for (let i = 0; i < samples.length; ++i) {
240
+ let s = floatSamples[i];
241
+ if (s >= 1)
242
+ s = 1;
243
+ else if (s <= -1)
244
+ s = -1;
245
+
246
+ samples[i] = s * 32767;
247
+ }
248
+
249
+ let buf = new ArrayBuffer(44 + samples.length * 2);
250
+ var view = new DataView(buf);
251
+
252
+ // http://soundfile.sapp.org/doc/WaveFormat/
253
+ // F F I R
254
+ view.setUint32(0, 0x46464952, true); // chunkID
255
+ view.setUint32(4, 36 + samples.length * 2, true); // chunkSize
256
+ // E V A W
257
+ view.setUint32(8, 0x45564157, true); // format
258
+ //
259
+ // t m f
260
+ view.setUint32(12, 0x20746d66, true); // subchunk1ID
261
+ view.setUint32(16, 16, true); // subchunk1Size, 16 for PCM
262
+ view.setUint32(20, 1, true); // audioFormat, 1 for PCM
263
+ view.setUint16(22, 1, true); // numChannels: 1 channel
264
+ view.setUint32(24, sampleRate, true); // sampleRate
265
+ view.setUint32(28, sampleRate * 2, true); // byteRate
266
+ view.setUint16(32, 2, true); // blockAlign
267
+ view.setUint16(34, 16, true); // bitsPerSample
268
+ view.setUint32(36, 0x61746164, true); // Subchunk2ID
269
+ view.setUint32(40, samples.length * 2, true); // subchunk2Size
270
+
271
+ let offset = 44;
272
+ for (let i = 0; i < samples.length; ++i) {
273
+ view.setInt16(offset, samples[i], true);
274
+ offset += 2;
275
+ }
276
+
277
+ return new Blob([view], {type: 'audio/wav'});
278
+ }
279
+ ```
280
  ### Audio
281
  I cut with VAD tools and denoise with resemble-enhance