Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tickInterceptor to audio-clip #354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
caohongz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Loading