@madeinoz67/voice-server 0.1.9 → 0.1.11

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.
@@ -100,21 +100,24 @@ export class MLXTTSClient {
100
100
  args.push("--speed", this.config.speed.toString());
101
101
  }
102
102
 
103
- // For streaming, use smaller interval for smoother playback (< 0.5s)
104
- const streamingInterval = stream ? (this.config.streamingInterval || 0.3) : 2.0;
105
- args.push("--streaming_interval", streamingInterval.toString());
106
-
107
- // Add --play flag for streaming mode to enable audio playback
103
+ // Only add streaming_interval for actual streaming mode
104
+ // Do NOT add --play when using afplay for playback (avoids double audio)
105
+ // When stream=false, generate file without any streaming behavior
108
106
  if (stream) {
109
- args.push("--play");
107
+ // For streaming, use smaller interval for smoother playback (< 0.5s)
108
+ const streamingInterval = this.config.streamingInterval || 0.3;
109
+ args.push("--streaming_interval", streamingInterval.toString());
110
+ // NOTE: We DON'T add --play here because we use afplay instead
111
+ // This prevents MLX-audio from playing audio through sounddevice
110
112
  }
111
113
 
112
- logger.debug("Executing MLX-audio command", { args: args.join(" ") });
114
+ logger.info("Executing MLX-audio command", { args: args.join(" ") });
113
115
 
