123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /*******************************************************************************
- The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
- Technology released in source code form as part of the game integration package.
- The content of this file may not be used without valid licenses to the
- AUDIOKINETIC Wwise Technology.
- Note that the use of the game engine is subject to the Unreal(R) Engine End User
- License Agreement at https://www.unrealengine.com/en-US/eula/unreal
-
- License Usage
-
- Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
- this file in accordance with the end user license agreement provided with the
- software or, alternatively, in accordance with the terms contained
- in a written agreement between you and Audiokinetic Inc.
- Copyright (c) 2023 Audiokinetic Inc.
- *******************************************************************************/
- #include "WwiseEventTracking.h"
- #include "AkAudioEvent.h"
- #include "Wwise/WwiseExternalSourceManager.h"
- void FWwiseEventTracker::PostEventCallbackHandler(AkCallbackType in_eType, AkCallbackInfo * in_pCallbackInfo)
- {
- if (in_pCallbackInfo == nullptr)
- return;
- auto Tracker = (FWwiseEventTracker*)in_pCallbackInfo->pCookie;
- if (Tracker == nullptr)
- return;
- /* Event end */
- if (in_eType == AkCallbackType::AK_EndOfEvent)
- {
- const auto CBInfo = (AkEventCallbackInfo*)in_pCallbackInfo;
- const auto IDToStop = CBInfo->playingID;
- Tracker->RemovePlayingID(IDToStop);
- Tracker->RemoveScheduledStop(IDToStop);
- }/* Received close to the beginning of the event */
- else if (in_eType == AkCallbackType::AK_Duration)
- {
- const auto CBInfo = (AkDurationCallbackInfo*)in_pCallbackInfo;
- Tracker->CurrentDurationEstimation = (CBInfo->fEstimatedDuration * Tracker->CurrentDurationProportionRemaining) / 1000.0f;
- }
- }
- void FWwiseEventTracker::RemoveScheduledStop(AkPlayingID InID)
- {
- FScopeLock autoLock(&ScheduledStopsLock);
- for (auto PlayingID : ScheduledStops)
- {
- if (PlayingID == InID)
- {
- ScheduledStops.Remove(PlayingID);
- break;
- }
- }
- }
- void FWwiseEventTracker::RemovePlayingID(AkPlayingID InID)
- {
- FScopeLock autoLock(&PlayingIDsLock);
- for (auto PlayingID : PlayingIDs)
- {
- if (PlayingID == InID)
- {
- PlayingIDs.Remove(PlayingID);
- break;
- }
- }
- }
- void FWwiseEventTracker::TryAddPlayingID(const AkPlayingID & PlayingID)
- {
- if (PlayingID != AK_INVALID_PLAYING_ID)
- {
- FScopeLock autoLock(&PlayingIDsLock);
- PlayingIDs.Add(PlayingID);
- }
- }
- void FWwiseEventTracker::EmptyPlayingIDs()
- {
- FScopeLock autoLock(&PlayingIDsLock);
- PlayingIDs.Empty();
- }
- void FWwiseEventTracker::EmptyScheduledStops()
- {
- FScopeLock autoLock(&ScheduledStopsLock);
- ScheduledStops.Empty();
- }
- bool FWwiseEventTracker::PlayingIDHasScheduledStop(AkPlayingID InID)
- {
- FScopeLock autoLock(&ScheduledStopsLock);
- for (auto PlayingID : ScheduledStops)
- {
- if (PlayingID == InID)
- {
- return true;
- }
- }
- return false;
- }
- void FWwiseEventTracker::AddScheduledStop(AkPlayingID InID)
- {
- FScopeLock autoLock(&ScheduledStopsLock);
- ScheduledStops.Add(InID);
- }
- namespace WwiseEventTriggering
- {
- TArray<AkPlayingID, TInlineAllocator<16>> GetPlayingIds(FWwiseEventTracker & EventTracker)
- {
- FScopeLock autoLock(&EventTracker.PlayingIDsLock);
- return TArray<AkPlayingID, TInlineAllocator<16>> { EventTracker.PlayingIDs };
- }
- void LogDirtyPlaybackWarning()
- {
- UE_LOG(LogAkAudio, Warning, TEXT("Playback occurred from sequencer section with new changes. You may need to save your diry work units and re-generate your soundbanks."));
- }
- void StopAllPlayingIDs(FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker)
- {
- ensure(AudioDevice != nullptr);
- if (AudioDevice)
- {
- for (auto PlayingID : GetPlayingIds(EventTracker))
- {
- AudioDevice->StopPlayingID(PlayingID);
- }
- }
- }
- AkPlayingID PostEventOnDummyObject(FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker, float CurrentTime)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- UE_LOG(LogAkAudio, Warning, TEXT("Attempted to post an AkEvent from an empty Sequencer section."));
- return AK_INVALID_PLAYING_ID;
- }
- if (AudioDevice)
- {
- AkPlayingID PlayingID = AK_INVALID_PLAYING_ID;
- if (EventTracker.Event)
- {
- PlayingID = EventTracker.Event->PostAmbient(nullptr, &FWwiseEventTracker::PostEventCallbackHandler, &EventTracker,
- (AkCallbackType)(AK_EndOfEvent | AK_Duration), nullptr);
- }
- if (LIKELY(PlayingID != AK_INVALID_PLAYING_ID))
- {
- EventTracker.TryAddPlayingID(PlayingID);
- if (EventTracker.IsDirty)
- LogDirtyPlaybackWarning();
- return PlayingID;
- }
- }
- return AK_INVALID_PLAYING_ID;
- }
- AkPlayingID PostEvent(UObject * Object, FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker, float CurrentTime)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- UE_LOG(LogAkAudio, Warning, TEXT("Attempted to post an AkEvent from an empty Sequencer section."));
- return AK_INVALID_PLAYING_ID;
- }
- if (Object && AudioDevice)
- {
- auto AkComponent = Cast<UAkComponent>(Object);
- if (!IsValid(AkComponent))
- {
- auto Actor = CastChecked<AActor>(Object);
- if (IsValid(Actor))
- {
- AkComponent = AudioDevice->GetAkComponent(Actor->GetRootComponent(), FName(), NULL, EAttachLocation::KeepRelativeOffset);
- }
- }
- if (IsValid(AkComponent))
- {
- AkPlayingID PlayingID = AK_INVALID_PLAYING_ID;
- if (EventTracker.Event)
- {
- PlayingID = EventTracker.Event->PostOnComponent(AkComponent, nullptr, &FWwiseEventTracker::PostEventCallbackHandler, &EventTracker, (AkCallbackType)(AK_EndOfEvent | AK_Duration), nullptr, AkComponent->StopWhenOwnerDestroyed);
- }
- EventTracker.TryAddPlayingID(PlayingID);
- if (EventTracker.IsDirty)
- LogDirtyPlaybackWarning();
- return PlayingID;
- }
- }
- return AK_INVALID_PLAYING_ID;
- }
- void StopEvent(FAkAudioDevice * AudioDevice, AkPlayingID InPlayingID, FWwiseEventTracker * EventTracker)
- {
- ensure(AudioDevice != nullptr);
- if (AudioDevice)
- AudioDevice->StopPlayingID(InPlayingID);
- }
- void TriggerStopEvent(FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker, AkPlayingID PlayingID)
- {
- AudioDevice->StopPlayingID(PlayingID, (float)EventTracker.ScrubTailLengthMs, AkCurveInterpolation::AkCurveInterpolation_Log1);
- EventTracker.AddScheduledStop(PlayingID);
- }
- void ScheduleStopEventsForCurrentlyPlayingIDs(FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker)
- {
- ensure(AudioDevice != nullptr);
- if (AudioDevice)
- {
- for (auto PlayingID : GetPlayingIds(EventTracker))
- {
- if (!EventTracker.PlayingIDHasScheduledStop(PlayingID))
- {
- TriggerStopEvent(AudioDevice, EventTracker, PlayingID);
- }
- }
- }
- }
- void TriggerScrubSnippetOnDummyObject(FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- return;
- }
- if (AudioDevice)
- {
- AkPlayingID PlayingID = AK_INVALID_PLAYING_ID;
- if (EventTracker.Event)
- {
- PlayingID = EventTracker.Event->PostAmbient(nullptr, &FWwiseEventTracker::PostEventCallbackHandler, &EventTracker,
- (AkCallbackType)(AK_EndOfEvent | AK_Duration), nullptr);
- }
- if (LIKELY(PlayingID != AK_INVALID_PLAYING_ID))
- {
- EventTracker.TryAddPlayingID(PlayingID);
- if (EventTracker.IsDirty)
- LogDirtyPlaybackWarning();
- TriggerStopEvent(AudioDevice, EventTracker, PlayingID);
- }
- }
- }
- void TriggerScrubSnippet(UObject * Object, FAkAudioDevice * AudioDevice, FWwiseEventTracker & EventTracker)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- return;
- }
- if (Object && AudioDevice)
- {
- auto AkComponent = Cast<UAkComponent>(Object);
- if (!IsValid(AkComponent))
- {
- auto Actor = CastChecked<AActor>(Object);
- if (IsValid(Actor))
- {
- AkComponent = AudioDevice->GetAkComponent(Actor->GetRootComponent(), FName(), NULL, EAttachLocation::KeepRelativeOffset);
- }
- }
- if (IsValid(AkComponent))
- {
- AkPlayingID PlayingID = AK_INVALID_PLAYING_ID;
- if (EventTracker.Event)
- {
- PlayingID = EventTracker.Event->PostOnComponent(AkComponent, nullptr, &FWwiseEventTracker::PostEventCallbackHandler, &EventTracker,
- (AkCallbackType)(AK_EndOfEvent | AK_Duration), nullptr, AkComponent->StopWhenOwnerDestroyed);
- }
- if (LIKELY(PlayingID != AK_INVALID_PLAYING_ID))
- {
- EventTracker.TryAddPlayingID(PlayingID);
- if (EventTracker.IsDirty)
- LogDirtyPlaybackWarning();
- TriggerStopEvent(AudioDevice, EventTracker, PlayingID);
- }
- }
- }
- }
- void SeekOnEvent(UObject * Object, FAkAudioDevice * AudioDevice, AkReal32 in_fPercent, FWwiseEventTracker & EventTracker, AkPlayingID InPlayingID)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- return;
- }
- if (Object && AudioDevice)
- {
- auto AkComponent = Cast<UAkComponent>(Object);
- if (!IsValid(AkComponent))
- {
- auto Actor = CastChecked<AActor>(Object);
- if (IsValid(Actor))
- {
- AkComponent = AudioDevice->GetAkComponent(Actor->GetRootComponent(), FName(), NULL, EAttachLocation::KeepRelativeOffset);
- }
- }
- if (IsValid(AkComponent))
- {
- const AkUInt32 ShortID = AudioDevice->GetShortID(EventTracker.Event, EventTracker.EventName);
- AudioDevice->SeekOnEvent(ShortID, AkComponent, in_fPercent, false, InPlayingID);
- }
- }
- }
- void SeekOnEvent(UObject * Object, FAkAudioDevice * AudioDevice, AkReal32 in_fPercent, FWwiseEventTracker & EventTracker)
- {
- for (auto PlayingID : GetPlayingIds(EventTracker))
- {
- WwiseEventTriggering::SeekOnEvent(Object, AudioDevice, in_fPercent, EventTracker, PlayingID);
- }
- }
- void SeekOnEventWithDummyObject(FAkAudioDevice * AudioDevice, AkReal32 ProportionalTime, FWwiseEventTracker & EventTracker, AkPlayingID InPlayingID)
- {
- ensure(AudioDevice != nullptr);
- if (EventTracker.EventName.IsEmpty())
- {
- return;
- }
- if (AudioDevice)
- {
- if (ProportionalTime < 1.0f && ProportionalTime >= 0.0f)
- {
- AActor* DummyActor = nullptr;
- const AkUInt32 ShortID = AudioDevice->GetShortID(EventTracker.Event, EventTracker.EventName);
- AudioDevice->SeekOnEvent(ShortID, DummyActor, ProportionalTime, false, InPlayingID);
- // Update the duration proportion remaining property of the event tracker, rather than updating the current duration directly here.
- // This way, we ensure that the current duration is updated first by any PostEvent callback,
- // before it is then multiplied by the remaining proportion.
- EventTracker.CurrentDurationProportionRemaining = 1.0f - ProportionalTime;
- }
- }
- }
- void SeekOnEventWithDummyObject(FAkAudioDevice * AudioDevice, AkReal32 ProportionalTime, FWwiseEventTracker & EventTracker)
- {
- for (auto PlayingID : GetPlayingIds(EventTracker))
- {
- WwiseEventTriggering::SeekOnEventWithDummyObject(AudioDevice, ProportionalTime, EventTracker, PlayingID);
- }
- }
- }
|