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

Merge pull request #24648 from ItsShamed/editor/checks/delayed-hitsounds

Add check for delayed hitsounds
This commit is contained in:
Dean Herbert 2023-12-13 14:43:36 +09:00 committed by GitHub
commit 7ff6b483b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 303 additions and 5 deletions

View File

@ -0,0 +1,105 @@
// 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()
{
// 1 ms of silence -> 1 ms of noise at 0.3 amplitude -> hitsound transient
// => The transient is delayed by 2 ms
// Waveform: https://github.com/ppy/osu/assets/39100084/d5b9edbe-0ba2-401d-94b0-6d57228bdbd3
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.IssueTemplateMinorDelay);
}
}
[Test]
public void TestDelayedHitsounds()
{
// 3 ms of silence -> 3 ms of noise at 0.3 amplitude -> hitsound transient
// => The transient is delayed by 6 ms
// Waveform: https://github.com/ppy/osu/assets/39100084/2509ff35-d908-414b-b7b9-583681348772
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()
{
// The hitsound is delayed by 10 ms
// Waveform: https://github.com/ppy/osu/assets/39100084/3a7ede0d-8523-4b99-a222-3624cd208267
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,181 @@
// 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;
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Delayed hit sounds.");
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
{
new IssueTemplateConsequentDelay(this),
new IssueTemplateDelay(this),
new IssueTemplateDelayNoSilence(this),
new IssueTemplateMinorDelay(this),
new IssueTemplateMinorDelayNoSilence(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 (!isHitSound(file.Filename))
continue;
using 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)
{
if (consequentDelay > 0)
yield return new IssueTemplateDelay(this).Create(file.Filename, consequentDelay, delay);
else
yield return new IssueTemplateDelayNoSilence(this).Create(file.Filename, delay);
}
else if (consequentDelay + delay >= delay_threshold_negligible)
{
if (consequentDelay > 0)
yield return new IssueTemplateMinorDelay(this).Create(file.Filename, consequentDelay, delay);
else
yield return new IssueTemplateMinorDelayNoSilence(this).Create(file.Filename, delay);
}
}
}
}
private bool isHitSound(string filename)
{
if (!AudioCheckUtils.HasAudioExtension(filename))
return false;
// <bank>-<sampleset>
string[] parts = filename.ToLowerInvariant().Split('-');
if (parts.Length != 2)
return false;
string bank = parts[0];
string sampleSet = parts[1];
return HitSampleInfo.AllBanks.Contains(bank)
&& HitSampleInfo.AllAdditions.Append(HitSampleInfo.HIT_NORMAL).Any(sampleSet.StartsWith);
}
public class IssueTemplateConsequentDelay : IssueTemplate
{
public IssueTemplateConsequentDelay(ICheck check)
: base(check, IssueType.Problem,
"\"{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 consequentDelay, int delay) => new Issue(this, filename, delay, consequentDelay);
}
public class IssueTemplateDelayNoSilence : IssueTemplate
{
public IssueTemplateDelayNoSilence(ICheck check)
: base(check, IssueType.Warning,
"\"{0}\" has a transient delay of ~{1:0.##} ms.")
{
}
public Issue Create(string filename, int delay) => new Issue(this, filename, delay);
}
public class IssueTemplateMinorDelay : IssueTemplate
{
public IssueTemplateMinorDelay(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 consequentDelay, int delay) => new Issue(this, filename, delay, consequentDelay);
}
public class IssueTemplateMinorDelayNoSilence : IssueTemplate
{
public IssueTemplateMinorDelayNoSilence(ICheck check)
: base(check, IssueType.Negligible,
"\"{0}\" has a transient delay of ~{1:0.##} ms.")
{
}
public Issue Create(string filename, int delay) => new Issue(this, filename, delay);
}
}
}

View File

@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ManagedBass;
using osu.Framework.Audio.Callbacks;
using osu.Game.Extensions;
@ -16,8 +15,6 @@ namespace osu.Game.Rulesets.Edit.Checks
private const int ms_threshold = 25;
private const int min_bytes_threshold = 100;
private readonly string[] audioExtensions = { "mp3", "ogg", "wav" };
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Too short audio files");
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
@ -46,7 +43,7 @@ namespace osu.Game.Rulesets.Edit.Checks
{
// If the file is not likely to be properly parsed by Bass, we don't produce Error issues about it.
// Image files and audio files devoid of audio data both fail, for example, but neither would be issues in this check.
if (hasAudioExtension(file.Filename) && probablyHasAudioData(data))
if (AudioCheckUtils.HasAudioExtension(file.Filename) && probablyHasAudioData(data))
yield return new IssueTemplateBadFormat(this).Create(file.Filename);
continue;
@ -63,7 +60,6 @@ namespace osu.Game.Rulesets.Edit.Checks
}
}
private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLowerInvariant().EndsWith);
private bool probablyHasAudioData(Stream data) => data.Length > min_bytes_threshold;
public class IssueTemplateTooShort : IssueTemplate

View File

@ -0,0 +1,15 @@
// 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;
namespace osu.Game.Rulesets.Edit.Checks.Components
{
public static class AudioCheckUtils
{
public static readonly string[] AUDIO_EXTENSIONS = { "mp3", "ogg", "wav" };
public static bool HasAudioExtension(string filename) => AUDIO_EXTENSIONS.Any(Path.GetExtension(filename).ToLowerInvariant().EndsWith);
}
}