2021-08-24 17:23:02 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-08-24 17:23:02 +08:00
using System.Linq ;
using osu.Framework.Graphics.Containers ;
using osu.Game.Audio ;
using osu.Game.Rulesets.Objects ;
2023-02-16 16:34:38 +08:00
using osu.Game.Rulesets.Objects.Drawables ;
2021-08-24 17:23:02 +08:00
using osu.Game.Skinning ;
namespace osu.Game.Rulesets.UI
{
/// <summary>
/// A component which can trigger the most appropriate hit sound for a given point in time, based on the state of a <see cref="HitObjectContainer"/>
/// </summary>
public partial class GameplaySampleTriggerSource : CompositeDrawable
{
/// <summary>
/// The number of concurrent samples allowed to be played concurrently so that it feels better when spam-pressing a key.
/// </summary>
private const int max_concurrent_hitsounds = OsuGameBase . SAMPLE_CONCURRENCY ;
2021-08-25 13:57:41 +08:00
private readonly HitObjectContainer hitObjectContainer ;
private int nextHitSoundIndex ;
2021-08-24 17:23:02 +08:00
private readonly Container < SkinnableSound > hitSounds ;
public GameplaySampleTriggerSource ( HitObjectContainer hitObjectContainer )
{
this . hitObjectContainer = hitObjectContainer ;
2021-08-25 14:29:47 +08:00
InternalChild = hitSounds = new Container < SkinnableSound >
2021-08-24 17:23:02 +08:00
{
2021-08-25 14:29:47 +08:00
Name = "concurrent sample pool" ,
2021-08-26 16:15:36 +08:00
ChildrenEnumerable = Enumerable . Range ( 0 , max_concurrent_hitsounds ) . Select ( _ = > new PausableSkinnableSound ( ) )
2021-08-24 17:23:02 +08:00
} ;
}
2021-08-25 13:57:41 +08:00
private HitObjectLifetimeEntry fallbackObject ;
2021-08-24 17:23:02 +08:00
/// <summary>
/// Play the most appropriate hit sound for the current point in time.
/// </summary>
2021-08-25 14:24:01 +08:00
public virtual void Play ( )
{
var nextObject = GetMostValidObject ( ) ;
if ( nextObject = = null )
return ;
var samples = nextObject . Samples
. Select ( s = > nextObject . SampleControlPoint . ApplyTo ( s ) )
. Cast < ISampleInfo > ( )
. ToArray ( ) ;
PlaySamples ( samples ) ;
}
2022-06-16 15:04:52 +08:00
protected void PlaySamples ( ISampleInfo [ ] samples ) = > Schedule ( ( ) = >
2021-08-25 14:24:01 +08:00
{
var hitSound = getNextSample ( ) ;
hitSound . Samples = samples ;
hitSound . Play ( ) ;
2022-06-16 15:04:52 +08:00
} ) ;
2021-08-25 14:24:01 +08:00
protected HitObject GetMostValidObject ( )
2021-08-24 17:23:02 +08:00
{
2021-08-25 13:57:41 +08:00
// The most optimal lookup case we have is when an object is alive. There are usually very few alive objects so there's no drawbacks in attempting this lookup each time.
2023-02-16 16:34:38 +08:00
var drawableHitObject = hitObjectContainer . AliveObjects . FirstOrDefault ( h = > h . Result ? . HasResult ! = true ) ;
if ( drawableHitObject ! = null )
{
// A hit object may have a more valid nested object.
drawableHitObject = getMostValidNestedDrawable ( drawableHitObject ) ;
return drawableHitObject . HitObject ;
}
2021-08-24 18:05:59 +08:00
2021-08-25 13:57:41 +08:00
// In the case a next object isn't available in drawable form, we need to do a somewhat expensive traversal to get a valid sound to play.
2023-02-16 16:34:38 +08:00
// This lookup can be skipped if the last entry is still valid (in the future and not yet hit).
if ( fallbackObject = = null | | fallbackObject . Result ? . HasResult = = true )
2021-08-24 18:05:59 +08:00
{
2023-02-16 16:34:38 +08:00
// We need to use lifetime entries to find the next object (we can't just use `hitObjectContainer.Objects` due to pooling - it may even be empty).
// If required, we can make this lookup more efficient by adding support to get next-future-entry in LifetimeEntryManager.
fallbackObject = hitObjectContainer . Entries
. Where ( e = > e . Result ? . HasResult ! = true ) . MinBy ( e = > e . HitObject . StartTime ) ;
if ( fallbackObject ! = null )
2023-02-17 17:59:31 +08:00
return getEarliestNestedObject ( fallbackObject . HitObject ) ;
2023-02-16 16:34:38 +08:00
2023-02-17 17:59:31 +08:00
// In the case there are no non-judged objects, the last hit object should be used instead.
2023-02-16 16:34:38 +08:00
fallbackObject ? ? = hitObjectContainer . Entries . LastOrDefault ( ) ;
2021-08-24 18:05:59 +08:00
}
2021-08-24 17:23:02 +08:00
2023-02-16 16:34:38 +08:00
if ( fallbackObject = = null )
return null ;
bool fallbackHasResult = fallbackObject . Result ? . HasResult = = true ;
// If the fallback has been judged then we want the sample from the object itself.
if ( fallbackHasResult )
return fallbackObject . HitObject ;
// Else we want the earliest (including nested).
// In cases of nested objects, they will always have earlier sample data than their parent object.
return getEarliestNestedObject ( fallbackObject . HitObject ) ;
}
private DrawableHitObject getMostValidNestedDrawable ( DrawableHitObject o )
{
var nestedWithoutResult = o . NestedHitObjects . FirstOrDefault ( n = > n . Result ? . HasResult ! = true ) ;
if ( nestedWithoutResult = = null )
return o ;
return getMostValidNestedDrawable ( nestedWithoutResult ) ;
}
private HitObject getEarliestNestedObject ( HitObject hitObject )
{
var nested = hitObject . NestedHitObjects . FirstOrDefault ( ) ;
return nested ! = null ? getEarliestNestedObject ( nested ) : hitObject ;
2021-08-24 17:23:02 +08:00
}
private SkinnableSound getNextSample ( )
{
2021-08-25 14:29:47 +08:00
SkinnableSound hitSound = hitSounds [ nextHitSoundIndex ] ;
2021-08-24 17:23:02 +08:00
// round robin over available samples to allow for concurrent playback.
nextHitSoundIndex = ( nextHitSoundIndex + 1 ) % max_concurrent_hitsounds ;
return hitSound ;
}
}
}