Suno-style web UI for a self-hosted MiniMax-Music3 inference server
TypeScript ai-musicdiffusersdockerexpressminimaxminimax-musicmusic-generationreactself-hostedsgl-omnisunotailwindcsstext-to-musictypescriptvite
feat(server): serve a rendering track's audio with a live-patched wav header
Adam Benhassen4c9a63f521aa
diff --git a/server/src/app.ts b/server/src/app.tsindex b8b91c0..f8aa770 100644--- a/server/src/app.ts+++ b/server/src/app.ts@@ -1,5 +1,6 @@import express, { type NextFunction, type Request, type Response } from 'express';import fs from 'node:fs/promises';+import fsSync from 'node:fs';import path from 'node:path';import { randomUUID } from 'node:crypto';import type { JsonStore, Library } from './library.js';@@ -8,6 +9,7 @@ import { normalizeGenerate, ValidationError } from './validate.js';import { normalizeMusicApi, SettingsStore } from './settings.js';import { randomTitle } from './names.js';import { FORMATS, type Template, type Track } from './types.js';+import { patchWavSizes, WAV_HEADER_BYTES } from './wav.js';export interface AppDeps {library: Library;@@ -22,6 +24,33 @@ export interface AppDeps {const MIME: Record<string, string> = { wav: 'audio/wav' };+/** A WAV still being written: sizes in its header are 0 until done, so patch them to the current length on the way out. */+async function servePartialWav(req: Request, res: Response, abs: string, type: string): Promise<void> {+ let size: number;+ try { size = (await fs.stat(abs)).size; } catch { res.status(404).json({ error: 'no audio yet' }); return; }+ if (size < WAV_HEADER_BYTES) { res.status(404).json({ error: 'no audio yet' }); return; }+ const raw = Buffer.alloc(WAV_HEADER_BYTES);+ const fh = await fs.open(abs, 'r');+ try { await fh.read(raw, 0, WAV_HEADER_BYTES, 0); } finally { await fh.close(); }+ const header = patchWavSizes(raw, size);+ let start = 0, end = size - 1;+ const m = /^bytes=(\d*)-(\d*)$/.exec(req.headers.range ?? '');+ if (m) {+ if (m[1]) start = Number(m[1]);+ if (m[2]) end = m[1] ? Math.min(size - 1, Number(m[2])) : size - 1;+ if (!m[1] && m[2]) start = Math.max(0, size - Number(m[2]));+ if (start > end || start >= size) { res.status(416).set('Content-Range', `bytes */${size}`).end(); return; }+ }+ res.status(m ? 206 : 200).set({+ 'Content-Type': type, 'Accept-Ranges': 'bytes', 'Cache-Control': 'no-store',+ 'Content-Length': String(end - start + 1),+ ...(m ? { 'Content-Range': `bytes ${start}-${end}/${size}` } : {}),+ });+ if (start < WAV_HEADER_BYTES) res.write(header.subarray(start, Math.min(WAV_HEADER_BYTES, end + 1)));+ if (end >= WAV_HEADER_BYTES) fsSync.createReadStream(abs, { start: Math.max(WAV_HEADER_BYTES, start), end }).pipe(res);+ else res.end();+}+export function createApp(deps: AppDeps) {const { library, templates, settings, upstream, queue, tracksDir } = deps;const log = deps.log ?? ((m: string) => console.log(`[app] ${m}`));@@ -92,6 +121,7 @@ export function createApp(deps: AppDeps) {if (!track || !track.file) return res.status(404).json({ error: 'no audio' });const abs = path.join(tracksDir, track.file);const type = MIME[extFor(track.format)] ?? 'application/octet-stream';+ if (track.status === 'running') return void servePartialWav(req, res, abs, type);const download = req.query.download !== undefined;res.sendFile(abs, {headers: {diff --git a/server/test/app.test.ts b/server/test/app.test.tsindex 0a46d60..355cb7d 100644--- a/server/test/app.test.ts+++ b/server/test/app.test.ts@@ -262,4 +262,25 @@ describe('app', () => {await until(() => fake!.requests.find((r) => r.path === '/v1/audio/speech')?.aborted === true);expect(lib.get(id)).toBeUndefined();});++ it('audio route serves the partial WAV of a running track with a patched header and honours Range', async () => {+ const { app, lib } = await setup({ stream: { windows: 40, secondsPerWindow: 0.1, windowMs: 40 } });+ const res = await request(app).post('/api/generate').send({ prompt: 'x' });+ const id = res.body[0].id as string;+ await until(() => (lib.get(id)?.renderedSeconds ?? 0) >= 0.2);+ const bin = (r: request.Response, cb: (err: Error | null, body: Buffer) => void) => { const c: Buffer[] = []; r.on('data', (d: Buffer) => c.push(d)); r.on('end', () => cb(null, Buffer.concat(c))); };+ const full = await request(app).get(`/api/tracks/${id}/audio`).buffer(true).parse(bin);+ expect(full.status).toBe(200);+ expect(full.headers['accept-ranges']).toBe('bytes');+ const buf = full.body as Buffer;+ expect(buf.toString('ascii', 0, 4)).toBe('RIFF');+ expect(buf.readUInt32LE(40)).toBe(buf.length - 44);+ expect(buf.readUInt32LE(4)).toBe(buf.length - 8);+ expect(buf.length).toBeGreaterThanOrEqual(44 + Math.round(0.2 * 44100) * 4);+ const part = await request(app).get(`/api/tracks/${id}/audio`).set('Range', 'bytes=40-51').buffer(true).parse(bin);+ expect(part.status).toBe(206);+ expect((part.body as Buffer).length).toBe(12);+ expect(part.headers['content-range']).toMatch(/^bytes 40-51\/\d+$/);+ await request(app).delete(`/api/tracks/${id}`);+ });});