summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/timeline/audio-separation/__tests__/index.test.ts
blob: f2b17d1a26fdfded704e8fe4f78477d96608a2ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, expect, test } from "bun:test";
import { upsertElementKeyframe } from "@/lib/animation";
import type { UploadAudioElement, VideoElement } from "@/lib/timeline";
import {
	buildSeparatedAudioElement,
	doesElementHaveEnabledAudio,
	isSourceAudioEnabled,
	isSourceAudioSeparated,
} from "..";

describe("audio separation", () => {
	test("treats missing source audio state as enabled", () => {
		const element = buildVideoElement({});

		expect(isSourceAudioEnabled({ element })).toBe(true);
		expect(isSourceAudioSeparated({ element })).toBe(false);
	});

	test("builds a detached audio element from the source clip audio state", () => {
		const element = buildVideoElement({
			duration: 8,
			startTime: 3,
			trimStart: 1.5,
			trimEnd: 0.5,
			sourceDuration: 12,
			volume: -6,
			muted: true,
			retime: { rate: 1.25, maintainPitch: true },
			animations: buildAnimations(),
		});

		const separatedAudioElement = buildSeparatedAudioElement({
			sourceElement: element,
		});

		expect(separatedAudioElement).toMatchObject({
			type: "audio",
			sourceType: "upload",
			mediaId: element.mediaId,
			name: element.name,
			duration: 8,
			startTime: 3,
			trimStart: 1.5,
			trimEnd: 0.5,
			sourceDuration: 12,
			volume: -6,
			muted: true,
			retime: { rate: 1.25, maintainPitch: true },
		});
		expect(Object.keys(separatedAudioElement.animations?.bindings ?? {})).toEqual([
			"volume",
		]);
		expect(Object.keys(separatedAudioElement.animations?.channels ?? {})).toEqual([
			"volume:value",
		]);
		expect(
			separatedAudioElement.animations?.channels["volume:value"]?.keys[0]?.id,
		).not.toBe("volume-keyframe");
	});

	test("skips source audio collection when the source clip is separated", () => {
		const mediaAsset = { hasAudio: true };
		const videoElement = buildVideoElement({
			isSourceAudioEnabled: false,
		});
		const audioElement = buildAudioElement();

		expect(
			doesElementHaveEnabledAudio({
				element: videoElement,
				mediaAsset,
			}),
		).toBe(false);
		expect(
			doesElementHaveEnabledAudio({
				element: audioElement,
			}),
		).toBe(true);
	});
});

function buildVideoElement(
	overrides: Partial<VideoElement>,
): VideoElement {
	return {
		id: "video-1",
		type: "video",
		mediaId: "media-1",
		name: "Clip",
		duration: 5,
		startTime: 0,
		trimStart: 0,
		trimEnd: 0,
		volume: 0,
		muted: false,
		isSourceAudioEnabled: true,
		transform: {
			scaleX: 1,
			scaleY: 1,
			position: { x: 0, y: 0 },
			rotate: 0,
		},
		opacity: 1,
		...overrides,
	};
}

function buildAudioElement(
	overrides: Partial<UploadAudioElement> = {},
): UploadAudioElement {
	return {
		id: "audio-1",
		type: "audio",
		sourceType: "upload",
		mediaId: "audio-media-1",
		name: "Detached audio",
		duration: 5,
		startTime: 0,
		trimStart: 0,
		trimEnd: 0,
		volume: 0,
		...overrides,
	} satisfies UploadAudioElement;
}

function buildAnimations() {
	const withVolume = upsertElementKeyframe({
		animations: undefined,
		propertyPath: "volume",
		time: 2,
		value: -12,
		interpolation: "linear",
		keyframeId: "volume-keyframe",
	});

	return upsertElementKeyframe({
		animations: withVolume,
		propertyPath: "opacity",
		time: 1,
		value: 0.5,
		interpolation: "linear",
		keyframeId: "opacity-keyframe",
	});
}