fixup! feat(speech): the speech capability over Cap'n Web, behind a gate
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
This commit is contained in:
1 parent
372f6a2b96
commit
a97ba6c174
12 files changed
+245
-92
No files matched your search
+42
-26
@@ -12,33 +12,49 @@ export const MAX_CLIP_BYTES = 4_000_000;
|
||||
export const MAX_SPEAK_CHARS = 1000;
|
||||
|
||||
/**
|
||||
* Speech: transcribe(clip) returns the text; speak(text) returns a WAV of it.
|
||||
* transcribe(clip) returns the text; speak(text) returns a WAV of it.
|
||||
* One job at a time: whisper and piper each use all of a small server's cores.
|
||||
* @param {(clip: Uint8Array) => Promise<string>} transcribe
|
||||
* @param {(text: string) => Promise<Uint8Array>} [speak]
|
||||
*/
|
||||
export const makeSpeech = (transcribe, speak) => {
|
||||
let last = Promise.resolve();
|
||||
const inTurn = (job) => {
|
||||
const result = last.then(job);
|
||||
last = result.catch(() => {});
|
||||
return result;
|
||||
};
|
||||
class Speech extends RpcTarget {
|
||||
/** @param {Uint8Array} clip a WAV file */
|
||||
transcribe(clip) {
|
||||
if (!(clip instanceof Uint8Array)) throw new TypeError('clip must be bytes');
|
||||
if (clip.length > MAX_CLIP_BYTES) throw new RangeError(`clip over ${MAX_CLIP_BYTES} bytes`);
|
||||
return inTurn(() => transcribe(clip));
|
||||
}
|
||||
export class Speech extends RpcTarget {
|
||||
#transcribe;
|
||||
#speak;
|
||||
/** @type {Promise<unknown>} the job before this one */
|
||||
#last = Promise.resolve();
|
||||
|
||||
/** @param {string} text */
|
||||
speak(text) {
|
||||
if (!speak) throw new Error('no voice here');
|
||||
if (typeof text !== 'string' || !text.trim()) throw new TypeError('speak needs text');
|
||||
if (text.length > MAX_SPEAK_CHARS) throw new RangeError(`text over ${MAX_SPEAK_CHARS} chars`);
|
||||
return inTurn(() => speak(text));
|
||||
}
|
||||
/**
|
||||
* @param {(clip: Uint8Array) => Promise<string>} transcribe
|
||||
* @param {(text: string) => Promise<Uint8Array>} [speak]
|
||||
*/
|
||||
constructor(transcribe, speak) {
|
||||
super();
|
||||
this.#transcribe = transcribe;
|
||||
this.#speak = speak;
|
||||
}
|
||||
return new Speech();
|
||||
};
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {() => Promise<T>} job
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
#inTurn(job) {
|
||||
const result = this.#last.then(job);
|
||||
this.#last = result.catch(() => {});
|
||||
return result;
|
||||
}
|
||||
|
||||
/** @param {Uint8Array} clip a WAV file */
|
||||
transcribe(clip) {
|
||||
if (!(clip instanceof Uint8Array)) throw new TypeError('clip must be bytes');
|
||||
if (clip.length > MAX_CLIP_BYTES) throw new RangeError(`clip over ${MAX_CLIP_BYTES} bytes`);
|
||||
return this.#inTurn(() => this.#transcribe(clip));
|
||||
}
|
||||
|
||||
/** @param {string} text */
|
||||
speak(text) {
|
||||
const speak = this.#speak;
|
||||
if (!speak) throw new Error('no voice here');
|
||||
if (typeof text !== 'string' || !text.trim()) throw new TypeError('speak needs text');
|
||||
if (text.length > MAX_SPEAK_CHARS) throw new RangeError(`text over ${MAX_SPEAK_CHARS} chars`);
|
||||
return this.#inTurn(() => speak(text));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user