Skip to content

Commit 055d0aa

Browse files
committed
fixed bug
1 parent 636667b commit 055d0aa

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

playground/main.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ const image = await composition.add(
3232
})
3333
);
3434

35-
const audioTrack = composition.createTrack('audio').stacked();
35+
const audioTrack = composition.createTrack('audio').stacked(true);
36+
const audioSource = await core.AudioSource.from('/harvard.MP3');
3637
await audioTrack.add(
37-
await new core.AudioClip(
38-
await core.AudioSource.from('/harvard.MP3')
39-
)
38+
await new core.AudioClip(audioSource)
4039
);
41-
await audioTrack.removeSilences();
40+
await audioTrack.removeSilences({
41+
minDuration: 300,
42+
windowSize: 1,
43+
});
4244

4345
image.animate()
4446
.rotation(-16).to(14, 5).to(-7, 10).to(24, 7).to(-3, 9).to(19, 7).to(-14, 12).to(5, 9).to(-30, 13)

src/sources/audio.spec.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,24 @@ vi.stubGlobal('OfflineAudioContext', MockOfflineAudioContext); // Stub the globa
2929

3030
describe('AudioUtils', () => {
3131
it('all silent', () => {
32-
const silences = findSilences(new Float32Array(100).fill(1), -50, 100, 100);
32+
const silences = findSilences(new Float32Array(100).fill(0), -50, 100, 100);
3333
expect(silences).toEqual([{
3434
start: new Timestamp(0),
3535
stop: new Timestamp(100),
3636
}]);
3737
});
3838

3939
it('no silences', () => {
40-
const silences = findSilences(new Float32Array(100).fill(0), -50, 100, 100);
40+
const silences = findSilences(new Float32Array(100).fill(1), -50, 100, 100);
4141
expect(silences).toEqual([]);
4242
});
4343

4444
it('find silences correctly', () => {
4545
const samples = Array.from({ length: 500 }, (_, index) => index > 300 ? (index < 400 ? 0 : 1) : -1);
4646
const silences = findSilences(new Float32Array(samples), -50, 100, 5000);
4747
expect(silences).toEqual([{
48-
start: new Timestamp(0),
49-
stop: new Timestamp(3010),
50-
}, {
51-
start: new Timestamp(4000),
52-
stop: new Timestamp(5000),
48+
start: new Timestamp(3010),
49+
stop: new Timestamp(4000),
5350
}]);
5451
});
5552
});
@@ -63,18 +60,32 @@ describe('AudioSource', () => {
6360
});
6461

6562
it('find silences correctly', async () => {
63+
const audioBuffer = {
64+
duration: 16,
65+
sampleRate: 1000,
66+
length: 16000,
67+
getChannelData: () => new Float32Array(16000).fill(0), // Return a dummy Float32Array
68+
} as any as AudioBuffer;
69+
audioSource.audioBuffer = audioBuffer;
6670
const silences = await audioSource.silences({});
6771
expect(silences).toEqual([{
6872
start: new Timestamp(0),
69-
stop: new Timestamp(5000),
73+
stop: new Timestamp(16000),
7074
}]);
7175
});
7276

7377
it('find silences correctly with too high minDuration', async () => {
78+
const audioBuffer = {
79+
duration: 16,
80+
sampleRate: 1000,
81+
length: 16000,
82+
getChannelData: () => new Float32Array(16000).fill(0), // Return a dummy Float32Array
83+
} as any as AudioBuffer;
84+
audioSource.audioBuffer = audioBuffer;
7485
const silences = await audioSource.silences({minDuration: 1e10});
7586
expect(silences).toEqual([{
7687
start: new Timestamp(0),
77-
stop: new Timestamp(5000),
88+
stop: new Timestamp(16000),
7889
}]);
7990
});
8091

src/sources/audio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class AudioSource<T extends Object = {}> extends Source<T> {
139139
*/
140140
public async silences({
141141
threshold = -50,
142-
minDuration = 5,
142+
minDuration = 100,
143143
windowSize = 50,
144144
}: SilenceOptions = {}): Promise<{ start: Timestamp; stop: Timestamp }[]> {
145145
if (this._silences) return this._silences;

src/sources/audio.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function findSilences(
2121
let silenceStart: number | null = null;
2222

2323
for (let i = 0; i < decibelValues.length; i++) {
24-
if (decibelValues[i] > threshold) {
24+
if (decibelValues[i] < threshold) {
2525
if (silenceStart === null) {
2626
silenceStart = i;
2727
}

src/tracks/media/media.ts

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

1010
import type { MediaClip } from '../../clips';
1111
import { Timestamp } from '../../models';
12-
import { StackInsertStrategy } from '../track/track.strategies';
12+
import { SilenceOptions } from '../../sources';
1313

1414
export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
1515
public clips: Clip[] = [];
@@ -26,15 +26,16 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
2626
*
2727
* @returns Array of silence periods with start and stop times in seconds
2828
*/
29-
public async removeSilences() {
30-
29+
public async removeSilences(options: SilenceOptions = {}) {
30+
const numClips = this.clips.length;
3131
// Process each clip
32-
for (const clip of this.clips) {
32+
for (let i = 0; i < numClips; i++) {
33+
const clip = this.clips[i];
3334
if (!clip.element) {
3435
continue;
3536
}
3637

37-
const silences = await clip.source.silences({});
38+
const silences = await clip.source.silences(options);
3839
if (silences.length === 0) {
3940
continue;
4041
}
@@ -54,21 +55,22 @@ export class MediaTrack<Clip extends MediaClip> extends Track<MediaClip> {
5455
let currentClip = clip;
5556

5657
for (const silence of applicableSilences) {
57-
if (silence.start.millis < start.millis) {
58+
if (silence.start.millis <= start.millis) {
5859
const newClip = await currentClip.split(silence.stop.add(currentClip.offset));
5960
currentClip.detach();
6061
start = silence.stop;
6162
currentClip = newClip;
6263
continue;
6364
}
6465

65-
if (silence.stop.millis > currentClip.range[1].millis) {
66+
if (silence.stop.millis >= currentClip.range[1].millis) {
6667
currentClip = await currentClip.split(silence.start.add(currentClip.offset));
6768
currentClip.detach();
6869
continue;
6970
}
7071

7172
const middleClip = await currentClip.split(silence.start.add(currentClip.offset));
73+
7274
currentClip = await middleClip.split(silence.stop.add(middleClip.offset));
7375
middleClip.detach();
7476
}

0 commit comments

Comments
 (0)