From fccfdbf393862cd49071beec40843a6ca939d360 Mon Sep 17 00:00:00 2001 From: Hivie Date: Sun, 13 Jul 2025 04:05:46 +0100 Subject: [PATCH] move audio format checks to reusable audio check utils --- .../Rulesets/Edit/Checks/CheckSongFormat.cs | 32 ++++--------- .../Edit/Checks/Components/AudioCheckUtils.cs | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs b/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs index dd01fe110a..aa039630d4 100644 --- a/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs +++ b/osu.Game/Rulesets/Edit/Checks/CheckSongFormat.cs @@ -2,12 +2,9 @@ // 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 @@ -36,28 +33,17 @@ namespace osu.Game.Rulesets.Edit.Checks if (beatmapSet == null) yield break; if (audioFile == null) yield break; - using (Stream data = context.WorkingBeatmap.GetStream(audioFile.File.GetStoragePath())) + var audioFormat = AudioCheckUtils.GetAudioFormatFromFile(context, context.Beatmap.Metadata.AudioFile); + + // If the format is not supported by BASS + if (audioFormat == 0) { - 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); + yield return new IssueTemplateFormatUnsupported(this).Create(audioFile.Filename); + yield break; } + + if (!allowedFormats.Contains(audioFormat)) + yield return new IssueTemplateIncorrectFormat(this).Create(audioFile.Filename); } public class IssueTemplateFormatUnsupported : IssueTemplate diff --git a/osu.Game/Rulesets/Edit/Checks/Components/AudioCheckUtils.cs b/osu.Game/Rulesets/Edit/Checks/Components/AudioCheckUtils.cs index 8a35b84170..7cd7738f69 100644 --- a/osu.Game/Rulesets/Edit/Checks/Components/AudioCheckUtils.cs +++ b/osu.Game/Rulesets/Edit/Checks/Components/AudioCheckUtils.cs @@ -3,6 +3,10 @@ using System.IO; using System.Linq; +using ManagedBass; +using osu.Framework.Audio.Callbacks; +using osu.Game.Beatmaps; +using osu.Game.Extensions; using osu.Game.Utils; namespace osu.Game.Rulesets.Edit.Checks.Components @@ -10,5 +14,47 @@ namespace osu.Game.Rulesets.Edit.Checks.Components public static class AudioCheckUtils { public static bool HasAudioExtension(string filename) => SupportedExtensions.AUDIO_EXTENSIONS.Contains(Path.GetExtension(filename).ToLowerInvariant()); + + /// + /// Gets the audio format (ChannelType) from a stream using BASS. + /// + /// The audio file stream. + /// The ChannelType of the audio, or 0 if detection fails. + public static ChannelType GetAudioFormat(Stream data) + { + if (data.Length <= 0) + return 0; + + var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data)); + int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode, fileCallbacks.Callbacks, fileCallbacks.Handle); + + if (decodeStream == 0) + return 0; + + var audioInfo = Bass.ChannelGetInfo(decodeStream); + Bass.StreamFree(decodeStream); + + return audioInfo.ChannelType; + } + + /// + /// Gets the audio format for a specific file in a beatmapset. + /// + /// The beatmap verifier context. + /// The filename to check. + /// The ChannelType of the audio file, or 0 if detection fails. + public static ChannelType GetAudioFormatFromFile(BeatmapVerifierContext context, string filename) + { + var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet; + var audioFile = beatmapSet?.GetFile(filename); + + if (beatmapSet == null || audioFile == null) + return 0; + + using (Stream data = context.WorkingBeatmap.GetStream(audioFile.File.GetStoragePath())) + { + return GetAudioFormat(data); + } + } } }