2019-01-24 08:43:03 +00:00
|
|
|
|
// 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-02-23 11:34:08 +00:00
|
|
|
|
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;
|
2018-02-23 11:34:08 +00:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2020-11-19 10:51:09 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2020-11-30 10:26:25 +00:00
|
|
|
|
using osu.Framework.Graphics.Audio;
|
2020-05-20 11:49:01 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-02-23 11:34:08 +00:00
|
|
|
|
using osu.Game.Audio;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-23 11:34:08 +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
|
|
|
|
|
{
|
2020-07-26 13:09:12 +00:00
|
|
|
|
public override bool RemoveWhenNotAlive => false;
|
|
|
|
|
public override bool RemoveCompletedTransforms => false;
|
|
|
|
|
|
2020-08-06 10:16:26 +00:00
|
|
|
|
/// <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>
|
2020-08-08 14:23:02 +00:00
|
|
|
|
/// Can serve as an optimisation if it is known ahead-of-time that this behaviour is allowed in a given use case.
|
2020-08-06 10:16:26 +00:00
|
|
|
|
/// </remarks>
|
2020-08-08 14:25:52 +00:00
|
|
|
|
protected bool PlayWhenZeroVolume => Looping;
|
2020-08-06 10:16:26 +00:00
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
/// <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
|
|
|
|
|
2023-01-26 08:52:49 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IPooledSampleProvider? samplePool { get; set; }
|
2020-07-27 07:15:49 +00:00
|
|
|
|
|
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()
|
2018-02-23 11:34:08 +00:00
|
|
|
|
{
|
2020-11-30 10:26:25 +00:00
|
|
|
|
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>
|
2023-01-26 08:52:49 +00:00
|
|
|
|
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>
|
2023-01-26 08:52:49 +00:00
|
|
|
|
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>
|
2023-01-27 10:32:30 +00:00
|
|
|
|
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-02-23 11:34:08 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2023-01-27 10:32:30 +00:00
|
|
|
|
public void ClearSamples() => Samples = Array.Empty<ISampleInfo>();
|
|
|
|
|
|
2020-07-27 07:15:49 +00:00
|
|
|
|
private bool looping;
|
2020-05-20 11:49:01 +00:00
|
|
|
|
|
2020-11-19 13:47:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the samples should loop on completion.
|
|
|
|
|
/// </summary>
|
2019-08-28 10:09:53 +00:00
|
|
|
|
public bool Looping
|
|
|
|
|
{
|
|
|
|
|
get => looping;
|
|
|
|
|
set
|
|
|
|
|
{
|
2019-09-02 10:01:17 +00:00
|
|
|
|
if (value == looping) return;
|
|
|
|
|
|
2019-08-28 10:09:53 +00:00
|
|
|
|
looping = value;
|
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
samplesContainer.ForEach(c => c.Looping = looping);
|
2019-08-28 10:09:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
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()
|
2020-07-27 07:15:49 +00:00
|
|
|
|
{
|
2023-03-15 08:00:34 +00:00
|
|
|
|
FlushPendingSkinChanges();
|
2023-03-14 20:35:52 +00:00
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
samplesContainer.ForEach(c =>
|
2020-07-27 07:15:49 +00:00
|
|
|
|
{
|
2020-08-08 14:25:52 +00:00
|
|
|
|
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
|
|
|
|
}
|
2020-07-27 07:15:49 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 10:22:47 +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().
|
2021-03-19 10:22:47 +00:00
|
|
|
|
if (!samplesContainer.Any())
|
|
|
|
|
updateSamples();
|
2021-03-20 01:51:58 +00:00
|
|
|
|
|
|
|
|
|
base.LoadAsyncComplete();
|
2021-03-19 10:22:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 13:47:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the samples.
|
|
|
|
|
/// </summary>
|
2020-09-30 06:45:14 +00:00
|
|
|
|
public virtual void Stop()
|
2020-07-27 07:15:49 +00:00
|
|
|
|
{
|
2020-11-30 10:26:25 +00:00
|
|
|
|
samplesContainer.ForEach(c => c.Stop());
|
2020-07-27 07:15:49 +00:00
|
|
|
|
}
|
2019-08-15 02:30:35 +00:00
|
|
|
|
|
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.
|
2022-08-26 06:19:05 +00:00
|
|
|
|
samplesContainer.RemoveAll(s => s.IsInPool, false);
|
2020-11-30 10:26:25 +00:00
|
|
|
|
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
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
samplesContainer.Add(sample);
|
2020-11-19 11:29:09 +00:00
|
|
|
|
}
|
2020-07-22 07:37:53 +00:00
|
|
|
|
|
2020-12-06 17:59:38 +00:00
|
|
|
|
if (wasPlaying && Looping)
|
2020-09-30 06:45:14 +00:00
|
|
|
|
Play();
|
2019-05-29 13:07:14 +00:00
|
|
|
|
}
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
|
|
|
|
#region Re-expose AudioContainer
|
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
public BindableNumber<double> Volume => samplesContainer.Volume;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
public BindableNumber<double> Balance => samplesContainer.Balance;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
public BindableNumber<double> Frequency => samplesContainer.Frequency;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-11-30 10:26:25 +00:00
|
|
|
|
public BindableNumber<double> Tempo => samplesContainer.Tempo;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2021-02-18 09:52:34 +00:00
|
|
|
|
public void BindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.BindAdjustments(component);
|
2020-07-28 06:10:37 +00:00
|
|
|
|
|
2021-02-18 09:52:34 +00:00
|
|
|
|
public void UnbindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.UnbindAdjustments(component);
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2021-02-18 09:52:34 +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>
|
2020-11-30 10:26:25 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2021-02-22 05:18:52 +00:00
|
|
|
|
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
|
2018-02-23 11:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|