Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions apps/studio/src/auditionView.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import {
FULL_VIEW,
globalToLocalRatio,
localToGlobalRatio,
slicePeaksForView,
timeAxisTicks,
zoomViewWindow,
} from "./auditionView";

describe("auditionView", () => {
it("zooms toward a pivot and stays in [0, 1]", () => {
const zoomed = zoomViewWindow(FULL_VIEW, 0.5, 0.25);
expect(zoomed.end - zoomed.start).toBeCloseTo(0.5, 5);
expect(zoomed.start).toBeGreaterThanOrEqual(0);
expect(zoomed.end).toBeLessThanOrEqual(1);
// Pivot at 0.25 of full view stays near 0.25 of new window.
const pivotGlobal = 0.25;
const local = globalToLocalRatio(pivotGlobal, zoomed);
expect(local).toBeCloseTo(0.25, 5);
});

it("maps local ↔ global ratios", () => {
const window = { start: 0.2, end: 0.6 };
expect(localToGlobalRatio(0, window)).toBeCloseTo(0.2);
expect(localToGlobalRatio(1, window)).toBeCloseTo(0.6);
expect(localToGlobalRatio(0.5, window)).toBeCloseTo(0.4);
expect(globalToLocalRatio(0.4, window)).toBeCloseTo(0.5);
});

it("slices peaks for the visible window", () => {
const peaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(slicePeaksForView(peaks, FULL_VIEW)).toEqual(peaks);
const mid = slicePeaksForView(peaks, { start: 0.4, end: 0.7 });
// last=9 → floor(0.4*9)=3, ceil(0.7*9)=7
expect(mid[0]).toBe(3);
expect(mid[mid.length - 1]).toBe(7);
});

it("builds time-axis ticks for a window", () => {
const ticks = timeAxisTicks({ start: 0, end: 1 }, 2, 5);
expect(ticks.length).toBeGreaterThanOrEqual(2);
expect(ticks[0]?.label).toBeTruthy();
});
});
125 changes: 125 additions & 0 deletions apps/studio/src/auditionView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/** Shared transport audition view window (zoom / pan) — ratios of full clip [0, 1]. */

export type ViewWindow = {
start: number;
end: number;
};

export const FULL_VIEW: ViewWindow = { start: 0, end: 1 };

/** Minimum visible span as a fraction of clip (≈1 ms at 1 s; still usable when zooming samples). */
export const MIN_VIEW_SPAN = 1 / 50_000;

export function clamp(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value));
}

export function viewSpan(window: ViewWindow): number {
return Math.max(MIN_VIEW_SPAN, window.end - window.start);
}

/** Zoom time window around a pivot in *view-local* ratio [0, 1]. factor < 1 zooms in. */
export function zoomViewWindow(
window: ViewWindow,
factor: number,
pivotInView = 0.5,
minSpan = MIN_VIEW_SPAN,
): ViewWindow {
const span = viewSpan(window);
const newSpan = clamp(span * factor, minSpan, 1);
const pivot = window.start + clamp(pivotInView, 0, 1) * span;
let start = pivot - clamp(pivotInView, 0, 1) * newSpan;
let end = start + newSpan;
if (start < 0) {
start = 0;
end = newSpan;
}
if (end > 1) {
end = 1;
start = 1 - newSpan;
}
return { start, end };
}

/** Map pointer ratio inside the visible plot → global clip ratio. */
export function localToGlobalRatio(local: number, window: ViewWindow): number {
return window.start + clamp(local, 0, 1) * viewSpan(window);
}

/** Map global playhead → position inside the visible plot (may be outside 0–1). */
export function globalToLocalRatio(global: number, window: ViewWindow): number {
const span = viewSpan(window);
if (span <= 0) return 0;
return (global - window.start) / span;
}

/** Slice peak envelope to the visible window (inclusive end index). */
export function slicePeaksForView(peaks: number[], window: ViewWindow): number[] {
if (peaks.length === 0) return [];
if (window.start <= 0 && window.end >= 1) return peaks;
const last = peaks.length - 1;
const i0 = clamp(Math.floor(window.start * last), 0, last);
const i1 = clamp(Math.ceil(window.end * last), i0, last);
return peaks.slice(i0, i1 + 1);
}

