1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 15:32:56 +08:00
osu-lazer/osu.Game/Skinning/SkinnableSound.cs

115 lines
3.3 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
2019-06-30 20:58:30 +08:00
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track;
2019-08-15 10:35:47 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Game.Audio;
namespace osu.Game.Skinning
{
public class SkinnableSound : SkinReloadableDrawable
{
2019-06-30 20:58:30 +08:00
private readonly ISampleInfo[] hitSamples;
2019-09-02 17:20:30 +08:00
private List<(AdjustableProperty property, BindableDouble bindable)> adjustments;
2018-04-13 17:19:50 +08:00
private SampleChannel[] channels;
2020-02-14 21:14:00 +08:00
[Resolved]
private ISampleStore samples { get; set; }
2018-04-13 17:19:50 +08:00
2019-06-30 20:58:30 +08:00
public SkinnableSound(IEnumerable<ISampleInfo> hitSamples)
2018-04-13 17:19:50 +08:00
{
2019-06-30 20:58:30 +08:00
this.hitSamples = hitSamples.ToArray();
}
public SkinnableSound(ISampleInfo hitSamples)
{
this.hitSamples = new[] { hitSamples };
2018-04-13 17:19:50 +08:00
}
private bool looping;
public bool Looping
{
get => looping;
set
{
if (value == looping) return;
looping = value;
2019-08-28 20:39:45 +08:00
channels?.ForEach(c => c.Looping = looping);
}
}
2019-08-15 10:35:47 +08:00
2018-04-13 17:19:50 +08:00
public void Play() => channels?.ForEach(c => c.Play());
2019-09-02 18:01:43 +08:00
2019-08-28 18:10:11 +08:00
public void Stop() => channels?.ForEach(c => c.Stop());
2018-04-13 17:19:50 +08:00
public void AddAdjustment(AdjustableProperty type, BindableDouble adjustBindable)
{
2019-09-02 17:18:59 +08:00
if (adjustments == null) adjustments = new List<(AdjustableProperty, BindableDouble)>();
adjustments.Add((type, adjustBindable));
channels?.ForEach(c => c.AddAdjustment(type, adjustBindable));
}
public void RemoveAdjustment(AdjustableProperty type, BindableDouble adjustBindable)
{
2019-09-02 17:18:59 +08:00
adjustments?.Remove((type, adjustBindable));
channels?.ForEach(c => c.RemoveAdjustment(type, adjustBindable));
}
public override bool IsPresent => Scheduler.HasPendingTasks;
2018-04-13 17:19:50 +08:00
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
2019-06-30 20:58:30 +08:00
channels = hitSamples.Select(s =>
2018-04-13 17:19:50 +08:00
{
var ch = skin.GetSample(s);
2018-04-13 17:19:50 +08:00
if (ch == null && allowFallback)
2019-11-11 19:53:22 +08:00
{
foreach (var lookup in s.LookupNames)
2019-11-11 19:53:22 +08:00
{
if ((ch = samples.Get($"Gameplay/{lookup}")) != null)
break;
2019-11-11 19:53:22 +08:00
}
}
2018-04-13 17:19:50 +08:00
if (ch != null)
{
ch.Looping = looping;
ch.Volume.Value = s.Volume / 100.0;
2019-09-02 17:18:59 +08:00
if (adjustments != null)
2019-11-11 19:53:22 +08:00
{
2019-11-12 18:19:48 +08:00
foreach (var (property, bindable) in adjustments)
ch.AddAdjustment(property, bindable);
2019-11-11 19:53:22 +08:00
}
}
2018-04-13 17:19:50 +08:00
return ch;
}).Where(c => c != null).ToArray();
2018-04-13 17:19:50 +08:00
}
2019-05-29 21:07:14 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (channels != null)
2019-11-11 19:53:22 +08:00
{
foreach (var c in channels)
c.Dispose();
2019-11-11 19:53:22 +08:00
}
2019-05-29 21:07:14 +08:00
}
2018-04-13 17:19:50 +08:00
}
}