Full self-hosted click track generator for cover bands. Core technical pieces implemented: - CTP (Click Track Protocol) TypeScript schema, Zod validator, and WAV renderer (44.1 kHz, 16-bit PCM, accented downbeats, ramp sections) - MusicBrainz API client with 1 req/s rate limiting - PostgreSQL schema (songs, tempo_maps, registry_sync_log) with triggers - Git registry sync logic (clone/pull → validate CTP → upsert DB) - Next.js 14 App Router: search page, track page, API routes (/api/songs, /api/tracks, /api/generate) - UI components: SearchBar, SongResult, TempoMapEditor, ClickTrackPlayer (Web Audio API in-browser playback + WAV download) - Docker Compose stack: app + postgres + redis + nginx + registry-sync - Multi-stage Dockerfile with standalone Next.js output - .env.example documenting all configuration variables - README with setup instructions, CTP format spec, and API reference Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { getTempoMapsForSong, insertTempoMap, query } from "@/lib/db/client";
|
|
import { validateCTP } from "@/lib/ctp/validate";
|
|
|
|
// ─── GET /api/tracks?mbid=<uuid> ─────────────────────────────────────────────
|
|
|
|
const GetSchema = z.object({
|
|
mbid: z.string().uuid(),
|
|
});
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const params = Object.fromEntries(req.nextUrl.searchParams);
|
|
const parsed = GetSchema.safeParse(params);
|
|
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "mbid (UUID) is required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const maps = await getTempoMapsForSong(parsed.data.mbid);
|
|
return NextResponse.json({ maps });
|
|
}
|
|
|
|
// ─── POST /api/tracks ─────────────────────────────────────────────────────────
|
|
// Body: a raw CTP document JSON
|
|
|
|
export async function POST(req: NextRequest) {
|
|
let body: unknown;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const validation = validateCTP(body);
|
|
if (!validation.success) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "CTP document validation failed",
|
|
details: validation.errors.flatten(),
|
|
},
|
|
{ status: 422 }
|
|
);
|
|
}
|
|
|
|
const doc = validation.data;
|
|
|
|
if (!doc.metadata.mbid) {
|
|
return NextResponse.json(
|
|
{ error: "CTP document must include a metadata.mbid to be stored" },
|
|
{ status: 422 }
|
|
);
|
|
}
|
|
|
|
// Ensure the song exists
|
|
const { rowCount } = await query("SELECT 1 FROM songs WHERE mbid = $1", [
|
|
doc.metadata.mbid,
|
|
]);
|
|
|
|
if (!rowCount || rowCount === 0) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "Song not found. Search for the song first to register it.",
|
|
mbid: doc.metadata.mbid,
|
|
},
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const map = await insertTempoMap({
|
|
song_mbid: doc.metadata.mbid,
|
|
ctp_data: body as Record<string, unknown>,
|
|
contributed_by: doc.metadata.contributed_by,
|
|
});
|
|
|
|
return NextResponse.json({ map }, { status: 201 });
|
|
}
|