2019-01-24 16:43:03 +08: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 17:19:50 +08:00
|
|
|
|
|
2019-06-30 20:58:30 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
2020-08-06 20:53:20 +08:00
|
|
|
|
using osu.Framework.Audio;
|
2019-09-04 19:28:21 +08:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2019-08-15 10:35:47 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2020-05-20 19:49:01 +08:00
|
|
|
|
using osu.Framework.Graphics.Audio;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Audio;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
|
{
|
2020-08-06 13:43:48 +08:00
|
|
|
|
public class SkinnableSound : SkinReloadableDrawable, IAdjustableAudioComponent
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-06-30 20:58:30 +08:00
|
|
|
|
private readonly ISampleInfo[] hitSamples;
|
2019-08-29 20:32:21 +08:00
|
|
|
|
|
2020-02-14 21:14:00 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private ISampleStore samples { get; set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-07-26 21:09:12 +08:00
|
|
|
|
public override bool RemoveWhenNotAlive => false;
|
|
|
|
|
public override bool RemoveCompletedTransforms => false;
|
|
|
|
|
|
2020-08-06 18:16:26 +08: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 22:23:02 +08: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 18:16:26 +08:00
|
|
|
|
/// </remarks>
|
2020-08-08 22:25:52 +08:00
|
|
|
|
protected bool PlayWhenZeroVolume => Looping;
|
2020-08-06 18:16:26 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
protected readonly AudioContainer<DrawableSample> SamplesContainer;
|
2020-07-27 15:15:49 +08:00
|
|
|
|
|
2020-05-20 19:49:01 +08:00
|
|
|
|
public SkinnableSound(ISampleInfo hitSamples)
|
|
|
|
|
: this(new[] { hitSamples })
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-06-30 20:58:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 19:49:01 +08:00
|
|
|
|
public SkinnableSound(IEnumerable<ISampleInfo> hitSamples)
|
2019-06-30 20:58:30 +08:00
|
|
|
|
{
|
2020-05-20 19:49:01 +08:00
|
|
|
|
this.hitSamples = hitSamples.ToArray();
|
2020-09-29 13:09:51 +08:00
|
|
|
|
InternalChild = SamplesContainer = new AudioContainer<DrawableSample>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 15:15:49 +08:00
|
|
|
|
private bool looping;
|
2020-05-20 19:49:01 +08:00
|
|
|
|
|
2019-08-28 18:09:53 +08:00
|
|
|
|
public bool Looping
|
|
|
|
|
{
|
|
|
|
|
get => looping;
|
|
|
|
|
set
|
|
|
|
|
{
|
2019-09-02 18:01:17 +08:00
|
|
|
|
if (value == looping) return;
|
|
|
|
|
|
2019-08-28 18:09:53 +08:00
|
|
|
|
looping = value;
|
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
SamplesContainer.ForEach(c => c.Looping = looping);
|
2019-08-28 18:09:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-15 10:35:47 +08:00
|
|
|
|
|
2020-09-30 14:45:14 +08:00
|
|
|
|
public virtual void Play()
|
2020-07-27 15:15:49 +08:00
|
|
|
|
{
|
2020-09-29 13:09:51 +08:00
|
|
|
|
SamplesContainer.ForEach(c =>
|
2020-07-27 15:15:49 +08:00
|
|
|
|
{
|
2020-08-08 22:25:52 +08:00
|
|
|
|
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
|
2020-07-27 15:15:49 +08:00
|
|
|
|
c.Play();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 14:45:14 +08:00
|
|
|
|
public virtual void Stop()
|
2020-07-27 15:15:49 +08:00
|
|
|
|
{
|
2020-09-29 13:09:51 +08:00
|
|
|
|
SamplesContainer.ForEach(c => c.Stop());
|
2020-07-27 15:15:49 +08:00
|
|
|
|
}
|
2019-08-15 10:30:35 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
|
|
|
|
{
|
2020-09-16 06:59:07 +08:00
|
|
|
|
bool wasPlaying = IsPlaying;
|
|
|
|
|
|
2020-05-20 19:49:01 +08:00
|
|
|
|
var channels = hitSamples.Select(s =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-08-23 19:55:38 +08:00
|
|
|
|
var ch = skin.GetSample(s);
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (ch == null && allowFallback)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-08-28 00:42:17 +08:00
|
|
|
|
foreach (var lookup in s.LookupNames)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-09-04 19:28:21 +08:00
|
|
|
|
if ((ch = samples.Get($"Gameplay/{lookup}")) != null)
|
2019-08-28 00:42:17 +08:00
|
|
|
|
break;
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-08-23 19:55:38 +08:00
|
|
|
|
if (ch != null)
|
2019-08-28 18:09:07 +08:00
|
|
|
|
{
|
|
|
|
|
ch.Looping = looping;
|
2019-08-23 19:55:38 +08:00
|
|
|
|
ch.Volume.Value = s.Volume / 100.0;
|
2019-08-28 18:09:07 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-07-02 13:18:41 +08:00
|
|
|
|
return ch;
|
2020-05-20 19:49:01 +08:00
|
|
|
|
}).Where(c => c != null);
|
2019-05-29 21:07:14 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
SamplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
|
2020-07-22 15:37:53 +08:00
|
|
|
|
|
2020-09-19 10:54:06 +08:00
|
|
|
|
// Start playback internally for the new samples if the previous ones were playing beforehand.
|
2020-09-16 06:59:07 +08:00
|
|
|
|
if (wasPlaying)
|
2020-09-30 14:45:14 +08:00
|
|
|
|
Play();
|
2019-05-29 21:07:14 +08:00
|
|
|
|
}
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
|
|
|
|
#region Re-expose AudioContainer
|
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
public BindableNumber<double> Volume => SamplesContainer.Volume;
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
public BindableNumber<double> Balance => SamplesContainer.Balance;
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
public BindableNumber<double> Frequency => SamplesContainer.Frequency;
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
public BindableNumber<double> Tempo => SamplesContainer.Tempo;
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-08-06 13:43:48 +08:00
|
|
|
|
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
2020-09-29 13:09:51 +08:00
|
|
|
|
=> SamplesContainer.AddAdjustment(type, adjustBindable);
|
2020-07-28 14:10:37 +08:00
|
|
|
|
|
2020-08-06 13:43:48 +08:00
|
|
|
|
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
2020-09-29 13:09:51 +08:00
|
|
|
|
=> SamplesContainer.RemoveAdjustment(type, adjustBindable);
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-08-06 13:43:48 +08:00
|
|
|
|
public void RemoveAllAdjustments(AdjustableProperty type)
|
2020-09-29 13:09:51 +08:00
|
|
|
|
=> SamplesContainer.RemoveAllAdjustments(type);
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
2020-09-29 13:09:51 +08:00
|
|
|
|
public bool IsPlaying => SamplesContainer.Any(s => s.Playing);
|
2020-07-27 15:02:52 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|