|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using AET.ModVerify.Reporting; |
| 5 | +using AET.ModVerify.Settings; |
| 6 | +using PG.StarWarsGame.Engine; |
| 7 | +using System.Threading; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +namespace AET.ModVerify.Verifiers.Commons; |
| 11 | + |
| 12 | +public class AudioFileVerifier( |
| 13 | + IGameVerifierInfo? parent, |
| 14 | + IStarWarsGameEngine gameEngine, |
| 15 | + GameVerifySettings settings, |
| 16 | + IServiceProvider serviceProvider) |
| 17 | + : GameVerifier<AudioFileInfo>(parent, gameEngine, settings, serviceProvider) |
| 18 | +{ |
| 19 | + private readonly IAlreadyVerifiedCache? _alreadyVerifiedCache = serviceProvider.GetService<IAlreadyVerifiedCache>(); |
| 20 | + |
| 21 | + public override string FriendlyName => "Audio File format"; |
| 22 | + |
| 23 | + public override void Verify(AudioFileInfo sampleInfo, IReadOnlyCollection<string> contextInfo, CancellationToken token) |
| 24 | + { |
| 25 | + var sampleString = sampleInfo.SampleName; |
| 26 | + |
| 27 | + using var sampleStream = Repository.TryOpenFile(sampleString.AsSpan()); |
| 28 | + if (sampleStream is null) |
| 29 | + { |
| 30 | + AddError(VerificationError.Create( |
| 31 | + VerifierChain, |
| 32 | + VerifierErrorCodes.FileNotFound, |
| 33 | + $"Audio file '{sampleString}' could not be found.", |
| 34 | + VerificationSeverity.Error, |
| 35 | + [..contextInfo], |
| 36 | + sampleString)); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!_alreadyVerifiedCache?.TryAddEntry(sampleInfo.SampleName) is false) |
| 41 | + return; |
| 42 | + |
| 43 | + if (sampleInfo.ExpectedType == AudioFileType.Mp3) |
| 44 | + { |
| 45 | + // TODO: MP3 support to be implemented |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + using var binaryReader = new BinaryReader(sampleStream); |
| 50 | + |
| 51 | + // Skip Header + "fmt " |
| 52 | + binaryReader.BaseStream.Seek(16, SeekOrigin.Begin); |
| 53 | + |
| 54 | + var fmtSize = binaryReader.ReadInt32(); |
| 55 | + var format = (WaveFormats)binaryReader.ReadInt16(); |
| 56 | + var channels = binaryReader.ReadInt16(); |
| 57 | + |
| 58 | + var sampleRate = binaryReader.ReadInt32(); |
| 59 | + var bytesPerSecond = binaryReader.ReadInt32(); |
| 60 | + |
| 61 | + var frameSize = binaryReader.ReadInt16(); |
| 62 | + var bitPerSecondPerChannel = binaryReader.ReadInt16(); |
| 63 | + |
| 64 | + if (format != WaveFormats.PCM) |
| 65 | + { |
| 66 | + AddError(VerificationError.Create( |
| 67 | + VerifierChain, |
| 68 | + VerifierErrorCodes.SampleNotPCM, |
| 69 | + $"Audio file '{sampleString}' has an invalid format '{format}'. Supported is {WaveFormats.PCM}", |
| 70 | + VerificationSeverity.Error, |
| 71 | + [..contextInfo], |
| 72 | + sampleString)); |
| 73 | + } |
| 74 | + |
| 75 | + if (channels > 1 && !sampleInfo.IsAmbient) |
| 76 | + { |
| 77 | + AddError(VerificationError.Create( |
| 78 | + VerifierChain, |
| 79 | + VerifierErrorCodes.SampleNotMono, |
| 80 | + $"Audio file '{sampleString}' is not mono audio.", |
| 81 | + VerificationSeverity.Information, |
| 82 | + sampleString)); |
| 83 | + } |
| 84 | + |
| 85 | + if (sampleRate > 48_000) |
| 86 | + { |
| 87 | + AddError(VerificationError.Create( |
| 88 | + VerifierChain, |
| 89 | + VerifierErrorCodes.InvalidSampleRate, |
| 90 | + $"Audio file '{sampleString}' has a too high sample rate of {sampleRate}. Maximum is 48.000Hz.", |
| 91 | + VerificationSeverity.Error, |
| 92 | + [..contextInfo], |
| 93 | + sampleString)); |
| 94 | + } |
| 95 | + |
| 96 | + if (bitPerSecondPerChannel > 16) |
| 97 | + { |
| 98 | + AddError(VerificationError.Create( |
| 99 | + VerifierChain, |
| 100 | + VerifierErrorCodes.InvalidBitsPerSeconds, |
| 101 | + $"Audio file '{sampleString}' has an invalid bit size of {bitPerSecondPerChannel}. Supported are 16bit.", |
| 102 | + VerificationSeverity.Error, |
| 103 | + [..contextInfo], |
| 104 | + sampleString)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private enum WaveFormats |
| 109 | + { |
| 110 | + PCM = 1, |
| 111 | + MSADPCM = 2, |
| 112 | + IEEE_Float = 3, |
| 113 | + } |
| 114 | +} |
0 commit comments