osu/osu.Game/Skinning/SkinnableSound.cs

151 lines
4.5 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
2020-11-19 10:51:09 +00:00
using System;
2019-06-30 12:58:30 +00:00
using System.Collections.Generic;
2018-04-13 09:19:50 +00:00
using System.Linq;
2020-11-19 11:38:36 +00:00
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2020-08-06 12:53:20 +00:00
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2019-08-15 02:35:47 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.IEnumerableExtensions;
2020-11-19 10:51:09 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-04-13 09:19:50 +00:00
using osu.Game.Audio;
namespace osu.Game.Skinning
{
2020-11-19 10:51:09 +00:00
public class SkinnableSound : SkinReloadableDrawable, IAdjustableAudioComponent
{
public override bool RemoveWhenNotAlive => false;
public override bool RemoveCompletedTransforms => false;
/// <summary>
/// Whether to play the underlying sample when aggregate volume is zero.
/// Note that this is checked at the point of calling <see cref="Play"/>; changing the volume post-play will not begin playback.
/// Defaults to false unless <see cref="Looping"/>.
/// </summary>
/// <remarks>
/// Can serve as an optimisation if it is known ahead-of-time that this behaviour is allowed in a given use case.
/// </remarks>
protected bool PlayWhenZeroVolume => Looping;
2020-11-19 10:51:09 +00:00
protected readonly AudioContainer<PoolableSkinnableSample> SamplesContainer;
[Resolved]
private ISampleStore sampleStore { get; set; }
[Resolved(CanBeNull = true)]
private IPooledSampleProvider pooledProvider { get; set; }
2020-11-19 11:38:36 +00:00
public SkinnableSound()
2018-04-13 09:19:50 +00:00
{
2020-11-19 12:01:38 +00:00
InternalChild = SamplesContainer = new AudioContainer<PoolableSkinnableSample>();
2019-06-30 12:58:30 +00:00
}
2020-11-19 11:38:36 +00:00
public SkinnableSound([NotNull] IEnumerable<ISampleInfo> samples)
: this()
2019-06-30 12:58:30 +00:00
{
2020-11-19 10:51:09 +00:00
this.samples = samples.ToArray();
}
2020-11-19 11:38:36 +00:00
public SkinnableSound([NotNull] ISampleInfo sample)
: this(new[] { sample })
{
}
2020-11-19 10:51:09 +00:00
private ISampleInfo[] samples;
public ISampleInfo[] Samples
{
get => samples;
set
{
2020-11-19 11:38:36 +00:00
value ??= Array.Empty<ISampleInfo>();
2020-11-19 10:51:09 +00:00
if (samples == value)
return;
samples = value;
if (LoadState >= LoadState.Ready)
updateSamples();
}
2018-04-13 09:19:50 +00:00
}
private bool looping;
public bool Looping
{
get => looping;
set
{
if (value == looping) return;
looping = value;
SamplesContainer.ForEach(c => c.Looping = looping);
}
}
2019-08-15 02:35:47 +00:00
public virtual void Play()
{
SamplesContainer.ForEach(c =>
{
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
c.Play();
});
}
public virtual void Stop()
{
SamplesContainer.ForEach(c => c.Stop());
}
2020-11-19 10:51:09 +00:00
private void updateSamples()
{
bool wasPlaying = IsPlaying;
2018-04-13 09:19:50 +00:00
2020-11-19 10:51:09 +00:00
// Remove all pooled samples (return them to the pool), and dispose the rest.
SamplesContainer.RemoveAll(s => s.IsInPool);
SamplesContainer.Clear();
2019-05-29 13:07:14 +00:00
2020-11-19 10:51:09 +00:00
foreach (var s in samples)
2020-11-19 11:29:09 +00:00
{
var sample = pooledProvider?.GetPooledSample(s) ?? new PoolableSkinnableSample(s);
sample.Looping = Looping;
2020-11-19 12:01:38 +00:00
sample.Volume.Value = s.Volume / 100.0;
2020-11-19 11:29:09 +00:00
SamplesContainer.Add(sample);
}
if (wasPlaying)
Play();
2019-05-29 13:07:14 +00:00
}
2020-07-27 07:02:52 +00:00
#region Re-expose AudioContainer
public BindableNumber<double> Volume => SamplesContainer.Volume;
2020-07-27 07:02:52 +00:00
public BindableNumber<double> Balance => SamplesContainer.Balance;
2020-07-27 07:02:52 +00:00
public BindableNumber<double> Frequency => SamplesContainer.Frequency;
2020-07-27 07:02:52 +00:00
public BindableNumber<double> Tempo => SamplesContainer.Tempo;
2020-07-27 07:02:52 +00:00
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
=> SamplesContainer.AddAdjustment(type, adjustBindable);
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
=> SamplesContainer.RemoveAdjustment(type, adjustBindable);
2020-07-27 07:02:52 +00:00
public void RemoveAllAdjustments(AdjustableProperty type)
=> SamplesContainer.RemoveAllAdjustments(type);
2020-07-27 07:02:52 +00:00
public bool IsPlaying => SamplesContainer.Any(s => s.Playing);
2020-07-27 07:02:52 +00:00
#endregion
2018-04-13 09:19:50 +00:00
}
}