1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00
osu-lazer/osu.Game/Skinning/PoolableSkinnableSample.cs

132 lines
4.3 KiB
C#
Raw Normal View History

2020-11-19 18:52:34 +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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
2020-11-19 20:01:38 +08:00
using osu.Framework.Graphics.Containers;
2020-11-19 18:52:34 +08:00
using osu.Game.Audio;
namespace osu.Game.Skinning
{
public class PoolableSkinnableSample : SkinReloadableDrawable, IAggregateAudioAdjustment, IAdjustableAudioComponent
{
2020-11-19 20:01:38 +08:00
private readonly AudioContainer<DrawableSample> sampleContainer;
2020-11-19 18:52:34 +08:00
private ISampleInfo sampleInfo;
private DrawableSample sample;
[Resolved]
private ISampleStore sampleStore { get; set; }
public PoolableSkinnableSample()
{
2020-11-19 20:01:38 +08:00
InternalChild = sampleContainer = new AudioContainer<DrawableSample> { RelativeSizeAxes = Axes.Both };
2020-11-19 18:52:34 +08:00
}
public PoolableSkinnableSample(ISampleInfo sampleInfo)
2020-11-19 20:01:38 +08:00
: this()
2020-11-19 18:52:34 +08:00
{
Apply(sampleInfo);
}
public void Apply(ISampleInfo sampleInfo)
{
if (this.sampleInfo != null)
throw new InvalidOperationException($"A {nameof(PoolableSkinnableSample)} cannot be applied multiple {nameof(ISampleInfo)}s.");
this.sampleInfo = sampleInfo;
2020-11-19 20:01:38 +08:00
Volume.Value = sampleInfo.Volume / 100.0;
2020-11-19 18:52:34 +08:00
if (LoadState >= LoadState.Ready)
updateSample();
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
base.SkinChanged(skin, allowFallback);
updateSample();
}
private void updateSample()
{
2020-11-19 20:01:38 +08:00
sampleContainer.Clear();
2020-11-19 18:52:34 +08:00
var ch = CurrentSkin.GetSample(sampleInfo);
if (ch == null && AllowDefaultFallback)
{
foreach (var lookup in sampleInfo.LookupNames)
{
if ((ch = sampleStore.Get(lookup)) != null)
break;
}
}
if (ch == null)
return;
2020-11-19 20:01:38 +08:00
sampleContainer.Add(sample = new DrawableSample(ch) { Looping = Looping });
2020-11-19 18:52:34 +08:00
}
public void Play(bool restart = true) => sample?.Play(restart);
public void Stop() => sample?.Stop();
public bool Playing => sample?.Playing ?? false;
private bool looping;
public bool Looping
{
get => looping;
set
{
looping = value;
if (sample != null)
sample.Looping = value;
}
}
/// <summary>
/// The volume of this component.
/// </summary>
2020-11-19 20:01:38 +08:00
public BindableNumber<double> Volume => sampleContainer.Volume;
2020-11-19 18:52:34 +08:00
/// <summary>
/// The playback balance of this sample (-1 .. 1 where 0 is centered)
/// </summary>
2020-11-19 20:01:38 +08:00
public BindableNumber<double> Balance => sampleContainer.Balance;
2020-11-19 18:52:34 +08:00
/// <summary>
/// Rate at which the component is played back (affects pitch). 1 is 100% playback speed, or default frequency.
/// </summary>
2020-11-19 20:01:38 +08:00
public BindableNumber<double> Frequency => sampleContainer.Frequency;
2020-11-19 18:52:34 +08:00
/// <summary>
/// Rate at which the component is played back (does not affect pitch). 1 is 100% playback speed.
/// </summary>
2020-11-19 20:01:38 +08:00
public BindableNumber<double> Tempo => sampleContainer.Tempo;
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => sampleContainer.AddAdjustment(type, adjustBindable);
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => sampleContainer.RemoveAdjustment(type, adjustBindable);
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public void RemoveAllAdjustments(AdjustableProperty type) => sampleContainer.RemoveAllAdjustments(type);
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public IBindable<double> AggregateVolume => sampleContainer.AggregateVolume;
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public IBindable<double> AggregateBalance => sampleContainer.AggregateBalance;
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public IBindable<double> AggregateFrequency => sampleContainer.AggregateFrequency;
2020-11-19 18:52:34 +08:00
2020-11-19 20:01:38 +08:00
public IBindable<double> AggregateTempo => sampleContainer.AggregateTempo;
2020-11-19 18:52:34 +08:00
}
}