Files
feuerwerk/tests/core/take.test.ts
T
adminandClaude Opus 5.5 23cb34f781 Keep a recorded take inside its song (#355)
A take used to land in the Assets panel as "Song – Take N", next to
every other sound of the project. Now a song can keep a sound, a
pattern or a sound FX to itself: an ordinary asset whose new field
`owner` names the song. Everything that finds an asset by its id
finds it, so playing, the tabs and the game need no change. Only
the places that list assets leave it out.

- `core/music/embedded.ts` holds the rules: which types can be
  embedded, `listedAssets`, the title "Song › Take 1", and the
  rows an embedded sound hides (Loop, Bus).
- One user per embedded asset. `SetSongCommand` settles the song
  after every edit: a place naming an asset an earlier place
  claimed, or one of another song, gets a copy, and what nothing
  names any more goes in the same undo step. The outcome is worked
  out once, so a redo brings back the same copies.
- A split, Ctrl+D and a paste get that for free. The clipboard
  keeps the embedded assets of the copied items, so a paste after a
  delete brings the take back.
- Deleting a song deletes what it keeps. Duplicating it copies
  that and points the copy at the copies. A duplicate of an
  embedded asset is a project asset.
- The item menu has Open, Make project asset and Embed a copy.
- An embedded item carries a bullet in front of its name. The
  first pick was a diamond, which the gate's font test refused:
  the UI font has no glyph for it.
- A take is "Take 1", 2, … numbered per song. With the song gone
  while it uploads, it becomes a project sound under a free name.
- Left out of: the Assets panel, every asset picker, the sound FX
  menu of a track, the test sound of the Sound FX tab and the
  naming of new assets. Delete on an embedded asset only says
  where it lives. A ping lights up its song.
- Loading drops an `owner` whose song is gone, the asset stays as
  a project asset. No migration: older projects have no owner.

Tests:
- Unit embedded (new): 13 green, 6 counter-checks each red
- Unit take, song, song-edit, asset-fx, asset-ref, serialize and
  neighbours: 116 green
- e2e 65 (melektaus-atx): ALL PASSED, 47 checks (1 new)
- Counter-check e2e 65 (asset panel unfiltered): exactly that
  check red
- Gate (melektaus-atx): CHECK EXIT 0, 269 files, 4167 tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:36:25 +02:00

86 lines
3.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { newTrack } from '../../src/core/music/song';
import {
cappedTake,
MAX_TAKE_BYTES,
maxTakeSamples,
sampleCount,
takeName,
takeStart,
takeTrack,
takeTrackIndex,
toPcm16,
trimmedTake,
wavBytes,
} from '../../src/core/music/take';
const pcm = (...values: number[]) => Int16Array.from(values);
describe('a recorded take (#342)', () => {
it('turns samples into 16 bits, clipped at full scale', () => {
expect([...toPcm16(Float32Array.from([0, 1, -1, 0.5, 2, -3]))]).toEqual([
0, 32767, -32768, 16384, 32767, -32768,
]);
});
it('starts where the song is heard back through the input', () => {
expect(takeStart(10, 0.02, 0.01)).toBeCloseTo(10.03, 9);
expect(takeStart(10, -1, -1)).toBe(10);
});
it('drops what came before the start and cuts the chunk it falls in', () => {
const chunks = [
{ frame: 0, samples: pcm(1, 2, 3, 4) },
{ frame: 4, samples: pcm(5, 6, 7, 8) },
{ frame: 8, samples: pcm(9, 10) },
];
expect(trimmedTake(chunks, 6).map((part) => [...part])).toEqual([
[7, 8],
[9, 10],
]);
expect(trimmedTake(chunks, 4).map((part) => [...part])).toEqual([
[5, 6, 7, 8],
[9, 10],
]);
expect(sampleCount(trimmedTake(chunks, 0))).toBe(10);
expect(trimmedTake(chunks, 20)).toEqual([]);
});
it('keeps no more than a sound may hold', () => {
expect(cappedTake([pcm(1, 2, 3), pcm(4, 5, 6)], 4).map((part) => [...part])).toEqual([[1, 2, 3], [4]]);
expect(maxTakeSamples() * 2 + 44).toBeLessThanOrEqual(MAX_TAKE_BYTES);
expect(MAX_TAKE_BYTES).toBeLessThan(256 * 1024 * 1024);
});
it('writes a mono 16-bit PCM WAV the server knows by its RIFF header', () => {
const bytes = wavBytes([pcm(1, -2), pcm(300)], 48000);
const view = new DataView(bytes.buffer);
const text = (at: number) => String.fromCharCode(...bytes.subarray(at, at + 4));
expect(bytes.length).toBe(44 + 6);
expect([text(0), text(8), text(12), text(36)]).toEqual(['RIFF', 'WAVE', 'fmt ', 'data']);
expect(view.getUint32(4, true)).toBe(36 + 6);
expect(view.getUint16(20, true)).toBe(1);
expect(view.getUint16(22, true)).toBe(1);
expect(view.getUint32(24, true)).toBe(48000);
expect(view.getUint32(28, true)).toBe(96000);
expect(view.getUint16(34, true)).toBe(16);
expect(view.getUint32(40, true)).toBe(6);
expect([view.getInt16(44, true), view.getInt16(46, true), view.getInt16(48, true)]).toEqual([1, -2, 300]);
});
it('is numbered past the takes its song keeps (#355)', () => {
expect(takeName([])).toBe('Take 1');
expect(takeName(['Take 1', 'Take 2', 'Intro'])).toBe('Take 3');
expect(takeName(['Take 2'])).toBe('Take 1');
});
it('gets a track of its own under the one clicked last, else at the bottom', () => {
const tracks = [newTrack(1), newTrack(2), newTrack(3)];
expect(takeTrackIndex(tracks, tracks[0]!.id)).toBe(1);
expect(takeTrackIndex(tracks, tracks[2]!.id)).toBe(3);
expect(takeTrackIndex(tracks, '')).toBe(3);
expect(takeTrack().name).toBe('Recording');
expect(takeTrack().id).not.toBe(takeTrack().id);
});
});