@@ -1,6 +1,23 @@
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { acquireQvac, releaseQvac, loadAuxiliaryModel, unloadAuxiliaryModel, qvacSdk, withQvacMaster, assertSdkVersion } from './qvac-master.js';
|
||||
|
||||
export function pcmS16le(samples, sampleRate = 44_100) {
|
||||
if (samples instanceof Int16Array) return { samples, sampleRate };
|
||||
const bytes = toUint8(samples);
|
||||
const even = bytes.byteLength - (bytes.byteLength % 2);
|
||||
const copy = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + even);
|
||||
return { samples: new Int16Array(copy), sampleRate };
|
||||
}
|
||||
|
||||
function toUint8(samples) {
|
||||
if (!samples) return new Uint8Array(0);
|
||||
if (samples instanceof ArrayBuffer) return new Uint8Array(samples);
|
||||
if (ArrayBuffer.isView(samples)) return new Uint8Array(samples.buffer, samples.byteOffset, samples.byteLength);
|
||||
if (typeof samples === 'string') return Uint8Array.from(Buffer.from(samples));
|
||||
if (Array.isArray(samples) || samples.length != null) return Uint8Array.from(Buffer.from(samples));
|
||||
return new Uint8Array(0);
|
||||
}
|
||||
|
||||
export class QvacVoiceAdapter extends EventEmitter {
|
||||
constructor({ asrModel = process.env.JARVIS_ASR_MODEL || 'WHISPER_TINY', ttsModel = process.env.JARVIS_TTS_MODEL || 'TTS_EN_SUPERTONIC_Q8_0' } = {}) {
|
||||
super(); this.asrModel = asrModel; this.ttsModel = ttsModel; this.asrId = null; this.ttsId = null; this.asrSession = null; this.acquired = false;
|
||||
@@ -33,10 +50,16 @@ export class QvacVoiceAdapter extends EventEmitter {
|
||||
async speak(text) {
|
||||
const sdk = await qvacSdk();
|
||||
const samples = await withQvacMaster(async () => {
|
||||
const result = sdk.textToSpeech({ modelId: this.ttsId, text: String(text), inputType: 'text', stream: false });
|
||||
return result.buffer;
|
||||
const result = await sdk.textToSpeech({ modelId: this.ttsId, text: String(text), inputType: 'text', stream: false });
|
||||
if (result?.buffer != null) return await result.buffer;
|
||||
if (result?.bufferStream) {
|
||||
const chunks = [];
|
||||
for await (const chunk of result.bufferStream) chunks.push(Buffer.from(chunk));
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
return { samples: Int16Array.from(samples), sampleRate: 44_100 };
|
||||
return pcmS16le(samples, 44_100);
|
||||
}
|
||||
|
||||
async stop() {
|
||||
|
||||
Reference in New Issue
Block a user