Skip to content

Commit 267727e

Browse files
committed
optimized for processing stack twice
1 parent a0bf5f1 commit 267727e

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/tracks/media/media.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ describe('The Media Track Object', () => {
9090
expect(track.clips.at(1)?.state).toBe('ATTACHED');
9191
});
9292

93+
it('removes silences twice', async () => {
94+
const clip = new MockMediaClip([new Timestamp(10000), new Timestamp(20000)], [
95+
{
96+
start: new Timestamp(0),
97+
stop: new Timestamp(500),
98+
},
99+
{
100+
start: new Timestamp(11000),
101+
stop: new Timestamp(15000),
102+
},
103+
{
104+
start: new Timestamp(19000),
105+
stop: new Timestamp(30500),
106+
},
107+
], new Audio());
108+
await track.add(clip);
109+
expect(clip.source).toBeDefined();
110+
await track.removeSilences();
111+
await track.removeSilences();
112+
expect(track.clips.length).toBe(2);
113+
expect(track.clips.at(0)?.id).not.toBe(clip.id);
114+
expect(track.clips.at(0)?.range[0].millis).toBe(10000);
115+
expect(track.clips.at(0)?.range[1].millis).toBe(11000);
116+
expect(track.clips.at(1)?.range[0].millis).toBe(15000);
117+
expect(track.clips.at(1)?.range[1].millis).toBe(19000);
118+
expect(track.clips.at(0)?.state).toBe('ATTACHED');
119+
expect(track.clips.at(1)?.state).toBe('ATTACHED');
120+
});
121+
93122
it('should propagate a seek call', async () => {
94123
const clip = new MediaClip();
95124
clip.element = new Audio();

src/tracks/media/media.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
3030
const numClips = this.clips.length;
3131

3232
let newClips: MediaClip<MediaClipProps>[] = [];
33+
let clipsToDetach: MediaClip<MediaClipProps>[] = [];
3334
// Process each clip
3435
for (let i = 0; i < numClips; i++) {
3536
const clip = this.clips[i];
3637
if (!clip.element) {
37-
newClips.push(clip);
3838
continue;
3939
}
4040

4141
const silences = await clip.source.silences(options);
4242
if (silences.length === 0) {
43-
newClips.push(clip);
4443
continue;
4544
}
4645

@@ -52,11 +51,10 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
5251
silence.stop.millis > clip.range[0].millis),
5352
);
5453
if (applicableSilences.length === 0) {
55-
newClips.push(clip);
5654
continue;
5755
}
5856

59-
clip.detach();
57+
clipsToDetach.push(clip);
6058
let start = clip.range[0];
6159
let currentClip = clip.copy();
6260

@@ -85,7 +83,7 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
8583
newClips.push(currentClip);
8684
}
8785
}
88-
86+
clipsToDetach.forEach((clip) => clip.detach());
8987
const promises = newClips.map((clip) => this.add(clip));
9088
await Promise.all(promises);
9189
}

0 commit comments

Comments
 (0)