Example #1
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;
        }