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-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;
|
2019-09-04 11:28:21 +00:00
|
|
|
|
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;
|
2020-05-20 11:49:01 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
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-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-07-27 07:15:49 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 07:15:49 +00:00
|
|
|
|
private bool looping;
|
2020-05-20 11:49:01 +00:00
|
|
|
|
|
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-09-29 05:09:51 +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-09-30 06:45:14 +00:00
|
|
|
|
public virtual void Play()
|
2020-07-27 07:15:49 +00:00
|
|
|
|
{
|
2020-09-29 05:09:51 +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)
|
2020-07-27 07:15:49 +00:00
|
|
|
|
c.Play();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 06:45:14 +00:00
|
|
|
|
public virtual void Stop()
|
2020-07-27 07:15:49 +00:00
|
|
|
|
{
|
2020-09-29 05:09:51 +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.
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-07-22 07:37:53 +00:00
|
|
|
|
|
2020-09-15 22:59:07 +00:00
|
|
|
|
if (wasPlaying)
|
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-09-29 05:09:51 +00:00
|
|
|
|
public BindableNumber<double> Volume => SamplesContainer.Volume;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-09-29 05:09:51 +00:00
|
|
|
|
public BindableNumber<double> Balance => SamplesContainer.Balance;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-09-29 05:09:51 +00:00
|
|
|
|
public BindableNumber<double> Frequency => SamplesContainer.Frequency;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-09-29 05:09:51 +00:00
|
|
|
|
public BindableNumber<double> Tempo => SamplesContainer.Tempo;
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-08-06 05:43:48 +00:00
|
|
|
|
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
2020-09-29 05:09:51 +00:00
|
|
|
|
=> SamplesContainer.AddAdjustment(type, adjustBindable);
|
2020-07-28 06:10:37 +00:00
|
|
|
|
|
2020-08-06 05:43:48 +00:00
|
|
|
|
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
2020-09-29 05:09:51 +00:00
|
|
|
|
=> SamplesContainer.RemoveAdjustment(type, adjustBindable);
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-08-06 05:43:48 +00:00
|
|
|
|
public void RemoveAllAdjustments(AdjustableProperty type)
|
2020-09-29 05:09:51 +00:00
|
|
|
|
=> SamplesContainer.RemoveAllAdjustments(type);
|
2020-07-27 07:02:52 +00:00
|
|
|
|
|
2020-09-29 05:09:51 +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
|
|
|
|
}
|
|
|
|
|
}
|