Sunday, October 9, 2016

Unreal 4 - Playing Sounds from C++ with Sound Cue using Sound Parameters (Sound Component)

Hopefully you're here if you want it clearly stated how to manipulate a sound-cue from C++ without relying on any blueprint.  I'm going to assume just about zero knowledge, so for the more experienced you may want to skip down to the code sections.

I'm powering my computer with a bicycle..., just show me the code!: (copyable code-blocks below)


Using: USoundCue UAudioComponent from C++, C (no blueprints needed)

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 2.) Save the sound-cue with a name (in my case 'airplane-engine') and open it.









Step 3.) Add a Wave Player to the Sound Cue.


Step 4.) Add a Continuous Modulator to the Sound Cue




Step 5.) Connect Wave Player to Modulator to Output.








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.



11 comments:

  1. Hi,
    Thanks for the detailed review. Clearly understandable and well explained. Helped me a lot.
    Thanks.

    ReplyDelete
  2. Hi,

    Once i get to line 17 "propellerAudioComponent->bAutoActivate = false;" i get an error "Pointer to incomplete class type is not allowed"

    Do you know how to solve this issue

    Thanks.

    ReplyDelete
  3. Unknown: You need to include the header file so it knows what an audiocomponent class is:

    #include "Components/AudioComponent.h"

    Also, AttachParent is now private, use this instead:
    ->SetupAttachment(GetRootComponent());

    Ben: Great tutorial, very useful.

    ReplyDelete
    Replies
    1. SetupAttachment(GetRootComponent()); is not working.

      Delete
    2. should look like this

      propellerAudioComponent->SetupAttachment(RootComponent);

      Delete
  4. This isn't working at my project :(
    The line: "propellerAudioComponent = CreateDefaultSubobject(TEXT("PropellerAudioComp"));"
    generates this error: No object initializer found during construction. But I don't know why. Can you help me?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  5. Thank you so much for this step-by-step explanation. Really helped me out!

    ReplyDelete
  6. Best UE4 tutorial I've ever seen -- solved my audio looping problem. Thanks so much!

    ReplyDelete
  7. Respect and that i have a dandy offer you: What Home Renovation Shows Are On Netflix house and renovation show

    ReplyDelete