MovieSceneAkAudioEventSection.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #pragma once
  16. #include "MovieSceneSection.h"
  17. #include "AkInclude.h"
  18. #include "AkAudioEvent.h"
  19. #include "WwiseEventTracking.h"
  20. #include "Dom/JsonObject.h"
  21. #include "MovieSceneAkAudioEventSection.generated.h"
  22. class FAkAudioDevice;
  23. struct FWwiseEventTracker;
  24. enum class AkEventSectionState : uint8
  25. {
  26. EUninitialized = 0,
  27. EUnrecognized,
  28. EInitialized
  29. };
  30. /**
  31. * A single floating point section that triggers a Wwise event.
  32. */
  33. UCLASS(MinimalAPI)
  34. class UMovieSceneAkAudioEventSection : public UMovieSceneSection
  35. {
  36. GENERATED_BODY()
  37. FCriticalSection WAAPISection;
  38. /** The AkAudioEvent represented by this section */
  39. UPROPERTY(EditAnywhere, Category = "AkAudioEvent", meta = (NoResetToDefault))
  40. UAkAudioEvent* Event = nullptr;
  41. /* Indicates whether the Wwise event will be re-triggered when the end is reached. */
  42. UPROPERTY(EditAnywhere, Category = "AkAudioEvent")
  43. bool RetriggerEvent = false;
  44. /* The length, in ms, of scrub snippets */
  45. UPROPERTY(EditAnywhere, Category = "AkAudioEvent",
  46. meta = (ClampMin = "30", ClampMax = "500", UIMin = "30", UIMax = "500"))
  47. int ScrubTailLengthMs = FWwiseEventTracker::GetScrubTimeMs();
  48. /** Indicates whether the Wwise event should be stopped when the section stops in the Unreal Sequencer. */
  49. UPROPERTY(EditAnywhere, Category = "AkAudioEvent")
  50. bool StopAtSectionEnd = true;
  51. /** The name of the AkAudioEvent represented by this section */
  52. UPROPERTY(EditAnywhere, AdvancedDisplay, Category = "AkAudioEvent")
  53. FString EventName = "";
  54. /** The duration of the longest Wwise source that the Wwise event contains (taking trim into account). */
  55. UPROPERTY(VisibleAnywhere, Category = "AkAudioEvent")
  56. float MaxSourceDuration = -1.0f;
  57. /** The ID of the longest Wwise source that the Wwise event contains. */
  58. UPROPERTY()
  59. FString MaxDurationSourceID = "";
  60. public:
  61. /** Update the audio source info and register the waapi connection callbacks. */
  62. AKAUDIO_API void Initialize();
  63. /** Returns the UAkAudioEvent that this section triggers. */
  64. AKAUDIO_API UAkAudioEvent* GetEvent() const { return Event; }
  65. AKAUDIO_API FString GetEventName() const { return (Event == nullptr) ? EventName : Event->GetName(); }
  66. AKAUDIO_API bool EventShouldStopAtSectionEnd() const;
  67. AKAUDIO_API int32 GetMaxEventDuration() const;
  68. AKAUDIO_API float GetStartTime() const;
  69. AKAUDIO_API float GetEndTime() const;
  70. /** Returns the minimum and maximum durations for the specified Event or EventName. This uses the generated XML data, not WAAPI. */
  71. AKAUDIO_API FFloatRange GetEventDuration() const;
  72. bool RequiresUpdate = false;
  73. AkEventSectionState InitState = AkEventSectionState::EUninitialized;
  74. /** A structure that tracks Wwise events as they are played, scrubbed, and stopped. */
  75. TSharedPtr<FWwiseEventTracker> EventTracker = MakeShareable(new FWwiseEventTracker());
  76. /** Indicates whether the Wwise event will be re-triggered when the end is reached. */
  77. bool ShouldRetriggerEvent() const { return RetriggerEvent; }
  78. /** Gets the section's Wwise event's GUID. */
  79. FGuid GetEventWwiseGUID() const;
  80. /** Gets the section's Wwise event's name. */
  81. FString GetEventWwiseName() const;
  82. virtual void BeginDestroy() override;
  83. #if WITH_EDITOR
  84. /* A collection of subscription IDs for WAAPI callbacks. */
  85. uint64 iTrimBeginSubscriptionID = 0;
  86. uint64 iTrimEndSubscriptionID = 0;
  87. uint64 iChildAddedSubscriptionID = 0;
  88. uint64 iChildRemovedSubscriptionID = 0;
  89. uint64 iChildAddedInitializeSubscriptionID = 0;
  90. TArray<uint64> EventActionSubscriptionIDs;
  91. FThreadSafeCounter iCallbackCounter = 0;
  92. /* An array of min-max magnitude pairs from the audio source data. */
  93. TArray<double> AudioSourcePeaks;
  94. /* We only keep track of the TrimBegin value (not TrimEnd), as the waveform length is calculated using the duration value. */
  95. float TrimBegin = 0.0f;
  96. /** Use WAAPI to update the MaxDurationSourceID and MaxSourceDuration. */
  97. AKAUDIO_API void UpdateAudioSourceInfo();
  98. /** Matches the duration of the Unreal Section to that of the Wwise Event. */
  99. AKAUDIO_API void MatchSectionLengthToEventLength();
  100. /** Get the audio peaks data (min max magnitude pairs) for the current MaxDurationSourceID, using WAAPI.
  101. * @param in_iNumPeaks - The number of peaks required.
  102. */
  103. AKAUDIO_API void UpdateAudioSourcePeaks(int in_iNumPeaks);
  104. /** Get the audio peaks data (min max magnitude pairs) for the current MaxDurationSourceID, using WAAPI.
  105. * @param in_iNumPeaks - The number of peaks required.
  106. * @param in_dTimeFrom - The start time of the time period for which peaks are required
  107. * @param in_dTimeTo - The end time of the time period for which peaks are required.
  108. */
  109. AKAUDIO_API void UpdateAudioSourcePeaks(int in_iNumPeaks, double in_dTimeFrom, double in_dTimeTo);
  110. /** Returns the number of min max magnitude pairs in the current peaks array. */
  111. AKAUDIO_API const int GetNumMinMaxPairs() const;
  112. /** Returns the current peaks array. */
  113. AKAUDIO_API const TArray<double>& GetAudioSourcePeaks() const;
  114. /** Returns the duration of the longest audio source that is used in the Wwise event that this section triggers. */
  115. AKAUDIO_API const float GetMaxSourceDuration() const;
  116. /** Resets the audio source information to an uninitialized state. */
  117. AKAUDIO_API void InvalidateAudioSourceInfo();
  118. /** Returns true if the audio source information is initialized and valid. */
  119. AKAUDIO_API bool AudioSourceInfoIsValid() const;
  120. #if !UE_4_26_OR_LATER
  121. /* UMovieSceneSection interface */
  122. AKAUDIO_API virtual FMovieSceneEvalTemplatePtr GenerateTemplate() const override;
  123. #endif
  124. /** Check if the workunit is dirty. If so, enable the soundbank generation notification in the event section. */
  125. AKAUDIO_API void CheckForWorkunitChanges(bool in_bNotifyTrack = false);
  126. /** Update the AK event info using the UAkAudioEvent Event member. This should be called when the event is changed. */
  127. AKAUDIO_API bool UpdateAkEventInfo();
  128. bool IsValid() const { return Event != nullptr || !EventName.IsEmpty(); }
  129. bool GetStopAtSectionEnd() const { return StopAtSectionEnd; }
  130. /** Returns the trim begin value for the Wwise event that this section triggers. */
  131. float GetTrimBegin() const { return TrimBegin; }
  132. /** Returns the duration of the scrub snippets that this section produces when scrubbed.
  133. * This can be changed using the ScrubTailLengthMs property.
  134. */
  135. int GetScrubTailLength() const { return ScrubTailLengthMs; }
  136. /** Returns true if this section is set to retrigger Wwise events after they finish. Change using the RetriggerEvent property. */
  137. bool DoesEventRetrigger() const { return RetriggerEvent; }
  138. virtual void PostEditChangeProperty(struct FPropertyChangedEvent& e) override;
  139. /** Associate a new AK audio event with this section. Also updates section time and audio source info. */
  140. bool SetEvent(UAkAudioEvent* AudioEvent, const FString& Name);
  141. /** Use WAAPI to get the peak data for the Wwise event, with the given arguments and options. This is called by GetAudioSourcePeaks(). */
  142. void WAAPIGetPeaks(const char* in_uri, TSharedRef<FJsonObject> in_getPeaksArgs,
  143. TSharedRef<FJsonObject> in_getPeaksOptions, TSharedPtr<FJsonObject> in_getPeaksResults);
  144. /** Update the trim data for the longest audio source used by the Wwise event that this section triggers. */
  145. void UpdateTrimData();
  146. /** Subscribe to TrimBegin and TrimEnd properties via WAAPI for the longest audio source that the Wwise event contains. */
  147. void SubscribeToTrimChanges();
  148. /** Subscribes to child added and child removed for each of the action targets in the Wwise event that this section triggers. */
  149. void SubscribeToEventChildren();
  150. /** Registers a call to update the audio source info when a child is added or removed from in_sParentID */
  151. void SubscribeToChildAddedRemoved(FString in_sParentID, uint64& in_iAddedSubID, uint64& in_iRemovedSubID);
  152. /** Subscribed to child added and child removed for the event ID. When a child is added or removed, UpdateAudioSourceInfo() is called. */
  153. void SubscribeToEventChildAddedRemoved();
  154. /** Unsubscribes a specific WAAPI subscription ID. */
  155. void UnsubscribeWAAPICallback(uint64& in_iSubID);
  156. /** Unsubscribes from all existing WAAPI subscriptions for this section. */
  157. void UnsubscribeAllWAAPICallbacks();
  158. /** Gets the containing work unit for the given object ID, and checks whether that workunit is dirty. */
  159. bool CheckWorkunitChangesForID(FGuid in_objectGUID);
  160. #endif //WITH_EDITOR
  161. };