I'm powering my computer with a bicycle..., just show me the code!: (copyable code-blocks below)
The final result of our pitch modulated Sound Cue
Sound cue refresher (skip me if you're familiar with a sound cue)
A sound cue represents an encapsulated group of logic around a sound or sounds. A sound cue can switch between playing multiple sounds, apply effects to the sound based on input, etc. To the engine, a sound cue is a singular object/sound, but may contain quite a lot of complexity, and mix together many different sounds. Sound cue's contain parameters just like materials contain parameters, and these parameters can be set during runtime to manipulate how the sound-cue behaves. In our case we are going to modify a modulation parameter to control the pitch of a looping sound cue.
Setup our sound cue in the editor (skip me if you have a sound cue w/parameters)
Step 1.) Create a new sound cue (content browser - Add New - sounds - sound cue)
Step 3.) Add a Wave Player to the Sound Cue.
Step 4.) Add a Continuous Modulator to the Sound Cue
Step 6.) Click on the Wave Player to configure it, and set what sound it plays (.wav file)
In our case, I want the sound to loop continiously, so I check 'Looping'.
Step 7.) Configure the modulator
It's important the parameter value is set, in my case I only care about manipulating pitch at runtime, and not volume, however you can set parameters for both if you need to manipulate both during runtime. We will use this parameter name 'pitch' later in our code.
Step 8.) My real configuration for my propeller
Step 9.) Save the cue and close it...
Code Configuration
First, I'm going to place all my logic in a Pawn class that inherits from AActor... you can theoretically place your sound logic anywhere, but this example will include a pawn.
Step 1.) We need to setup some fields in our header to hold references to our objects. Ensure you use the UPROPERTY() macros, as this prevents unreal from garbage collecting the object before it should.
Create a pawn class and in your header file ensure these properties and methods are defined.
Step 2.) We need to configure our constructor to get references to and configure our sound objects.
Step 3.) We need to add our Sound Cue to our Audio Component, which you should avoid doing in the constructor, so we'll perform the same action in PostInitializeComponents()
Step 4.) When our pawn is spawned (-1 for rhyming) we need to have it start playing the Cue audio. You may want to do this based on some other event, but for my needs I want the sound to start playing on BeginPlay
Step 5.) Each frame we need to update the sound parameter pitch based on our engines speed.
Step 4.) When our pawn is spawned (-1 for rhyming) we need to have it start playing the Cue audio. You may want to do this based on some other event, but for my needs I want the sound to start playing on BeginPlay
Step 5.) Each frame we need to update the sound parameter pitch based on our engines speed.