2020-11-19 10:52:34 +00: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 ;
2020-11-19 13:30:21 +00:00
using JetBrains.Annotations ;
2020-11-19 10:52:34 +00:00
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 12:01:38 +00:00
using osu.Framework.Graphics.Containers ;
2020-11-19 10:52:34 +00:00
using osu.Game.Audio ;
namespace osu.Game.Skinning
{
2020-11-19 13:30:21 +00:00
/// <summary>
/// A sample corresponding to an <see cref="ISampleInfo"/> that supports being pooled and responding to skin changes.
/// </summary>
2020-11-19 10:52:34 +00:00
public class PoolableSkinnableSample : SkinReloadableDrawable , IAggregateAudioAdjustment , IAdjustableAudioComponent
{
2020-11-19 13:30:21 +00:00
/// <summary>
/// The currently-loaded <see cref="DrawableSample"/>.
/// </summary>
[CanBeNull]
public DrawableSample Sample { get ; private set ; }
2020-11-19 12:01:38 +00:00
2020-11-19 13:30:21 +00:00
private readonly AudioContainer < DrawableSample > sampleContainer ;
2020-11-19 10:52:34 +00:00
private ISampleInfo sampleInfo ;
[Resolved]
private ISampleStore sampleStore { get ; set ; }
2020-11-19 13:30:21 +00:00
/// <summary>
/// Creates a new <see cref="PoolableSkinnableSample"/> with no applied <see cref="ISampleInfo"/>.
/// An <see cref="ISampleInfo"/> can be applied later via <see cref="Apply"/>.
/// </summary>
2020-11-19 10:52:34 +00:00
public PoolableSkinnableSample ( )
{
2020-11-19 12:01:38 +00:00
InternalChild = sampleContainer = new AudioContainer < DrawableSample > { RelativeSizeAxes = Axes . Both } ;
2020-11-19 10:52:34 +00:00
}
2020-11-19 13:30:21 +00:00
/// <summary>
/// Creates a new <see cref="PoolableSkinnableSample"/> with an applied <see cref="ISampleInfo"/>.
/// </summary>
/// <param name="sampleInfo">The <see cref="ISampleInfo"/> to attach.</param>
2020-11-19 10:52:34 +00:00
public PoolableSkinnableSample ( ISampleInfo sampleInfo )
2020-11-19 12:01:38 +00:00
: this ( )
2020-11-19 10:52:34 +00:00
{
Apply ( sampleInfo ) ;
}
2020-11-19 13:30:21 +00:00
/// <summary>
/// Applies an <see cref="ISampleInfo"/> that describes the sample to retrieve.
/// Only one <see cref="ISampleInfo"/> can ever be applied to a <see cref="PoolableSkinnableSample"/>.
/// </summary>
/// <param name="sampleInfo">The <see cref="ISampleInfo"/> to apply.</param>
/// <exception cref="InvalidOperationException">If an <see cref="ISampleInfo"/> has already been applied to this <see cref="PoolableSkinnableSample"/>.</exception>
2020-11-19 10:52:34 +00:00
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 12:01:38 +00:00
Volume . Value = sampleInfo . Volume / 100.0 ;
2020-11-19 10:52:34 +00: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 13:30:21 +00:00
if ( sampleInfo = = null )
return ;
bool wasPlaying = Playing ;
2020-11-19 12:01:38 +00:00
sampleContainer . Clear ( ) ;
2020-11-30 10:19:24 +00:00
Sample = null ;
2020-11-19 10:52:34 +00: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 13:30:21 +00:00
sampleContainer . Add ( Sample = new DrawableSample ( ch ) { Looping = Looping } ) ;
// Start playback internally for the new sample if the previous one was playing beforehand.
2020-12-06 17:59:38 +00:00
if ( wasPlaying & & Looping )
2020-11-19 13:30:21 +00:00
Play ( ) ;
2020-11-19 10:52:34 +00:00
}
2020-11-19 13:30:21 +00:00
/// <summary>
/// Plays the sample.
/// </summary>
/// <param name="restart">Whether to play the sample from the beginning.</param>
public void Play ( bool restart = true ) = > Sample ? . Play ( restart ) ;
2020-11-19 10:52:34 +00:00
2020-11-19 13:30:21 +00:00
/// <summary>
/// Stops the sample.
/// </summary>
public void Stop ( ) = > Sample ? . Stop ( ) ;
2020-11-19 10:52:34 +00:00
2020-11-19 13:30:21 +00:00
/// <summary>
/// Whether the sample is currently playing.
/// </summary>
public bool Playing = > Sample ? . Playing ? ? false ;
2020-11-19 10:52:34 +00:00
private bool looping ;
2020-11-19 13:30:21 +00:00
/// <summary>
2020-11-19 13:47:11 +00:00
/// Whether the sample should loop on completion.
2020-11-19 13:30:21 +00:00
/// </summary>
2020-11-19 10:52:34 +00:00
public bool Looping
{
get = > looping ;
set
{
looping = value ;
2020-11-19 13:30:21 +00:00
if ( Sample ! = null )
Sample . Looping = value ;
2020-11-19 10:52:34 +00:00
}
}
2020-11-30 09:40:22 +00:00
#region Re - expose AudioContainer
2020-11-19 12:01:38 +00:00
public BindableNumber < double > Volume = > sampleContainer . Volume ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public BindableNumber < double > Balance = > sampleContainer . Balance ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public BindableNumber < double > Frequency = > sampleContainer . Frequency ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public BindableNumber < double > Tempo = > sampleContainer . Tempo ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public void AddAdjustment ( AdjustableProperty type , BindableNumber < double > adjustBindable ) = > sampleContainer . AddAdjustment ( type , adjustBindable ) ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public void RemoveAdjustment ( AdjustableProperty type , BindableNumber < double > adjustBindable ) = > sampleContainer . RemoveAdjustment ( type , adjustBindable ) ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public void RemoveAllAdjustments ( AdjustableProperty type ) = > sampleContainer . RemoveAllAdjustments ( type ) ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public IBindable < double > AggregateVolume = > sampleContainer . AggregateVolume ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public IBindable < double > AggregateBalance = > sampleContainer . AggregateBalance ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public IBindable < double > AggregateFrequency = > sampleContainer . AggregateFrequency ;
2020-11-19 10:52:34 +00:00
2020-11-19 12:01:38 +00:00
public IBindable < double > AggregateTempo = > sampleContainer . AggregateTempo ;
2020-11-30 09:40:22 +00:00
#endregion
2020-11-19 10:52:34 +00:00
}
}