1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 16:12:57 +08:00
osu-lazer/osu.Game/Skinning/SkinnableSound.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

212 lines
7.0 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
2020-11-19 18:51:09 +08:00
using System;
2019-06-30 20:58:30 +08:00
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
2020-08-06 20:53:20 +08:00
using osu.Framework.Audio;
2019-08-15 10:35:47 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
2020-11-19 18:51:09 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
using osu.Game.Audio;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
2020-11-19 21:47:11 +08:00
/// <summary>
/// A sound consisting of one or more samples to be played.
/// </summary>
2020-11-19 18:51:09 +08:00
public partial class SkinnableSound : SkinReloadableDrawable, IAdjustableAudioComponent
{
/// <summary>
/// The minimum allowable volume for <see cref="Samples"/>.
/// <see cref="Samples"/> that specify a lower <see cref="ISampleInfo.Volume"/> will be forcibly pulled up to this volume.
/// </summary>
public int MinimumSampleVolume { get; set; }
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;
/// <summary>
/// All raw <see cref="DrawableSamples"/>s contained in this <see cref="SkinnableSound"/>.
/// </summary>
protected IEnumerable<DrawableSample> DrawableSamples => samplesContainer.Select(c => c.Sample).Where(s => s != null);
private readonly AudioContainer<PoolableSkinnableSample> samplesContainer;
2020-11-19 18:51:09 +08:00
[Resolved]
private IPooledSampleProvider? samplePool { get; set; }
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/>.
/// </summary>
2020-11-19 19:38:36 +08:00
public SkinnableSound()
{
InternalChild = samplesContainer = new AudioContainer<PoolableSkinnableSample>();
2019-06-30 20:58:30 +08:00
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with some initial samples.
/// </summary>
/// <param name="samples">The initial samples.</param>
public SkinnableSound(IEnumerable<ISampleInfo> samples)
2020-11-19 19:38:36 +08:00
: this()
2019-06-30 20:58:30 +08:00
{
2020-11-19 18:51:09 +08:00
this.samples = samples.ToArray();
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Creates a new <see cref="SkinnableSound"/> with an initial sample.
/// </summary>
/// <param name="sample">The initial sample.</param>
public SkinnableSound(ISampleInfo sample)
2020-11-19 19:38:36 +08:00
: this(new[] { sample })
{
}
2020-11-19 21:42:44 +08:00
private ISampleInfo[] samples = Array.Empty<ISampleInfo>();
2020-11-19 18:51:09 +08:00
2020-11-19 21:47:11 +08:00
/// <summary>
/// The samples that should be played.
/// </summary>
public ISampleInfo[] Samples
2020-11-19 18:51:09 +08:00
{
get => samples;
set
{
if (samples == value)
return;
samples = value;
if (LoadState >= LoadState.Ready)
updateSamples();
}
}
2018-04-13 17:19:50 +08:00
public void ClearSamples() => Samples = Array.Empty<ISampleInfo>();
private bool looping;
2020-11-19 21:47:11 +08:00
/// <summary>
/// Whether the samples should loop on completion.
/// </summary>
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
2020-11-19 21:47:11 +08:00
/// <summary>
/// Plays the samples.
/// </summary>
2021-01-19 16:11:40 +08:00
public virtual void Play()
{
FlushPendingSkinChanges();
samplesContainer.ForEach(c =>
{
if (PlayWhenZeroVolume || c.AggregateVolume.Value > 0)
2021-01-20 12:59:30 +08:00
{
c.Stop();
2021-01-19 16:11:40 +08:00
c.Play();
2021-01-20 12:59:30 +08:00
}
});
}
protected override void LoadAsyncComplete()
{
2021-03-20 09:51:58 +08:00
// ensure samples are constructed before SkinChanged() is called via base.LoadAsyncComplete().
if (!samplesContainer.Any())
updateSamples();
2021-03-20 09:51:58 +08:00
base.LoadAsyncComplete();
}
2020-11-19 21:47:11 +08:00
/// <summary>
/// Stops the samples.
/// </summary>
public virtual void Stop()
{
samplesContainer.ForEach(c => c.Stop());
}
2020-11-19 18:51:09 +08:00
private void updateSamples()
{
bool wasPlaying = IsPlaying;
2018-04-13 17:19:50 +08:00
2020-11-19 18:51:09 +08:00
// Remove all pooled samples (return them to the pool), and dispose the rest.
samplesContainer.RemoveAll(s => s.IsInPool, false);
samplesContainer.Clear();
2019-05-29 21:07:14 +08:00
2020-11-19 18:51:09 +08:00
foreach (var s in samples)
2020-11-19 19:29:09 +08:00
{
2020-11-30 17:40:22 +08:00
var sample = samplePool?.GetPooledSample(s) ?? new PoolableSkinnableSample(s);
2020-11-19 19:29:09 +08:00
sample.Looping = Looping;
sample.Volume.Value = Math.Max(s.Volume, MinimumSampleVolume) / 100.0;
2020-11-19 19:29:09 +08:00
samplesContainer.Add(sample);
2020-11-19 19:29:09 +08:00
}
2020-12-07 01:59:38 +08:00
if (wasPlaying && Looping)
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;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Balance => samplesContainer.Balance;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Frequency => samplesContainer.Frequency;
2020-07-27 15:02:52 +08:00
public BindableNumber<double> Tempo => samplesContainer.Tempo;
2020-07-27 15:02:52 +08:00
public void BindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.BindAdjustments(component);
public void UnbindAdjustments(IAggregateAudioAdjustment component) => samplesContainer.UnbindAdjustments(component);
2020-07-27 15:02:52 +08:00
public void AddAdjustment(AdjustableProperty type, IBindable<double> adjustBindable) => samplesContainer.AddAdjustment(type, adjustBindable);
public void RemoveAdjustment(AdjustableProperty type, IBindable<double> adjustBindable) => samplesContainer.RemoveAdjustment(type, adjustBindable);
public void RemoveAllAdjustments(AdjustableProperty type) => samplesContainer.RemoveAllAdjustments(type);
2020-07-27 15:02:52 +08:00
2020-11-19 21:47:11 +08:00
/// <summary>
2020-11-30 17:40:22 +08:00
/// Whether any samples are currently playing.
2020-11-19 21:47:11 +08:00
/// </summary>
public bool IsPlaying => samplesContainer.Any(s => s.Playing);
2020-07-27 15:02:52 +08:00
2021-01-19 16:11:40 +08:00
public bool IsPlayed => samplesContainer.Any(s => s.Played);
public IBindable<double> AggregateVolume => samplesContainer.AggregateVolume;
public IBindable<double> AggregateBalance => samplesContainer.AggregateBalance;
public IBindable<double> AggregateFrequency => samplesContainer.AggregateFrequency;
public IBindable<double> AggregateTempo => samplesContainer.AggregateTempo;
2020-07-27 15:02:52 +08:00
#endregion
}
}