Exemplo n.º 1
0
        private static void Render(Settings settings, AudioLoader loader)
        {
            if (settings.OutputFile != null)
            {
                // Emit normalized data to a WAV file for later mixing
                if (settings.MasterAudioFile == null && !settings.NoMasterMix)
                {
                    settings.MasterAudioFile = settings.OutputFile + ".wav";
                    loader.MixToFile(settings.MasterAudioFile, !settings.NoMasterMixReplayGain);
                }
            }

            Console.WriteLine("Generating background image...");

            var backgroundImage = new BackgroundRenderer(settings.Width, settings.Height, ParseColor(settings.BackgroundColor));

            if (settings.BackgroundImageFile != null)
            {
                using (var bm = Image.FromFile(settings.BackgroundImageFile))
                {
                    backgroundImage.Add(new ImageInfo(bm, ContentAlignment.MiddleCenter, true, DockStyle.None, 0.5f));
                }
            }

            if (settings.LogoImageFile != null)
            {
                using (var bm = Image.FromFile(settings.LogoImageFile))
                {
                    backgroundImage.Add(new ImageInfo(bm, ContentAlignment.BottomRight, false, DockStyle.None, 1));
                }
            }

            if (settings.VgmFile != null)
            {
                var gd3     = Gd3Tag.LoadFromVgm(settings.VgmFile);
                var gd3Text = gd3.ToString();
                if (gd3Text.Length > 0)
                {
                    backgroundImage.Add(new TextInfo(gd3Text, settings.Gd3Font, settings.Gd3FontSize, ContentAlignment.BottomLeft, FontStyle.Regular,
                                                     DockStyle.Bottom, ParseColor(settings.Gd3FontColor)));
                }
            }

            var renderer = new WaveformRenderer
            {
                BackgroundImage            = backgroundImage.Image,
                Columns                    = settings.Columns,
                FramesPerSecond            = settings.FramesPerSecond,
                Width                      = settings.Width,
                Height                     = settings.Height,
                SamplingRate               = loader.SampleRate,
                RenderedLineWidthInSamples = settings.ViewWidthMs * loader.SampleRate / 1000,
                RenderingBounds            = backgroundImage.WaveArea
            };

            if (settings.GridLineWidth > 0)
            {
                renderer.Grid = new WaveformRenderer.GridConfig
                {
                    Color      = ParseColor(settings.GridColor),
                    Width      = settings.GridLineWidth,
                    DrawBorder = settings.GridBorder
                };
            }

            if (settings.ZeroLineWidth > 0)
            {
                renderer.ZeroLine = new WaveformRenderer.ZeroLineConfig
                {
                    Color = ParseColor(settings.ZeroLineColor),
                    Width = settings.ZeroLineWidth
                };
            }

            // Add the data to the renderer
            foreach (var channel in loader.Data)
            {
                renderer.AddChannel(new Channel(
                                        channel.Samples,
                                        ParseColor(settings.LineColor),
                                        settings.LineWidth,
                                        GuessChannelName(channel.Filename),
                                        CreateTriggerAlgorithm(settings.TriggerAlgorithm),
                                        settings.TriggerLookahead));
            }

            if (settings.ChannelLabelsFont != null)
            {
                renderer.ChannelLabels = new WaveformRenderer.LabelConfig
                {
                    Color    = ParseColor(settings.ChannelLabelsColor),
                    FontName = settings.ChannelLabelsFont,
                    Size     = settings.ChannelLabelsSize
                };
            }

            var outputs = new List <IGraphicsOutput>();

            if (settings.FfMpegPath != null)
            {
                Console.WriteLine("Adding FFMPEG renderer...");
                outputs.Add(new FfmpegOutput(settings.FfMpegPath, settings.OutputFile, settings.Width, settings.Height, settings.FramesPerSecond, settings.FfMpegExtraOptions, settings.MasterAudioFile));
            }

            if (settings.PreviewFrameskip > 0)
            {
                Console.WriteLine("Adding preview renderer...");
                outputs.Add(new PreviewOutput(settings.PreviewFrameskip));
            }

            try
            {
                Console.WriteLine("Rendering...");
                var sw = Stopwatch.StartNew();
                renderer.Render(outputs);
                sw.Stop();
                int numFrames = (int)(loader.Length.TotalSeconds * settings.FramesPerSecond);
                Console.WriteLine($"Rendering complete in {sw.Elapsed:g}, average {numFrames / sw.Elapsed.TotalSeconds:N} fps");
            }
            catch (Exception ex)
            {
                // Should mean it was cancelled
                Console.WriteLine($"Rendering cancelled: {ex.Message}");
            }
            finally
            {
                foreach (var graphicsOutput in outputs)
                {
                    graphicsOutput.Dispose();
                }
            }
        }