// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; 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.Game.Audio; namespace osu.Game.Skinning { public interface IPooledSampleProvider { [CanBeNull] PoolableSkinnableSample GetPooledSample(ISampleInfo sampleInfo); } public class PoolableSkinnableSample : SkinReloadableDrawable, IAggregateAudioAdjustment, IAdjustableAudioComponent { private ISampleInfo sampleInfo; private DrawableSample sample; [Resolved] private ISampleStore sampleStore { get; set; } [Cached] private readonly AudioAdjustments adjustments = new AudioAdjustments(); public PoolableSkinnableSample() { } public PoolableSkinnableSample(ISampleInfo sampleInfo) { Apply(sampleInfo); } public void Apply(ISampleInfo sampleInfo) { if (this.sampleInfo != null) throw new InvalidOperationException($"A {nameof(PoolableSkinnableSample)} cannot be applied multiple {nameof(ISampleInfo)}s."); this.sampleInfo = sampleInfo; if (LoadState >= LoadState.Ready) updateSample(); } protected override void SkinChanged(ISkinSource skin, bool allowFallback) { base.SkinChanged(skin, allowFallback); updateSample(); } private void updateSample() { ClearInternal(); var ch = CurrentSkin.GetSample(sampleInfo); if (ch == null && AllowDefaultFallback) { foreach (var lookup in sampleInfo.LookupNames) { if ((ch = sampleStore.Get(lookup)) != null) break; } } if (ch == null) return; AddInternal(sample = new DrawableSample(ch) { Looping = Looping, Volume = { Value = sampleInfo.Volume / 100.0 } }); } public void Play(bool restart = true) => sample?.Play(restart); public void Stop() => sample?.Stop(); public bool Playing => sample?.Playing ?? false; private bool looping; public bool Looping { get => looping; set { looping = value; if (sample != null) sample.Looping = value; } } /// /// The volume of this component. /// public BindableNumber Volume => adjustments.Volume; /// /// The playback balance of this sample (-1 .. 1 where 0 is centered) /// public BindableNumber Balance => adjustments.Balance; /// /// Rate at which the component is played back (affects pitch). 1 is 100% playback speed, or default frequency. /// public BindableNumber Frequency => adjustments.Frequency; /// /// Rate at which the component is played back (does not affect pitch). 1 is 100% playback speed. /// public BindableNumber Tempo => adjustments.Tempo; public void AddAdjustment(AdjustableProperty type, BindableNumber adjustBindable) => adjustments.AddAdjustment(type, adjustBindable); public void RemoveAdjustment(AdjustableProperty type, BindableNumber adjustBindable) => adjustments.RemoveAdjustment(type, adjustBindable); public void RemoveAllAdjustments(AdjustableProperty type) => adjustments.RemoveAllAdjustments(type); public IBindable AggregateVolume => adjustments.AggregateVolume; public IBindable AggregateBalance => adjustments.AggregateBalance; public IBindable AggregateFrequency => adjustments.AggregateFrequency; public IBindable AggregateTempo => adjustments.AggregateTempo; } public class SkinnableSound : SkinReloadableDrawable, IAdjustableAudioComponent { 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 PlayWhenZeroVolume => Looping; protected readonly AudioContainer SamplesContainer; [Resolved] private ISampleStore sampleStore { get; set; } [Resolved(CanBeNull = true)] private IPooledSampleProvider pooledProvider { get; set; } public SkinnableSound(ISampleInfo sample) : this(new[] { sample }) { } public SkinnableSound(IEnumerable samples) { this.samples = samples.ToArray(); InternalChild = SamplesContainer = new AudioContainer(); } private ISampleInfo[] samples; public ISampleInfo[] Samples { get => samples; set { if (value == null) throw new ArgumentNullException(nameof(value)); if (samples == value) return; samples = value; if (LoadState >= LoadState.Ready) updateSamples(); } } private bool looping; public bool Looping { get => looping; set { if (value == looping) return; looping = value; SamplesContainer.ForEach(c => c.Looping = looping); } } public virtual void Play() { SamplesContainer.ForEach(c => { if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0) c.Play(); }); } public virtual void Stop() { SamplesContainer.ForEach(c => c.Stop()); } protected override void SkinChanged(ISkinSource skin, bool allowFallback) { // Start playback internally for the new samples if the previous ones were playing beforehand. if (IsPlaying) Play(); } private void updateSamples() { bool wasPlaying = IsPlaying; // Remove all pooled samples (return them to the pool), and dispose the rest. SamplesContainer.RemoveAll(s => s.IsInPool); SamplesContainer.Clear(); foreach (var s in samples) SamplesContainer.Add(pooledProvider?.GetPooledSample(s) ?? new PoolableSkinnableSample(s)); if (wasPlaying) 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 void AddAdjustment(AdjustableProperty type, BindableNumber adjustBindable) => SamplesContainer.AddAdjustment(type, adjustBindable); public void RemoveAdjustment(AdjustableProperty type, BindableNumber adjustBindable) => SamplesContainer.RemoveAdjustment(type, adjustBindable); public void RemoveAllAdjustments(AdjustableProperty type) => SamplesContainer.RemoveAllAdjustments(type); public bool IsPlaying => SamplesContainer.Any(s => s.Playing); #endregion } }