mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Command Center now plays short synthesized blips instead of the default chimes, and every layout gets a mute toggle sound. The layout uses the self-hosted VT323 pixel font throughout; a design option switches back to the previous sans/mono mix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
73 lines
2.8 KiB
TypeScript
73 lines
2.8 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"],
|
|
};
|
|
|
|
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 };
|
|
}
|