|
| 1 | +//============================================================================== |
| 2 | +/* |
| 3 | + * ██████ ██ ███ ███ ███████ ████████ ██ ██ ██████ ██ ██ ██ ██ |
| 4 | + * ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 5 | + * ██ ██ ██ ██ ████ ██ █████ ██ ███████ ██ ██ ███ ████ |
| 6 | + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 7 | + * ██████ ██ ██ ██ ███████ ██ ██ ██ ██████ ██ ██ ██ |
| 8 | + * |
| 9 | + * Copyright (C) 2024 Dimethoxy Audio (https://dimethoxy.com) |
| 10 | + * |
| 11 | + * This file is part of the Dimethoxy Library, a collection of essential |
| 12 | + * classes used across various Dimethoxy projects. |
| 13 | + * These files are primarily designed for internal use within our repositories. |
| 14 | + * |
| 15 | + * License: |
| 16 | + * This code is licensed under the GPLv3 license. You are permitted to use and |
| 17 | + * modify this code under the terms of this license. |
| 18 | + * You must adhere GPLv3 license for any project using this code or parts of it. |
| 19 | + * Your are not allowed to use this code in any closed-source project. |
| 20 | + * |
| 21 | + * Description: |
| 22 | + * Path stroke oscilloscope renderer. Draws a continuous waveform path using |
| 23 | + * JUCE's Path and PathStrokeType for high-quality, anti-aliased rendering. |
| 24 | + * This is the original (and most expensive) rendering algorithm. |
| 25 | + * |
| 26 | + * Authors: |
| 27 | + * Lunix-420 (Primary Author) |
| 28 | + */ |
| 29 | +//============================================================================== |
| 30 | + |
| 31 | +#pragma once |
| 32 | + |
| 33 | +//============================================================================== |
| 34 | + |
| 35 | +#include "gui/widget/OscilloscopeRenderer.h" |
| 36 | +#include <JuceHeader.h> |
| 37 | + |
| 38 | +//============================================================================== |
| 39 | + |
| 40 | +namespace dmt { |
| 41 | +namespace gui { |
| 42 | +namespace widget { |
| 43 | + |
| 44 | +//============================================================================== |
| 45 | +/** |
| 46 | + * @brief Path stroke oscilloscope renderer for high-quality waveform drawing. |
| 47 | + * |
| 48 | + * @tparam SampleType The sample type (e.g., float, double) used for audio data. |
| 49 | + * |
| 50 | + * @details |
| 51 | + * This renderer builds a continuous JUCE Path from the audio samples and |
| 52 | + * strokes it with a configurable thickness. It produces high-quality, |
| 53 | + * anti-aliased waveform visuals at the cost of higher CPU usage compared to |
| 54 | + * simpler rendering strategies. |
| 55 | + * |
| 56 | + * The renderer maintains persistent state (currentX and currentSample) between |
| 57 | + * frames to ensure visual continuity of the waveform across render calls. |
| 58 | + * Sub-pixel positioning is preserved to avoid visual jitter. |
| 59 | + * |
| 60 | + * Samples are read directly from the ring buffer — no data is copied. |
| 61 | + */ |
| 62 | +template<typename SampleType> |
| 63 | +class PathStrokeRenderer : public OscilloscopeRenderer<SampleType> |
| 64 | +{ |
| 65 | + //============================================================================ |
| 66 | +public: |
| 67 | + using RingBuffer = typename OscilloscopeRenderer<SampleType>::RingBuffer; |
| 68 | + using RenderContext = |
| 69 | + typename OscilloscopeRenderer<SampleType>::RenderContext; |
| 70 | + |
| 71 | + //============================================================================ |
| 72 | + /** |
| 73 | + * @brief Draws a waveform segment using path stroking. |
| 74 | + * |
| 75 | + * @param _graphics The JUCE Graphics context targeting the oscilloscope |
| 76 | + * image. |
| 77 | + * @param _ringBuffer Reference to the ring buffer containing audio samples. |
| 78 | + * @param _channel The audio channel index to read from. |
| 79 | + * @param _context Pre-computed rendering parameters for this frame. |
| 80 | + * |
| 81 | + * @details |
| 82 | + * Builds a continuous path from the provided samples, maintaining sub-pixel |
| 83 | + * continuity with the previous frame via the persistent currentX state. |
| 84 | + * The path is stroked with mitered joints and square end caps for a clean |
| 85 | + * waveform appearance. |
| 86 | + */ |
| 87 | + inline void draw(juce::Graphics& _graphics, |
| 88 | + RingBuffer& _ringBuffer, |
| 89 | + int _channel, |
| 90 | + const RenderContext& _context) override |
| 91 | + { |
| 92 | + // Maintain sub-pixel continuity across frames |
| 93 | + currentX = currentX - static_cast<int>(currentX) + _context.drawStartX; |
| 94 | + |
| 95 | + const float startY = |
| 96 | + static_cast<float>(_context.halfHeight) + |
| 97 | + currentSample * _context.halfHeight * _context.amplitude; |
| 98 | + const auto startPoint = juce::Point<float>(currentX, startY); |
| 99 | + |
| 100 | + juce::Path path; |
| 101 | + path.startNewSubPath(startPoint); |
| 102 | + |
| 103 | + for (size_t i = 0; i < static_cast<size_t>(_context.sampleCount); ++i) { |
| 104 | + const int sampleIndex = _context.firstSampleIndex + static_cast<int>(i); |
| 105 | + currentSample = _ringBuffer.getSample(_channel, sampleIndex); |
| 106 | + currentX += _context.pixelsPerSample; |
| 107 | + const float y = static_cast<float>(_context.halfHeight) + |
| 108 | + currentSample * _context.halfHeight * _context.amplitude; |
| 109 | + const auto point = juce::Point<float>(currentX, y); |
| 110 | + path.lineTo(point); |
| 111 | + } |
| 112 | + |
| 113 | + juce::PathStrokeType strokeType(_context.thickness * _context.sizeFactor, |
| 114 | + juce::PathStrokeType::JointStyle::mitered, |
| 115 | + juce::PathStrokeType::EndCapStyle::square); |
| 116 | + |
| 117 | + _graphics.setColour(juce::Colours::white); |
| 118 | + _graphics.strokePath(path, strokeType); |
| 119 | + } |
| 120 | + |
| 121 | + //============================================================================ |
| 122 | +private: |
| 123 | + //============================================================================ |
| 124 | + /** Last sample value for waveform continuity between frames. */ |
| 125 | + SampleType currentSample = static_cast<SampleType>(0.0f); |
| 126 | + |
| 127 | + /** Current X position with sub-pixel precision for visual continuity. */ |
| 128 | + float currentX = 0.0f; |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace widget |
| 132 | +} // namespace gui |
| 133 | +} // namespace dmt |
0 commit comments