Files
MagnetismHeroes/tools/android/check-count-audio-residual.ps1

21 lines
1.8 KiB
PowerShell

param([Parameter(Mandatory)][string]$CapturedPath,[Parameter(Mandatory)][string]$MatchDir,[Parameter(Mandatory)][string]$Prefix,[Parameter(Mandatory)][string]$OutputPath)
$ErrorActionPreference='Stop'
Add-Type -TypeDefinition @'
using System;
using System.IO;
public static class CountPcmResidual {
public static double Rms(string file,double start,double end) {
byte[] bytes=File.ReadAllBytes(file);int rate=BitConverter.ToInt32(bytes,24);
if(BitConverter.ToInt16(bytes,20)!=1||BitConverter.ToInt16(bytes,22)!=1||BitConverter.ToInt16(bytes,34)!=16||System.Text.Encoding.ASCII.GetString(bytes,36,4)!="data")throw new Exception("Canonical actual monoPCM16 required");
int a=Math.Max(0,(int)Math.Ceiling(start*rate)),b=Math.Min((bytes.Length-44)/2,(int)Math.Floor(end*rate));
if(b<=a)return 0;double sum=0;for(int i=a;i<b;i++){double v=BitConverter.ToInt16(bytes,44+i*2);sum+=v*v;}return Math.Sqrt(sum/(b-a));
}
}
'@
$matches=@(1..10|ForEach-Object{Get-Content -LiteralPath (Join-Path $MatchDir ('{0}-count_{1:D2}-match.json'-f$Prefix,$_)) -Raw|ConvertFrom-Json})
$intervals=@(for($i=0;$i-lt10;$i++){$start=$matches[$i].onsetSeconds+$matches[$i].referenceDurationSeconds+.015;$end=if($i-lt9){$matches[$i+1].onsetSeconds-.015}else{$matches[$i].capturedDurationSeconds};$rms=[CountPcmResidual]::Rms((Resolve-Path $CapturedPath).Path,$start,$end);@{afterCount=$i+1;start=$start;end=$end;rmsPcm=$rms;passed=$rms-le32}})
$passed=$intervals.Count-eq10-and@($intervals|Where-Object passed -ne $true).Count-eq0
@{passed=$passed;thresholdRmsPcm=32;excludedLeadBeforeFirstNumber='May include interrupted collection guide; all numeric gaps/tail checked';intervals=$intervals;capturedSha256=(Get-FileHash $CapturedPath).Hash}|ConvertTo-Json -Depth 5|Set-Content -LiteralPath $OutputPath
if(!$passed){exit 1}
exit 0