1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:07:52 +08:00

feat(editor/checks): check for delayed hitsounds

I really just borrowed the implementation from MapsetVerifier
This commit is contained in:
tsrk 2023-08-24 21:05:40 +02:00
parent 71ec29041b
commit a885bf6ebf
No known key found for this signature in database
GPG Key ID: EBD46BB3049B56D6
7 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,97 @@
// 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.IO;
using System.Linq;
using ManagedBass;
using Moq;
using NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Models;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Checks;
using osu.Game.Rulesets.Objects;
using osu.Game.Tests.Beatmaps;
using osu.Game.Tests.Resources;
using osuTK.Audio;
namespace osu.Game.Tests.Editing.Checks
{
[TestFixture]
public class CheckDelayedHitsoundsTest
{
private CheckDelayedHitsounds check = null!;
private IBeatmap beatmap = null!;
[SetUp]
public void SetUp()
{
check = new CheckDelayedHitsounds();
beatmap = new Beatmap<HitObject>
{
BeatmapInfo = new BeatmapInfo
{
BeatmapSet = new BeatmapSetInfo
{
Files =
{
new RealmNamedFileUsage(new RealmFile { Hash = "abcdef" }, "normal-hitnormal.wav"),
}
}
}
};
if (!Bass.Init(0) && Bass.LastError != Errors.Already)
throw new AudioException("Could not initialize Bass.");
}
[Test]
public void TestNoDelayedHitsounds()
{
using var resourceStream = TestResources.OpenResource("Samples/hitsound-no-delay.wav");
Assert.IsEmpty(check.Run(getContext(resourceStream)));
}
[Test]
public void TestMinorDelayedHitsounds()
{
using (var resourceStream = TestResources.OpenResource("Samples/hitsound-minor-delay.wav"))
{
var issues = check.Run(getContext(resourceStream)).ToList();
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckDelayedHitsounds.IssuTemplateMinorDelay);
}
}
[Test]
public void TestDelayedHitsounds()
{
using var resourceStream = TestResources.OpenResource("Samples/hitsound-delay.wav");
var issues = check.Run(getContext(resourceStream)).ToList();
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckDelayedHitsounds.IssueTemplateDelay);
}
[Test]
public void TestConsequentlyDelayedHitsounds()
{
using var resourceStream = TestResources.OpenResource("Samples/hitsound-consequent-delay.wav");
var issues = check.Run(getContext(resourceStream)).ToList();
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckDelayedHitsounds.IssueTemplateConsequentDelay);
}
private BeatmapVerifierContext getContext(Stream? resourceStream)
{
var mockWorkingBeatmap = new Mock<TestWorkingBeatmap>(beatmap, null, null);
mockWorkingBeatmap.Setup(w => w.GetStream(It.IsAny<string>())).Returns(resourceStream);
return new BeatmapVerifierContext(beatmap, mockWorkingBeatmap.Object);
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -26,6 +26,7 @@ namespace osu.Game.Rulesets.Edit
new CheckFewHitsounds(),
new CheckTooShortAudioFiles(),
new CheckAudioInVideo(),
new CheckDelayedHitsounds(),
// Files
new CheckZeroByteFiles(),

View File

@ -0,0 +1,138 @@
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using osu.Framework.Audio.Track;
using osu.Game.Audio;
using osu.Game.Extensions;
using osu.Game.Rulesets.Edit.Checks.Components;
namespace osu.Game.Rulesets.Edit.Checks
{
public class CheckDelayedHitsounds : ICheck
{
/// <summary>
/// Threshold at which point the sample is considered silent.
/// </summary>
private const float silence_threshold = 0.001f;
private const float falloff_factor = 0.95f;
private const int delay_threshold = 5;
private const int delay_threshold_negligible = 1;
private readonly string[] audioExtensions = { "mp3", "ogg", "wav" };
private readonly string[] sampleBankPrefixes = { HitSampleInfo.BANK_NORMAL, HitSampleInfo.BANK_SOFT, HitSampleInfo.BANK_DRUM };
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Delayed hit sounds.");
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
{
new IssueTemplateConsequentDelay(this),
new IssueTemplateDelay(this),
new IssuTemplateMinorDelay(this)
};
private float getAverageAmplitude(Waveform.Point point) => (point.AmplitudeLeft + point.AmplitudeRight) / 2;
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
{
var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet;
if (beatmapSet == null)
yield break;
foreach (var file in beatmapSet.Files)
{
using (Stream stream = context.WorkingBeatmap.GetStream(file.File.GetStoragePath()))
{
if (stream == null)
continue;
if (!hasAudioExtension(file.Filename))
continue;
if (!isHitSound(file.Filename))
continue;
Waveform waveform = new Waveform(stream);
var points = waveform.GetPoints();
// Skip muted samples
if (points.Length == 0 || points.Sum(getAverageAmplitude) <= silence_threshold)
continue;
float maxAmplitude = points.Select(getAverageAmplitude).Max();
int consequentDelay = 0;
int delay = 0;
float amplitude = 0;
while (delay + consequentDelay < points.Length)
{
amplitude += getAverageAmplitude(points[delay]);
// Reached peak amplitude/transient
if (amplitude >= maxAmplitude)
break;
amplitude *= falloff_factor;
if (amplitude < silence_threshold)
{
amplitude = 0;
consequentDelay++;
}
delay++;
}
if (consequentDelay >= delay_threshold)
yield return new IssueTemplateConsequentDelay(this).Create(file.Filename, consequentDelay);
else if (consequentDelay + delay >= delay_threshold)
yield return new IssueTemplateDelay(this).Create(file.Filename, consequentDelay, delay);
else if (consequentDelay + delay >= delay_threshold_negligible)
yield return new IssuTemplateMinorDelay(this).Create(file.Filename, consequentDelay, delay);
}
}
}
private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLowerInvariant().EndsWith);
private bool isHitSound(string filename) => sampleBankPrefixes.Select(p => p + "-").Any(filename.ToLowerInvariant().StartsWith);
public class IssueTemplateConsequentDelay : IssueTemplate
{
public IssueTemplateConsequentDelay(ICheck check)
: base(check, IssueType.Error,
"\"{0}\" has a {1:0.##} ms period of complete silence at the start.")
{
}
public Issue Create(string filename, int pureDelay) => new Issue(this, filename, pureDelay);
}
public class IssueTemplateDelay : IssueTemplate
{
public IssueTemplateDelay(ICheck check)
: base(check, IssueType.Warning,
"\"{0}\" has a transient delay of ~{1:0.##} ms, of which {2:0.##} ms is complete silence.")
{
}
public Issue Create(string filename, int pureDelay, int delay) => new Issue(this, filename, delay, pureDelay);
}
public class IssuTemplateMinorDelay : IssueTemplate
{
public IssuTemplateMinorDelay(ICheck check)
: base(check, IssueType.Negligible,
"\"{0}\"has a transient delay of ~{1:0.##} ms, of which {2:0.##} ms is complete silence.")
{
}
public Issue Create(string filename, int pureDelay, int delay) => new Issue(this, filename, delay, pureDelay);
}
}
}