@adea-ai/voice 0.0.0-stage → 0.4.0
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/README.md +136 -2
- package/dist/cli/helpers-cli.d.ts +2 -0
- package/dist/cli/helpers-cli.js +117 -0
- package/dist/contract/contract.d.ts +157 -0
- package/dist/contract/errors.d.ts +26 -0
- package/dist/contract/index.d.ts +10 -0
- package/dist/contract/index.js +64 -0
- package/dist/contract/session.d.ts +71 -0
- package/dist/contract/turns.d.ts +91 -0
- package/dist/contract/wire.d.ts +279 -0
- package/dist/desktop/conversation.d.ts +86 -0
- package/dist/desktop/desktop-voice.d.ts +212 -0
- package/dist/desktop/helper-lock.d.ts +121 -0
- package/dist/desktop/helper-process.d.ts +96 -0
- package/dist/desktop/index.d.ts +9 -0
- package/dist/desktop/index.js +1454 -0
- package/dist/desktop/macho-digest.d.ts +9 -0
- package/dist/index-7n7epq1a.js +47 -0
- package/dist/index-a7d617zr.js +295 -0
- package/dist/index-ght8wwpd.js +156 -0
- package/dist/index-m4g2tpeq.js +522 -0
- package/dist/index-r4hz8gk0.js +10 -0
- package/dist/index-tvz9s9nz.js +77 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +96 -0
- package/dist/settings/index.d.ts +5 -0
- package/dist/settings/index.js +34 -0
- package/dist/settings/settings.d.ts +163 -0
- package/dist/web/index.d.ts +16 -0
- package/dist/web/index.js +1697 -0
- package/dist/web/stt/speech-recognition.d.ts +226 -0
- package/dist/web/tts/locale-match.d.ts +40 -0
- package/dist/web/tts/phonon.d.ts +139 -0
- package/dist/web/tts/player.d.ts +115 -0
- package/dist/web/tts/system-voices.d.ts +162 -0
- package/dist/web/turns.d.ts +91 -0
- package/package.json +68 -4
- package/src/cli/helpers-cli.ts +135 -0
- package/src/contract/contract.ts +393 -0
- package/src/contract/errors.ts +98 -0
- package/src/contract/index.ts +10 -0
- package/src/contract/session.ts +77 -0
- package/src/contract/turns.ts +48 -0
- package/src/contract/wire.ts +384 -0
- package/src/desktop/conversation.ts +347 -0
- package/src/desktop/desktop-voice.ts +1129 -0
- package/src/desktop/helper-lock.ts +550 -0
- package/src/desktop/helper-process.ts +408 -0
- package/src/desktop/index.ts +18 -0
- package/src/desktop/macho-digest.ts +135 -0
- package/src/index.ts +7 -0
- package/src/settings/index.ts +5 -0
- package/src/settings/settings.ts +478 -0
- package/src/web/index.ts +16 -0
- package/src/web/stt/speech-recognition.ts +1013 -0
- package/src/web/tts/locale-match.ts +131 -0
- package/src/web/tts/phonon.ts +386 -0
- package/src/web/tts/player.ts +271 -0
- package/src/web/tts/system-voices.ts +510 -0
- package/src/web/turns.ts +235 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scheduled PCM playback for streaming synthesis.
|
|
3
|
+
*
|
|
4
|
+
* The engine yields mono float chunks in real time; this player schedules each one on the
|
|
5
|
+
* AudioContext clock with a small lead so chunk boundaries do not click, and it owns the only
|
|
6
|
+
* mutable playback state: stop (barge-in) flushes every scheduled source, suspend/resume follow
|
|
7
|
+
* the platform's audio focus rules, and dispose closes the context so nothing outlives the
|
|
8
|
+
* component.
|
|
9
|
+
*
|
|
10
|
+
* Browser globals are injected (`AudioContextFactory`), so the scheduling logic is testable.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface AudioBufferLike {
|
|
14
|
+
readonly duration: number;
|
|
15
|
+
readonly numberOfChannels?: number;
|
|
16
|
+
readonly length?: number;
|
|
17
|
+
getChannelData?(channel: number): Float32Array;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AudioBufferSourceLike {
|
|
21
|
+
buffer: unknown;
|
|
22
|
+
onended: (() => void) | null;
|
|
23
|
+
connect(destination: unknown): void;
|
|
24
|
+
disconnect?(destination?: unknown): void;
|
|
25
|
+
start(when?: number): void;
|
|
26
|
+
stop(when?: number): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface AudioContextLike {
|
|
30
|
+
readonly currentTime: number;
|
|
31
|
+
readonly destination: unknown;
|
|
32
|
+
readonly state?: string;
|
|
33
|
+
createBuffer(channels: number, length: number, sampleRate: number): AudioBufferLike;
|
|
34
|
+
createBufferSource(): AudioBufferSourceLike;
|
|
35
|
+
resume?(): Promise<void>;
|
|
36
|
+
suspend?(): Promise<void>;
|
|
37
|
+
close?(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AudioContextFactory {
|
|
41
|
+
create(options: { sampleRate: number }): AudioContextLike;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface StreamingPcmPlayerOptions {
|
|
45
|
+
readonly sampleRate: number;
|
|
46
|
+
readonly factory?: AudioContextFactory;
|
|
47
|
+
/** Scheduling lead ahead of the context clock, in seconds. Default 0.05. */
|
|
48
|
+
readonly leadSeconds?: number;
|
|
49
|
+
/** Called when the first chunk of the current utterance is scheduled. */
|
|
50
|
+
readonly onFirstAudio?: (atContextTime: number) => void;
|
|
51
|
+
/** Called when every scheduled chunk has played. */
|
|
52
|
+
readonly onDrained?: () => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const defaultAudioContextFactory: AudioContextFactory = {
|
|
56
|
+
create(options) {
|
|
57
|
+
const Constructor = (globalThis as { AudioContext?: new (options?: { sampleRate?: number }) => AudioContextLike })
|
|
58
|
+
.AudioContext;
|
|
59
|
+
if (Constructor === undefined) {
|
|
60
|
+
throw new Error("this environment has no AudioContext");
|
|
61
|
+
}
|
|
62
|
+
return new Constructor({ sampleRate: options.sampleRate });
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* One utterance's slice of the player's schedule: its own sources, its own stop, and a `drained`
|
|
68
|
+
* promise that settles when its last scheduled chunk has played (or it was stopped). Stopping one
|
|
69
|
+
* utterance leaves the others scheduled.
|
|
70
|
+
*/
|
|
71
|
+
export class PlaybackUtterance {
|
|
72
|
+
private readonly player: StreamingPcmPlayer;
|
|
73
|
+
readonly sources = new Set<AudioBufferSourceLike>();
|
|
74
|
+
private sealed = false;
|
|
75
|
+
private stoppedFlag = false;
|
|
76
|
+
private settled = false;
|
|
77
|
+
private resolveDrained: (played: boolean) => void = () => {};
|
|
78
|
+
/** Resolves `true` when everything scheduled played, `false` when the utterance was stopped. */
|
|
79
|
+
readonly drained: Promise<boolean>;
|
|
80
|
+
|
|
81
|
+
constructor(player: StreamingPcmPlayer) {
|
|
82
|
+
this.player = player;
|
|
83
|
+
this.drained = new Promise<boolean>((resolve) => {
|
|
84
|
+
this.resolveDrained = resolve;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Whether `stop()` (or the player's stop/dispose) ended this utterance. */
|
|
89
|
+
get stopped(): boolean {
|
|
90
|
+
return this.stoppedFlag;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Schedules one chunk of this utterance; null after `finish()`/`stop()` or when disposed. */
|
|
94
|
+
enqueue(pcm: Float32Array): number | null {
|
|
95
|
+
if (this.sealed || this.stoppedFlag) return null;
|
|
96
|
+
return this.player.schedule(pcm, this);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** No more chunks will come: `drained` resolves once the scheduled ones have played. */
|
|
100
|
+
finish(): Promise<boolean> {
|
|
101
|
+
this.sealed = true;
|
|
102
|
+
this.check();
|
|
103
|
+
return this.drained;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Stops and discards only this utterance's scheduled audio. Idempotent. */
|
|
107
|
+
stop(): void {
|
|
108
|
+
if (this.stoppedFlag) return;
|
|
109
|
+
this.stoppedFlag = true;
|
|
110
|
+
this.sealed = true;
|
|
111
|
+
this.player.unschedule(this);
|
|
112
|
+
this.settle(false);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @internal called by the player when one of this utterance's sources ended. */
|
|
116
|
+
ended(source: AudioBufferSourceLike): void {
|
|
117
|
+
this.sources.delete(source);
|
|
118
|
+
this.check();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private check(): void {
|
|
122
|
+
if (this.sealed && this.sources.size === 0) this.settle(!this.stoppedFlag);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private settle(played: boolean): void {
|
|
126
|
+
if (this.settled) return;
|
|
127
|
+
this.settled = true;
|
|
128
|
+
this.resolveDrained(played);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export class StreamingPcmPlayer {
|
|
133
|
+
private readonly context: AudioContextLike;
|
|
134
|
+
private readonly sampleRate: number;
|
|
135
|
+
private readonly leadSeconds: number;
|
|
136
|
+
private readonly onFirstAudio: ((atContextTime: number) => void) | undefined;
|
|
137
|
+
private readonly onDrained: (() => void) | undefined;
|
|
138
|
+
|
|
139
|
+
private sources: AudioBufferSourceLike[] = [];
|
|
140
|
+
private readonly endsAt = new Map<AudioBufferSourceLike, number>();
|
|
141
|
+
private readonly owners = new Map<AudioBufferSourceLike, PlaybackUtterance>();
|
|
142
|
+
private readonly utterances = new Set<PlaybackUtterance>();
|
|
143
|
+
private nextStartTime = 0;
|
|
144
|
+
private firstAudioReported = false;
|
|
145
|
+
private disposed = false;
|
|
146
|
+
|
|
147
|
+
constructor(options: StreamingPcmPlayerOptions) {
|
|
148
|
+
this.sampleRate = options.sampleRate;
|
|
149
|
+
this.context = (options.factory ?? defaultAudioContextFactory).create({ sampleRate: options.sampleRate });
|
|
150
|
+
this.leadSeconds = options.leadSeconds ?? 0.05;
|
|
151
|
+
this.onFirstAudio = options.onFirstAudio;
|
|
152
|
+
this.onDrained = options.onDrained;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Starts an utterance: a group of chunks with its own stop and drain. Utterances queue behind
|
|
157
|
+
* each other on the same schedule, so they never overlap.
|
|
158
|
+
*/
|
|
159
|
+
beginUtterance(): PlaybackUtterance {
|
|
160
|
+
const utterance = new PlaybackUtterance(this);
|
|
161
|
+
if (this.disposed) {
|
|
162
|
+
utterance.stop();
|
|
163
|
+
return utterance;
|
|
164
|
+
}
|
|
165
|
+
this.utterances.add(utterance);
|
|
166
|
+
void utterance.drained.then(() => this.utterances.delete(utterance));
|
|
167
|
+
return utterance;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Schedules one chunk and returns the context time it starts at, or null when disposed. */
|
|
171
|
+
enqueue(pcm: Float32Array): number | null {
|
|
172
|
+
return this.schedule(pcm, undefined);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @internal Schedules one chunk for `owner` (or for no utterance). */
|
|
176
|
+
schedule(pcm: Float32Array, owner: PlaybackUtterance | undefined): number | null {
|
|
177
|
+
if (this.disposed || pcm.length === 0) return null;
|
|
178
|
+
const buffer = this.context.createBuffer(1, pcm.length, this.sampleRate);
|
|
179
|
+
buffer.getChannelData?.(0).set(pcm);
|
|
180
|
+
const source = this.context.createBufferSource();
|
|
181
|
+
source.buffer = buffer;
|
|
182
|
+
source.connect(this.context.destination);
|
|
183
|
+
const startAt = Math.max(this.context.currentTime + this.leadSeconds, this.nextStartTime);
|
|
184
|
+
source.onended = () => {
|
|
185
|
+
this.forget(source);
|
|
186
|
+
source.disconnect?.();
|
|
187
|
+
owner?.ended(source);
|
|
188
|
+
if (this.sources.length === 0) this.onDrained?.();
|
|
189
|
+
};
|
|
190
|
+
this.sources.push(source);
|
|
191
|
+
this.endsAt.set(source, startAt + buffer.duration);
|
|
192
|
+
if (owner) {
|
|
193
|
+
this.owners.set(source, owner);
|
|
194
|
+
owner.sources.add(source);
|
|
195
|
+
}
|
|
196
|
+
source.start(startAt);
|
|
197
|
+
this.nextStartTime = startAt + buffer.duration;
|
|
198
|
+
if (!this.firstAudioReported) {
|
|
199
|
+
this.firstAudioReported = true;
|
|
200
|
+
this.onFirstAudio?.(startAt);
|
|
201
|
+
}
|
|
202
|
+
return startAt;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Stops playback and throws away everything scheduled, for every utterance: the barge-in path. */
|
|
206
|
+
stop(): void {
|
|
207
|
+
const scheduled = this.sources;
|
|
208
|
+
this.sources = [];
|
|
209
|
+
this.endsAt.clear();
|
|
210
|
+
this.owners.clear();
|
|
211
|
+
for (const source of scheduled) this.halt(source);
|
|
212
|
+
for (const utterance of [...this.utterances]) {
|
|
213
|
+
utterance.sources.clear();
|
|
214
|
+
utterance.stop();
|
|
215
|
+
}
|
|
216
|
+
this.utterances.clear();
|
|
217
|
+
this.nextStartTime = 0;
|
|
218
|
+
this.firstAudioReported = false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** @internal Stops only `utterance`'s sources and closes the gap they leave in the schedule. */
|
|
222
|
+
unschedule(utterance: PlaybackUtterance): void {
|
|
223
|
+
for (const source of utterance.sources) {
|
|
224
|
+
this.forget(source);
|
|
225
|
+
this.halt(source);
|
|
226
|
+
}
|
|
227
|
+
utterance.sources.clear();
|
|
228
|
+
this.utterances.delete(utterance);
|
|
229
|
+
let latest = 0;
|
|
230
|
+
for (const end of this.endsAt.values()) latest = Math.max(latest, end);
|
|
231
|
+
this.nextStartTime = latest;
|
|
232
|
+
if (this.sources.length === 0) this.firstAudioReported = false;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private forget(source: AudioBufferSourceLike): void {
|
|
236
|
+
this.sources = this.sources.filter((candidate) => candidate !== source);
|
|
237
|
+
this.endsAt.delete(source);
|
|
238
|
+
this.owners.delete(source);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private halt(source: AudioBufferSourceLike): void {
|
|
242
|
+
source.onended = null;
|
|
243
|
+
try {
|
|
244
|
+
source.stop();
|
|
245
|
+
} catch {
|
|
246
|
+
// stopping a source that already ended is not an error
|
|
247
|
+
}
|
|
248
|
+
source.disconnect?.();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Whether something is scheduled or playing. */
|
|
252
|
+
get isPlaying(): boolean {
|
|
253
|
+
return this.sources.length > 0;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async suspend(): Promise<void> {
|
|
257
|
+
await this.context.suspend?.();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async resume(): Promise<void> {
|
|
261
|
+
await this.context.resume?.();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Stops playback and closes the context. Idempotent. */
|
|
265
|
+
async dispose(): Promise<void> {
|
|
266
|
+
if (this.disposed) return;
|
|
267
|
+
this.stop();
|
|
268
|
+
this.disposed = true;
|
|
269
|
+
await this.context.close?.();
|
|
270
|
+
}
|
|
271
|
+
}
|