114
- // For streaming mode, MLX-audio plays audio directly via sounddevice
115
- // We just need to wait for completion and return minimal response
116
+ // For streaming mode, generate audio to file and play with afplay
117
+ // This avoids MLX-audio's buggy sounddevice implementation that
118
+ // closes the audio stream before playback completes
116
119
  if (stream) {
117
- logger.info("MLX-audio streaming mode - audio will play directly via sounddevice");
120
+ logger.info("MLX-audio streaming mode - generating audio file for playback");
118
121
 
119
122
  // Set ESPEAK_DATA_PATH for Kokoro model
120
123
  const env = {
@@ -122,31 +125,83 @@ export class MLXTTSClient {
122
125
  ESPEAK_DATA_PATH: "/opt/homebrew/Cellar/espeak-ng/1.52.0/share/espeak-ng-data",
123
126
  };
124
127
 
125
- // Use inherit for stderr so sounddevice can access audio device properly
126
- // and we can see any error messages
128
+ // Generate audio file WITHOUT --play flag
127
129
  const proc = Bun.spawn(args, {
128
- stdout: "inherit",
129
- stderr: "inherit",
130
+ stdout: "pipe",
131
+ stderr: "pipe",
130
132
  env,
131
133
  });
132
134
 
133
- // Wait for streaming to complete
135
+ // Wait for generation to complete
136
+ const stderr = await new Promise<string>((resolve) => {
137
+ const reader = proc.stderr!.getReader();
138
+ const chunks: Buffer[] = [];
139
+
140
+ const read = () => {
141
+ reader.read().then(({ done, value }) => {
142
+ if (done) {
143
+ resolve(Buffer.concat(chunks).toString());
144
+ return;
145
+ }
146
+ if (value) {
147
+ chunks.push(value as Buffer);
148
+ }
149
+ read();
150
+ });
151
+ };
152
+ read();
153
+ });
154
+
134
155
  const exitCode = await proc.exited;
135
156
 
136
157
  if (exitCode !== 0) {
137
- throw new Error(`MLX-audio streaming failed with exit code ${exitCode}`);
158
+ throw new Error(`MLX-audio generation failed with exit code ${exitCode}: ${stderr}`);
159
+ }
160
+
161
+ // Find the generated audio file
162
+ const audioFile = `${filePrefix}_000.wav`;
163
+ if (!existsSync(audioFile)) {
164
+ throw new Error(`MLX-audio did not generate expected audio file: ${audioFile}`);
165
+ }
166
+
167
+ logger.info("Audio file generated, playing with afplay", { path: audioFile });
168
+
169
+ // Play audio with afplay (macOS native, reliable sequential playback)
170
+ const playStartTime = Date.now();
171
+ logger.info("Spawning afplay process", { file: audioFile });
172
+
173
+ const afplayProc = Bun.spawn(["afplay", audioFile], {
174
+ stdout: "pipe",
175
+ stderr: "pipe",
176
+ });
177
+
178
+ logger.info("afplay spawned", { pid: afplayProc.pid });
179
+ const afplayExitCode = await afplayProc.exited;
180
+ logger.info("afplay exited", { pid: afplayProc.pid, exitCode: afplayExitCode });
181
+
182
+ if (afplayExitCode !== 0) {
183
+ logger.warn("afplay exited with non-zero code", { code: afplayExitCode });
184
+ }
185
+
186
+ const playTimeMs = Date.now() - playStartTime;
187
+
188
+ // Clean up temp file after playback
189
+ try {
190
+ unlinkSync(audioFile);
191
+ } catch {
192
+ logger.warn("Failed to clean up temp file", { path: audioFile });
138
193
  }
139
194
 
140
195
  const processingTimeMs = Date.now() - startTime;
141
196
 
142
- logger.info("MLX-audio streaming complete", {
197
+ logger.info("MLX-audio streaming with afplay complete", {
143
198
  processing_time_ms: processingTimeMs,
199
+ playback_time_ms: playTimeMs,
144
200
  });
145
201
 
146
- // Return minimal response for streaming (audio played directly)
147
202
  return {
148
- audio_data: "", // No audio data returned when streaming
149
- duration_ms: 0, // Duration unknown for streaming
203
+ audio_data: "",
204
+ duration_ms: 0,
150
205
  format: "streaming",
151
206
  sample_rate: 24000,
152
207
  };
@@ -0,0 +1,382 @@
1
+ /**
2
+ * Notification Queue Service
3
+ * FIFO queue for processing voice notifications sequentially
4
+ */
5
+
6
+ import type {
7
+ QueuedNotification,
8
+ QueueState,
9
+ QueueConfig,
10
+ QueueHealth,
11
+ QueueProcessingStatus,
12
+ EnqueueResult,
13
+ DrainResult,
14
+ QueueMetrics,
15
+ } from "@/models/queue.js";
16
+ import type { NotificationRequest } from "@/models/notification.js";
17
+ import { isValidNotificationRequest } from "@/models/notification.js";
18
+ import { getVoiceLoader } from "@/services/voice-loader.js";
19
+ import { logger } from "@/utils/logger.js";
20
+ import { createDefaultQueueConfig } from "@/models/queue.js";
21
+
22
+ /**
23
+ * TTS processor function type
24
+ * Processes a notification request by synthesizing and playing audio
25
+ */
26
+ export type TTSProcessor = (request: NotificationRequest) => Promise<void>;
27
+
28
+ /**
29
+ * Notification Queue Service
30
+ * Manages FIFO queue of notifications with single worker processing
31
+ */
32
+ export class NotificationQueue {
33
+ private queue: QueuedNotification[] = [];
34
+ private processingStatus: QueueProcessingStatus = "idle";
35
+ private isProcessing = false;
36
+ private metrics: QueueMetrics = {
37
+ itemsProcessed: 0,
38
+ itemsFailed: 0,
39
+ averageProcessingTimeMs: 0,
40
+ };
41
+ private processingTimes: number[] = [];
42
+ private ttsHealth: QueueHealth = "healthy";
43
+ private config: QueueConfig;
44
+ private ttsProcessor: TTSProcessor;
45
+
46
+ constructor(ttsProcessor: TTSProcessor, config?: Partial<QueueConfig>) {
47
+ this.ttsProcessor = ttsProcessor;
48
+ this.config = { ...createDefaultQueueConfig(), ...config };
49
+ logger.info("NotificationQueue initialized", {
50
+ maxDepth: this.config.maxDepth,
51
+ degradedThreshold: this.config.degradedThreshold,
52
+ });
53
+ }
54
+
55
+ /**
56
+ * Enqueue a notification request
57
+ * Returns immediately with 201 Accepted if successful
58
+ */
59
+ async enqueue(request: NotificationRequest): Promise<EnqueueResult> {
60
+ // Reject if draining or stopped
61
+ if (this.processingStatus === "draining" || this.processingStatus === "stopped") {
62
+ return {
63
+ success: false,
64
+ statusCode: 503,
65
+ message: `Queue is ${this.processingStatus}, not accepting new items`,
66
+ };
67
+ }
68
+
69
+ // Validate request before queuing
70
+ if (!isValidNotificationRequest(request)) {
71
+ return {
72
+ success: false,
73
+ statusCode: 400,
74
+ message: "Invalid notification request: missing required fields or invalid data",
75
+ };
76
+ }
77
+
78
+ // Validate voice_id
79
+ const voiceLoader = getVoiceLoader();
80
+ try {
81
+ const voiceInfo = voiceLoader.getVoiceInfo(request.voice_id || request.voice_name || "");
82
+ if (!voiceInfo) {
83
+ // Check if it's a known voice ID from the voice loader
84
+ const availableVoices = await voiceLoader.getAvailableVoices();
85
+ const voiceId = request.voice_id || request.voice_name || "";
86
+ if (!availableVoices.includes(voiceId) && voiceId !== "marrvin" && voiceId !== "marlin" && voiceId !== "daniel") {
87
+ return {
88
+ success: false,
89
+ statusCode: 400,
90
+ message: `Invalid voice_id: ${voiceId}. Available voices: ${availableVoices.join(", ")}`,
91
+ };
92
+ }
93
+ }
94
+ } catch (error) {
95
+ logger.warn("Voice validation failed, proceeding with default voice", {
96
+ error: (error as Error).message,
97
+ });
98
+ }
99
+
100
+ // Check queue depth
101
+ if (this.queue.length >= this.config.maxDepth) {
102
+ logger.warn("Queue is full, rejecting request", {
103
+ depth: this.queue.length,
104
+ maxDepth: this.config.maxDepth,
105
+ });
106
+ return {
107
+ success: false,
108
+ statusCode: 429,
109
+ message: `Queue is full (${this.queue.length}/${this.config.maxDepth} items)`,
110
+ };
111
+ }
112
+
113
+ // Create queued item
114
+ const item: QueuedNotification = {
115
+ id: this.generateId(),
116
+ request: { ...request },
117
+ status: "pending",
118
+ arrivedAt: new Date(),
119
+ };
120
+
121
+ this.queue.push(item);
122
+ logger.info("Notification queued", {
123
+ id: item.id,
124
+ position: this.queue.length - 1,
125
+ depth: this.queue.length,
126
+ });
127
+
128
+ // Start processing if not already running
129
+ this.processQueue().catch((error) => {
130
+ logger.error("Error in queue processor", error);
131
+ });
132
+
133
+ return {
134
+ success: true,
135
+ statusCode: 201,
136
+ message: "Notification accepted and queued",
137
+ queuePosition: this.queue.length - 1,
138
+ itemId: item.id,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Get current queue state
144
+ */
145
+ getQueueState(): QueueState {
146
+ const health = this.calculateHealth();
147
+ return {
148
+ depth: this.queue.length,
149
+ processingStatus: this.processingStatus,
150
+ health,
151
+ metrics: { ...this.metrics },
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Drain the queue (process all remaining items)
157
+ * Called during graceful shutdown
158
+ */
159
+ async drain(): Promise<DrainResult> {
160
+ if (this.processingStatus === "draining") {
161
+ // Already draining
162
+ return await this.waitForDrain();
163
+ }
164
+
165
+ logger.info("Starting queue drain", {
166
+ itemsRemaining: this.queue.length,
167
+ timeoutMs: this.config.drainTimeoutMs,
168
+ });
169
+
170
+ this.processingStatus = "draining";
171
+
172
+ try {
173
+ // Wait for queue to empty or timeout
174
+ const result = await this.waitForDrain();
175
+
176
+ if (result.timedOut) {
177
+ logger.warn("Queue drain timed out", {
178
+ remainingItems: result.remainingItems,
179
+ });
180
+ } else {
181
+ logger.info("Queue drain complete", {
182
+ itemsProcessed: result.itemsProcessed,
183
+ itemsFailed: result.itemsFailed,
184
+ });
185
+ }
186
+
187
+ return result;
188
+ } finally {
189
+ this.processingStatus = "stopped";
190
+ this.stop();
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Stop the queue (for testing or shutdown)
196
+ */
197
+ stop(): void {
198
+ this.processingStatus = "stopped";
199
+ this.isProcessing = false;
200
+ logger.debug("NotificationQueue stopped");
201
+ }
202
+
203
+ /**
204
+ * Process queue items sequentially
205
+ */
206
+ private async processQueue(): Promise<void> {
207
+ // Prevent concurrent processing
208
+ if (this.isProcessing) {
209
+ return;
210
+ }
211
+
212
+ // Check if we should be processing
213
+ if (this.processingStatus === "draining" && this.queue.length === 0) {
214
+ return;
215
+ }
216
+
217
+ this.isProcessing = true;
218
+
219
+ try {
220
+ while (this.queue.length > 0 && this.processingStatus !== "stopped") {
221
+ const item = this.queue[0];
222
+
223
+ // Skip already processed items
224
+ if (item.status !== "pending") {
225
+ this.queue.shift();
226
+ continue;
227
+ }
228
+
229
+ // Update status
230
+ this.processingStatus = "active";
231
+ item.status = "processing";
232
+ item.processingStartedAt = new Date();
233
+
234
+ logger.info("Processing queued notification", {
235
+ id: item.id,
236
+ position: 0,
237
+ });
238
+
239
+ const startTime = Date.now();
240
+
241
+ try {
242
+ // Process TTS
243
+ await this.ttsProcessor(item.request);
244
+
245
+ // Mark as completed
246
+ item.status = "completed";
247
+ item.completedAt = new Date();
248
+
249
+ const processingTime = Date.now() - startTime;
250
+ this.recordProcessingTime(processingTime);
251
+ this.metrics.itemsProcessed++;
252
+
253
+ logger.info("Notification processed successfully", {
254
+ id: item.id,
255
+ processingTimeMs: processingTime,
256
+ });
257
+
258
+ // Update TTS health
259
+ this.ttsHealth = "healthy";
260
+
261
+ } catch (error) {
262
+ // Mark as failed but continue processing
263
+ item.status = "failed";
264
+ item.completedAt = new Date();
265
+ item.error = (error as Error).message;
266
+
267
+ this.metrics.itemsFailed++;
268
+
269
+ logger.warn("Notification processing failed, skipping to next item", {
270
+ id: item.id,
271
+ error: (error as Error).message,
272
+ });
273
+
274
+ // Update TTS health if failing
275
+ this.ttsHealth = "unavailable";
276
+ }
277
+
278
+ // Remove from queue
279
+ this.queue.shift();
280
+ }
281
+
282
+ // Reset status when queue is empty
283
+ if (this.queue.length === 0) {
284
+ this.processingStatus = this.processingStatus === "draining" ? "draining" : "idle";
285
+ }
286
+ } finally {
287
+ this.isProcessing = false;
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Wait for queue to drain (process all items)
293
+ */
294
+ private async waitForDrain(): Promise<DrainResult> {
295
+ const startTime = Date.now();
296
+ const initialProcessed = this.metrics.itemsProcessed;
297
+ const initialFailed = this.metrics.itemsFailed;
298
+
299
+ return new Promise<DrainResult>((resolve) => {
300
+ const checkInterval = setInterval(() => {
301
+ const elapsed = Date.now() - startTime;
302
+
303
+ if (this.queue.length === 0 || this.processingStatus === "stopped") {
304
+ clearInterval(checkInterval);
305
+ resolve({
306
+ success: true,
307
+ itemsProcessed: this.metrics.itemsProcessed - initialProcessed,
308
+ itemsFailed: this.metrics.itemsFailed - initialFailed,
309
+ timedOut: false,
310
+ });
311
+ return;
312
+ }
313
+
314
+ if (elapsed >= this.config.drainTimeoutMs) {
315
+ clearInterval(checkInterval);
316
+ resolve({
317
+ success: false,
318
+ itemsProcessed: this.metrics.itemsProcessed - initialProcessed,
319
+ itemsFailed: this.metrics.itemsFailed - initialFailed,
320
+ timedOut: true,
321
+ remainingItems: this.queue.length,
322
+ });
323
+ return;
324
+ }
325
+ }, 50);
326
+ });
327
+ }
328
+
329
+ /**
330
+ * Calculate health indicator based on queue state and TTS status
331
+ */
332
+ private calculateHealth(): QueueHealth {
333
+ // If TTS has been failing, report unavailable
334
+ if (this.ttsHealth === "unavailable") {
335
+ // Check if we've had recent successes
336
+ const recentFailures = this.metrics.itemsFailed > 0;
337
+ if (recentFailures && this.metrics.itemsProcessed === 0) {
338
+ return "unavailable";
339
+ }
340
+ }
341
+
342
+ // Check if queue depth exceeds degraded threshold
343
+ if (this.queue.length >= this.config.degradedThreshold) {
344
+ return "degraded";
345
+ }
346
+
347
+ return "healthy";
348
+ }
349
+
350
+ /**
351
+ * Record processing time for metrics
352
+ */
353
+ private recordProcessingTime(timeMs: number): void {
354
+ this.processingTimes.push(timeMs);
355
+
356
+ // Keep only last 100 measurements
357
+ if (this.processingTimes.length > 100) {
358
+ this.processingTimes.shift();
359
+ }
360
+
361
+ // Calculate average
362
+ const sum = this.processingTimes.reduce((a, b) => a + b, 0);
363
+ this.metrics.averageProcessingTimeMs = Math.round(sum / this.processingTimes.length);
364
+ }
365
+
366
+ /**
367
+ * Generate unique ID for queued item
368
+ */
369
+ private generateId(): string {
370
+ return `queue_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Create a notification queue with default TTS processor
376
+ */
377
+ export function createNotificationQueue(
378
+ ttsProcessor: TTSProcessor,
379
+ config?: Partial<QueueConfig>
380
+ ): NotificationQueue {
381
+ return new NotificationQueue(ttsProcessor, config);
382
+ }