1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-23 15:00:46 +08:00

move audio format checks to reusable audio check utils

This commit is contained in:
Hivie
2025-07-13 04:05:46 +01:00
Unverified
parent c8f9f533e9
commit fccfdbf393
2 changed files with 55 additions and 23 deletions
@@ -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
@@ -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());
/// <summary>
/// Gets the audio format (ChannelType) from a stream using BASS.
/// </summary>
/// <param name="data">The audio file stream.</param>
/// <returns>The ChannelType of the audio, or 0 if detection fails.</returns>
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;
}
/// <summary>
/// Gets the audio format for a specific file in a beatmapset.
/// </summary>
/// <param name="context">The beatmap verifier context.</param>
/// <param name="filename">The filename to check.</param>
/// <returns>The ChannelType of the audio file, or 0 if detection fails.</returns>
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);
}
}
}
}