osu/osu.Game/Skinning/SkinnableSound.cs

179 lines
5.9 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 09:19:50 +00:00
2019-06-30 12:58:30 +00:00
using System.Collections.Generic;
2018-04-13 09:19:50 +00:00
using System.Linq;
using osu.Framework.Allocation;
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;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transforms;
2018-04-13 09:19:50 +00:00
using osu.Game.Audio;
using osu.Game.Screens.Play;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
public class SkinnableSound : SkinReloadableDrawable
{
2019-06-30 12:58:30 +00:00
private readonly ISampleInfo[] hitSamples;
2020-02-14 13:14:00 +00:00
[Resolved]
private ISampleStore samples { get; set; }
2018-04-13 09:19:50 +00:00
private bool requestedPlaying;
public override bool RemoveWhenNotAlive => false;
public override bool RemoveCompletedTransforms => false;
private readonly AudioContainer<DrawableSample> samplesContainer;
public SkinnableSound(ISampleInfo hitSamples)
: this(new[] { hitSamples })
2018-04-13 09:19:50 +00:00
{
2019-06-30 12:58:30 +00:00
}
public SkinnableSound(IEnumerable<ISampleInfo> hitSamples)
2019-06-30 12:58:30 +00:00
{
this.hitSamples = hitSamples.ToArray();
InternalChild = samplesContainer = new AudioContainer<DrawableSample>();
2018-04-13 09:19:50 +00:00
}
private Bindable<bool> gameplayClockPaused;
[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;
public bool Looping
{
get => looping;
set
{
if (value == looping) return;
looping = value;
samplesContainer.ForEach(c => c.Looping = looping);
}
}
2019-08-15 02:35:47 +00:00
public void Play()
{
requestedPlaying = true;
play();
}
2019-09-02 09:18:59 +00:00
private void play()
{
samplesContainer.ForEach(c =>
{
if (c.AggregateVolume.Value > 0)
c.Play();
});
}
public void Stop()
{
requestedPlaying = false;
stop();
}
private void stop()
{
samplesContainer.ForEach(c => c.Stop());
}
2018-04-13 09:19:50 +00:00
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
var channels = hitSamples.Select(s =>
2018-04-13 09:19:50 +00:00
{
var ch = skin.GetSample(s);
2018-04-13 09:19:50 +00:00
if (ch == null && allowFallback)
2019-11-11 11:53:22 +00:00
{
foreach (var lookup in s.LookupNames)
2019-11-11 11:53:22 +00:00
{
if ((ch = samples.Get($"Gameplay/{lookup}")) != null)
break;
2019-11-11 11:53:22 +00:00
}
}
2018-04-13 09:19:50 +00:00
if (ch != null)
{
ch.Looping = looping;
ch.Volume.Value = s.Volume / 100.0;
}
2018-04-13 09:19:50 +00:00
return ch;
}).Where(c => c != null);
2019-05-29 13:07:14 +00:00
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
if (requestedPlaying)
Play();
2019-05-29 13:07:14 +00:00
}
2020-07-27 07:02:52 +00: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 bool IsPlaying => samplesContainer.Any(s => s.Playing);
2020-07-27 07:02:52 +00:00
/// <summary>
/// Smoothly adjusts <see cref="Volume"/> over time.
/// </summary>
/// <returns>A <see cref="TransformSequence{T}"/> to which further transforms can be added.</returns>
public TransformSequence<DrawableAudioWrapper> VolumeTo(double newVolume, double duration = 0, Easing easing = Easing.None) =>
samplesContainer.VolumeTo(newVolume, duration, easing);
/// <summary>
/// Smoothly adjusts <see cref="Balance"/> over time.
/// </summary>
/// <returns>A <see cref="TransformSequence{T}"/> to which further transforms can be added.</returns>
public TransformSequence<DrawableAudioWrapper> BalanceTo(double newBalance, double duration = 0, Easing easing = Easing.None) =>
samplesContainer.BalanceTo(newBalance, duration, easing);
/// <summary>
/// Smoothly adjusts <see cref="Frequency"/> over time.
/// </summary>
/// <returns>A <see cref="TransformSequence{T}"/> to which further transforms can be added.</returns>
public TransformSequence<DrawableAudioWrapper> FrequencyTo(double newFrequency, double duration = 0, Easing easing = Easing.None) =>
samplesContainer.FrequencyTo(newFrequency, duration, easing);
/// <summary>
/// Smoothly adjusts <see cref="Tempo"/> over time.
/// </summary>
/// <returns>A <see cref="TransformSequence{T}"/> to which further transforms can be added.</returns>
public TransformSequence<DrawableAudioWrapper> TempoTo(double newTempo, double duration = 0, Easing easing = Easing.None) =>
samplesContainer.TempoTo(newTempo, duration, easing);
#endregion
2018-04-13 09:19:50 +00:00
}
}