Skip to content

Commit

Permalink
Merge pull request #354 from caohongz/feat--add-tickInterceptor-to-au…
Browse files Browse the repository at this point in the history
…dio-clip

feat: add tickInterceptor to audio-clip
  • Loading branch information
hughfenghen authored Jan 15, 2025
2 parents d7ef761 + 876c2d5 commit 6b14182
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-rules-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@webav/av-cliper': patch
---

add tickInterceptor to audio-clip
15 changes: 15 additions & 0 deletions packages/av-cliper/src/clips/__tests__/audio-clip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ test('AudioClip tick', async () => {
expect(s2).toBe('done');
});

test('AudioClip tickInterceptor', async () => {
const clip = new AudioClip((await fetch(m4a_44kHz_2chan)).body!);
let frameCnt = 0;
clip.tickInterceptor = async (_, tickRet) => {
if (tickRet.audio != null) frameCnt += 1;
return tickRet;
};
await clip.ready;
await clip.tick(1000 * 30 * 2);
await clip.tick(-1);
await clip.tick(180 * 1e6);

expect(frameCnt).toBe(3);
});

test('AudioClip volume', async () => {
const clip1 = new AudioClip((await fetch(m4a_44kHz_2chan)).body!);
const clip2 = new AudioClip((await fetch(m4a_44kHz_2chan)).body!, {
Expand Down
20 changes: 16 additions & 4 deletions packages/av-cliper/src/clips/audio-clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ export class AudioClip implements IClip {
);
}

/**
* 拦截 {@link AudioClip.tick} 方法返回的数据,用于对音频数据二次处理
* @param time 调用 tick 的时间
* @param tickRet tick 返回的数据
*
* @see [移除视频绿幕背景](https://bilibili.github.io/WebAV/demo/3_2-chromakey-video)
*/
tickInterceptor: <T extends Awaited<ReturnType<AudioClip['tick']>>>(
time: number,
tickRet: T,
) => Promise<T> = async (_, tickRet) => tickRet;

// 微秒
#ts = 0;
#frameOffset = 0;
Expand All @@ -131,7 +143,7 @@ export class AudioClip implements IClip {
}> {
if (!this.#opts.loop && time >= this.#meta.duration) {
// 待观察:如果time跨度较大,返回done,理论上会丢失一些音频帧
return { audio: [], state: 'done' };
return await this.tickInterceptor(time, { audio: [], state: 'done' });
}

const deltaTime = time - this.#ts;
Expand All @@ -142,10 +154,10 @@ export class AudioClip implements IClip {
this.#frameOffset = Math.ceil(
(this.#ts / 1e6) * DEFAULT_AUDIO_CONF.sampleRate,
);
return {
return await this.tickInterceptor(time, {
audio: [new Float32Array(0), new Float32Array(0)],
state: 'success',
};
});
}

this.#ts = time;
Expand All @@ -164,7 +176,7 @@ export class AudioClip implements IClip {
];
this.#frameOffset = endIdx;

return { audio, state: 'success' };
return await this.tickInterceptor(time, { audio, state: 'success' });
}

/**
Expand Down

0 comments on commit 6b14182

Please sign in to comment.