Files
webspeak3/web/src/soundpack.ts
T
MoepchiandClaude Opus 5.5 7b98cb2d7c Whisper settings: incoming whisper rights, sound, history auto-open/cleanup
Options -> Whispern like TS3: allow/block by default with per-contact
overrides (new Whisper field in contacts) or block all. The connector drops
blocked whispers before decoding (whisperpolicy stdin command) and reports
accepted ones as whisperReceived, which logs them in the whisper history,
plays the new whisper sound and can open the history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-10-23 19:01:53 +00:00

76 lines
2.9 KiB
TypeScript

import { unzipSync } from "fflate";
import type { SoundEventId } from "./sounds";
const AUDIO_EXTENSIONS = [".wav", ".mp3", ".ogg", ".m4a"];
const MIME_BY_EXTENSION: Record<string, string> = {
".wav": "audio/wav",
".mp3": "audio/mpeg",
".ogg": "audio/ogg",
".m4a": "audio/mp4",
};
/** Real .ts3soundpack files use TeamSpeak's own internal event names, which
* vary between client versions and aren't documented anywhere stable - so
* this matches by keyword against the filename (soundpacks are zips of
* individual audio files, not proprietary-encoded) rather than depending on
* an exact manifest format. Falls back to a "not matched" list the caller
* can surface, instead of silently dropping unrecognized files. */
const EVENT_KEYWORDS: Record<SoundEventId, string[]> = {
connect: ["connectionaccepted", "connectionestablished", "youhaveconnected", "connect"],
disconnect: ["connectionlost", "connectiondisconnected", "youhavebeendisconnected", "disconnect"],
clientJoin: ["userenteredchannel", "userenteredvisibility", "cliententerview", "userjoined", "enter"],
clientLeave: ["userleftchannel", "userleftvisibility", "clientleftview", "userleave", "userleft", "leave"],
message: ["textmessagereceived", "chatmessage", "newmessage", "message"],
poke: ["gotpoked", "poke"],
mute: ["micmuted", "microphonemuted", "micactivated", "microphoneactivated"],
micClickOn: ["micclickon"],
micClickOff: ["micclickoff"],
whisper: ["whisperreceived", "whisper"],
};
function normalize(filename: string): string {
const base = filename.slice(filename.lastIndexOf("/") + 1);
const dot = base.lastIndexOf(".");
return (dot === -1 ? base : base.slice(0, dot)).toLowerCase().replace(/[^a-z0-9]/g, "");
}
function extensionOf(filename: string): string | null {
const lower = filename.toLowerCase();
return AUDIO_EXTENSIONS.find((ext) => lower.endsWith(ext)) ?? null;
}
export interface SoundpackImportResult {
matched: Partial<Record<SoundEventId, { name: string; blob: Blob }>>;
unmatchedFiles: string[];
}
export async function parseSoundpack(file: File): Promise<SoundpackImportResult> {
const buffer = new Uint8Array(await file.arrayBuffer());
const entries = unzipSync(buffer, {
filter: (entry) => extensionOf(entry.name) !== null,
});
const matched: SoundpackImportResult["matched"] = {};
const unmatchedFiles: string[] = [];
for (const [name, data] of Object.entries(entries)) {
const ext = extensionOf(name);
if (!ext) continue;
const key = normalize(name);
const eventId = (Object.keys(EVENT_KEYWORDS) as SoundEventId[]).find(
(id) => !matched[id] && EVENT_KEYWORDS[id].some((keyword) => key.includes(keyword))
);
if (eventId) {
matched[eventId] = {
name: name.slice(name.lastIndexOf("/") + 1),
blob: new Blob([data.slice()], { type: MIME_BY_EXTENSION[ext] }),
};
} else {
unmatchedFiles.push(name);
}
}
return { matched, unmatchedFiles };
}