Skip to content

Open source CapCut alternative.

TypeScript

feat(site): sound presets ride the library shelf [rebuild]

David Le749cab6d3799
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index c7e7b8ef..535d69e2 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -85,6 +85,7 @@ import {
availableResidencies,
type Residency,
} from "@/cut/lib/residency";
+import { isSoundPresetTemplate } from "@/cut/lib/soundPresets";
import { isStylePresetTemplate } from "@/cut/lib/stylePresets";
import { retryUpload } from "@/cut/lib/importQueue";
import {
@@ -1515,10 +1516,10 @@ function LibraryPanel({ projectId }: { projectId: string }) {
const loaded = library.data !== undefined || library.isError;
const assets = (library.data?.assets ?? []).filter((a) => reachable(a.residency));
const folders = (library.data?.folders ?? []).filter((f) => reachable(f.residency));
- // Saved text styles ride the template rails but are not templates: they
- // belong to the text inspector, not this shelf.
+ // Saved text styles and sound presets ride the template rails but are not
+ // templates: they belong to their inspectors, not this shelf.
const templates = (library.data?.templates ?? []).filter(
- (t) => reachable(t.residency) && !isStylePresetTemplate(t)
+ (t) => reachable(t.residency) && !isStylePresetTemplate(t) && !isSoundPresetTemplate(t)
);
const patch = useCallback(
(fn: (prev: LibraryData) => LibraryData) => patchLibrary(client, fn),
diff --git a/site/src/cut/lib/backend/browser/library.ts b/site/src/cut/lib/backend/browser/library.ts
index f2610ee7..c3d92b43 100644
--- a/site/src/cut/lib/backend/browser/library.ts
+++ b/site/src/cut/lib/backend/browser/library.ts
@@ -411,8 +411,14 @@ type TemplateInput = {
audio: TemplateAudio[];
texts: LibraryTemplate["texts"];
cues: LibraryTemplate["cues"];
+ sound?: LibraryTemplate["sound"];
};
+/** A template with nothing on it saves nothing; a sound preset is a template
+ * carrying only its treatment. */
+const templateEmpty = (input: TemplateInput) =>
+ !input.media?.length && !input.texts?.length && !input.cues?.length && !input.sound;
+
async function saveTemplate(req: Request): Promise<Response> {
try {
const { projectId, ...input } = (await req.json()) as {
@@ -420,9 +426,7 @@ async function saveTemplate(req: Request): Promise<Response> {
} & TemplateInput;
if (!(await store.readDoc(projectId)))
return err("Project not found.", 404);
- if (!input.media?.length && !input.texts?.length && !input.cues?.length) {
- return err("Nothing to save.", 500);
- }
+ if (templateEmpty(input)) return err("Nothing to save.", 500);
const media: TemplateMedia[] = [];
for (const m of input.media ?? []) {
media.push({
@@ -440,6 +444,7 @@ async function saveTemplate(req: Request): Promise<Response> {
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
};
await mutateIndex((idx) => {
idx.templates.push(template);
@@ -484,6 +489,7 @@ async function importTemplate(req: Request): Promise<Response> {
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
folderId: input.folderId ?? null,
};
await mutateIndex((idx) => {
diff --git a/site/src/cut/lib/soundPresets.ts b/site/src/cut/lib/soundPresets.ts
new file mode 100644
index 00000000..aef745c2
--- /dev/null
+++ b/site/src/cut/lib/soundPresets.ts
@@ -0,0 +1,68 @@
+"use client";
+
+/**
+ * Named sound-quality presets, saved in the shared Library. A preset rides
+ * the existing template rails: it is a library template with no media, no
+ * layers and no text, carrying only a `sound` treatment — so every residency
+ * stores, lists and deletes it with machinery that already exists, and a
+ * preset saved on this Mac shows up beside one saved in the cloud.
+ */
+
+import { normalizeSound, type ClipSound } from "@donkeycut/effects-kit";
+import { deleteTemplate, fetchLibrary, saveTemplate } from "./library";
+import type { Residency } from "./residency";
+import type { LibraryTemplate } from "./types";
+
+export interface SoundPreset {
+ id: string;
+ name: string;
+ residency: Residency;
+ sound: ClipSound;
+}
+
+/** Whether a library template is a saved sound preset rather than a template
+ * the user can drop on the timeline. Template surfaces filter on this so
+ * presets stay in the audio inspector where they were saved. */
+export function isSoundPresetTemplate(t: LibraryTemplate): boolean {
+ if (!t.sound) return false;
+ return !t.media.length && !t.layers.length && !t.audio.length && !t.texts.length && !t.cues.length;
+}
+
+/** Read a template as a sound preset, or null when it is an ordinary one. */
+export function soundPresetOf(t: LibraryTemplate & { residency: Residency }): SoundPreset | null {
+ if (!isSoundPresetTemplate(t)) return null;
+ const sound = normalizeSound(t.sound);
+ if (!sound) return null;
+ return { id: t.id, name: t.name, residency: t.residency, sound };
+}
+
+/** Every sound preset across every reachable shelf, newest first. */
+export async function listSoundPresets(): Promise<SoundPreset[]> {
+ const lib = await fetchLibrary();
+ return lib.templates.map(soundPresetOf).filter((p): p is SoundPreset => p !== null);
+}
+
+/** Save a treatment as a named preset on the active shelf. */
+export async function saveSoundPreset(
+ projectId: string,
+ name: string,
+ sound: ClipSound
+): Promise<SoundPreset | null> {
+ const normalized = normalizeSound(sound);
+ if (!normalized) throw new Error("A flat treatment is nothing to save.");
+ const saved = await saveTemplate(projectId, {
+ name,
+ duration: 0,
+ media: [],
+ layers: [],
+ audio: [],
+ texts: [],
+ cues: [],
+ sound: normalized,
+ });
+ return soundPresetOf(saved);
+}
+
+export function deleteSoundPreset(preset: SoundPreset): Promise<void> {
+ return deleteTemplate(preset.residency, preset.id);
+}
diff --git a/site/src/cut/server/cloud/library.ts b/site/src/cut/server/cloud/library.ts
index 540ef899..53b20e7a 100644
--- a/site/src/cut/server/cloud/library.ts
+++ b/site/src/cut/server/cloud/library.ts
@@ -62,8 +62,14 @@ interface TemplateDoc {
audio: TemplateAudio[];
texts: unknown[];
cues: unknown[];
+ sound?: LibraryTemplate["sound"];
}
+/** A template with nothing on it saves nothing; a sound preset is a template
+ * carrying only its treatment. */
+const templateEmpty = (input: TemplateInput) =>
+ !input.media?.length && !input.texts?.length && !input.cues?.length && !input.sound;
+
type MediaObjectRow = {
id: string;
r2Key: string;
@@ -120,6 +126,7 @@ function templateView(row: {
audio: doc.audio ?? [],
texts: doc.texts ?? [],
cues: doc.cues ?? [],
+ ...(doc.sound ? { sound: doc.sound } : {}),
};
}
@@ -752,9 +759,7 @@ export const libraryCloud = {
} & TemplateInput;
if (!(await getProject(userId, projectId)))
throw new Error("Project not found.");
- if (!input.media?.length && !input.texts?.length && !input.cues?.length) {
- throw new Error("Nothing to save.");
- }
+ if (templateEmpty(input)) throw new Error("Nothing to save.");
const taken = await takenLibraryNames(userId);
const media: TemplateMedia[] = [];
for (const m of input.media) {
@@ -778,6 +783,7 @@ export const libraryCloud = {
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
};
const row = await prisma.cutTemplate.create({
data: {
@@ -800,9 +806,7 @@ export const libraryCloud = {
keys?: string[];
folderId?: string | null;
};
- if (!input.media?.length && !input.texts?.length && !input.cues?.length) {
- throw new Error("Nothing to add.");
- }
+ if (templateEmpty(input)) throw new Error("Nothing to add.");
const media: TemplateMedia[] = [];
for (const [i, m] of (input.media ?? []).entries()) {
const key = keys?.[i];
@@ -837,6 +841,7 @@ export const libraryCloud = {
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
};
const row = await prisma.cutTemplate.create({
data: {
diff --git a/site/src/cut/server/library.ts b/site/src/cut/server/library.ts
index 4ecf6ef4..68918e26 100644
--- a/site/src/cut/server/library.ts
+++ b/site/src/cut/server/library.ts
@@ -1,6 +1,7 @@
import { spawn } from "node:child_process";
import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import path from "node:path";
+import type { ClipSound } from "@donkeycut/effects-kit";
import { cutDataRoot } from "./dataDir";
import { assertLocalRuntime } from "./local-only";
import { mediaPath as projectMediaPath, readProject } from "./projects";
@@ -78,6 +79,7 @@ export interface TemplateLayer {
opacity?: number;
muted: boolean;
speed?: number;
+ sound?: ClipSound;
track: number;
asClip?: boolean; // re-materializes as a track-0 timeline clip rather than an overlay
}
@@ -90,6 +92,7 @@ export interface TemplateAudio {
fadeIn?: number;
fadeOut?: number;
speed?: number;
+ sound?: ClipSound;
}
export interface LibraryTemplate {
id: string;
@@ -102,6 +105,8 @@ export interface LibraryTemplate {
audio: TemplateAudio[];
texts: unknown[]; // opaque TextOverlay[] round-tripped for the client
cues: unknown[]; // opaque SubtitleCue[]
+ /** A template carrying only this is a saved sound preset. */
+ sound?: ClipSound;
}
interface LibraryIndex {
@@ -447,8 +452,14 @@ export interface TemplateInput {
audio: TemplateAudio[];
texts: unknown[];
cues: unknown[];
+ sound?: LibraryTemplate["sound"];
}
+/** A template with nothing on it saves nothing; a sound preset is a template
+ * carrying only its treatment. */
+const templateEmpty = (input: TemplateInput) =>
+ !input.media?.length && !input.texts?.length && !input.cues?.length && !input.sound;
+
export async function listTemplates(): Promise<LibraryTemplate[]> {
const idx = await readIndex();
return (idx.templates ?? []).slice().sort((a, b) => b.addedAt - a.addedAt);
@@ -467,9 +478,7 @@ export async function importTemplate(
input: TemplateInput & { folderId?: string | null },
files: File[],
): Promise<LibraryTemplate> {
- if (!input.media?.length && !input.texts?.length && !input.cues?.length) {
- throw new Error("Nothing to add.");
- }
+ if (templateEmpty(input)) throw new Error("Nothing to add.");
await mkdir(libMedia(), { recursive: true });
const media: TemplateMedia[] = [];
for (const [i, m] of (input.media ?? []).entries()) {
@@ -489,6 +498,7 @@ export async function importTemplate(
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
folderId: input.folderId ?? null,
};
await mutateIndex((idx) => {
@@ -502,9 +512,7 @@ export async function saveTemplate(
input: TemplateInput,
): Promise<LibraryTemplate> {
if (!(await readProject(projectId))) throw new Error("Project not found.");
- if (!input.media?.length && !input.texts?.length && !input.cues?.length) {
- throw new Error("Nothing to save.");
- }
+ if (templateEmpty(input)) throw new Error("Nothing to save.");
await mkdir(libMedia(), { recursive: true });
const media: TemplateMedia[] = [];
for (const m of input.media) {
@@ -532,6 +540,7 @@ export async function saveTemplate(
audio: input.audio ?? [],
texts: input.texts ?? [],
cues: input.cues ?? [],
+ ...(input.sound ? { sound: input.sound } : {}),
};
await mutateIndex((idx) => {
idx.templates = [...(idx.templates ?? []), template];