export function formatAxisSeconds(sec: number): string {
if (!Number.isFinite(sec) || sec < 0) return "0.00";
if (sec < 60) return sec.toFixed(sec < 10 ? 2 : 1);
const m = Math.floor(sec / 60);
const s = sec - m * 60;
return `${m}:${s.toFixed(s < 10 ? 2 : 1).padStart(4, "0")}`;
}

export function timeAxisTicks(
window: ViewWindow,
durationSec: number,
maxTicks = 6,
): Array<{ ratio: number; label: string }> {
if (!(durationSec > 0) || maxTicks < 2) return [];
const startSec = window.start * durationSec;
const endSec = window.end * durationSec;
const spanSec = Math.max(1e-9, endSec - startSec);
const rough = spanSec / (maxTicks - 1);
const nice = niceTimeStep(rough);
const first = Math.ceil(startSec / nice) * nice;
const ticks: Array<{ ratio: number; label: string }> = [];
for (let t = first; t <= endSec + nice * 1e-6; t += nice) {
if (t < startSec - nice * 1e-6) continue;
const ratio = (t - startSec) / spanSec;
if (ratio < -0.01 || ratio > 1.01) continue;
ticks.push({ ratio: clamp(ratio, 0, 1), label: formatAxisSeconds(t) });
}
if (ticks.length === 0) {
ticks.push(
{ ratio: 0, label: formatAxisSeconds(startSec) },
{ ratio: 1, label: formatAxisSeconds(endSec) },
);
}
return ticks;
}

function niceTimeStep(rough: number): number {
if (!(rough > 0)) return 0.01;
const exp = Math.floor(Math.log10(rough));
const base = Math.pow(10, exp);
const err = rough / base;
let mult = 1;
if (err > 5) mult = 10;
else if (err > 2) mult = 5;
else if (err > 1) mult = 2;
return mult * base;
}

export function clampAmpZoom(z: number): number {
return clamp(z, 1, 32);
}

export function clampFreqZoom(z: number): number {
return clamp(z, 1, 32);
}

/** Visible Nyquist fraction: 1 = full band, 2 = bottom half of spectrum, etc. */
export function visibleMaxFreqHz(maxFreqHz: number, freqZoom: number): number {
return maxFreqHz / clampFreqZoom(freqZoom);
}
8 changes: 4 additions & 4 deletions apps/studio/src/components/ComplianceDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ export default function ComplianceDrawer({
) : (
<p className="compliance-hint">
{missingModelCount > 0
? `${missingModelCount} model${missingModelCount === 1 ? "" : "s"} will be installed, then the Preview branch will render. Click Play to audition.`
: "Models are ready. The workflow will render only its Preview branch — click Play to audition."}
? `${missingModelCount} model${missingModelCount === 1 ? "" : "s"} will be installed, then the PreviewAudio branch will render. Click Play to audition.`
: "Models are ready. The workflow will render only its PreviewAudio branch — click Play to audition."}
</p>
)}
{fastPathError ? (
Expand Down Expand Up @@ -720,8 +720,8 @@ export default function ComplianceDrawer({
onClick={() => void runFastPath()}
>
{missingModelCount > 0
? "Install chain & render Preview"
: "Render Preview"}
? "Install chain & render PreviewAudio"
: "Render PreviewAudio"}
</button>
)}
</section>
Expand Down
1 change: 1 addition & 0 deletions apps/studio/src/components/NodePalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const CORE_IO = new Set([
"SynthMidi",
"Mix",
"ChannelMerge",
"ChannelSplit",
"Note",
"Prompt",
]);
Expand Down
6 changes: 6 additions & 0 deletions apps/studio/src/components/PreviewVideoTransport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export function PreviewVideoTransport({
const onEnded = () => {
setPlaying(false);
setCurrent(video.duration || 0);
// Remute after local play so the next transport A/B pass does not leave
// an unmuted native track that can click at EOF.
video.muted = true;
};

video.addEventListener("loadedmetadata", onMeta);
Expand Down Expand Up @@ -86,10 +89,13 @@ export function PreviewVideoTransport({
return;
}
if (video.paused || video.ended) {
// Local HTML5 audio only when Space has no PCM listen target (pure T2V).
// A/B PreviewVideo should use transport PCM via resolveNodeListenId.
video.muted = false;
void video.play().catch(() => {});
} else {
video.pause();
video.muted = true;
}
}, []);

Expand Down
Loading
Loading