audio_falloff_set_model

要为音频引擎添加更多函数,GameMaker允许您选择适合您的游戏的衰减模型。此模型将用于游戏或应用程序中的所有音频函数,因此,您应确保选择的模型是正确的,因为每个模型都会影响收听者通过发射器或使用函数audio_play_sound_at()来感知您播放的声音。

默认衰减模型为audio_falloff_none,这意味着使用发射器或定位音频时没有衰减,除非您更改衰减模型。

衰减模型

当通过audio_play_sound_at()播放音频或为发射器设置衰减时,需要设置三个参数,每个参数都适合特定模型,并且将影响播放器听到最终声音的方式,这取决于收听者与源之间的距离。三个论点是:

受这些参数影响的衰减模型在GameMaker中由以下常量表示(该表也显示了使用的精确计算):

音频衰减常量
常量增益计算
audio_falloff_exponent_distance
gain = (listener_distance / reference_distance) ^ (-falloff_factor)
audio_falloff_exponent_distance_clamped
distance = clamp(listener_distance, reference_distance, maximum_distance)
gain = (distance / reference_distance) ^ (-falloff_factor)
audio_falloff_exponent_distance_scaled
distance = clamp(listener_distance, reference_distance, maximum_distance)

gain = ((distance / reference_distance) ^ (-falloff_factor)) * (((maximum_distance - distance) / (maximum_distance - reference_distance)) ^ (distance / maximum_distance))
audio_falloff_inverse_distance
gain = reference_distance / (reference_distance + falloff_factor * (listener_distance - reference_distance))
audio_falloff_inverse_distance_clamped
distance = clamp(listener_distance, reference_distance, maximum_distance)
gain = reference_distance / (reference_distance + falloff_factor * (distance - reference_distance))
audio_falloff_inverse_distance_scaled
distance = clamp(listener_distance, reference_distance, maximum_distance)

gain = (reference_distance / (reference_distance + falloff_factor * (distance - reference_distance))) * (((maximum_distance - distance) / (maximum_distance - reference_distance)) ^ (distance / maximum_distance))
audio_falloff_linear_distance
distance = min(distance, maximum_distance)
gain = (1 - falloff_factor * (distance - reference_distance) / (maximum_distance - reference_distance))
audio_falloff_linear_distance_clamped
distance = clamp(listener_distance, reference_distance, maximum_distance)
gain = (1 - falloff_factor * (distance - reference_distance) / (maximum_distance - reference_distance))
audio_falloff_none
gain = 1

"_scaled"模型的缩放方式确保声音以最大距离完全脱落。

下图是上述一些常数如何工作和影响正在播放的声音的视觉表示:

Distance Model Examples

HTML5 Limitations

在 HTML5 中,不支持_clamped_scaled变体,因为网络音频不支持它们。如果您在 HTML5 上将衰减模型设置为这些常量之一,GameMaker将在内部使用最接近的可用模型,如下所示:

此更改仅在内部发生,衰减模型值仍然是您传递给函数的值。

 

语法:

audio_falloff_set_model(model);

参数类型描述
model音频衰减常量用于设置衰减模型的常量

 

返回:

N/A

 

例子:

audio_falloff_set_model(audio_falloff_exponent_distance_clamped);
audio_play_sound_at(snd_Waterfall, x, y, 0, 100, 300, 1, true, 1);

上述代码设置衰减模型,然后播放在变量"snd_Waterfall"中索引的声音,该声音将在其房间位置循环,衰减参考值为100,衰减距离为300,衰减因子为1,优先级较低。