From 307bf8a10d47d0057f7253446b51857e53308dd1 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Wed, 15 Jan 2025 21:53:54 +0800 Subject: [PATCH] feat: All intrinsic audio effects --- src/module/audio/effect.ts | 42 +++++++++++++++++++++++++++++--------- src/module/audio/player.ts | 11 ++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/module/audio/effect.ts b/src/module/audio/effect.ts index f6d1989..1471058 100644 --- a/src/module/audio/effect.ts +++ b/src/module/audio/effect.ts @@ -83,7 +83,11 @@ export class StereoEffect extends AudioEffect { * @param y 朝向y坐标 * @param z 朝向z坐标 */ - setOrientation(x: number, y: number, z: number) {} + setOrientation(x: number, y: number, z: number) { + this.output.orientationX.value = x; + this.output.orientationY.value = y; + this.output.orientationZ.value = z; + } /** * 设置音频位置,x正方形水平向右,y正方形垂直于地面向上,z正方向垂直屏幕远离用户 @@ -91,7 +95,11 @@ export class StereoEffect extends AudioEffect { * @param y 位置y坐标 * @param z 位置z坐标 */ - setPosition(x: number, y: number, z: number) {} + setPosition(x: number, y: number, z: number) { + this.output.positionX.value = x; + this.output.positionY.value = y; + this.output.positionZ.value = z; + } end(): void {} @@ -113,12 +121,16 @@ export class VolumeEffect extends AudioEffect { * 设置音量大小 * @param volume 音量大小 */ - setVolume(volume: number) {} + setVolume(volume: number) { + this.output.gain.value = volume; + } /** * 获取音量大小 */ - getVolume(): number {} + getVolume(): number { + return this.output.gain.value; + } end(): void {} @@ -148,16 +160,22 @@ export class ChannelVolumeEffect extends AudioEffect { /** * 设置某个声道的音量大小 - * @param channel 要设置的声道 + * @param channel 要设置的声道,可填0-5 * @param volume 这个声道的音量大小 */ - setVolume(channel: number, volume: number) {} + setVolume(channel: number, volume: number) { + if (!this.gain[channel]) return; + this.gain[channel].gain.value = volume; + } /** - * 获取某个声道的音量大小 + * 获取某个声道的音量大小,可填0-5 * @param channel 要获取的声道 */ - getVolume(channel: number): number {} + getVolume(channel: number): number { + if (!this.gain[channel]) return 0; + return this.gain[channel].gain.value; + } end(): void {} @@ -179,12 +197,16 @@ export class DelayEffect extends AudioEffect { * 设置延迟时长 * @param delay 延迟时长,单位秒 */ - setDelay(delay: number) {} + setDelay(delay: number) { + this.output.delayTime.value = delay; + } /** * 获取延迟时长 */ - getDelay() {} + getDelay() { + return this.output.delayTime.value; + } end(): void {} diff --git a/src/module/audio/player.ts b/src/module/audio/player.ts index d52e4ae..abc26b1 100644 --- a/src/module/audio/player.ts +++ b/src/module/audio/player.ts @@ -8,6 +8,7 @@ import { import { AudioEffect, ChannelVolumeEffect, + DelayEffect, EchoEffect, IAudioOutput, StereoEffect, @@ -138,6 +139,16 @@ export class AudioPlayer extends EventEmitter { return new ChannelVolumeEffect(this.ac); } + /** + * 创建一个延迟效果器 + * |-----------| + * Input ----> | DelayNode | ----> Output + * |-----------| + */ + createDelay() { + return new DelayEffect(this.ac); + } + /** * 创建一个回声效果器 * ```txt