2019-01-24 17:43:03 +09: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 18:19:50 +09:00
|
|
|
|
|
2019-06-30 21:58:30 +09:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
2020-08-06 21:53:20 +09:00
|
|
|
|
using osu.Framework.Audio;
|
2019-09-04 20:28:21 +09:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2019-08-15 05:35:47 +03:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2020-05-20 20:49:01 +09:00
|
|
|
|
using osu.Framework.Graphics.Audio;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
using osu.Game.Audio;
|
2020-07-27 16:15:49 +09:00
|
|
|
|
using osu.Game.Screens.Play;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
|
{
|
2020-08-06 14:43:48 +09:00
|
|
|
|
public class SkinnableSound : SkinReloadableDrawable, IAdjustableAudioComponent
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
2019-06-30 21:58:30 +09:00
|
|
|
|
private readonly ISampleInfo[] hitSamples;
|
2019-08-29 15:32:21 +03:00
|
|
|
|
|
2020-02-14 20:14:00 +07:00
|
|
|
|
[Resolved]
|
|
|
|
|
private ISampleStore samples { get; set; }
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
private bool requestedPlaying;
|
|
|
|
|
|
2020-07-26 15:09:12 +02:00
|
|
|
|
public override bool RemoveWhenNotAlive => false;
|
|
|
|
|
public override bool RemoveCompletedTransforms => false;
|
|
|
|
|
|
2020-08-06 19:16:26 +09: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 23:23:02 +09: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 19:16:26 +09:00
|
|
|
|
/// </remarks>
|
2020-08-08 23:25:52 +09:00
|
|
|
|
protected bool PlayWhenZeroVolume => Looping;
|
2020-08-06 19:16:26 +09:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
private readonly AudioContainer<DrawableSample> samplesContainer;
|
|
|
|
|
|
2020-05-20 20:49:01 +09:00
|
|
|
|
public SkinnableSound(ISampleInfo hitSamples)
|
|
|
|
|
: this(new[] { hitSamples })
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
2019-06-30 21:58:30 +09:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 20:49:01 +09:00
|
|
|
|
public SkinnableSound(IEnumerable<ISampleInfo> hitSamples)
|
2019-06-30 21:58:30 +09:00
|
|
|
|
{
|
2020-05-20 20:49:01 +09:00
|
|
|
|
this.hitSamples = hitSamples.ToArray();
|
|
|
|
|
InternalChild = samplesContainer = new AudioContainer<DrawableSample>();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
private Bindable<bool> gameplayClockPaused;
|
2019-08-28 13:09:53 +03:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(GameplayClock gameplayClock)
|
|
|
|
|
{
|
|
|
|
|
// if in a gameplay context, pause sample playback when gameplay is paused.
|
|
|
|
|
gameplayClockPaused = gameplayClock?.IsPaused.GetBoundCopy();
|
|
|
|
|
gameplayClockPaused?.BindValueChanged(paused =>
|
|
|
|
|
{
|
|
|
|
|
if (requestedPlaying)
|
|
|
|
|
{
|
|
|
|
|
if (paused.NewValue)
|
|
|
|
|
stop();
|
|
|
|
|
// it's not easy to know if a sample has finished playing (to end).
|
|
|
|
|
// to keep things simple only resume playing looping samples.
|
|
|
|
|
else if (Looping)
|
|
|
|
|
play();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool looping;
|
2020-05-20 20:49:01 +09:00
|
|
|
|
|
2019-08-28 13:09:53 +03:00
|
|
|
|
public bool Looping
|
|
|
|
|
{
|
|
|
|
|
get => looping;
|
|
|
|
|
set
|
|
|
|
|
{
|
2019-09-02 19:01:17 +09:00
|
|
|
|
if (value == looping) return;
|
|
|
|
|
|
2019-08-28 13:09:53 +03:00
|
|
|
|
looping = value;
|
|
|
|
|
|
2020-05-20 20:49:01 +09:00
|
|
|
|
samplesContainer.ForEach(c => c.Looping = looping);
|
2019-08-28 13:09:53 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-15 05:35:47 +03:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
public void Play()
|
2019-08-29 15:32:21 +03:00
|
|
|
|
{
|
2020-07-27 16:15:49 +09:00
|
|
|
|
requestedPlaying = true;
|
|
|
|
|
play();
|
|
|
|
|
}
|
2019-09-02 18:18:59 +09:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
private void play()
|
|
|
|
|
{
|
|
|
|
|
samplesContainer.ForEach(c =>
|
|
|
|
|
{
|
2020-08-08 23:25:52 +09:00
|
|
|
|
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
|
2020-07-27 16:15:49 +09:00
|
|
|
|
c.Play();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
requestedPlaying = false;
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stop()
|
|
|
|
|
{
|
|
|
|
|
samplesContainer.ForEach(c => c.Stop());
|
|
|
|
|
}
|
2019-08-15 05:30:35 +03:00
|
|
|
|
|
2018-04-13 18:19:50 +09:00
|
|
|
|
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
|
|
|
|
{
|
2020-05-20 20:49:01 +09:00
|
|
|
|
var channels = hitSamples.Select(s =>
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
2019-08-23 14:55:38 +03:00
|
|
|
|
var ch = skin.GetSample(s);
|
|
|
|
|
|
2018-04-13 18:19:50 +09:00
|
|
|
|
if (ch == null && allowFallback)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-08-27 19:42:17 +03:00
|
|
|
|
foreach (var lookup in s.LookupNames)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-09-04 20:28:21 +09:00
|
|
|
|
if ((ch = samples.Get($"Gameplay/{lookup}")) != null)
|
2019-08-27 19:42:17 +03:00
|
|
|
|
break;
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-08-23 14:55:38 +03:00
|
|
|
|
if (ch != null)
|
2019-08-28 13:09:07 +03:00
|
|
|
|
{
|
|
|
|
|
ch.Looping = looping;
|
2019-08-23 14:55:38 +03:00
|
|
|
|
ch.Volume.Value = s.Volume / 100.0;
|
2019-08-28 13:09:07 +03:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-07-02 14:18:41 +09:00
|
|
|
|
return ch;
|
2020-05-20 20:49:01 +09:00
|
|
|
|
}).Where(c => c != null);
|
2019-05-29 22:07:14 +09:00
|
|
|
|
|
2020-05-20 20:49:01 +09:00
|
|
|
|
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
|
2020-07-22 16:37:53 +09:00
|
|
|
|
|
2020-07-27 16:15:49 +09:00
|
|
|
|
if (requestedPlaying)
|
2020-07-22 16:37:53 +09:00
|
|
|
|
Play();
|
2019-05-29 22:07:14 +09:00
|
|
|
|
}
|
2020-07-27 16:02:52 +09:00
|
|
|
|
|
|
|
|
|
#region Re-expose AudioContainer
|
|
|
|
|
|
|
|
|
|
public BindableNumber<double> Volume => samplesContainer.Volume;
|
|
|
|
|
|
|
|
|
|
public BindableNumber<double> Balance => samplesContainer.Balance;
|
|
|
|
|
|
|
|
|
|
public BindableNumber<double> Frequency => samplesContainer.Frequency;
|
|
|
|
|
|
|
|
|
|
public BindableNumber<double> Tempo => samplesContainer.Tempo;
|
|
|
|
|
|
2020-08-06 14:43:48 +09:00
|
|
|
|
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
|
|
|
|
=> samplesContainer.AddAdjustment(type, adjustBindable);
|
2020-07-28 15:10:37 +09:00
|
|
|
|
|
2020-08-06 14:43:48 +09:00
|
|
|
|
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
|
|
|
|
|
=> samplesContainer.RemoveAdjustment(type, adjustBindable);
|
2020-07-27 16:02:52 +09:00
|
|
|
|
|
2020-08-06 14:43:48 +09:00
|
|
|
|
public void RemoveAllAdjustments(AdjustableProperty type)
|
|
|
|
|
=> samplesContainer.RemoveAllAdjustments(type);
|
2020-07-27 16:02:52 +09:00
|
|
|
|
|
2020-08-06 14:43:48 +09:00
|
|
|
|
public bool IsPlaying => samplesContainer.Any(s => s.Playing);
|
2020-07-27 16:02:52 +09:00
|
|
|
|
|
|
|
|
|
#endregion
|
2018-04-13 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
}
|