1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 15:13:22 +08:00
osu-lazer/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 17:19:50 +08:00
2020-11-19 18:51:09 +08:00
using System;
2019-06-30 20:58:30 +08:00
using System.Collections.Generic;
using System.Linq;
2020-11-19 19:38:36 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
2020-08-06 20:53:20 +08:00
using osu.Framework.Audio;
2019-08-15 10:35:47 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
2020-11-19 18:51:09 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
using osu.Game.Audio;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
2020-11-19 21:47:11 +08:00
/// <summary>
/// A sound consisting of one or more samples to be played.
/// </summary>
2020-11-19 18:51:09 +08: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;
/// <summary>
/// All raw <see cref="DrawableSamples"/>s contained in this <see cref="SkinnableSound"/>.
/// </summary>
[NotNull, ItemNotNull]
protected IEnumerable<DrawableSample> DrawableSamples => samplesContainer.Select(c => c.Sample).Where(s => s != null);
private readonly AudioContainer<PoolableSkinnableSample> samplesContainer;
2020-11-19 18:51:09 +08:00
[Resolved(CanBeNull = true)]
2020-11-30 17:40:22 +08:00
private IPooledSampleProvider samplePool { get; set; }
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/>.
/// </summary>
2020-11-19 19:38:36 +08:00
public SkinnableSound()
{
InternalChild = samplesContainer = new AudioContainer<PoolableSkinnableSample>();
2019-06-30 20:58:30 +08:00
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with some initial samples.
/// </summary>
/// <param name="samples">The initial samples.</param>
2020-11-19 19:38:36 +08:00
public SkinnableSound([NotNull] IEnumerable<ISampleInfo> samples)
: this()
2019-06-30 20:58:30 +08:00
{
2020-11-19 18:51:09 +08:00
this.samples = samples.ToArray();
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with an initial sample.
/// </summary>
/// <param name="sample">The initial sample.</param>
2020-11-19 19:38:36 +08:00
public SkinnableSound([NotNull] ISampleInfo sample)
: this(new[] { sample })
{
}
2020-11-19 21:42:44 +08:00
private ISampleInfo[] samples = Array.Empty<ISampleInfo>();
2020-11-19 18:51:09 +08:00
2020-11-19 21:47:11 +08:00
/// <summary>
/// The samples that should be played.
/// </summary>
2020-11-19 18:51:09 +08:00
public ISampleInfo[] Samples
{
get => samples;
set
{
2020-11-19 19:38:36 +08:00
value ??= Array.Empty<ISampleInfo>();
2020-11-19 18:51:09 +08:00
if (samples == value)
return;
samples = value;
if (LoadState >= LoadState.Ready)
updateSamples();
}
}
2018-04-13 17:19:50 +08:00
private bool looping;
2020-11-19 21:47:11 +08: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 10:35:47 +08:00
2020-11-19 21:47:11 +08:00
/// <summary>
/// Plays the samples.
/// </summary>
2021-01-19 16:11:40 +08:00
public virtual void Play()
{
samplesContainer.ForEach(c =>
{
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
2021-01-20 12:59:30 +08:00
{
c.Stop();
2021-01-19 16:11:40 +08:00
c.Play();
2021-01-20 12:59:30 +08:00
}
});
}
protected override void LoadAsyncComplete()
{
2021-03-20 09:51:58 +08:00
// ensure samples are constructed before SkinChanged() is called via base.LoadAsyncComplete().
if (!samplesContainer.Any())
updateSamples();
2021-03-20 09:51:58 +08:00
base.LoadAsyncComplete();
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Stops the samples.
/// </summary>
public virtual void Stop()
{
samplesContainer.ForEach(c => c.Stop());
}
2020-11-19 18:51:09 +08:00
private void updateSamples()
{
bool wasPlaying = IsPlaying;
2018-04-13 17:19:50 +08:00
2020-11-19 18:51:09 +08:00
// Remove all pooled samples (return them to the pool), and dispose the rest.
samplesContainer.RemoveAll(s => s.IsInPool);
samplesContainer.Clear();
2019-05-29 21:07:14 +08:00
2020-11-19 18:51:09 +08:00
foreach (var s in samples)
2020-11-19 19:29:09 +08:00
{
2020-11-30 17:40:22 +08:00
var sample = samplePool?.GetPooledSample(s) ?? new PoolableSkinnableSample(s);
2020-11-19 19:29:09 +08:00
sample.Looping = Looping;
2020-11-19 20:01:38 +08:00
sample.Volume.Value = s.Volume / 100.0;
2020-11-19 19:29:09 +08:00
samplesContainer.Add(sample);
2020-11-19 19:29:09 +08:00
}
2020-12-07 01:59:38 +08:00
if (wasPlaying && Looping)
Play();
2019-05-29 21:07:14 +08:00
}
2020-07-27 15:02:52 +08:00
#region Re-expose AudioContainer
public BindableNumber<double> Volume => samplesContainer.Volume;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Balance => samplesContainer.Balance;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Frequency => samplesContainer.Frequency;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Tempo => samplesContainer.Tempo;
2020-07-27 15:02:52 +08:00
public void BindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.BindAdjustments(component);
public void UnbindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.UnbindAdjustments(component);
2020-07-27 15:02:52 +08: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 15:02:52 +08:00
2020-11-19 21:47:11 +08:00
/// <summary>
2020-11-30 17:40:22 +08:00
/// Whether any samples are currently playing.
2020-11-19 21:47:11 +08:00
/// </summary>
public bool IsPlaying => samplesContainer.Any(s => s.Playing);
2020-07-27 15:02:52 +08:00
2021-01-19 16:11:40 +08: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 15:02:52 +08:00
#endregion
}
}