-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathaudio-player.ts
More file actions
191 lines (150 loc) · 5.97 KB
/
Copy pathaudio-player.ts
File metadata and controls
191 lines (150 loc) · 5.97 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { KeyframeBuilder } from "@animations/keyframe-builder";
import type { Edit } from "@core/edit-session";
import { type Size } from "@layouts/geometry";
import { AudioLoadParser } from "@loaders/audio-load-parser";
import { type AudioAsset, type ResolvedClip, type Keyframe } from "@schemas";
import * as howler from "howler";
import * as pixi from "pixi.js";
import { Player, PlayerType } from "./player";
export class AudioPlayer extends Player {
private audioResource: howler.Howl | null;
private isPlaying: boolean;
private volumeKeyframeBuilder!: KeyframeBuilder;
private syncTimer: number;
constructor(edit: Edit, clipConfiguration: ResolvedClip) {
super(edit, clipConfiguration, PlayerType.Audio);
this.audioResource = null;
this.isPlaying = false;
this.syncTimer = 0;
}
public override async load(): Promise<void> {
await super.load();
const audioClipConfiguration = this.clipConfiguration.asset as AudioAsset;
const identifier = audioClipConfiguration.src;
if (!identifier) {
throw new Error("Audio asset is missing a 'src'.");
}
const loadOptions: pixi.UnresolvedAsset = { src: identifier, parser: AudioLoadParser.Name };
const audioResource = await this.edit.assetLoader.load<howler.Howl>(identifier, loadOptions);
const isValidAudioSource = audioResource instanceof howler.Howl;
if (!isValidAudioSource) {
throw new Error(`Invalid audio source '${audioClipConfiguration.src}'.`);
}
this.audioResource = audioResource;
// Create volume keyframes after timing is resolved (not in constructor)
const baseVolume = typeof audioClipConfiguration.volume === "number" ? audioClipConfiguration.volume : 1;
this.volumeKeyframeBuilder = new KeyframeBuilder(this.createVolumeKeyframes(audioClipConfiguration, baseVolume), this.getLength(), baseVolume);
// Set initial volume immediately so the Howl never sits at the default of 1.0
this.audioResource.volume(this.getVolume());
this.configureKeyframes();
}
public override update(deltaTime: number, elapsed: number): void {
super.update(deltaTime, elapsed);
const { trim = 0 } = this.clipConfiguration.asset as AudioAsset;
this.syncTimer += elapsed;
this.getContainer().alpha = 0;
if (!this.audioResource) {
return;
}
const shouldClipPlay = this.edit.isPlaying && this.isActive();
// getPlaybackTime() returns seconds
const playbackTime = this.getPlaybackTime();
if (shouldClipPlay) {
if (!this.isPlaying) {
this.isPlaying = true;
this.audioResource.volume(this.getVolume());
this.audioResource.seek(playbackTime + trim);
this.audioResource.play();
}
if (this.audioResource.volume() !== this.getVolume()) {
this.audioResource.volume(this.getVolume());
}
// Desync threshold: 0.1 seconds (100ms)
const desyncThreshold = 0.1;
// Both audioResource.seek() and playbackTime are in seconds
const shouldSync = Math.abs(this.audioResource.seek() - trim - playbackTime) > desyncThreshold;
if (shouldSync) {
this.audioResource.seek(playbackTime + trim);
}
}
if (this.isPlaying && !shouldClipPlay) {
this.isPlaying = false;
this.audioResource.pause();
}
// When paused, sync every 100ms for scrubbing
const shouldSync = this.syncTimer > 100;
if (!this.edit.isPlaying && this.isActive() && shouldSync) {
this.syncTimer = 0;
this.audioResource.seek(playbackTime + trim);
}
}
public override dispose(): void {
if (this.audioResource) {
this.audioResource.stop();
this.audioResource.unload();
}
this.audioResource = null;
super.dispose();
}
/** Reload the audio asset when asset.src changes (e.g., merge field update or loadEdit) */
public override async reloadAsset(): Promise<void> {
if (this.audioResource) {
this.audioResource.stop();
this.audioResource.unload();
}
this.audioResource = null;
this.isPlaying = false;
this.syncTimer = 0;
const audioAsset = this.clipConfiguration.asset as AudioAsset;
if (!audioAsset.src) {
throw new Error("Audio asset is missing a 'src'.");
}
const loadOptions: pixi.UnresolvedAsset = { src: audioAsset.src, parser: AudioLoadParser.Name };
const audioResource = await this.edit.assetLoader.load<howler.Howl>(audioAsset.src, loadOptions);
if (!(audioResource instanceof howler.Howl)) {
throw new Error(`Invalid audio source '${audioAsset.src}'.`);
}
this.audioResource = audioResource;
this.audioResource.volume(this.getVolume());
}
public override reconfigureAfterRestore(): void {
super.reconfigureAfterRestore();
// Rebuild volume keyframes with updated timing
const audioAsset = this.clipConfiguration.asset as AudioAsset;
const baseVolume = typeof audioAsset.volume === "number" ? audioAsset.volume : 1;
this.volumeKeyframeBuilder = new KeyframeBuilder(this.createVolumeKeyframes(audioAsset, baseVolume), this.getLength(), baseVolume);
}
public override getSize(): Size {
return { width: 0, height: 0 };
}
public getVolume(): number {
return this.volumeKeyframeBuilder.getValue(this.getPlaybackTime());
}
public getCurrentDrift(): number {
if (!this.audioResource) return 0;
const { trim = 0 } = this.clipConfiguration.asset as AudioAsset;
const audioTime = this.audioResource.seek() as number;
// getPlaybackTime() returns seconds, audioTime is also seconds
const playbackTime = this.getPlaybackTime();
return Math.abs(audioTime - trim - playbackTime);
}
private createVolumeKeyframes(asset: AudioAsset, baseVolume: number): Keyframe[] | number {
const { effect, volume } = asset;
if (!effect || effect === "none" || Array.isArray(volume)) {
return volume ?? 1;
}
const clipLength = this.getLength();
const fade = Math.min(2, clipLength / 2);
if (effect === "fadeIn") {
return [{ from: 0, to: baseVolume, start: 0, length: fade }];
}
if (effect === "fadeOut") {
return [{ from: baseVolume, to: 0, start: clipLength - fade, length: fade }];
}
// fadeInFadeOut
return [
{ from: 0, to: baseVolume, start: 0, length: fade },
{ from: baseVolume, to: 0, start: clipLength - fade, length: fade }
];
}
}