Пример #1
0
        public override void AudioVisualizerPaint() //object sender, PaintEventArgs e)
        {
            InitPaint();

            if (_wavePeaks != null)
            {
                bool showSpectrogram     = IsSpectrogramAvailable && ShowSpectrogram;
                bool showSpectrogramOnly = showSpectrogram && !ShowWaveform;
                int  waveformHeight      = Height - (showSpectrogram ? SpectrogramDisplayHeight : 0);

                // background
                DrawBackground();

                // grid lines
                if (ShowGridLines && !showSpectrogramOnly)
                {
                    DrawGridLines(waveformHeight);
                }

                // spectrogram
                if (showSpectrogram)
                {
                    DrawSpectrogram();
                }

                // waveform
                if (ShowWaveform)
                {
                    var colorNormal                = Color.ToCGColor();
                    var colorSelected              = SelectedColor.ToCGColor();
                    var isSelectedHelper           = new IsSelectedHelper(_allSelectedParagraphs, _wavePeaks.SampleRate);
                    int baseHeight                 = (int)(_wavePeaks.HighestPeak / VerticalZoomFactor);
                    int halfWaveformHeight         = waveformHeight / 2;
                    Func <float, float> calculateY = (value) =>
                    {
                        float offset = (value / baseHeight) * halfWaveformHeight;
                        if (offset > halfWaveformHeight)
                        {
                            offset = halfWaveformHeight;
                        }
                        if (offset < -halfWaveformHeight)
                        {
                            offset = -halfWaveformHeight;
                        }
                        return(halfWaveformHeight - offset);
                    };
                    for (int x = 0; x < Width; x++)
                    {
                        float pos  = (float)RelativeXPositionToSeconds(x) * _wavePeaks.SampleRate;
                        int   pos0 = (int)pos;
                        int   pos1 = pos0 + 1;
                        if (pos1 >= _wavePeaks.Peaks.Count)
                        {
                            break;
                        }
                        float pos1Weight = pos - pos0;
                        float pos0Weight = 1F - pos1Weight;
                        var   peak0      = _wavePeaks.Peaks[pos0];
                        var   peak1      = _wavePeaks.Peaks[pos1];
                        float max        = peak0.Max * pos0Weight + peak1.Max * pos1Weight;
                        float min        = peak0.Min * pos0Weight + peak1.Min * pos1Weight;
                        float yMax       = calculateY(max);
                        float yMin       = Math.Max(calculateY(min), yMax + 0.1F);
                        var   c          = isSelectedHelper.IsSelected(pos0) ? colorNormal : colorSelected;
                        _context.SetStrokeColor(c);
                        DrawLine(x, (int)Math.Round(yMax), x, (int)Math.Round(yMin));
                    }
                }

                // time line
                if (!showSpectrogramOnly)
                {
                    DrawTimeLine(waveformHeight);
                }

                // scene changes
                //                if (_sceneChanges != null)
                //                {
                //                    foreach (double time in _sceneChanges)
                //                    {
                //                        int pos = SecondsToXPosition(time - StartPositionSeconds);
                //                        if (pos > 0 && pos < Width)
                //                        {
                //                            using (var p = new Pen(Color.AntiqueWhite))
                //                                graphics.DrawLine(p, pos, 0, pos, Height);
                //                        }
                //                    }
                //                }

                // current video position
                if (_currentVideoPositionSeconds > 0)
                {
                    int pos = SecondsToXPosition(_currentVideoPositionSeconds - StartPositionSeconds);
                    if (pos > 0 && pos < Width)
                    {
                        _context.SetStrokeColor(Color.Turquoise.ToCGColor());
                        DrawLine(pos, 0, pos, Height);
//                        using (var p = new Pen(Color.Turquoise))
//                            graphics.DrawLine(p, pos, 0, pos, Height);
                    }
                }

                // paragraphs
                foreach (Paragraph p in _displayableParagraphs)
                {
                    if (p.EndTime.TotalSeconds >= StartPositionSeconds && p.StartTime.TotalSeconds <= EndPositionSeconds)
                    {
                        DrawParagraph(p);
                    }
                }

                // current selection
//                if (NewSelectionParagraph != null)
//                {
//                    int currentRegionLeft = SecondsToXPosition(NewSelectionParagraph.StartTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionRight = SecondsToXPosition(NewSelectionParagraph.EndTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionWidth = currentRegionRight - currentRegionLeft;
//                    if (currentRegionRight >= 0 && currentRegionLeft <= Width)
//                    {
//                        using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
//                            graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height);
//
//                        if (currentRegionWidth > 40)
//                        {
//                            using (var brush = new SolidBrush(Color.Turquoise))
//                                graphics.DrawString(string.Format("{0:0.###} {1}", ((double)currentRegionWidth / _wavePeaks.SampleRate / _zoomFactor), Configuration.Settings.Language.Waveform.Seconds), Font, brush, new PointF(currentRegionLeft + 3, Height - 32));
//                        }
//                    }
//                }
            }
            else
            {
                DrawBackground();

                if (ShowGridLines)
                {
                    DrawGridLines(Height);
                }

//                using (var textBrush = new SolidBrush(TextColor))
//                using (var textFont = new Font(Font.FontFamily, 8))
//                {
//                    if (Width > 90)
//                    {
//                        graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(Width / 2 - 65, Height / 2 - 10));
//                    }
//                    else
//                    {
//                        using (var stringFormat = new StringFormat(StringFormatFlags.DirectionVertical))
//                            graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(1, 10), stringFormat);
//                    }
//                }
            }
//            if (Focused)
//            {
//                using (var p = new Pen(SelectedColor))
//                    graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
//            }

            // _imageView.Image = _bitmap.ToNSImage();
            _imageView.Image = new NSImage(_context.ToImage(), new CGSize(Width, Height));
            ;

//            public CGImage ToCGImage()
//            {
//                var rawData = new byte[Width * Height * 4];
//                var bytesPerPixel = 4;
//                var bytesPerRow = bytesPerPixel * Width;
//                var bitsPerComponent = 8;
//                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
//                {
//                    using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
//                    {
//                        for (int y = 0; y < Height; y++)
//                        {
//                            for (int x = 0; x < Width; x++)
//                            {
//                                Color c = GetPixel(x, y);
//                                var i = bytesPerRow * y + bytesPerPixel * x;
//                                rawData[i + 0] = c.R;
//                                rawData[i + 1] = c.G;
//                                rawData[i + 2] = c.B;
//                                rawData[i + 3] = c.A;
//                            }
//                        }
//                        //context.Flush();
//
//                        context.SetFillColor(new CGColor(1,0,0, 1));
//                        context.FillRect(new CGRect(0,0, 20, 20));
//
//                        context.SetTextDrawingMode(CGTextDrawingMode.Clip);
//                        context.SetFillColor(new CGColor(0,0,0, 1));
//                        context.ShowTextAtPoint(20, 20, "HEJ");
//                        context.SetTextDrawingMode(CGTextDrawingMode.Fill);
//                        context.ShowTextAtPoint(30, 30, "Yo");
//                        return context.ToImage();
//                    }
//                }
//            }
        }