惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 司徒正美
A
About on SuperTechFans
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
H
Help Net Security
量子位
IT之家
IT之家

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(tts): pre-transcode synthesized audio to opus-in-CAF ...
omarshahine · 2026-04-28 · via Recent Commits to openclaw:main

@@ -0,0 +1,134 @@

1+

import { spawn } from "node:child_process";

2+

import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";

3+

import { join } from "node:path";

4+

import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/sandbox";

5+6+

/** Container token (file-extension shape, no leading dot) the host knows how

7+

* to pre-transcode into. Update in lockstep with `pickAfconvertRecipe`. */

8+

export type HostTranscodableContainer = "caf";

9+10+

export type TranscodeOutcome =

11+

| { ok: true; buffer: Buffer }

12+

| {

13+

ok: false;

14+

reason:

15+

| "platform-unsupported"

16+

| "invalid-extension"

17+

| "noop-same-container"

18+

| "no-recipe"

19+

| "transcoder-failed";

20+

detail?: string;

21+

};

22+23+

/**

24+

* Best-effort audio container transcode using macOS `afconvert`.

25+

*

26+

* Used by the TTS pipeline to pre-encode synthesized audio into a channel's

27+

* preferred container (see `ChannelTtsVoiceDeliveryCapabilities.preferAudioFileFormat`)

28+

* so the channel's downstream does not have to perform a container

29+

* conversion of its own. Returns a discriminated outcome so callers can

30+

* distinguish "we didn't try" (platform/recipe/noop) from "we tried and the

31+

* transcoder failed", which is the case worth logging.

32+

*

33+

* Currently only macOS is supported because `afconvert` is the only widely

34+

* available encoder we ship a recipe for.

35+

*/

36+

export async function transcodeAudioBuffer(params: {

37+

audioBuffer: Buffer;

38+

sourceExtension: string;

39+

targetExtension: string;

40+

timeoutMs?: number;

41+

}): Promise<TranscodeOutcome> {

42+

// Validate inputs first so callers get a specific reason regardless of

43+

// host platform. Platform-unsupported is the gate immediately before the

44+

// actual `afconvert` invocation.

45+

const source = normalizeExt(params.sourceExtension);

46+

const target = normalizeExt(params.targetExtension);

47+

if (!source || !target) {

48+

return { ok: false, reason: "invalid-extension" };

49+

}

50+

if (source === target) {

51+

return { ok: false, reason: "noop-same-container" };

52+

}

53+

const recipe = pickAfconvertRecipe(source, target);

54+

if (!recipe) {

55+

return { ok: false, reason: "no-recipe" };

56+

}

57+

if (process.platform !== "darwin") {

58+

return { ok: false, reason: "platform-unsupported" };

59+

}

60+61+

const tmpRoot = resolvePreferredOpenClawTmpDir();

62+

mkdirSync(tmpRoot, { recursive: true, mode: 0o700 });

63+

const tmpDir = mkdtempSync(join(tmpRoot, "tts-transcode-"));

64+

const inPath = join(tmpDir, `in.${source}`);

65+

const outPath = join(tmpDir, `out.${target}`);

66+

try {

67+

writeFileSync(inPath, params.audioBuffer, { mode: 0o600 });

68+

const result = await runAfconvert({

69+

args: [...recipe, inPath, outPath],

70+

timeoutMs: params.timeoutMs ?? 5000,

71+

});

72+

if (!result.ok) {

73+

return { ok: false, reason: "transcoder-failed", detail: result.detail };

74+

}

75+

return { ok: true, buffer: readFileSync(outPath) };

76+

} catch (err) {

77+

return { ok: false, reason: "transcoder-failed", detail: (err as Error).message };

78+

} finally {

79+

try {

80+

rmSync(tmpDir, { recursive: true, force: true });

81+

} catch {

82+

// best-effort cleanup

83+

}

84+

}

85+

}

86+87+

function normalizeExt(ext: string): string | undefined {

88+

// Pattern matches the sibling helper in src/media/audio-transcode.ts: a short

89+

// alphanumeric extension token. Keeps the value safe to interpolate into

90+

// tmp-file names below without introducing a path-traversal surface.

91+

const trimmed = ext.trim().toLowerCase().replace(/^\./, "");

92+

return /^[a-z0-9]{1,12}$/.test(trimmed) ? trimmed : undefined;

93+

}

94+95+

function pickAfconvertRecipe(source: string, target: string): string[] | undefined {

96+

// Currently only the MP3→CAF path used by BlueBubbles voice memos. Keep

97+

// this in lockstep with `HostTranscodableContainer` above so a typo at the

98+

// channel-capability declaration site is a compile-time error.

99+

if (target === "caf") {

100+

// Opus-in-CAF, mono, 24 kHz. Validated against macOS 15.x Messages.app's

101+

// native voice-memo CAF descriptor (1 ch, 24000 Hz, opus); other CAF

102+

// flavors (PCM, AAC) get downgraded to plain audio attachments along the

103+

// BlueBubbles → Messages.app path. If iMessage stops rendering the result

104+

// as a voice memo after a system update, try forcing frames-per-packet

105+

// explicitly via `opus@24000#480` and re-validate. See #72506.

106+

return ["-f", "caff", "-d", "opus@24000", "-c", "1"];

107+

}

108+

return undefined;

109+

}

110+111+

function runAfconvert(params: {

112+

args: string[];

113+

timeoutMs: number;

114+

}): Promise<{ ok: true } | { ok: false; detail: string }> {

115+

return new Promise((resolve) => {

116+

const child = spawn("/usr/bin/afconvert", params.args, { stdio: "ignore" });

117+

const timer = setTimeout(() => {

118+

child.kill("SIGKILL");

119+

resolve({ ok: false, detail: `timeout-${params.timeoutMs}ms` });

120+

}, params.timeoutMs);

121+

child.once("error", (err) => {

122+

clearTimeout(timer);

123+

resolve({ ok: false, detail: err.message });

124+

});

125+

child.once("exit", (code) => {

126+

clearTimeout(timer);

127+

if (code === 0) {

128+

resolve({ ok: true });

129+

} else {

130+

resolve({ ok: false, detail: `exit-${code ?? "unknown"}` });

131+

}

132+

});

133+

});

134+

}