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

77 lines
2.2 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
using System;
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.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;
2018-04-13 17:19:50 +08:00
private SampleChannel[] channels;
private AudioManager audio;
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
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
this.audio = audio;
}
public void Play() => channels?.ForEach(c => c.Play());
public override bool IsPresent => false; // We don't need to receive updates.
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 = loadChannel(s, skin.GetSample);
if (ch == null && allowFallback)
ch = loadChannel(s, audio.Samples.Get);
2018-04-13 17:19:50 +08:00
return ch;
}).Where(c => c != null).ToArray();
}
2019-06-30 20:58:30 +08:00
private SampleChannel loadChannel(ISampleInfo info, Func<string, SampleChannel> getSampleFunction)
2018-04-13 17:19:50 +08:00
{
foreach (var lookup in info.LookupNames)
{
var ch = getSampleFunction($"Gameplay/{lookup}");
if (ch == null)
continue;
2018-04-13 17:19:50 +08:00
ch.Volume.Value = info.Volume / 100.0;
return ch;
}
2018-04-13 17:19:50 +08:00
return null;
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);
foreach (var c in channels)
c.Dispose();
}
2018-04-13 17:19:50 +08:00
}
}