// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Bindables; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Audio; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Transforms; using osu.Game.Audio; using osu.Game.Screens.Play; namespace osu.Game.Skinning { public class SkinnableSound : SkinReloadableDrawable { private readonly ISampleInfo[] hitSamples; [Resolved] private ISampleStore samples { get; set; } private bool requestedPlaying; public override bool RemoveWhenNotAlive => false; public override bool RemoveCompletedTransforms => false; /// /// Whether to play the underlying sample when aggregate volume is zero. /// Note that this is checked at the point of calling ; changing the volume post-play will not begin playback. /// Defaults to false unless . /// /// /// Can serve as an optimisation if it is known ahead-of-time that this behaviour is allowed in a given use case. /// protected bool SkipPlayWhenZeroVolume => !Looping; private readonly AudioContainer samplesContainer; public SkinnableSound(ISampleInfo hitSamples) : this(new[] { hitSamples }) { } public SkinnableSound(IEnumerable hitSamples) { this.hitSamples = hitSamples.ToArray(); InternalChild = samplesContainer = new AudioContainer(); } private Bindable gameplayClockPaused; [BackgroundDependencyLoader(true)] private void load(GameplayClock gameplayClock) { // if in a gameplay context, pause sample playback when gameplay is paused. gameplayClockPaused = gameplayClock?.IsPaused.GetBoundCopy(); gameplayClockPaused?.BindValueChanged(paused => { if (requestedPlaying) { if (paused.NewValue) stop(); // it's not easy to know if a sample has finished playing (to end). // to keep things simple only resume playing looping samples. else if (Looping) play(); } }); } private bool looping; public bool Looping { get => looping; set { if (value == looping) return; looping = value; samplesContainer.ForEach(c => c.Looping = looping); } } public void Play() { requestedPlaying = true; play(); } private void play() { samplesContainer.ForEach(c => { if (!SkipPlayWhenZeroVolume || c.AggregateVolume.Value > 0) c.Play(); }); } public void Stop() { requestedPlaying = false; stop(); } private void stop() { samplesContainer.ForEach(c => c.Stop()); } protected override void SkinChanged(ISkinSource skin, bool allowFallback) { var channels = hitSamples.Select(s => { var ch = skin.GetSample(s); if (ch == null && allowFallback) { foreach (var lookup in s.LookupNames) { if ((ch = samples.Get($"Gameplay/{lookup}")) != null) break; } } if (ch != null) { ch.Looping = looping; ch.Volume.Value = s.Volume / 100.0; } return ch; }).Where(c => c != null); samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c)); if (requestedPlaying) Play(); } #region Re-expose AudioContainer public BindableNumber Volume => samplesContainer.Volume; public BindableNumber Balance => samplesContainer.Balance; public BindableNumber Frequency => samplesContainer.Frequency; public BindableNumber Tempo => samplesContainer.Tempo; public bool IsPlaying => samplesContainer.Any(s => s.Playing); /// /// Smoothly adjusts over time. /// /// A to which further transforms can be added. public TransformSequence> VolumeTo(double newVolume, double duration = 0, Easing easing = Easing.None) => samplesContainer.VolumeTo(newVolume, duration, easing); /// /// Smoothly adjusts over time. /// /// A to which further transforms can be added. public TransformSequence> BalanceTo(double newBalance, double duration = 0, Easing easing = Easing.None) => samplesContainer.BalanceTo(newBalance, duration, easing); /// /// Smoothly adjusts over time. /// /// A to which further transforms can be added. public TransformSequence> FrequencyTo(double newFrequency, double duration = 0, Easing easing = Easing.None) => samplesContainer.FrequencyTo(newFrequency, duration, easing); /// /// Smoothly adjusts over time. /// /// A to which further transforms can be added. public TransformSequence> TempoTo(double newTempo, double duration = 0, Easing easing = Easing.None) => samplesContainer.TempoTo(newTempo, duration, easing); #endregion } }