Beispiel #1
0
        private void MIDIConversion(Control Form, Panel ThreadsPanel, String OPath)
        {
            try
            {
                Status = "prep";
                Int32      MT = Properties.Settings.Default.MultiThreadedMode ? Properties.Settings.Default.MultiThreadedLimitV : 1;
                WaveFormat WF = new WaveFormat(Properties.Settings.Default.Frequency, 32, 2, AudioEncoding.IeeeFloat);

                Debug.PrintToConsole("ok", "Initializing BASS...");
                if (!Bass.BASS_Init(0, WF.SampleRate, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
                {
                    throw new Exception("Unable to initialize BASS!");
                }

                LoadSoundFonts();

                try
                {
                    Debug.PrintToConsole("ok", "Preparing Parallel.ForEach loop...");
                    CTS = new CancellationTokenSource();
                    ParallelOptions PO = new ParallelOptions {
                        MaxDegreeOfParallelism = MT, CancellationToken = CTS.Token
                    };
                    Debug.PrintToConsole("ok", String.Format("ParallelOptions prepared, MaxDegreeOfParallelism = {0}", MT));

                    Parallel.ForEach(Program.MIDIList, PO, (MFile, LS) =>
                    {
                        if (StopRequested)
                        {
                            Debug.PrintToConsole("ok", "Stop requested. Stopping Parallel.ForEach...");
                            LS.Stop();
                            return;
                        }

                        IWaveSource Stream;

                        // Begin conversion
                        Status = "sconv";
                        MDV.SetCurrentMIDI(MFile.Path);

                        // Prepare the filename
                        String OutputDir = String.Format("{0}\\{1}.{2}",
                                                         OPath, Path.GetFileNameWithoutExtension(MFile.Name), Properties.Settings.Default.Codec);

                        // Check if file already exists
                        if (File.Exists(OutputDir))
                        {
                            OutputDir = String.Format("{0}\\{1} - {2}.{3}",
                                                      OPath, Path.GetFileNameWithoutExtension(MFile.Name),
                                                      DateTime.Now.ToString("dd-MM-yyyy HHmmsstt"), Properties.Settings.Default.Codec);
                        }

                        Debug.PrintToConsole("ok", String.Format("Output file: {0}", OutputDir));

                        MIDIThreadStatus MIDIT = new MIDIThreadStatus(MFile.Name);
                        MIDIT.Dock             = DockStyle.Top;
                        ThreadsPanel.Invoke((MethodInvoker) delegate
                        {
                            Debug.PrintToConsole("ok", "Added MIDIThreadStatus control for MIDI.");
                            ThreadsPanel.Controls.Add(MIDIT);
                        });

                        ConvertWorker Worker = new ConvertWorker(MFile.GetFullMIDITimeBased(), MFile.TimeLength.TotalSeconds);

                        Stream FOpen           = new BufferedStream(File.Open(OutputDir, FileMode.Create), 65536);
                        WaveWriter Destination = new WaveWriter(FOpen, WF);
                        ISampleWriter Writer   = new WaveSampleWriter(Destination);
                        Debug.PrintToConsole("ok", "Output file is open.");

                        Task ConvThread = Task.Run(() =>
                        {
                            Worker.Convert(Writer, WF, Properties.Settings.Default.LoudMax, PO.CancellationToken);
                        });

                        while (!ConvThread.IsCompleted)
                        {
                            if (StopRequested)
                            {
                                break;
                            }

                            MIDIT.Invoke((MethodInvoker) delegate
                            {
                                MIDIT.UpdatePB(Convert.ToInt32(Math.Round(Worker.Progress * 100)));
                            });

                            Thread.Sleep(200);
                        }

                        ConvThread.Wait();

                        Debug.PrintToConsole("ok", String.Format("Thread for MIDI {0} is done rendering data.", OutputDir));

                        ThreadsPanel.Invoke((MethodInvoker) delegate
                        {
                            Debug.PrintToConsole("ok", "Removed MIDIThreadStatus control for MIDI.");
                            ThreadsPanel.Controls.Remove(MIDIT);
                        });

                        if (!StopRequested)
                        {
                            MDV.AddValidMIDI();
                        }

                        Destination.Dispose();
                        FOpen.Dispose();
                    });
                }
                catch (OperationCanceledException) { }
                finally { CTS.Dispose(); CTS = null; }

                FreeSoundFonts();

                Debug.PrintToConsole("ok", "BASS freed.");
                Bass.BASS_Free();
            }
            catch (Exception ex)
            {
                Status  = "crsh";
                StError = String.Format("The converter encountered an error during the conversion process.\nError: {0}", ex.Message.ToString());
                IsCrash = true;

                Debug.PrintToConsole("err", String.Format("{0} - {1}", ex.InnerException.ToString(), ex.Message.ToString()));
            }

            if (!StopRequested && !IsCrash)
            {
                Form.Invoke((MethodInvoker) delegate { ((Form)Form).Close(); });
            }
        }