using OGSoundRemaster.Audio;
string root = "/home/claude/build/payload/OGSound";
string tmp  = "/home/claude/build/test/tmp"; Directory.CreateDirectory(tmp);
static float[] ReadF32(string p){ var b=File.ReadAllBytes(p); var f=new float[b.Length/4]; Buffer.BlockCopy(b,0,f,0,f.Length*4); return f; }

var files = Directory.GetFiles(root,"*",SearchOption.AllDirectories)
                     .Where(f=>f.EndsWith(".wav",StringComparison.OrdinalIgnoreCase)).OrderBy(f=>f).ToArray();
int exact=0, mismatch=0, failed=0; long totalFrames=0, totalFloatBytes=0; double worst=0; string worstFile="";
var sw=System.Diagnostics.Stopwatch.StartNew(); long decodeTicks=0;
foreach(var path in files){
    var bytes=File.ReadAllBytes(path);
    var t0=System.Diagnostics.Stopwatch.GetTimestamp();
    if(!WavDecoder.TryDecode(bytes,bytes.Length,out var pcm,out var err)){ Console.WriteLine($"FAIL {Path.GetRelativePath(root,path)}: {err}"); failed++; continue; }
    decodeTicks += System.Diagnostics.Stopwatch.GetTimestamp()-t0;
    totalFrames+=pcm.Frames; totalFloatBytes+=pcm.ByteSize;
    string refp=Path.Combine(tmp,"r.f32");
    var psi=new System.Diagnostics.ProcessStartInfo("ffmpeg",$"-v error -y -i \"{path}\" -f f32le -acodec pcm_f32le \"{refp}\""){RedirectStandardError=true};
    var pr=System.Diagnostics.Process.Start(psi); pr.WaitForExit();
    var r=ReadF32(refp);
    if(r.Length!=pcm.Samples.Length){ Console.WriteLine($"LEN  {Path.GetRelativePath(root,path)} mine={pcm.Samples.Length} ref={r.Length}"); mismatch++; continue; }
    double mx=0; for(int i=0;i<r.Length;i++){ double d=Math.Abs(pcm.Samples[i]-r[i]); if(d>mx)mx=d; }
    if(mx==0) exact++; else { mismatch++; if(mx>worst){worst=mx;worstFile=Path.GetRelativePath(root,path);} }
}
Console.WriteLine($"\nfiles={files.Length}  bit-exact={exact}  mismatched={mismatch}  failed={failed}");
Console.WriteLine($"total frames={totalFrames:N0}   float32 footprint if ALL resident = {totalFloatBytes/1048576.0:F1} MB");
Console.WriteLine($"pure decode time for entire corpus = {decodeTicks*1000.0/System.Diagnostics.Stopwatch.Frequency:F0} ms  (wall incl. ffmpeg {sw.ElapsedMilliseconds} ms)");
if(mismatch>0) Console.WriteLine($"worst abs err {worst:E3} in {worstFile}");
return (mismatch+failed)==0?0:1;
