From 3bf133a24cca41ba8bb8839e72cf95c16a6e5975 Mon Sep 17 00:00:00 2001 From: Piero Dotti Date: Sat, 1 Jun 2024 19:05:44 +0200 Subject: [PATCH] Audio audio-volume and audio-pitch css properties (#109) * wip: added audio-volume and audio-pitch css properties * fix: feedback after testing in the editor * fix: more testing in the editor * fix(audio): compound form * dont store volume and pitch in state --------- Co-authored-by: Gokhan Kurt --- Editor/Renderer/EditorContext.cs | 2 +- Runtime/Core/ReactContext.cs | 2 +- Runtime/Frameworks/Noop/NoopContext.cs | 2 +- Runtime/Frameworks/UGUI/General/UGUIContext.cs | 5 +++-- .../UIToolkit/General/ReactRendererUIToolkit.cs | 5 +++-- .../UIToolkit/General/UIToolkitContext.cs | 8 ++++---- Runtime/Styling/NodeStyle.cs | 2 ++ Runtime/Styling/Properties/StyleProperties.cs | 6 ++++++ Runtime/Styling/Shorthands/AudioShorthand.cs | 15 +++++++++++++++ Runtime/Styling/StyleState.cs | 8 ++++++-- Tests/Runtime/Animations/AudioTests.cs | 10 +++++++++- 11 files changed, 51 insertions(+), 14 deletions(-) diff --git a/Editor/Renderer/EditorContext.cs b/Editor/Renderer/EditorContext.cs index 920ee2aa..e26573e1 100644 --- a/Editor/Renderer/EditorContext.cs +++ b/Editor/Renderer/EditorContext.cs @@ -99,7 +99,7 @@ protected override IReactComponent CreateComponentInternal(string tag, string te return res; } - public override void PlayAudio(AudioClip clip) + public override void PlayAudio(AudioClip clip, float volume, float pitch) { EditorSFX.PlayClip(clip); } diff --git a/Runtime/Core/ReactContext.cs b/Runtime/Core/ReactContext.cs index efd7e1e3..469a9f6a 100644 --- a/Runtime/Core/ReactContext.cs +++ b/Runtime/Core/ReactContext.cs @@ -168,7 +168,7 @@ public virtual ScriptSource CreateStaticScript(string path) return src; } - public abstract void PlayAudio(AudioClip clip); + public abstract void PlayAudio(AudioClip clip, float volume, float pitch); public void Start(Action afterStart = null) { diff --git a/Runtime/Frameworks/Noop/NoopContext.cs b/Runtime/Frameworks/Noop/NoopContext.cs index d623b87a..79d79e34 100644 --- a/Runtime/Frameworks/Noop/NoopContext.cs +++ b/Runtime/Frameworks/Noop/NoopContext.cs @@ -67,6 +67,6 @@ protected override IReactComponent CreatePseudoComponentInternal(string tag) return tc; } - public override void PlayAudio(AudioClip clip) { } + public override void PlayAudio(AudioClip clip, float volume, float pitch) { } } } diff --git a/Runtime/Frameworks/UGUI/General/UGUIContext.cs b/Runtime/Frameworks/UGUI/General/UGUIContext.cs index 4af9f443..ea982509 100644 --- a/Runtime/Frameworks/UGUI/General/UGUIContext.cs +++ b/Runtime/Frameworks/UGUI/General/UGUIContext.cs @@ -155,10 +155,11 @@ protected override IReactComponent CreatePseudoComponentInternal(string tag) return tc; } - public override void PlayAudio(AudioClip clip) + public override void PlayAudio(AudioClip clip, float volume, float pitch) { var source = (Host as HostComponent).GetOrAddComponent(); - source.PlayOneShot(clip); + source.pitch = pitch; + source.PlayOneShot(clip, volume); } public GameObject CreateNativeObject(string name, params Type[] components) diff --git a/Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs b/Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs index a845a779..b04deeed 100644 --- a/Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs +++ b/Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs @@ -40,10 +40,11 @@ protected override ReactContext CreateContext(ScriptSource script) return ctx; } - public void PlayAudio(AudioClip clip) + public void PlayAudio(AudioClip clip, float volume, float pitch) { var source = GetComponent(); - source.PlayOneShot(clip); + source.pitch = pitch; + source.PlayOneShot(clip, volume); } protected override IMediaProvider CreateMediaProvider() diff --git a/Runtime/Frameworks/UIToolkit/General/UIToolkitContext.cs b/Runtime/Frameworks/UIToolkit/General/UIToolkitContext.cs index 14a0966f..d9f5eb66 100644 --- a/Runtime/Frameworks/UIToolkit/General/UIToolkitContext.cs +++ b/Runtime/Frameworks/UIToolkit/General/UIToolkitContext.cs @@ -12,7 +12,7 @@ public class UIToolkitContext : ReactContext public new class Options : ReactContext.Options { public VisualElement HostElement; - public Action OnAudioPlayback; + public Action OnAudioPlayback; public override bool CalculatesLayout => false; } @@ -69,7 +69,7 @@ public static Dictionary OnAudioPlayback = null; + private Action OnAudioPlayback = null; public VisualElement HostElement { get; } @@ -115,9 +115,9 @@ protected override IReactComponent CreatePseudoComponentInternal(string tag) return tc; } - public override void PlayAudio(AudioClip clip) + public override void PlayAudio(AudioClip clip, float volume, float pitch) { - OnAudioPlayback?.Invoke(clip); + OnAudioPlayback?.Invoke(clip, volume, pitch); } } } diff --git a/Runtime/Styling/NodeStyle.cs b/Runtime/Styling/NodeStyle.cs index cc3e221c..5227c758 100644 --- a/Runtime/Styling/NodeStyle.cs +++ b/Runtime/Styling/NodeStyle.cs @@ -122,6 +122,8 @@ public class NodeStyle public ICssValueList audioClip => GetStyleValue(StyleProperties.audioClip); public ICssValueList audioIterationCount => GetStyleValue(StyleProperties.audioIterationCount); public ICssValueList audioDelay => GetStyleValue(StyleProperties.audioDelay); + public ICssValueList audioVolume => GetStyleValue(StyleProperties.audioVolume); + public ICssValueList audioPitch => GetStyleValue(StyleProperties.audioPitch); #endregion diff --git a/Runtime/Styling/Properties/StyleProperties.cs b/Runtime/Styling/Properties/StyleProperties.cs index f689c1db..3e6f41ea 100644 --- a/Runtime/Styling/Properties/StyleProperties.cs +++ b/Runtime/Styling/Properties/StyleProperties.cs @@ -113,6 +113,8 @@ public static class StyleProperties public static readonly ValueListStyleProperty audioClip = new ValueListStyleProperty("audioClip"); public static readonly ValueListStyleProperty audioIterationCount = new ValueListStyleProperty("audioIterationCount", 1); public static readonly ValueListStyleProperty audioDelay = new ValueListStyleProperty("audioDelay"); + public static readonly ValueListStyleProperty audioVolume = new ValueListStyleProperty("audioVolume", 1f, true, baseConverter: AllConverters.PercentageConverter); + public static readonly ValueListStyleProperty audioPitch = new ValueListStyleProperty("audioPitch", 1f); public static readonly Dictionary PropertyMap = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { @@ -233,10 +235,14 @@ public static class StyleProperties { "audioClip", audioClip }, { "audioDelay", audioDelay }, { "audioIterationCount", audioIterationCount }, + { "audioVolume", audioVolume }, + { "audioPitch", audioPitch }, { "audio-clip", audioClip }, { "audio-delay", audioDelay }, { "audio-iteration-count", audioIterationCount }, + { "audio-volume", audioVolume }, + { "audio-pitch", audioPitch }, { "border-image-source", borderImageSource }, { "border-image-slice", borderImageSlice }, diff --git a/Runtime/Styling/Shorthands/AudioShorthand.cs b/Runtime/Styling/Shorthands/AudioShorthand.cs index 2cceca2a..af3fc60d 100644 --- a/Runtime/Styling/Shorthands/AudioShorthand.cs +++ b/Runtime/Styling/Shorthands/AudioShorthand.cs @@ -22,6 +22,7 @@ protected override List ModifyInternal(IDictionary ModifyInternal(IDictionary ModifyInternal(IDictionary ModifyInternal(IDictionary { if (state.Loaded && state.ShouldStart) { state.ShouldStart = false; - Context.PlayAudio(state.Clip); + Context.PlayAudio(state.Clip, curVolume, curPitch); } }; diff --git a/Tests/Runtime/Animations/AudioTests.cs b/Tests/Runtime/Animations/AudioTests.cs index 098f434e..3430bc6b 100644 --- a/Tests/Runtime/Animations/AudioTests.cs +++ b/Tests/Runtime/Animations/AudioTests.cs @@ -14,7 +14,7 @@ public IEnumerator ParsingWorksCorrectly() { var view = Q("#test"); - view.Style.Set("audio", $"url({TestHelpers.ClickUrl}) 3s 5, url(https://example.com/file.ogg) infinite 2s, url(res:something)"); + view.Style.Set("audio", $"url({TestHelpers.ClickUrl}) 3s 5, url(https://example.com/file.ogg) infinite 2s, url(res:something), url(res:something) 50%"); yield return null; var st = view.ComputedStyle; @@ -35,6 +35,13 @@ public IEnumerator ParsingWorksCorrectly() Assert.AreEqual(1, st.audioIterationCount.Get(2)); + Assert.AreEqual(AssetReferenceType.Resource, st.audioClip.Get(3).Type); + Assert.AreEqual("something", st.audioClip.Get(3).Value); + Assert.AreEqual(0, st.audioDelay.Get(3)); + Assert.AreEqual(1, st.audioIterationCount.Get(3)); + Assert.AreEqual(0.5f, st.audioVolume.Get(3)); + + view.Style.Set("audio", "none"); yield return null; @@ -42,6 +49,7 @@ public IEnumerator ParsingWorksCorrectly() Assert.AreEqual(null, st.audioClip.Get(0)); Assert.AreEqual(0, st.audioDelay.Get(0)); Assert.AreEqual(1, st.audioIterationCount.Get(0, 1)); + Assert.AreEqual(1, st.audioVolume.Get(0, 1)); } }