2021-07-13 09:46:45 +08:00
|
|
|
// 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;
|
2021-11-19 15:07:55 +08:00
|
|
|
using osu.Game.Extensions;
|
2021-07-13 09:46:45 +08:00
|
|
|
using osu.Game.Rulesets.Edit.Checks.Components;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Edit.Checks
|
|
|
|
{
|
|
|
|
public class CheckTooShortAudioFiles : ICheck
|
|
|
|
{
|
|
|
|
private const int ms_threshold = 25;
|
|
|
|
|
|
|
|
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Too short audio files");
|
|
|
|
|
|
|
|
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
|
|
|
|
{
|
|
|
|
new IssueTemplateTooShort(this),
|
|
|
|
};
|
|
|
|
|
|
|
|
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
|
|
|
|
{
|
|
|
|
var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet;
|
|
|
|
|
2022-01-12 21:34:07 +08:00
|
|
|
if (beatmapSet != null)
|
2021-07-13 09:46:45 +08:00
|
|
|
{
|
2022-01-12 21:34:07 +08:00
|
|
|
foreach (var file in beatmapSet.Files)
|
2021-10-12 09:46:26 +08:00
|
|
|
{
|
2022-01-12 21:34:07 +08:00
|
|
|
using (Stream data = context.WorkingBeatmap.GetStream(file.File.GetStoragePath()))
|
|
|
|
{
|
|
|
|
if (data == null)
|
|
|
|
continue;
|
2021-07-13 09:46:45 +08:00
|
|
|
|
2022-01-12 21:34:07 +08:00
|
|
|
var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
|
|
|
|
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle);
|
2021-07-13 09:46:45 +08:00
|
|
|
|
2024-04-16 17:53:55 +08:00
|
|
|
if (decodeStream == 0) continue;
|
2021-07-13 09:46:45 +08:00
|
|
|
|
2022-01-12 21:34:07 +08:00
|
|
|
long length = Bass.ChannelGetLength(decodeStream);
|
|
|
|
double ms = Bass.ChannelBytes2Seconds(decodeStream, length) * 1000;
|
2021-07-13 09:46:45 +08:00
|
|
|
|
2022-01-12 21:34:07 +08:00
|
|
|
// Extremely short audio files do not play on some soundcards, resulting in nothing being heard in-game for some users.
|
|
|
|
if (ms > 0 && ms < ms_threshold)
|
|
|
|
yield return new IssueTemplateTooShort(this).Create(file.Filename, ms);
|
|
|
|
}
|
2021-10-12 09:46:26 +08:00
|
|
|
}
|
2021-07-13 09:46:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class IssueTemplateTooShort : IssueTemplate
|
|
|
|
{
|
|
|
|
public IssueTemplateTooShort(ICheck check)
|
2021-07-13 12:07:04 +08:00
|
|
|
: base(check, IssueType.Problem, "\"{0}\" is too short ({1:0} ms), should be at least {2:0} ms.")
|
2021-07-13 09:46:45 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public Issue Create(string filename, double ms) => new Issue(this, filename, ms, ms_threshold);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|