File size: 1,339 Bytes
b3361ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// npm i @huggingface/transformers   (Node 18+); run from the repo root:  node examples/node_example.mjs
import { writeFileSync } from 'node:fs';
import { KecilTTS, SAMPLE_RATE } from '../js/kecil-tts.js';
import { G2P } from '../js/g2p/ms_g2p.js';
import { readFileSync } from 'node:fs';

const load = (f) => JSON.parse(readFileSync(new URL('../js/g2p/' + f, import.meta.url), 'utf8'));
const g2p = new G2P({ vocab: load('vocab.json'), lexE: load('lexicon_e.json'), lexX: load('lexicon_exceptions.json'), lexEn: load('lexicon_en.json') },
  { stress: false, r: 'r', diph: 'glide' });
const tts = await KecilTTS.load({ model: 'Suarify/kecil-tts-michael-v0.2', g2p });
const { audio } = await tts.speak('Selamat pagi, boleh saya bantu encik hari ini?');
const pcm = Buffer.alloc(44 + audio.length * 2);
pcm.write('RIFF', 0); pcm.writeUInt32LE(36 + audio.length * 2, 4); pcm.write('WAVEfmt ', 8); pcm.writeUInt32LE(16, 16); pcm.writeUInt16LE(1, 20);
pcm.writeUInt16LE(1, 22); pcm.writeUInt32LE(SAMPLE_RATE, 24); pcm.writeUInt32LE(SAMPLE_RATE * 2, 28); pcm.writeUInt16LE(2, 32); pcm.writeUInt16LE(16, 34);
pcm.write('data', 36); pcm.writeUInt32LE(audio.length * 2, 40);
audio.forEach((x, i) => pcm.writeInt16LE(Math.round(Math.max(-1, Math.min(1, x)) * 32767), 44 + i * 2));
writeFileSync('hello.wav', pcm);
console.log('hello.wav written');