Download

Sound creation with MixerDevice, WaveInstance, SoundBase and ActiveSound crashing the editor

Hi, I’m at a loss of what I’ve done wrong here, I’m in way over my head, but I feel like I’m almost there.
I’m trying to make a sound that’s different in the left and right earphones, GetAzimuth() and GetRange() are custom float-returning functions.

Any help would be hugely appreciated.



// Called when the game starts or when spawned
void ABinauralTestNine::BeginPlay()
{
    Super::BeginPlay();

    if (Audio)
    {
        ActiveSound.SetSound(Audio);
        ActiveSound.bAllowSpatialization = true;
        if (Audio->GetFullName().Contains("wav")) {CreateSound();}
        else {GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, Audio->GetFullName() + ": must be .wav filetype");}
    }
}

// Generates the output sound
void ABinauralTestNine::CreateSound()
{
    // Makes sure the Audio channels and buffer are ready
    Audio->NumChannels = 2;
    Buffer.Reset();

    // Sets the spatialisation settings for the left and right wave instances
    ActiveSound.AddWaveInstance(LeftSound.WaveInstanceHash);
    ActiveSound.AddWaveInstance(RightSound.WaveInstanceHash);
    LeftSoundPtr = &LeftSound;
    RightSoundPtr = &RightSound;
    WaveInstanceSetup(LeftSoundPtr, ECloserEar::LeftEar);
    WaveInstanceSetup(RightSoundPtr, ECloserEar::RightEar);

    // Creates the sound source from the generated buffer
    MixerDevice->MaxChannels = 2;
    SoundSource = MixerDevice->CreateSoundSource();
}

// Sets up the Wave Instance variables
void ABinauralTestNine::WaveInstanceSetup(FWaveInstance* WaveToMod, ECloserEar SideEar)
{
    WaveToMod->WaveData = Audio;
    WaveToMod->SetUseSpatialization(true);
    WaveToMod->Location = PlayerReference->GetActorLocation();
    WaveToMod->ListenerToSoundDistanceForPanning = GetRange();

    // Gets the correct volume, pitch and time differences for each ear's sound
    if (CloserEar == SideEar)
    {
        WaveToMod->Pitch = BasePitch;
        WaveToMod->SetVolumeMultiplier(Volume);
        WaveToMod->StartTime = PickUpTime;
    } else
    {
        WaveToMod->Pitch = BasePitch - PitchArray[FMath::FloorToInt(GetAzimuth())];
        WaveToMod->SetVolumeMultiplier(Volume - VolumeArray[FMath::FloorToInt(GetAzimuth())]);
        WaveToMod->StartTime = PickUpTime - DelayArray[FMath::FloorToInt(GetAzimuth())];
    }

    // Adds wave instance to the buffer
    if (SideEar == ECloserEar::LeftEar)
    MixerDevice->Get3DChannelMap(ChannelFormat, WaveToMod, 360 - GetAzimuth(), WaveToMod->GetUseSpatialization(), Buffer);
    else
    MixerDevice->Get3DChannelMap(ChannelFormat, WaveToMod, GetAzimuth(), WaveToMod->GetUseSpatialization(), Buffer);
}