音频 API 参考
scratch-audio 库提供了一个强大的音频引擎,用于在 Scratch 扩展中播放声音、应用效果和管理音频。本参考涵盖开发扩展时可用的关键类和方法。
扩展开发者概述
创建具有音频功能的 Scratch 扩展时,你将使用:
- AudioEngine - 通过
util.runtime.audioEngine访问 - SoundPlayer - 用于播放单个声音
- SoundBank - 用于管理角色特定的声音
- EffectChain - 用于应用音频效果
扩展音频系统架构
Extension Context
├── Runtime Audio Engine (util.runtime.audioEngine)
├── Target Sound Banks (util.target.sprite.soundBank)
├── Effect Chains (for audio processing)
└── Sound Players (for individual sound playback)
在扩展中访问音频
获取音频引擎
在扩展积木中,通过运行时访问音频引擎:
playCustomSound(args, util) {
const audioEngine = util.runtime.audioEngine;
if (!audioEngine) {
console.warn('Audio engine not available');
return;
}
// 使用 audioEngine 进行音频操作
}
扩展声音加载模式
class MyAudioExtension {
constructor(runtime) {
this.runtime = runtime;
this.sounds = new Map(); // 扩展声音缓存
}
async loadSound(soundData, soundName) {
const audioEngine = this.runtime.audioEngine;
try {
const soundPlayer = await audioEngine.decodeSoundPlayer({
data: soundData // 音频文件的 ArrayBuffer
});
soundPlayer.connect(audioEngine);
this.sounds.set(soundName, soundPlayer);
return soundPlayer;
} catch (error) {
console.error('Failed to load sound:', error);
return null;
}
}
}
## 核心类
### AudioEngine
处理全局功能的主音频引擎。
#### 构造函数
```javascript
const audioEngine = new AudioEngine(audioContext);
参数:
audioContext(可选)- 自定义 AudioContext。如果未提 供,将创建一个新的。
关键属性
// 音频时间线上的当前时间
audioEngine.currentTime // number
// 主音量控制节点
audioEngine.inputNode // GainNode
// Web Audio API 操作的音 频上下文
audioEngine.audioContext // AudioContext
// 可用效果名称
## 扩展核心类
### AudioEngine
通过扩展积木中的 `util.runtime.audioEngine` 访问的主音频引擎。
#### 关键属性(只读)
```javascript
const audioEngine = util.runtime.audioEngine;
audioEngine.currentTime // number - 当前音频时间线位置
audioEngine.inputNode // GainNode - 主音量控制
audioEngine.EFFECT_NAMES // { pitch: 'pitch', pan: 'pan' }
扩展的关键方法
decodeSoundPlayer(sound)
解码音频数据并返回 SoundPlayer 以供立即使用。
// 在扩展积木函数中
async loadExtensionSound(args, util) {
const audioEngine = util.runtime.audioEngine;
const soundData = {
data: arrayBuffer // 你的音频文件作为 ArrayBuffer
};
try {
const soundPlayer = await audioEngine.decodeSoundPlayer(soundData);
soundPlayer.connect(audioEngine);
return soundPlayer;
} catch (error) {
console.error('Failed to decode sound:', error);
return null;
}
}
createEffectChain()
创建用于处理音频的效果链。
applyEffectsToSound(args, util) {
const audioEngine = util.runtime.audioEngine;
const effectChain = audioEngine.createEffectChain();
effectChain.set('pitch', 10); // +1 半音
effectChain.set('pan', -50); // 向左平移
effectChain.set('volume', 80); // 80% 音量
effectChain.connect(audioEngine);
return effectChain;
}
getLoudness()
获取当前麦克风音量(0-100)。
getMicrophoneLevel(args, util) {
const audioEngine = util.runtime.audioEngine;
try {
return audioEngine.getLoudness();
} catch (error) {
console.warn('Microphone not available:', error);
return 0;
}
}
SoundPlayer
管理单个声音播放。通常通过 audioEngine.decodeSoundPlayer() 创建。
关键属性
soundPlayer.id // string - 唯一标识符
soundPlayer.isPlaying // boolean - 当前是否正在播放
soundPlayer.isStarting // boolean - 是否正在启动
soundPlayer.buffer // AudioBuffer - 音频数据
soundPlayer.playbackRate // number - 播放速度 (1.0 = 正常)
关键方法
play()
开始播放声音。
playExtensionSound(args, util) {
const soundPlayer = this.getSoundPlayer(args.SOUND_NAME);
if (soundPlayer) {
soundPlayer.play();
// 可选:监听事件
soundPlayer.once('stop', () => {
console.log('Sound finished playing');
});
}
}
stop()
停止播放并淡出。
stopExtensionSound(args, util) {
const soundPlayer = this.getSoundPlayer(args.SOUND_NAME);
if (soundPlayer && soundPlayer.isPlaying) {
soundPlayer.stop();
}
}
connect(target)
连接到音频目标或效果链。
// 直接连接到引擎
soundPlayer.connect(audioEngine);
// 通过效果连接
soundPlayer.connect(effectChain);
effectChain.connect(audioEngine);
SoundBank(角色集成)
每个角色都有一个声音库,可通过 util.target.sprite.soundBank 访问。
关键方法
playSound(target, soundId)
播放角色的声音并应用其效果。
playSpriteSound(args, util) {
const target = util.target;
const soundIndex = parseInt(args.SOUND_INDEX);
if (target.sprite.soundBank && target.sprite.sounds[soundIndex]) {
const sound = target.sprite.sounds[soundIndex];
target.sprite.soundBank.playSound(target, sound.soundId);
}
}
音频效果
可用效果
- 音调效果 - 通过改变播放速率来改变音调
- 平移效果 - 在扬声器之间左右移动声音
- 音量效果 - 控制振幅/音量
使用效果链
const effectChain = audioEngine.createEffectChain();
// 设置绝对值
effectChain.set('pitch', 20); // +2 半音
effectChain.set('pan', -100); // 完全左 移
effectChain.set('volume', 50); // 50% 音量
// 相对于当前值改变
effectChain.change('pitch', 10); // 再增加 1 个半音
effectChain.change('volume', -10); // 减少 10% 音量
// 清除所有效果
effectChain.clear();
// 通过效果链连接声音
soundPlayer.connect(effectChain);
effectChain.connect(audioEngine);
效果值范围
// 音调:每 10 个增量 = 1 个半音
// 典型范围 -1200 到 +1200(共 10 个八度)
effectChain.set('pitch', 120); // +1 个八度
// 平移:-100(完全左)到 +100(完全右)
effectChain.set('pan', 0); // 居中
// 音量:0(静音)到 100+(正常 = 100)
effectChain.set('volume', 100); // 正常音量