From 9ef27104ce50c5844004659b3782d984f3c05c8f Mon Sep 17 00:00:00 2001 From: Arthur Araujo Date: Tue, 16 Apr 2024 06:15:21 -0300 Subject: [PATCH] Add checks for audio formats --- .../Checks/CheckHitsoundsFormatTest.cs | 96 +++++++++++++++++ .../Editing/Checks/CheckSongFormatTest.cs | 100 ++++++++++++++++++ osu.Game/Rulesets/Edit/BeatmapVerifier.cs | 2 + .../Edit/Checks/CheckHitsoundsFormat.cs | 93 ++++++++++++++++ .../Rulesets/Edit/Checks/CheckSongFormat.cs | 81 ++++++++++++++ 5 files changed, 372 insertions(+) create mode 100644 osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs create mode 100644 osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs create mode 100644 osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs create mode 100644 osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs diff --git a/osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs b/osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs new file mode 100644 index 0000000000..912a7468f5 --- /dev/null +++ b/osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs @@ -0,0 +1,96 @@ +// Copyright (c) ppy Pty Ltd . 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.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 CheckHitsoundsFormatTest + { + private CheckHitsoundsFormat check = null!; + + private IBeatmap beatmap = null!; + + [SetUp] + public void Setup() + { + check = new CheckHitsoundsFormat(); + beatmap = new Beatmap + { + BeatmapInfo = new BeatmapInfo + { + BeatmapSet = new BeatmapSetInfo + { + Files = { CheckTestHelpers.CreateMockFile("wav") } + } + } + }; + + // 0 = No output device. This still allows decoding. + if (!Bass.Init(0) && Bass.LastError != Errors.Already) + throw new AudioException("Could not initialize Bass."); + } + + [Test] + public void TestMP3Audio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample-cut.mp3")) + { + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(1)); + Assert.That(issues.Single().Template is CheckHitsoundsFormat.IssueTemplateIncorrectFormat); + } + } + + [Test] + public void TestOGGAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample.ogg")) + { + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(0)); + } + } + + [Test] + public void TestWAVAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/hitsound-delay.wav")) + { + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(0)); + } + } + + [Test] + public void TestWEBMAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample.webm")) + { + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(1)); + Assert.That(issues.Single().Template is CheckHitsoundsFormat.IssueTemplateFormatUnsupported); + } + } + + private BeatmapVerifierContext getContext(Stream? resourceStream) + { + var mockWorkingBeatmap = new Mock(beatmap, null, null); + mockWorkingBeatmap.Setup(w => w.GetStream(It.IsAny())).Returns(resourceStream); + + return new BeatmapVerifierContext(beatmap, mockWorkingBeatmap.Object); + } + } +} diff --git a/osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs b/osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs new file mode 100644 index 0000000000..acbf25ebad --- /dev/null +++ b/osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs @@ -0,0 +1,100 @@ +// Copyright (c) ppy Pty Ltd . 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.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 partial class CheckSongFormatTest + { + private CheckSongFormat check = null!; + + private IBeatmap beatmap = null!; + + [SetUp] + public void Setup() + { + check = new CheckSongFormat(); + beatmap = new Beatmap + { + BeatmapInfo = new BeatmapInfo + { + BeatmapSet = new BeatmapSetInfo + { + Files = { CheckTestHelpers.CreateMockFile("mp3") } + } + } + }; + + // 0 = No output device. This still allows decoding. + if (!Bass.Init(0) && Bass.LastError != Errors.Already) + throw new AudioException("Could not initialize Bass."); + } + + [Test] + public void TestMP3Audio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample-cut.mp3")) + { + beatmap.Metadata.AudioFile = "abc123.mp3"; + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(0)); + } + } + + [Test] + public void TestOGGAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample.ogg")) + { + beatmap.Metadata.AudioFile = "abc123.mp3"; + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(0)); + } + } + + [Test] + public void TestWAVAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/hitsound-delay.wav")) + { + beatmap.Metadata.AudioFile = "abc123.mp3"; + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(1)); + Assert.That(issues.Single().Template is CheckSongFormat.IssueTemplateIncorrectFormat); + } + } + + [Test] + public void TestWEBMAudio() + { + using (var resourceStream = TestResources.OpenResource("Samples/test-sample.webm")) + { + beatmap.Metadata.AudioFile = "abc123.mp3"; + var issues = check.Run(getContext(resourceStream)).ToList(); + Assert.That(issues, Has.Count.EqualTo(1)); + Assert.That(issues.Single().Template is CheckSongFormat.IssueTemplateFormatUnsupported); + } + } + + private BeatmapVerifierContext getContext(Stream? resourceStream) + { + var mockWorkingBeatmap = new Mock(beatmap, null, null); + mockWorkingBeatmap.Setup(w => w.GetStream(It.IsAny())).Returns(resourceStream); + + return new BeatmapVerifierContext(beatmap, mockWorkingBeatmap.Object); + } + } +} diff --git a/osu.Game/Rulesets/Edit/BeatmapVerifier.cs b/osu.Game/Rulesets/Edit/BeatmapVerifier.cs index 7d3c7d0b2f..a9681e13ba 100644 --- a/osu.Game/Rulesets/Edit/BeatmapVerifier.cs +++ b/osu.Game/Rulesets/Edit/BeatmapVerifier.cs @@ -28,6 +28,8 @@ namespace osu.Game.Rulesets.Edit new CheckTooShortAudioFiles(), new CheckAudioInVideo(), new CheckDelayedHitsounds(), + new CheckSongFormat(), + new CheckHitsoundsFormat(), // Files new CheckZeroByteFiles(), diff --git a/osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs b/osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs new file mode 100644 index 0000000000..e490a23963 --- /dev/null +++ b/osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs @@ -0,0 +1,93 @@ +// Copyright (c) ppy Pty Ltd . 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 ManagedBass; +using osu.Framework.Audio.Callbacks; +using osu.Game.Beatmaps; +using osu.Game.Extensions; +using osu.Game.Rulesets.Edit.Checks.Components; + +namespace osu.Game.Rulesets.Edit.Checks +{ + public class CheckHitsoundsFormat : ICheck + { + public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Checks for hitsound formats."); + + public IEnumerable PossibleTemplates => new IssueTemplate[] + { + new IssueTemplateFormatUnsupported(this), + new IssueTemplateIncorrectFormat(this), + }; + + private IEnumerable allowedFormats => new ChannelType[] + { + ChannelType.WavePCM, + ChannelType.WaveFloat, + ChannelType.OGG, + ChannelType.Wave | ChannelType.OGG, + }; + + public IEnumerable Run(BeatmapVerifierContext context) + { + var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet; + var audioFile = beatmapSet?.GetFile(context.Beatmap.Metadata.AudioFile); + + if (beatmapSet == null) yield break; + + foreach (var file in beatmapSet.Files) + { + if (audioFile != null && file.File == audioFile.File) continue; + + using (Stream data = context.WorkingBeatmap.GetStream(file.File.GetStoragePath())) + { + if (data == null) + continue; + + var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data)); + int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle); + + // If the format is not supported by BASS + if (decodeStream == 0) + { + if (AudioCheckUtils.HasAudioExtension(file.Filename) && probablyHasAudioData(data)) + yield return new IssueTemplateFormatUnsupported(this).Create(file.Filename); + + continue; + } + + var audioInfo = Bass.ChannelGetInfo(decodeStream); + + if (!allowedFormats.Contains(audioInfo.ChannelType)) + { + yield return new IssueTemplateIncorrectFormat(this).Create(file.Filename, audioInfo.ChannelType.ToString()); + } + } + } + } + + private bool probablyHasAudioData(Stream data) => data.Length > 100; + + public class IssueTemplateFormatUnsupported : IssueTemplate + { + public IssueTemplateFormatUnsupported(ICheck check) + : base(check, IssueType.Problem, "\"{0}\" is using a unsupported format; Use wav or ogg for hitsounds.") + { + } + + public Issue Create(string file) => new Issue(this, file); + } + + public class IssueTemplateIncorrectFormat : IssueTemplate + { + public IssueTemplateIncorrectFormat(ICheck check) + : base(check, IssueType.Problem, "\"{0}\" is using a incorrect format ({1}); Use wav or ogg for hitsounds.") + { + } + + public Issue Create(string file, string format) => new Issue(this, file, format); + } + } +} diff --git a/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs b/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs new file mode 100644 index 0000000000..4162bf20a3 --- /dev/null +++ b/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs @@ -0,0 +1,81 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using ManagedBass; +using osu.Framework.Audio.Callbacks; +using osu.Game.Beatmaps; +using osu.Game.Extensions; +using osu.Game.Rulesets.Edit.Checks.Components; + +namespace osu.Game.Rulesets.Edit.Checks +{ + public class CheckSongFormat : ICheck + { + public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Checks for song formats."); + + public IEnumerable PossibleTemplates => new IssueTemplate[] + { + new IssueTemplateFormatUnsupported(this), + new IssueTemplateIncorrectFormat(this), + }; + + private IEnumerable allowedFormats => new ChannelType[] + { + ChannelType.MP3, + ChannelType.OGG, + }; + + public IEnumerable Run(BeatmapVerifierContext context) + { + var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet; + var audioFile = beatmapSet?.GetFile(context.Beatmap.Metadata.AudioFile); + + if (beatmapSet == null) yield break; + if (audioFile == null) yield break; + + using (Stream data = context.WorkingBeatmap.GetStream(audioFile.File.GetStoragePath())) + { + if (data == null || data.Length <= 0) yield break; + + var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data)); + int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle); + + // If the format is not supported by BASS + if (decodeStream == 0) + { + yield return new IssueTemplateFormatUnsupported(this).Create(audioFile.Filename); + yield break; + } + + var audioInfo = Bass.ChannelGetInfo(decodeStream); + + if (!allowedFormats.Contains(audioInfo.ChannelType)) + yield return new IssueTemplateIncorrectFormat(this).Create(audioFile.Filename, audioInfo.ChannelType.ToString()); + } + } + + public class IssueTemplateFormatUnsupported : IssueTemplate + { + public IssueTemplateFormatUnsupported(ICheck check) + : base(check, IssueType.Problem, "\"{0}\" is using a unsupported format; Use mp3 or ogg for the song's audio.") + { + } + + public Issue Create(string file) => new Issue(this, file); + } + + public class IssueTemplateIncorrectFormat : IssueTemplate + { + public IssueTemplateIncorrectFormat(ICheck check) + : base(check, IssueType.Problem, "\"{0}\" is using a incorrect format ({1}); Use mp3 or ogg for the song's audio.") + { + } + + public Issue Create(string file, string format) => new Issue(this, file, format); + } + } +}