mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 20:05:29 +08:00
Merge pull request #27895 from 64ArthurAraujo/verify-check-incorrect-audio-formats
Add verify checks for incorrect audio formats
This commit is contained in:
commit
308744348f
128
osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs
Normal file
128
osu.Game.Tests/Editing/Checks/CheckHitsoundsFormatTest.cs
Normal file
@ -0,0 +1,128 @@
|
||||
// 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.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<HitObject>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNotAnAudioFile()
|
||||
{
|
||||
beatmap = new Beatmap<HitObject>
|
||||
{
|
||||
BeatmapInfo = new BeatmapInfo
|
||||
{
|
||||
BeatmapSet = new BeatmapSetInfo
|
||||
{
|
||||
Files = { CheckTestHelpers.CreateMockFile("png") }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using (var resourceStream = TestResources.OpenResource("Textures/test-image.png"))
|
||||
{
|
||||
var issues = check.Run(getContext(resourceStream)).ToList();
|
||||
Assert.That(issues, Has.Count.EqualTo(0));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCorruptAudio()
|
||||
{
|
||||
using (var resourceStream = TestResources.OpenResource("Samples/corrupt.wav"))
|
||||
{
|
||||
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<TestWorkingBeatmap>(beatmap, null, null);
|
||||
mockWorkingBeatmap.Setup(w => w.GetStream(It.IsAny<string>())).Returns(resourceStream);
|
||||
|
||||
return new BeatmapVerifierContext(beatmap, mockWorkingBeatmap.Object);
|
||||
}
|
||||
}
|
||||
}
|
112
osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs
Normal file
112
osu.Game.Tests/Editing/Checks/CheckSongFormatTest.cs
Normal file
@ -0,0 +1,112 @@
|
||||
// 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.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<HitObject>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCorruptAudio()
|
||||
{
|
||||
using (var resourceStream = TestResources.OpenResource("Samples/corrupt.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.IssueTemplateFormatUnsupported);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -95,18 +95,6 @@ namespace osu.Game.Tests.Editing.Checks
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCorruptAudioFile()
|
||||
{
|
||||
using (var resourceStream = TestResources.OpenResource("Samples/corrupt.wav"))
|
||||
{
|
||||
var issues = check.Run(getContext(resourceStream)).ToList();
|
||||
|
||||
Assert.That(issues, Has.Count.EqualTo(1));
|
||||
Assert.That(issues.Single().Template is CheckTooShortAudioFiles.IssueTemplateBadFormat);
|
||||
}
|
||||
}
|
||||
|
||||
private BeatmapVerifierContext getContext(Stream? resourceStream)
|
||||
{
|
||||
var mockWorkingBeatmap = new Mock<TestWorkingBeatmap>(beatmap, null, null);
|
||||
|
BIN
osu.Game.Tests/Resources/Samples/test-sample.ogg
Normal file
BIN
osu.Game.Tests/Resources/Samples/test-sample.ogg
Normal file
Binary file not shown.
BIN
osu.Game.Tests/Resources/Samples/test-sample.webm
Normal file
BIN
osu.Game.Tests/Resources/Samples/test-sample.webm
Normal file
Binary file not shown.
@ -28,6 +28,8 @@ namespace osu.Game.Rulesets.Edit
|
||||
new CheckTooShortAudioFiles(),
|
||||
new CheckAudioInVideo(),
|
||||
new CheckDelayedHitsounds(),
|
||||
new CheckSongFormat(),
|
||||
new CheckHitsoundsFormat(),
|
||||
|
||||
// Files
|
||||
new CheckZeroByteFiles(),
|
||||
|
86
osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs
Normal file
86
osu.Game/Rulesets/Edit/Checks/CheckHitsoundsFormat.cs
Normal file
@ -0,0 +1,86 @@
|
||||
// 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 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<IssueTemplate> PossibleTemplates => new IssueTemplate[]
|
||||
{
|
||||
new IssueTemplateFormatUnsupported(this),
|
||||
new IssueTemplateIncorrectFormat(this),
|
||||
};
|
||||
|
||||
public IEnumerable<Issue> 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 && ReferenceEquals(file.File, audioFile.File)) continue;
|
||||
|
||||
using (Stream data = context.WorkingBeatmap.GetStream(file.File.GetStoragePath()))
|
||||
{
|
||||
if (data == null)
|
||||
continue;
|
||||
|
||||
if (!AudioCheckUtils.HasAudioExtension(file.Filename) || !probablyHasAudioData(data))
|
||||
continue;
|
||||
|
||||
var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
|
||||
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode, fileCallbacks.Callbacks, fileCallbacks.Handle);
|
||||
|
||||
// If the format is not supported by BASS
|
||||
if (decodeStream == 0)
|
||||
{
|
||||
yield return new IssueTemplateFormatUnsupported(this).Create(file.Filename);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var audioInfo = Bass.ChannelGetInfo(decodeStream);
|
||||
|
||||
if ((audioInfo.ChannelType & ChannelType.Wave) == 0 && audioInfo.ChannelType != ChannelType.OGG)
|
||||
yield return new IssueTemplateIncorrectFormat(this).Create(file.Filename);
|
||||
|
||||
Bass.StreamFree(decodeStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool probablyHasAudioData(Stream data) => data.Length > 100;
|
||||
|
||||
public class IssueTemplateFormatUnsupported : IssueTemplate
|
||||
{
|
||||
public IssueTemplateFormatUnsupported(ICheck check)
|
||||
: base(check, IssueType.Problem, "\"{0}\" may be corrupt or using a unsupported audio 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. Use wav or ogg for hitsounds.")
|
||||
{
|
||||
}
|
||||
|
||||
public Issue Create(string file) => new Issue(this, file);
|
||||
}
|
||||
}
|
||||
}
|
83
osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs
Normal file
83
osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs
Normal file
@ -0,0 +1,83 @@
|
||||
// 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 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<IssueTemplate> PossibleTemplates => new IssueTemplate[]
|
||||
{
|
||||
new IssueTemplateFormatUnsupported(this),
|
||||
new IssueTemplateIncorrectFormat(this),
|
||||
};
|
||||
|
||||
private IEnumerable<ChannelType> allowedFormats => new[]
|
||||
{
|
||||
ChannelType.MP3,
|
||||
ChannelType.OGG,
|
||||
};
|
||||
|
||||
public IEnumerable<Issue> 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, 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);
|
||||
|
||||
Bass.StreamFree(decodeStream);
|
||||
}
|
||||
}
|
||||
|
||||
public class IssueTemplateFormatUnsupported : IssueTemplate
|
||||
{
|
||||
public IssueTemplateFormatUnsupported(ICheck check)
|
||||
: base(check, IssueType.Problem, "\"{0}\" may be corrupt or using a unsupported audio 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. Use mp3 or ogg for the song's audio.")
|
||||
{
|
||||
}
|
||||
|
||||
public Issue Create(string file) => new Issue(this, file);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,14 +13,12 @@ namespace osu.Game.Rulesets.Edit.Checks
|
||||
public class CheckTooShortAudioFiles : ICheck
|
||||
{
|
||||
private const int ms_threshold = 25;
|
||||
private const int min_bytes_threshold = 100;
|
||||
|
||||
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Too short audio files");
|
||||
|
||||
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
|
||||
{
|
||||
new IssueTemplateTooShort(this),
|
||||
new IssueTemplateBadFormat(this)
|
||||
};
|
||||
|
||||
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
|
||||
@ -39,15 +37,7 @@ namespace osu.Game.Rulesets.Edit.Checks
|
||||
var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
|
||||
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle);
|
||||
|
||||
if (decodeStream == 0)
|
||||
{
|
||||
// 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 (AudioCheckUtils.HasAudioExtension(file.Filename) && probablyHasAudioData(data))
|
||||
yield return new IssueTemplateBadFormat(this).Create(file.Filename);
|
||||
|
||||
continue;
|
||||
}
|
||||
if (decodeStream == 0) continue;
|
||||
|
||||
long length = Bass.ChannelGetLength(decodeStream);
|
||||
double ms = Bass.ChannelBytes2Seconds(decodeStream, length) * 1000;
|
||||
@ -60,8 +50,6 @@ namespace osu.Game.Rulesets.Edit.Checks
|
||||
}
|
||||
}
|
||||
|
||||
private bool probablyHasAudioData(Stream data) => data.Length > min_bytes_threshold;
|
||||
|
||||
public class IssueTemplateTooShort : IssueTemplate
|
||||
{
|
||||
public IssueTemplateTooShort(ICheck check)
|
||||
@ -71,15 +59,5 @@ namespace osu.Game.Rulesets.Edit.Checks
|
||||
|
||||
public Issue Create(string filename, double ms) => new Issue(this, filename, ms, ms_threshold);
|
||||
}
|
||||
|
||||
public class IssueTemplateBadFormat : IssueTemplate
|
||||
{
|
||||
public IssueTemplateBadFormat(ICheck check)
|
||||
: base(check, IssueType.Error, "Could not check whether \"{0}\" is too short (code \"{1}\").")
|
||||
{
|
||||
}
|
||||
|
||||
public Issue Create(string filename) => new Issue(this, filename, Bass.LastError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user