예제 #1
0
        private bool CheckFileType()
        {
            try {
                using (RiffReader f1 = new RiffReader(textBoxFile1.Text))
                    using (RiffReader f2 = new RiffReader(textBoxFile2.Text)) {
                        bool b1 = f1.Parse();
                        bool b2 = f2.Parse();

                        if (b1 && b2)
                        {
                            if (f1.WaveFormat.Channels != f2.WaveFormat.Channels)
                            {
                                MessageBox.Show("チャンネル数が異なります。比較を中断します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return(false);
                            }
                            else if (f1.WaveFormat.FormatTag != f2.WaveFormat.FormatTag)
                            {
                                MessageBox.Show("フォーマットが異なります。比較を中断します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return(false);
                            }
                            else if (f1.WaveFormat.SamplesPerSecond != f2.WaveFormat.SamplesPerSecond)
                            {
                                MessageBox.Show("サンプリングレートが異なります。比較を中断します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return(false);
                            }
                            else if (f1.WaveFormat.BitsPerSample != f2.WaveFormat.BitsPerSample)
                            {
                                MessageBox.Show("量子化ビット数が異なります。比較を中断します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return(false);
                            }
                            else
                            {
                                m_Comp.type = CompType.Wavs;
                            }
                        }
                        else if (!b1 && !b2)
                        {
                            MessageBox.Show("どちらもWAVファイルではありません。先頭からバイト単位で比較します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            m_Comp.type = CompType.Raws;
                        }
                        else
                        {
#if true
                            m_Comp.type = CompType.Raws;
                            if (b1)
                            {
                                MessageBox.Show("ファイル2はWAVファイルではありません。先頭からバイト単位で比較します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                m_Comp.rawFile = 1;
                            }
                            else
                            {
                                MessageBox.Show("ファイル1はWAVファイルではありません。先頭からバイト単位で比較します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                m_Comp.rawFile = 0;
                            }
#else
                            m_Comp.type = CompType.RawWav;
                            if (b1)
                            {
                                MessageBox.Show("ファイル2はWAVファイルではありません。Rawストリームとみなして比較します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                m_Comp.rawFile = 1;
                            }
                            else
                            {
                                MessageBox.Show("ファイル1はWAVファイルではありません。Rawストリームとみなして比較します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                m_Comp.rawFile = 0;
                            }
#endif
                        }
                        return(true);
                    }
            } catch (Exception) {
                MessageBox.Show("ファイルがオープンできません。比較を中断します。", "情報", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }
        }
예제 #2
0
        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;
        }
예제 #3
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);
        }