Skip to content

Commit 263496a

Browse files
committed
added caching to silence detection
1 parent c99cd44 commit 263496a

File tree

4 files changed

+16
-98
lines changed

4 files changed

+16
-98
lines changed

src/sources/audio.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ describe('AudioSource', () => {
7070
}]);
7171
});
7272

73+
it('find silences correctly after caching', async () => {
74+
const silences = await audioSource.silences({});
75+
const cachedSilences = await audioSource.silences({threshold: 0, minDuration: 1e10, windowSize: 1e10});
76+
expect(silences).toEqual(cachedSilences);
77+
});
78+
7379
it('should decode an audio buffer correctly', async () => {
7480
const buffer = await audioSource.decode(2, 44100, true);
7581
expect(buffer.duration).toBe(5); // Mock duration

src/sources/audio.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const DEFAULT_SAMPLE_RATE = 3000;
1818
export class AudioSource<T extends Object = {}> extends Source<T> {
1919
public readonly type: ClipType = 'audio';
2020
private decoding = false;
21+
private _silences?: { start: Timestamp; stop: Timestamp }[];
2122

2223
public transcript?: Transcript;
2324
public audioBuffer?: AudioBuffer;
@@ -130,7 +131,7 @@ export class AudioSource<T extends Object = {}> extends Source<T> {
130131
}
131132

132133
/**
133-
* Find silences in the audio clip
134+
* Find silences in the audio clip. Results are cached.
134135
*
135136
* uses default sample rate of 3000
136137
* @param options - Silences options.
@@ -141,11 +142,14 @@ export class AudioSource<T extends Object = {}> extends Source<T> {
141142
minDuration = 5,
142143
windowSize = 50,
143144
}: SilenceOptions): Promise<{ start: Timestamp; stop: Timestamp }[]> {
145+
if (this._silences) return this._silences;
146+
144147
const audioBuffer = this.audioBuffer ?? (await this.decode(1, DEFAULT_SAMPLE_RATE, true));
145148
const length = Math.floor(audioBuffer.length / windowSize);
146149
const samples = await this.fastsampler({ length, logarithmic: false });
147150

148151
const silences = findSilences(samples, threshold, minDuration, this.duration.millis);
152+
this._silences = silences;
149153

150154
return silences;
151155
}

src/tracks/media/media.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Track } from '../track';
99

1010
import type { MediaClip } from '../../clips';
1111
import type { Timestamp } from '../../models';
12-
import { getSilenceArrayBuffer } from './media.utils';
1312

1413
export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
1514
public clips: Clip[] = [];
@@ -29,30 +28,23 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
2928
* @param minSilenceDuration Minimum duration in seconds for a silence period to be included
3029
* @returns Array of silence periods with start and stop times in seconds
3130
*/
32-
public async detectSilences(
31+
public async removeSilences(
3332
subSample: number = 1000,
3433
silenceThreshold: number = -10,
3534
minSilenceDuration: number = 0.1
36-
): Promise<{ start: number; stop: number }[]> {
37-
const silences: { start: number; stop: number }[] = [];
38-
const audioContext = new AudioContext();
35+
) {
3936

4037
// Process each clip
4138
for (const clip of this.clips) {
4239
if (!clip.element) {
4340
continue;
4441
}
4542

46-
// Get audio data for this clip
47-
const arrayBuffer = await (await clip.source.getFile()).arrayBuffer();
48-
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
43+
const silences = await clip.source.silences({});
44+
if (silences.length === 0) continue;
4945

50-
silences.push(...getSilenceArrayBuffer(audioBuffer, subSample, minSilenceDuration, silenceThreshold, clip.start.seconds));
46+
5147

5248
}
53-
54-
await audioContext.close();
55-
56-
return silences.sort((a, b) => a.start - b.start);
5749
}
5850
}

src/tracks/media/media.utils.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)