private void bgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { const int COPY_LEN = 50 * 1024; BackgroundWorker worker = sender as BackgroundWorker; string[] files = (string[])e.Argument; try { using (RiffReader rr = new RiffReader(files[0])) using (RiffWriter rw = new RiffWriter(files[1])) { // 読み取りファイル解析 bool b = rr.Parse(); if (!b) { e.Result = -2; return; } WaveFormatEx wf = rr.WaveFormat; // 拡張ヘッダーを無効化できる条件 if (wf.Channels <= 2 && wf.Extensible && wf.BitsPerSample != 32) { wf.Extensible = false; wf.FormatTag = WaveFormatTag.PCM; } // ヘッダーの書き出し b = rw.WriteHeader(wf); if (!b) { e.Result = -1; return; } // ループ回数を計算。進捗にも使う long max = rr.Length / COPY_LEN; if ((rr.Length % COPY_LEN) > 0) { max++; } for (long i = 0; i < max; i++) { byte[] arr = rr.Read8(COPY_LEN); if (!rw.WriteStream8(arr)) { e.Result = -1; return; } int percentage = (int)((i + 1) * 100 / max); worker.ReportProgress(percentage); } if (!rw.WriteFinalize()) { return; } } } catch { // エラー e.Result = -3; return; } e.Result = 0; }
private int CompareWav(BackgroundWorker worker, string[] files, CompareInfo ci, DiffWriter sw) { CompareCore cc = new CompareCore(sw); cc.HexOut = ci.setting.hexOut; int ret = 0; try { using (RiffReader rr1 = new RiffReader(files[0])) using (RiffReader rr2 = new RiffReader(files[1])) { if (!rr1.Parse()) { return(-1); } if (!rr2.Parse()) { return(-1); } if (!ci.setting.streamOrigin) { cc.Offset = rr1.StreamOffset; } // 読み込み終端。単位に注意 long limit = 0; if (ci.setting.sampleOrder) { limit = (rr1.Samples > rr2.Samples) ? rr2.Samples : rr1.Samples; } else { limit = (rr1.Length > rr2.Length) ? rr2.Length : rr1.Length; } // 現在処理位置。単位に注意 long read = 0; int prevProgress = 0; int BLOCK_LENGTH = 2 * 1024 * 1024; while (read < limit) { int size = BLOCK_LENGTH; if (read + BLOCK_LENGTH > limit) { size = (int)(limit - read); } if (ci.setting.sampleOrder) { int q = rr1.WaveFormat.BitsPerSample / 8; // まとめて読んで比較 if (q == 1) { byte[] ba1 = rr1.Read8(size); byte[] ba2 = rr2.Read8(size); if (cc.CompareLoop(ba1, ba2, read)) { ret = 1; } } else if (q == 2) { short[] sa1 = rr1.Read16(size); short[] sa2 = rr2.Read16(size); if (cc.CompareLoop(sa1, sa2, read * q)) { ret = 1; } } else if (q == 3) { int[] sa1 = rr1.Read24(size); int[] sa2 = rr2.Read24(size); if (cc.CompareLoop(sa1, sa2, read * q, q)) { ret = 1; } } else if (q == 4) { if (rr1.WaveFormat.FormatTag == WaveFormatTag.IEEE_FLOAT) { float[] sa1 = rr1.Read32F(size); float[] sa2 = rr2.Read32F(size); if (cc.CompareLoop(sa1, sa2, read * q)) { ret = 1; } } else { int[] sa1 = rr1.Read32(size); int[] sa2 = rr2.Read32(size); if (cc.CompareLoop(sa1, sa2, read * q, q)) { ret = 1; } } } else { return(-1); } } else { // まとめて読んで比較 byte[] ba1 = rr1.Read8(size); byte[] ba2 = rr2.Read8(size); if (cc.CompareLoop(ba1, ba2, read)) { ret = 1; } } read += size; // 進捗を通知 int progress = (int)(read * 100 / limit); if (progress > prevProgress) { prevProgress = progress; worker.ReportProgress(progress); } } } } catch { return(-1); } if (sw != null && ret == 1) { if (cc.DiffType) { sw.PrintResult(cc.Count, cc.Max); } else { sw.PrintResult(cc.Count, cc.fMax); } } return(ret); }