Skip to content

Commit 6b14182

Browse files
authored
Merge pull request #354 from caohongz/feat--add-tickInterceptor-to-audio-clip
feat: add tickInterceptor to audio-clip
2 parents d7ef761 + 876c2d5 commit 6b14182

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

.changeset/violet-rules-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@webav/av-cliper': patch
3+
---
4+
5+
add tickInterceptor to audio-clip

packages/av-cliper/src/clips/__tests__/audio-clip.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ test('AudioClip tick', async () => {
2828
expect(s2).toBe('done');
2929
});
3030

31+
test('AudioClip tickInterceptor', async () => {
32+
const clip = new AudioClip((await fetch(m4a_44kHz_2chan)).body!);
33+
let frameCnt = 0;
34+
clip.tickInterceptor = async (_, tickRet) => {
35+
if (tickRet.audio != null) frameCnt += 1;
36+
return tickRet;
37+
};
38+
await clip.ready;
39+
await clip.tick(1000 * 30 * 2);
40+
await clip.tick(-1);
41+
await clip.tick(180 * 1e6);
42+
43+
expect(frameCnt).toBe(3);
44+
});
45+
3146
test('AudioClip volume', async () => {
3247
const clip1 = new AudioClip((await fetch(m4a_44kHz_2chan)).body!);
3348
const clip2 = new AudioClip((await fetch(m4a_44kHz_2chan)).body!, {

packages/av-cliper/src/clips/audio-clip.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ export class AudioClip implements IClip {
113113
);
114114
}
115115

116+
/**
117+
* 拦截 {@link AudioClip.tick} 方法返回的数据,用于对音频数据二次处理
118+
* @param time 调用 tick 的时间
119+
* @param tickRet tick 返回的数据
120+
*
121+
* @see [移除视频绿幕背景](https://bilibili.github.io/WebAV/demo/3_2-chromakey-video)
122+
*/
123+
tickInterceptor: <T extends Awaited<ReturnType<AudioClip['tick']>>>(
124+
time: number,
125+
tickRet: T,
126+
) => Promise<T> = async (_, tickRet) => tickRet;
127+
116128
// 微秒
117129
#ts = 0;
118130
#frameOffset = 0;
@@ -131,7 +143,7 @@ export class AudioClip implements IClip {
131143
}> {
132144
if (!this.#opts.loop && time >= this.#meta.duration) {
133145
// 待观察:如果time跨度较大,返回done,理论上会丢失一些音频帧
134-
return { audio: [], state: 'done' };
146+
return await this.tickInterceptor(time, { audio: [], state: 'done' });
135147
}
136148

137149
const deltaTime = time - this.#ts;
@@ -142,10 +154,10 @@ export class AudioClip implements IClip {
142154
this.#frameOffset = Math.ceil(
143155
(this.#ts / 1e6) * DEFAULT_AUDIO_CONF.sampleRate,
144156
);
145-
return {
157+
return await this.tickInterceptor(time, {
146158
audio: [new Float32Array(0), new Float32Array(0)],
147159
state: 'success',
148-
};
160+
});
149161
}
150162

151163
this.#ts = time;
@@ -164,7 +176,7 @@ export class AudioClip implements IClip {
164176
];
165177
this.#frameOffset = endIdx;
166178

167-
return { audio, state: 'success' };
179+
return await this.tickInterceptor(time, { audio, state: 'success' });
168180
}
169181

170182
/**

0 commit comments

Comments
 (0)