WE Core
Loading...
Searching...
No Matches
RichterWaveViewer.h
Go to the documentation of this file.
1/*
2 * File: RichterWaveViewer.h
3 *
4 * Created: 16/07/2020
5 *
6 * This file is part of WECore.
7 *
8 * WECore is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * WECore is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the WECore. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "JuceHeader.h"
27
28namespace WECore::Richter {
29
30 class WaveViewer : public juce::Component,
31 public juce::SettableTooltipClient {
32 public:
34
35 inline void setWave(const double* pointer, double depth, double phaseShift, bool isInverted);
36
37 inline virtual void paint(juce::Graphics& g);
38
39 void stop() { _waveArrayPointer = nullptr; }
40
42 {
43 highlightColourId = 0x1201201
44 };
45
46 private:
47 const double* _waveArrayPointer;
48 double _depth;
51 };
52
53 void WaveViewer::setWave(const double* pointer, double depth, double phaseShift, bool isInverted) {
54 _waveArrayPointer = pointer;
55 _depth = depth;
56 _phaseShift = phaseShift;
57 _isInverted = isInverted;
58 }
59
60 void WaveViewer::paint(juce::Graphics &g) {
61
62 // Down sample the wave array
63 constexpr int NUM_SAMPLES {25};
64 constexpr float SCALE {0.4};
65 constexpr float MARGIN { (1 - SCALE) / 2 };
66 const float INCREMENT {static_cast<float>(Wavetables::SIZE) / NUM_SAMPLES};
67
68 if (_waveArrayPointer != nullptr) {
69 juce::Path p;
70
71 for (size_t idx {0}; idx < NUM_SAMPLES; idx++) {
72 // Calculate the index of the sample accounting for downsampling and phase shift
73 const int phaseIndexOffset {
74 static_cast<int>((_phaseShift / Parameters::PHASE.maxValue) * Wavetables::SIZE)
75 };
76 const int sampleIdx {(
77 (static_cast<int>(idx * INCREMENT + phaseIndexOffset) % Wavetables::SIZE)
78 )};
79
80 // Get the sample for this value
81 const double sample {_waveArrayPointer[sampleIdx] * _depth * (_isInverted ? -1 : 1)};
82
83 // Invert the wave and scale to the height of this component
84 const double sampleX {(static_cast<double>(idx) / NUM_SAMPLES) * getWidth()};
85 const double sampleY = (0.5 - sample) * getHeight() * SCALE + getHeight() * MARGIN;
86
87 // Add it to the path
88 if (idx == 0) {
89 p.startNewSubPath(0, sampleY);
90 } else {
91 p.lineTo(sampleX, sampleY);
92 }
93 }
94
95 g.setColour(findColour(highlightColourId));
96 g.strokePath(p, juce::PathStrokeType(3.0f));
97 }
98 }
99}
virtual void paint(juce::Graphics &g)
void setWave(const double *pointer, double depth, double phaseShift, bool isInverted)
const ParameterDefinition::RangedParameter< double > PHASE(0, 360, 0)