@suarify/kecil-tts 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +21 -0
- package/README.md +224 -2
- package/docs/screenshot.jpg +0 -0
- package/examples/node-server.mjs +48 -0
- package/package.json +63 -4
- package/src/effects.js +250 -0
- package/src/g2p/data.js +6 -0
- package/src/g2p/index.js +16 -0
- package/src/g2p/ms_g2p.js +277 -0
- package/src/g2p/ms_norm.js +217 -0
- package/src/index.js +224 -0
- package/src/runtime.js +44 -0
- package/src/tts.js +149 -0
- package/src/wav.js +46 -0
package/src/effects.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Offline version of the Kecil-TTS web Space's effects chain.
|
|
2
|
+
// Pure JS (no Web Audio API), so the same settings work in browsers and Node.
|
|
3
|
+
// Parameters, ranges, defaults and presets mirror index.html in the Space.
|
|
4
|
+
|
|
5
|
+
const DEFAULT_SAMPLE_RATE = 24000;
|
|
6
|
+
|
|
7
|
+
export const FX_PARAMETERS = Object.freeze([
|
|
8
|
+
{ group: 'Tone', params: [
|
|
9
|
+
{ key: 'bdG', label: 'Body dB', min: -9, max: 9, step: 0.1, default: 0 },
|
|
10
|
+
{ key: 'prG', label: 'Presence dB', min: -9, max: 9, step: 0.1, default: 0 },
|
|
11
|
+
{ key: 'aiG', label: 'Air dB', min: -9, max: 9, step: 0.1, default: 0 },
|
|
12
|
+
{ key: 'exM', label: 'Exciter', min: 0, max: 1, step: 0.01, default: 0 },
|
|
13
|
+
] },
|
|
14
|
+
{ group: 'Second layer (doubling / chorus)', params: [
|
|
15
|
+
{ key: 'l2G', label: 'Level', min: 0, max: 0.8, step: 0.01, default: 0 },
|
|
16
|
+
{ key: 'l2D', label: 'Delay ms', min: 4, max: 60, step: 0.5, default: 18 },
|
|
17
|
+
{ key: 'l2M', label: 'Wobble ms', min: 0, max: 6, step: 0.05, default: 1.5 },
|
|
18
|
+
{ key: 'l2R', label: 'Wobble Hz', min: 0.1, max: 9, step: 0.05, default: 0.7 },
|
|
19
|
+
{ key: 'l2P', label: 'Pan', min: -1, max: 1, step: 0.01, default: 0.4 },
|
|
20
|
+
] },
|
|
21
|
+
{ group: 'Echo tail', params: [
|
|
22
|
+
{ key: 'ecM', label: 'Mix', min: 0, max: 0.7, step: 0.01, default: 0 },
|
|
23
|
+
{ key: 'ecT', label: 'Time ms', min: 40, max: 500, step: 1, default: 120 },
|
|
24
|
+
{ key: 'ecF', label: 'Feedback', min: 0, max: 0.8, step: 0.01, default: 0.3 },
|
|
25
|
+
] },
|
|
26
|
+
{ group: 'Room', params: [
|
|
27
|
+
{ key: 'rvM', label: 'Mix', min: 0, max: 0.5, step: 0.01, default: 0 },
|
|
28
|
+
{ key: 'rvD', label: 'Decay s', min: 0.2, max: 2.5, step: 0.05, default: 0.6 },
|
|
29
|
+
] },
|
|
30
|
+
{ group: 'Output', params: [
|
|
31
|
+
{ key: 'mG', label: 'Gain dB', min: -9, max: 6, step: 0.1, default: 0 },
|
|
32
|
+
] },
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
export const FX_DEFAULTS = Object.freeze(Object.fromEntries(
|
|
36
|
+
FX_PARAMETERS.flatMap((g) => g.params.map((p) => [p.key, p.default])),
|
|
37
|
+
));
|
|
38
|
+
|
|
39
|
+
export const FX_PRESETS = Object.freeze({
|
|
40
|
+
'No effects': {},
|
|
41
|
+
'Warm + presence': { bdG: 3, prG: 3, aiG: 2, mG: -1 },
|
|
42
|
+
Brighter: { prG: 4, aiG: 5, exM: 0.35 },
|
|
43
|
+
'Haas double': { l2G: 0.45, l2D: 18, l2M: 0.8, l2R: 0.6, l2P: 0.7 },
|
|
44
|
+
'Chorus thick': { l2G: 0.5, l2D: 24, l2M: 3.5, l2R: 0.9, l2P: -0.5 },
|
|
45
|
+
'Drag tail': { ecM: 0.35, ecT: 140, ecF: 0.45, l2G: 0.25, l2D: 35, l2M: 2, l2R: 0.4 },
|
|
46
|
+
'Small room': { rvM: 0.22, rvD: 0.45 },
|
|
47
|
+
'Smooth the robot (try)': {
|
|
48
|
+
bdG: 2, prG: 2.5, aiG: 2, exM: 0.25,
|
|
49
|
+
l2G: 0.35, l2D: 12, l2M: 2.2, l2R: 1.4, l2P: 0.5,
|
|
50
|
+
rvM: 0.12, rvD: 0.35,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export function listFxParameters() {
|
|
55
|
+
return FX_PARAMETERS.map((g) => ({ group: g.group, params: g.params.map((p) => ({ ...p })) }));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function listFxPresets() {
|
|
59
|
+
return Object.fromEntries(Object.entries(FX_PRESETS).map(([k, v]) => [k, { ...v }]));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolveSettings(settings) {
|
|
63
|
+
const out = { ...FX_DEFAULTS };
|
|
64
|
+
if (typeof settings === 'string') {
|
|
65
|
+
if (!(settings in FX_PRESETS)) throw new Error(`Unknown effects preset "${settings}". Choose one of: ${Object.keys(FX_PRESETS).join(', ')}`);
|
|
66
|
+
Object.assign(out, FX_PRESETS[settings]);
|
|
67
|
+
} else if (settings && typeof settings === 'object') {
|
|
68
|
+
if (settings.preset) {
|
|
69
|
+
if (!(settings.preset in FX_PRESETS)) throw new Error(`Unknown effects preset "${settings.preset}"`);
|
|
70
|
+
Object.assign(out, FX_PRESETS[settings.preset]);
|
|
71
|
+
}
|
|
72
|
+
for (const [key, value] of Object.entries(settings)) {
|
|
73
|
+
if (key === 'preset') continue;
|
|
74
|
+
if (!(key in out)) throw new Error(`Unknown effects parameter "${key}"`);
|
|
75
|
+
out[key] = Number(value);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// RBJ biquad coefficients, normalized by a0.
|
|
82
|
+
function biquad(type, sr, freq, Q = 0.707, gainDb = 0) {
|
|
83
|
+
const w0 = 2 * Math.PI * freq / sr;
|
|
84
|
+
const c = Math.cos(w0);
|
|
85
|
+
const s = Math.sin(w0);
|
|
86
|
+
const alpha = s / (2 * Q);
|
|
87
|
+
const A = Math.pow(10, gainDb / 40);
|
|
88
|
+
let b0, b1, b2, a0, a1, a2;
|
|
89
|
+
if (type === 'peaking') {
|
|
90
|
+
b0 = 1 + alpha * A; b1 = -2 * c; b2 = 1 - alpha * A;
|
|
91
|
+
a0 = 1 + alpha / A; a1 = -2 * c; a2 = 1 - alpha / A;
|
|
92
|
+
} else if (type === 'highshelf') {
|
|
93
|
+
const beta = 2 * Math.sqrt(A) * alpha;
|
|
94
|
+
b0 = A * ((A + 1) + (A - 1) * c + beta);
|
|
95
|
+
b1 = -2 * A * ((A - 1) + (A + 1) * c);
|
|
96
|
+
b2 = A * ((A + 1) + (A - 1) * c - beta);
|
|
97
|
+
a0 = (A + 1) - (A - 1) * c + beta;
|
|
98
|
+
a1 = 2 * ((A - 1) - (A + 1) * c);
|
|
99
|
+
a2 = (A + 1) - (A - 1) * c - beta;
|
|
100
|
+
} else if (type === 'highpass') {
|
|
101
|
+
b0 = (1 + c) / 2; b1 = -(1 + c); b2 = (1 + c) / 2;
|
|
102
|
+
a0 = 1 + alpha; a1 = -2 * c; a2 = 1 - alpha;
|
|
103
|
+
} else { // lowpass
|
|
104
|
+
b0 = (1 - c) / 2; b1 = 1 - c; b2 = (1 - c) / 2;
|
|
105
|
+
a0 = 1 + alpha; a1 = -2 * c; a2 = 1 - alpha;
|
|
106
|
+
}
|
|
107
|
+
return [b0 / a0, b1 / a0, b2 / a0, a1 / a0, a2 / a0];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function filter(input, coeffs) {
|
|
111
|
+
const [b0, b1, b2, a1, a2] = coeffs;
|
|
112
|
+
const out = new Float32Array(input.length);
|
|
113
|
+
let x1 = 0, x2 = 0, y1 = 0, y2 = 0;
|
|
114
|
+
for (let i = 0; i < input.length; i++) {
|
|
115
|
+
const x = input[i];
|
|
116
|
+
const y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
|
|
117
|
+
out[i] = y;
|
|
118
|
+
x2 = x1; x1 = x; y2 = y1; y1 = y;
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function modulatedDelay(input, sr, baseMs, wobbleMs, rateHz) {
|
|
124
|
+
const n = input.length;
|
|
125
|
+
const maxSec = 0.2;
|
|
126
|
+
const len = Math.ceil(maxSec * sr) + 4;
|
|
127
|
+
const buf = new Float32Array(len);
|
|
128
|
+
const out = new Float32Array(n);
|
|
129
|
+
let write = 0;
|
|
130
|
+
let phase = 0;
|
|
131
|
+
const step = 2 * Math.PI * Math.max(0.1, rateHz) / sr;
|
|
132
|
+
for (let i = 0; i < n; i++) {
|
|
133
|
+
const delaySec = Math.min(maxSec, Math.max(0.0005, baseMs / 1000 + (wobbleMs / 2000) * Math.sin(phase) + 0.0005));
|
|
134
|
+
const d = delaySec * sr;
|
|
135
|
+
const i0 = Math.floor(d);
|
|
136
|
+
const frac = d - i0;
|
|
137
|
+
const a = buf[(write - i0 + len) % len];
|
|
138
|
+
const b = buf[(write - i0 - 1 + len) % len];
|
|
139
|
+
out[i] = a + (b - a) * frac;
|
|
140
|
+
buf[write] = input[i];
|
|
141
|
+
write = (write + 1) % len;
|
|
142
|
+
phase += step;
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function echo(input, sr, mix, timeMs, feedback) {
|
|
148
|
+
if (mix <= 0) return new Float32Array(input.length);
|
|
149
|
+
const d = Math.max(1, Math.round(Math.min(0.5, timeMs / 1000) * sr));
|
|
150
|
+
const buf = new Float32Array(d);
|
|
151
|
+
const out = new Float32Array(input.length);
|
|
152
|
+
const lp = 1 - Math.exp(-2 * Math.PI * 3500 / sr);
|
|
153
|
+
let idx = 0, low = 0;
|
|
154
|
+
const fb = Math.min(0.8, Math.max(0, feedback));
|
|
155
|
+
for (let i = 0; i < input.length; i++) {
|
|
156
|
+
const delayed = buf[idx];
|
|
157
|
+
low += (delayed - low) * lp;
|
|
158
|
+
buf[idx] = input[i] + low * fb;
|
|
159
|
+
out[i] = delayed * mix;
|
|
160
|
+
idx = (idx + 1) % d;
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function reverb(input, sr, mix, decaySec) {
|
|
166
|
+
if (mix <= 0) return new Float32Array(input.length);
|
|
167
|
+
const combTimes = [0.0297, 0.0331, 0.0405, 0.0437];
|
|
168
|
+
const allTimes = [0.005, 0.0017];
|
|
169
|
+
const wet = new Float32Array(input.length);
|
|
170
|
+
let scale = 0;
|
|
171
|
+
for (const t of combTimes) {
|
|
172
|
+
const len = Math.max(4, Math.round(t * sr));
|
|
173
|
+
const buf = new Float32Array(len);
|
|
174
|
+
const fb = Math.min(0.92, Math.pow(0.001, (len / sr) / Math.max(0.1, decaySec)));
|
|
175
|
+
let idx = 0;
|
|
176
|
+
for (let i = 0; i < input.length; i++) {
|
|
177
|
+
const v = buf[idx];
|
|
178
|
+
buf[idx] = input[i] + v * fb;
|
|
179
|
+
wet[i] += v;
|
|
180
|
+
idx = (idx + 1) % len;
|
|
181
|
+
}
|
|
182
|
+
scale += 1;
|
|
183
|
+
}
|
|
184
|
+
for (let i = 0; i < wet.length; i++) wet[i] /= scale;
|
|
185
|
+
for (const t of allTimes) {
|
|
186
|
+
const len = Math.max(2, Math.round(t * sr));
|
|
187
|
+
const buf = new Float32Array(len);
|
|
188
|
+
let idx = 0;
|
|
189
|
+
for (let i = 0; i < wet.length; i++) {
|
|
190
|
+
const v = buf[idx];
|
|
191
|
+
buf[idx] = wet[i] + v * 0.5;
|
|
192
|
+
wet[i] = -v + wet[i] * 0.5;
|
|
193
|
+
idx = (idx + 1) % len;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
for (let i = 0; i < wet.length; i++) wet[i] *= mix;
|
|
197
|
+
return wet;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Apply the Space's effect settings to PCM audio. Returns a new Float32Array.
|
|
201
|
+
export function applyEffects(audio, options = {}) {
|
|
202
|
+
const raw = options.effects ?? options.settings ?? options;
|
|
203
|
+
if (raw === 'No effects' || (raw && typeof raw === 'object' && raw.preset === 'No effects')) {
|
|
204
|
+
return Float32Array.from(audio);
|
|
205
|
+
}
|
|
206
|
+
const sampleRate = Number(options.sampleRate ?? DEFAULT_SAMPLE_RATE);
|
|
207
|
+
const q = resolveSettings(raw);
|
|
208
|
+
const n = audio.length;
|
|
209
|
+
|
|
210
|
+
let tone = filter(audio, biquad('peaking', sampleRate, 350, 1, q.bdG));
|
|
211
|
+
tone = filter(tone, biquad('peaking', sampleRate, 2800, 1, q.prG));
|
|
212
|
+
tone = filter(tone, biquad('highshelf', sampleRate, 7000, 0.707, q.aiG));
|
|
213
|
+
|
|
214
|
+
const mix = new Float32Array(tone);
|
|
215
|
+
|
|
216
|
+
if (q.exM > 0) {
|
|
217
|
+
const hp = filter(tone, biquad('highpass', sampleRate, 3500));
|
|
218
|
+
const k = 1 + 0.4 * 40;
|
|
219
|
+
const sh = new Float32Array(n);
|
|
220
|
+
for (let i = 0; i < n; i++) sh[i] = Math.tanh(k * hp[i]) / Math.tanh(k);
|
|
221
|
+
const sh2 = filter(sh, biquad('highpass', sampleRate, 3500));
|
|
222
|
+
const amount = q.exM * 1.5;
|
|
223
|
+
for (let i = 0; i < n; i++) mix[i] += sh2[i] * amount;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (q.l2G > 0) {
|
|
227
|
+
let layer = filter(tone, biquad('highpass', sampleRate, 300));
|
|
228
|
+
layer = filter(layer, biquad('lowpass', sampleRate, 9000));
|
|
229
|
+
layer = modulatedDelay(layer, sampleRate, q.l2D, q.l2M, q.l2R);
|
|
230
|
+
for (let i = 0; i < n; i++) mix[i] += layer[i] * q.l2G;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (q.ecM > 0) {
|
|
234
|
+
const tail = echo(tone, sampleRate, q.ecM, q.ecT, q.ecF);
|
|
235
|
+
for (let i = 0; i < n; i++) mix[i] += tail[i];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (q.rvM > 0) {
|
|
239
|
+
const room = reverb(mix, sampleRate, q.rvM, q.rvD);
|
|
240
|
+
for (let i = 0; i < n; i++) mix[i] += room[i];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const gain = Math.pow(10, q.mG / 20);
|
|
244
|
+
const out = new Float32Array(n);
|
|
245
|
+
for (let i = 0; i < n; i++) {
|
|
246
|
+
const x = mix[i] * gain;
|
|
247
|
+
out[i] = Math.tanh(x * 1.1) / Math.tanh(1.1); // simple limiter
|
|
248
|
+
}
|
|
249
|
+
return out;
|
|
250
|
+
}
|