Skip to content

Commit a0bf5f1

Browse files
committed
performance improvements to remove silences
1 parent a4783a5 commit a0bf5f1

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/tracks/media/media.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('The Media Track Object', () => {
6060
await track.removeSilences();
6161
expect(track.clips.length).toBe(1);
6262
expect(track.clips.at(0)).toBe(clip);
63+
expect(track.clips.at(0)?.state).toBe('ATTACHED');
6364
});
6465

6566
it('removes silences', async () => {
@@ -81,10 +82,12 @@ describe('The Media Track Object', () => {
8182
expect(clip.source).toBeDefined();
8283
await track.removeSilences();
8384
expect(track.clips.length).toBe(2);
84-
expect(track.clips.at(0)?.range[0].millis).toBe(10051);
85+
expect(track.clips.at(0)?.range[0].millis).toBe(10050);
8586
expect(track.clips.at(0)?.range[1].millis).toBe(11000);
86-
expect(track.clips.at(1)?.range[0].millis).toBe(15001);
87+
expect(track.clips.at(1)?.range[0].millis).toBe(15000);
8788
expect(track.clips.at(1)?.range[1].millis).toBe(19000);
89+
expect(track.clips.at(0)?.state).toBe('ATTACHED');
90+
expect(track.clips.at(1)?.state).toBe('ATTACHED');
8891
});
8992

9093
it('should propagate a seek call', async () => {

src/tracks/media/media.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { Track } from '../track';
99

10-
import type { MediaClip } from '../../clips';
10+
import type { MediaClip, MediaClipProps } from '../../clips';
1111
import { Timestamp } from '../../models';
1212
import { SilenceOptions } from '../../sources';
1313

@@ -28,15 +28,19 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
2828
*/
2929
public async removeSilences(options: SilenceOptions = {}) {
3030
const numClips = this.clips.length;
31+
32+
let newClips: MediaClip<MediaClipProps>[] = [];
3133
// Process each clip
3234
for (let i = 0; i < numClips; i++) {
3335
const clip = this.clips[i];
3436
if (!clip.element) {
37+
newClips.push(clip);
3538
continue;
3639
}
3740

3841
const silences = await clip.source.silences(options);
3942
if (silences.length === 0) {
43+
newClips.push(clip);
4044
continue;
4145
}
4246

@@ -48,32 +52,41 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
4852
silence.stop.millis > clip.range[0].millis),
4953
);
5054
if (applicableSilences.length === 0) {
55+
newClips.push(clip);
5156
continue;
5257
}
5358

59+
clip.detach();
5460
let start = clip.range[0];
55-
let currentClip = clip;
61+
let currentClip = clip.copy();
5662

5763
for (const silence of applicableSilences) {
58-
if (silence.start.millis <= start.millis) {
59-
const newClip = await currentClip.split(silence.stop.add(currentClip.offset));
60-
currentClip.detach();
61-
start = silence.stop;
62-
currentClip = newClip;
64+
if (silence.start.millis <= start.millis) {
65+
start = silence.stop;
66+
currentClip.range[0] = silence.stop;
6367
continue;
6468
}
6569

6670
if (silence.stop.millis >= currentClip.range[1].millis) {
67-
currentClip = await currentClip.split(silence.start.add(currentClip.offset));
68-
currentClip.detach();
71+
currentClip.range[1] = silence.start;
72+
start = silence.stop;
73+
newClips.push(currentClip);
6974
continue;
7075
}
71-
72-
const middleClip = await currentClip.split(silence.start.add(currentClip.offset));
73-
74-
currentClip = await middleClip.split(silence.stop.add(middleClip.offset));
75-
middleClip.detach();
76+
const middleClip = currentClip.copy();
77+
middleClip.range[0] = silence.stop;
78+
79+
currentClip.range[1] = silence.start;
80+
newClips.push(currentClip);
81+
82+
currentClip = middleClip;
83+
}
84+
if (currentClip.id !== newClips.at(-1)?.id) {
85+
newClips.push(currentClip);
7686
}
7787
}
88+
89+
const promises = newClips.map((clip) => this.add(clip));
90+
await Promise.all(promises);
7891
}
7992
}

0 commit comments

Comments
 (0)