File size: 2,967 Bytes
d9e3e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**

 * @fileoverview TTS service REST routes (Hono). Mounted at `/api/tts`.

 */
import { Hono, type Context } from "hono";
import { createLogger } from "@mentra/cloud-shared";
import {
  DEFAULT_TTS_AUDIO_FORMAT,
  isTtsAudioFormat,
  ttsService,
  type TtsAudioFormat,
} from "../services/tts/tts.service";

const logger = createLogger("tts").child({ service: "tts.api" });

export const ttsApi = new Hono();

type SpeakRequest =
  | {
      ok: true;
      text: string;
      voiceId?: string;
      modelId?: string;
      voiceSettings?: Record<string, unknown>;
      format: TtsAudioFormat;
    }
  | { ok: false; response: Response };

function parseSpeakRequest(c: Context): SpeakRequest {
  const text = c.req.query("text");
  const voiceId = c.req.query("voice_id");
  const modelId = c.req.query("model_id");
  const voiceSettingsRaw = c.req.query("voice_settings");
  const formatRaw = c.req.query("format") || DEFAULT_TTS_AUDIO_FORMAT;

  if (!text) {
    return {
      ok: false,
      response: c.json(
        {
          success: false,
          message: "Text parameter is required and must be a string",
        },
        400,
      ),
    };
  }

  if (!isTtsAudioFormat(formatRaw)) {
    return {
      ok: false,
      response: c.json(
        {
          success: false,
          message: `Unsupported format: ${formatRaw}`,
        },
        400,
      ),
    };
  }

  let voiceSettings: Record<string, unknown> | undefined;
  if (voiceSettingsRaw) {
    try {
      voiceSettings = JSON.parse(voiceSettingsRaw) as Record<string, unknown>;
    } catch (error) {
      logger.error({ error }, "invalid voice_settings JSON format");
      return {
        ok: false,
        response: c.json(
          {
            success: false,
            message: "Invalid voice_settings JSON format",
          },
          400,
        ),
      };
    }
  }

  return {
    ok: true,
    text,
    voiceId,
    modelId,
    voiceSettings,
    format: formatRaw,
  };
}

/**

 * GET /api/tts/speak

 *

 * Stream TTS audio for host playback. The cloud-client owns this endpoint

 * shape and preflights it before handing a source URL to the mobile player.

 * Hono dispatches HEAD through GET and strips the body, so branch here to keep

 * preflight from synthesizing audio.

 *

 * `format` selects the encoding: `mp3` (default) or `pcm_24000`, raw signed

 * 16-bit little-endian mono PCM at 24 kHz that the Mentra App streams into its

 * PCM player as it arrives.

 */
ttsApi.get("/speak", async (c) => {
  const parsed = parseSpeakRequest(c);
  if (!parsed.ok) return parsed.response;
  if (c.req.method === "HEAD") return ttsService.preflight(parsed.format);

  return ttsService.speak(parsed.text, {
    voiceId: parsed.voiceId,
    modelId: parsed.modelId,
    voiceSettings: parsed.voiceSettings,
    format: parsed.format,
  });
});