该文主要简单讲述UE4 声音模块的导入声音ImportSound,声音资源SoundCue,声音编辑器SoundEditor,音效类SoundClass,音效类混音SoundClassMix,声音衰减SoundAttenuation,声音并发SoundConcurrency,播放声音PlaySound。
虚幻引擎当前可导入未压缩、小端字节、16位的Wave(WAV)文件(支持各种采样率,但推荐采样率为44.1 kHz或22.05 kHz)。
在内部,UE4以16位未压缩的 .wav 格式存储导入的音频文件。
导出多声道声音资源将创建每个通道的单声道 .WAV 文件,同时执行多声道音频扩展名命名规则。
左前 _fl 右前 _fr 前中 _fc 低频_lf 左侧 _sl 右侧 _sr 左后 _bl 右后 _br
如:
在虚幻音频引擎中,有一种资产会将多个音频资产概念性地合并成一个抽象的音效对象,那个资产就是 Sound Cue。它实际上并不是声音本身,而是 声音的抽象概念。
Sound Cue会在运行时计算,它是多个非确切声音的集合。换句话说,每次播放Sound Cue时,声音都可能会有所不同,具体取决于它的使用背景。
Sound Cue编辑器使音效设计师能够创建自己的声音设计拓扑,可以将简单的播放音效事件转换为任意的复杂音效设计事件。
总体上说,Sound Cue支持资产随机化、Gameplay驱动的参数映射、用于实现距离衰减的自定义逻辑、体积衰减,音高变化以及许多其他功能。
项目中使用的声音不要直接播放SoundWav资源,应该只使用SoundCue来作为资源,才是正确的流程。
音效类(Sound Class) 是 虚幻引擎(UE4) 中音频引擎的一种资产类型,用户可以使用音效类将多种声音分组到一起,然后同时修改所有相关声波的参数。
音量(Volume):类中每种音效音量的乘数。此属性在声波对象自身的音量设置上起作用。
音高(Pitch):类种每种音效音高的乘数,在声波对象中的音高设置上起作用。
低通滤波器频率(Low Pass Filter Frequency):低通滤波器的截至频率,影响类中的所有音效;值20,000或以上对声波没有影响。
衰减距离缩放(Attenuation Distance Scale):缩放类中每种音效的衰减距离,允许动态调整衰减设置。
始终播放(Always Play):启用后,将阻止音效类中的音效被优先级低于 始终(Always) 的音效从声音池弹出。
它可以像程序类型一样,拥有继承的关系,设置子类和父类。
音效类可以具有附加的 音效类混音(Sound Class Mixes) 数组,这些混音可以在播放该音效类中的任何音效时自动触发。这些混音被称为 被动 混音,因为它们不需要手动激活。
例如,在播放音效类的某个音效时,将会自动触发这些混音,而不需要明确发出 Push Sound Mix Modifier 命令。如果需要,你仍然可以通过 Push 命令调用混音修正器,并且多个音效类或蓝图可以引用同一混音修正器。
设置SoundClassMix,一种方法可以直接在SoundClass 的编辑器里面 Passive Sound Mix Modifiers 位置直接新增并设置,还可以通过程序或者蓝图调用 UGameplayStatics类中:
// --- Audio Functions ----------------------------
/** Set the sound mix of the audio system for special EQing **/
UFUNCTION(BlueprintCallable, Category="Audio", meta=(WorldContext = "WorldContextObject"))
static void SetBaseSoundMix(const UObject* WorldContextObject, class USoundMix* InSoundMix);
/** Push a sound mix modifier onto the audio system **/
UFUNCTION(BlueprintCallable, Category="Audio", meta=(WorldContext = "WorldContextObject"))
static void PushSoundMixModifier(const UObject* WorldContextObject, class USoundMix* InSoundMixModifier);
设置整体声音大小,通过预先在SoundCue上设置的SoundClass,控制不同SoundClass的所有声音的音量等属性。
/** Overrides the sound class adjuster in the given sound mix. If the sound class does not exist in the input sound mix, the sound class adjustment will be added to the sound mix.
* @param InSoundMixModifier The sound mix to modify.
* @param InSoundClass The sound class to override (or add) in the sound mix.
* @param Volume The volume scale to set the sound class adjuster to.
* @param Pitch The pitch scale to set the sound class adjuster to.
* @param FadeInTime The interpolation time to use to go from the current sound class adjuster values to the new values.
* @param bApplyToChildren Whether or not to apply this override to the sound class' children or to just the specified sound class.
*/
UFUNCTION(BlueprintCallable, Category = "Audio", meta = (WorldContext = "WorldContextObject"))
static void SetSoundMixClassOverride(const UObject* WorldContextObject, class USoundMix* InSoundMixModifier, class USoundClass* InSoundClass, float Volume = 1.0f, float Pitch = 1.0f, float FadeInTime = 1.0f, bool bApplyToChildren = true);
音效衰减设置资源的参考。该资源会根据音效相对于聆听者的距离来控制音效的的各个方面。
衰减函数有 线性,对数,指数,反函数,对数反函数,自然音效,或自定义曲线等。
衰减形状分为 球体,胶囊体,盒装,锥体等。
游戏混音经常被忽视的问题是管理 音效并发,本质上是可以同时播放多少给定类型的音效。若无谨慎的管理,游戏很容易生成大量同一类型的音效,如枪支和其他敌人武器。
虚幻音频引擎为音效设计师提供了通过音效并发资产控制其并发组的工具。此资产定义了组中允许的音效数量的限制,以及达到该限制后该怎么做。
如何播放声音:
/**
* Plays a sound directly with no attenuation, perfect for UI sounds.
*
* * Fire and Forget.
* * Not Replicated.
* @param Sound - Sound to play.
* @param VolumeMultiplier - Multiplied with the volume to make the sound louder or softer.
* @param PitchMultiplier - Multiplies the pitch.
* @param ConcurrencySettings - Override concurrency settings package to play sound with
* @param StartTime - How far in to the sound to begin playback at
* @param ConcurrencySettings - Override concurrency settings package to play sound with
* @param OwningActor - The actor to use as the "owner" for concurrency settings purposes. Allows PlaySound calls to do a concurrency limit per owner.
* @param bIsUISound - True if sound is UI related, else false
*/
UFUNCTION(BlueprintCallable, BlueprintCosmetic, Category="Audio", meta=( WorldContext="WorldContextObject", AdvancedDisplay = "2", UnsafeDuringActorConstruction = "true" ))
static void PlaySound2D(const UObject* WorldContextObject, USoundBase* Sound, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundConcurrency* ConcurrencySettings = nullptr, AActor* OwningActor = nullptr, bool bIsUISound = true);
/**
* Spawns a sound with no attenuation, perfect for UI sounds.
*
* * Not Replicated.
* @param Sound - Sound to play.
* @param VolumeMultiplier - Multiplied with the volume to make the sound louder or softer.
* @param PitchMultiplier - Multiplies the pitch.
* @param StartTime - How far in to the sound to begin playback at
* @param ConcurrencySettings - Override concurrency settings package to play sound with
* @param PersistAcrossLevelTransition - Whether the sound should continue to play when the map it was played in is unloaded
* @param bAutoDestroy - Whether the returned audio component will be automatically cleaned up when the sound finishes (by completing or stopping) or whether it can be reactivated
* @return An audio component to manipulate the spawned sound
*/
UFUNCTION(BlueprintCallable, BlueprintCosmetic, Category="Audio", meta=( WorldContext="WorldContextObject", AdvancedDisplay = "2", UnsafeDuringActorConstruction = "true", Keywords = "play" ))
static UAudioComponent* SpawnSound2D(const UObject* WorldContextObject, USoundBase* Sound, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundConcurrency* ConcurrencySettings = nullptr, bool bPersistAcrossLevelTransition = false, bool bAutoDestroy = true);
/**
* Spawns a sound at the given location. This does not travel with any actor. Replication is also not handled at this point.
* @param Sound - sound to play
* @param Location - World position to play sound at
* @param Rotation - World rotation to play sound at
* @param VolumeMultiplier - Volume multiplier
* @param PitchMultiplier - PitchMultiplier
* @param StartTime - How far in to the sound to begin playback at
* @param AttenuationSettings - Override attenuation settings package to play sound with
* @param ConcurrencySettings - Override concurrency settings package to play sound with
* @param bAutoDestroy - Whether the returned audio component will be automatically cleaned up when the sound finishes (by completing or stopping) or whether it can be reactivated
* @return An audio component to manipulate the spawned sound
*/
UFUNCTION(BlueprintCallable, Category="Audio", meta=(WorldContext="WorldContextObject", AdvancedDisplay = "3", UnsafeDuringActorConstruction = "true", Keywords = "play"))
static UAudioComponent* SpawnSoundAtLocation(const UObject* WorldContextObject, USoundBase* Sound, FVector Location, FRotator Rotation = FRotator::ZeroRotator, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, class USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, bool bAutoDestroy = true);
UAudioComponent* TmAudioCom = UGameplayStatics::SpawnSoundAtLocation(this, SoundBallHit, GetActorLocation());
if (TmAudioCom)
{
TmAudioCom->SetIntParameter(TEXT("Index"), 2);
}