/* Does the locally-exported Tamil ONNX load and speak through Transformers.js? */ import { pipeline, env } from '@huggingface/transformers'; import fs from 'node:fs'; // Load from the local folder, not the Hub. env.allowRemoteModels = false; env.localModelPath = './assets/tts'; for (const dtype of ['q8', 'fp32']) { try { const t0 = performance.now(); const tts = await pipeline('text-to-speech', 'mms-tts-tam', { dtype }); const load = performance.now() - t0; const TEXT = 'மூவாயிரம் நானூற்று இருபத்தேழு புதிய லீட்கள் உள்ளன.'; await tts(TEXT); // warm const t1 = performance.now(); const out = await tts(TEXT); const ms = performance.now() - t1; const audioMs = (out.audio.length / out.sampling_rate) * 1000; console.log( `${dtype.padEnd(5)} load ${(load / 1000).toFixed(1)}s | ` + `${ms.toFixed(0)}ms for ${audioMs.toFixed(0)}ms audio @ ${out.sampling_rate}Hz | ` + `RTF ${(ms / audioMs).toFixed(2)}x`, ); // Non-silent output is the real proof the graph is wired correctly. const peak = out.audio.reduce((m, v) => Math.max(m, Math.abs(v)), 0); console.log(` samples ${out.audio.length}, peak amplitude ${peak.toFixed(3)} ${peak > 0.01 ? '✅ audible' : '⚠️ SILENT'}`); if (dtype === 'q8') { const wav = toWav(out.audio, out.sampling_rate); fs.writeFileSync('scripts/tamil-sample.wav', wav); console.log(' wrote scripts/tamil-sample.wav — play it to judge quality'); } } catch (e) { console.log(`${dtype.padEnd(5)} FAILED: ${e.message.slice(0, 160)}`); } } function toWav(samples, rate) { const buf = Buffer.alloc(44 + samples.length * 2); buf.write('RIFF', 0); buf.writeUInt32LE(36 + samples.length * 2, 4); buf.write('WAVE', 8); buf.write('fmt ', 12); buf.writeUInt32LE(16, 16); buf.writeUInt16LE(1, 20); buf.writeUInt16LE(1, 22); buf.writeUInt32LE(rate, 24); buf.writeUInt32LE(rate * 2, 28); buf.writeUInt16LE(2, 32); buf.writeUInt16LE(16, 34); buf.write('data', 36); buf.writeUInt32LE(samples.length * 2, 40); for (let i = 0; i < samples.length; i++) { const s = Math.max(-1, Math.min(1, samples[i])); buf.writeInt16LE(s < 0 ? s * 0x8000 : s * 0x7fff, 44 + i * 2); } return buf; } process.exit(0);