osu/osu.Game/Skinning/SkinnableSound.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
6.7 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;
using System.Linq;
using osu.Framework.Allocation;
2020-08-06 12:53:20 +00:00
using osu.Framework.Audio;
2019-08-15 02:35:47 +00:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
2020-11-19 10:51:09 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
using osu.Game.Audio;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
2020-11-19 13:47:11 +00:00
/// <summary>
/// A sound consisting of one or more samples to be played.
/// </summary>
2020-11-19 10:51:09 +00:00
public partial 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;
/// <summary>
/// All raw <see cref="DrawableSamples"/>s contained in this <see cref="SkinnableSound"/>.
/// </summary>
protected IEnumerable<DrawableSample> DrawableSamples => samplesContainer.Select(c => c.Sample).Where(s => s != null);
private readonly AudioContainer<PoolableSkinnableSample> samplesContainer;
2020-11-19 10:51:09 +00:00
[Resolved]
private IPooledSampleProvider? samplePool { get; set; }
2020-11-19 13:47:11 +00:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/>.
/// </summary>
2020-11-19 11:38:36 +00:00
public SkinnableSound()
{
InternalChild = samplesContainer = new AudioContainer<PoolableSkinnableSample>();
2019-06-30 12:58:30 +00:00
}
2020-11-19 13:47:11 +00:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with some initial samples.
/// </summary>
/// <param name="samples">The initial samples.</param>
public SkinnableSound(IEnumerable<ISampleInfo> samples)
2020-11-19 11:38:36 +00:00
: this()
2019-06-30 12:58:30 +00:00
{
2020-11-19 10:51:09 +00:00
this.samples = samples.ToArray();
}
2020-11-19 13:47:11 +00:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with an initial sample.
/// </summary>
/// <param name="sample">The initial sample.</param>
public SkinnableSound(ISampleInfo sample)
2020-11-19 11:38:36 +00:00
: this(new[] { sample })
{
}
2020-11-19 13:42:44 +00:00
private ISampleInfo[] samples = Array.Empty<ISampleInfo>();
2020-11-19 10:51:09 +00:00
2020-11-19 13:47:11 +00:00
/// <summary>
/// The samples that should be played.
/// </summary>
public ISampleInfo[] Samples
2020-11-19 10:51:09 +00:00
{
get => samples;
set
{
if (samples == value)
return;
samples = value;
if (LoadState >= LoadState.Ready)
updateSamples();
}
}
2018-04-13 09:19:50 +00:00
public void ClearSamples() => Samples = Array.Empty<ISampleInfo>();
private bool looping;
2020-11-19 13:47:11 +00:00
/// <summary>
/// Whether the samples should loop on completion.
/// </summary>
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
2020-11-19 13:47:11 +00:00
/// <summary>
/// Plays the samples.
/// </summary>
2021-01-19 08:11:40 +00:00
public virtual void Play()
{
FlushPendingSkinChanges();
samplesContainer.ForEach(c =>
{
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
2021-01-20 04:59:30 +00:00
{
c.Stop();
2021-01-19 08:11:40 +00:00
c.Play();
2021-01-20 04:59:30 +00:00
}
});
}
protected override void LoadAsyncComplete()
{
2021-03-20 01:51:58 +00:00
// ensure samples are constructed before SkinChanged() is called via base.LoadAsyncComplete().
if (!samplesContainer.Any())
updateSamples();
2021-03-20 01:51:58 +00:00
base.LoadAsyncComplete();
}
2020-11-19 13:47:11 +00:00
/// <summary>
/// Stops the samples.
/// </summary>
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, false);
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
{
2020-11-30 09:40:22 +00:00
var sample = samplePool?.GetPooledSample(s) ?? new PoolableSkinnableSample(s);
2020-11-19 11:29:09 +00:00
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);
2020-11-19 11:29:09 +00:00
}
2020-12-06 17:59:38 +00:00
if (wasPlaying && Looping)
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 BindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.BindAdjustments(component);
public void UnbindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.UnbindAdjustments(component);
2020-07-27 07:02:52 +00:00
public void AddAdjustment(AdjustableProperty type, IBindable<double> adjustBindable) => samplesContainer.AddAdjustment(type, adjustBindable);
public void RemoveAdjustment(AdjustableProperty type, IBindable<double> adjustBindable) => samplesContainer.RemoveAdjustment(type, adjustBindable);
public void RemoveAllAdjustments(AdjustableProperty type) => samplesContainer.RemoveAllAdjustments(type);
2020-07-27 07:02:52 +00:00
2020-11-19 13:47:11 +00:00
/// <summary>
2020-11-30 09:40:22 +00:00
/// Whether any samples are currently playing.
2020-11-19 13:47:11 +00:00
/// </summary>
public bool IsPlaying => samplesContainer.Any(s => s.Playing);
2020-07-27 07:02:52 +00:00
2021-01-19 08:11:40 +00:00
public bool IsPlayed => samplesContainer.Any(s => s.Played);
public IBindable<double> AggregateVolume => samplesContainer.AggregateVolume;
public IBindable<double> AggregateBalance => samplesContainer.AggregateBalance;
public IBindable<double> AggregateFrequency => samplesContainer.AggregateFrequency;
public IBindable<double> AggregateTempo => samplesContainer.AggregateTempo;
2020-07-27 07:02:52 +00:00
#endregion
}
}