1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 10:17:32 +08:00
osu-lazer/osu.Game/Skinning/SkinnableSound.cs

180 lines
5.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
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;
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;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Audio;
using osu.Game.Screens.Play;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
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;
2020-02-14 21:14:00 +08:00
[Resolved]
private ISampleStore samples { get; set; }
2018-04-13 17:19:50 +08:00
private bool requestedPlaying;
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;
protected virtual bool PlayWhenPaused => false;
private readonly AudioContainer<DrawableSample> samplesContainer;
public SkinnableSound(ISampleInfo hitSamples)
: this(new[] { hitSamples })
2018-04-13 17:19:50 +08:00
{
2019-06-30 20:58:30 +08:00
}
public SkinnableSound(IEnumerable<ISampleInfo> hitSamples)
2019-06-30 20:58:30 +08:00
{
this.hitSamples = hitSamples.ToArray();
InternalChild = samplesContainer = new AudioContainer<DrawableSample>();
2018-04-13 17:19:50 +08:00
}
2020-09-29 11:45:20 +08:00
private readonly IBindable<bool> samplePlaybackDisabled = new Bindable<bool>();
[BackgroundDependencyLoader(true)]
2020-09-29 11:45:20 +08:00
private void load(ISamplePlaybackDisabler samplePlaybackDisabler)
{
// if in a gameplay context, pause sample playback when gameplay is paused.
2020-09-29 11:45:20 +08:00
if (samplePlaybackDisabler != null)
{
2020-09-29 11:45:20 +08:00
samplePlaybackDisabled.BindTo(samplePlaybackDisabler.SamplePlaybackDisabled);
samplePlaybackDisabled.BindValueChanged(disabled =>
{
2020-09-29 11:45:20 +08:00
if (requestedPlaying)
{
if (disabled.NewValue && !PlayWhenPaused)
2020-09-29 11:45:20 +08:00
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;
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
public void Play()
{
requestedPlaying = true;
play();
}
2019-09-02 17:18:59 +08:00
private void play()
{
if (samplePlaybackDisabled.Value && !PlayWhenPaused)
2020-09-29 11:45:20 +08:00
return;
samplesContainer.ForEach(c =>
{
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
c.Play();
});
}
public void Stop()
{
requestedPlaying = false;
stop();
}
private void stop()
{
samplesContainer.ForEach(c => c.Stop());
}
2018-04-13 17:19:50 +08:00
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
bool wasPlaying = IsPlaying;
var channels = hitSamples.Select(s =>
2018-04-13 17:19:50 +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
{
foreach (var lookup in s.LookupNames)
2019-11-11 19:53:22 +08:00
{
if ((ch = samples.Get($"Gameplay/{lookup}")) != null)
break;
2019-11-11 19:53:22 +08:00
}
}
2018-04-13 17:19:50 +08:00
if (ch != null)
{
ch.Looping = looping;
ch.Volume.Value = s.Volume / 100.0;
}
2018-04-13 17:19:50 +08:00
return ch;
}).Where(c => c != null);
2019-05-29 21:07:14 +08:00
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
// Start playback internally for the new samples if the previous ones were playing beforehand.
if (wasPlaying)
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;
public BindableNumber<double> Balance => samplesContainer.Balance;
public BindableNumber<double> Frequency => samplesContainer.Frequency;
public BindableNumber<double> Tempo => samplesContainer.Tempo;
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
=> samplesContainer.AddAdjustment(type, adjustBindable);
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable)
=> samplesContainer.RemoveAdjustment(type, adjustBindable);
2020-07-27 15:02:52 +08:00
public void RemoveAllAdjustments(AdjustableProperty type)
=> samplesContainer.RemoveAllAdjustments(type);
2020-07-27 15:02:52 +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
}
}