Oscilloscope 0.5.0
A simple oscilloscope VST
Loading...
Searching...
No Matches
SampleFinder.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 SampleFinder.cpp
5 Created: 2 Apr 2023 5:30:47pm
6 Author: covariant
7
8 ==============================================================================
9*/
10
11#include "SampleFinder.h"
12
13void AutoSampleFinder::parameterChanged(const juce::String &parameterID, float newValue)
14{
15 // if decrescent
16 if (newValue)
17 {
18 this->findSample = [](float triggerLevel, std::vector<float> currentlyDisplayedData)
19 { return FindMethods::autoDecrescentFirst(triggerLevel, currentlyDisplayedData); };
20 }
21 // if crescent
22 else
23 {
24 this->findSample = [](float triggerLevel, std::vector<float> currentlyDisplayedData)
25 { return FindMethods::autoCrescentFirst(triggerLevel, currentlyDisplayedData); };
26 }
27}
28
29void ManualSampleFinder::parameterChanged(const juce::String &parameterID, float newValue)
30{
31 // if decrescent
32 if (newValue)
33 {
34 this->findSample = [](float triggerLevel, std::vector<float> currentlyDisplayedData)
35 { return FindMethods::decrescentFirst(triggerLevel, currentlyDisplayedData); };
36 }
37 // if crescent
38 else
39 {
40 this->findSample = [](float triggerLevel, std::vector<float> currentlyDisplayedData)
41 { return FindMethods::crescentFirst(triggerLevel, currentlyDisplayedData); };
42 }
43}
44
45SampleFinder::SampleFinder(bool isCrescent, bool isAuto)
46{
47 // set current state for all listeners
48 parameterChanged("", isAuto);
49 autoFinder.parameterChanged("", isCrescent);
50 manualFinder.parameterChanged("", isCrescent);
51}
52
54
55int SampleFinder::findFirstSample(float triggerLevel,
56 std::vector<float> currentlyDisplayedData)
57{
58 // just return the value found from the currently active finder
59 return this->currentFinder->findFirstSample(triggerLevel, currentlyDisplayedData);
60}
61
62void SampleFinder::parameterChanged(const juce::String &parameterID, float newValue)
63{
64 // if automatic
65 if (newValue)
66 {
68 }
69 // if manual
70 else
71 {
73 }
74}
75
76int FindMethods::crescentFirst(float triggerLevel, std::vector<float> currentlyDisplayedData)
77{
78 // declare variables
79 auto data = currentlyDisplayedData.begin();
80 auto numSamples = currentlyDisplayedData.size();
81 std::vector<float>::iterator firstToPlot;
82
83 // condition to stop searching (lamda func)
84 auto condition = [](float x, float subseq, float trig)
85 {
86 bool firstCondition = std::abs(x - trig) < 0.05;
87 bool secondCondition = subseq > x;
88 return firstCondition && secondCondition;
89 };
90
91 // search in all currentlyDisplayedData
92 for (auto i = currentlyDisplayedData.begin(); i < currentlyDisplayedData.end() - 1; ++i)
93 {
94 firstToPlot = i;
95 // when found return
96 if (condition(i[0], i[1], triggerLevel))
97 {
98 int distance = std::distance(currentlyDisplayedData.begin(), firstToPlot);
99 return distance;
100 }
101 }
102 // if not found return -1
103 int notFound = -1;
104 return notFound;
105}
106
107int FindMethods::autoCrescentFirst(float triggerLevel, std::vector<float> currentlyDisplayedData)
108{
109 // declare variables
110 float max = *std::max_element(currentlyDisplayedData.begin(), currentlyDisplayedData.end());
111 float triggerPoint = max * triggerLevel;
112 std::vector<float>::iterator firstToPlot;
113
114 auto data = currentlyDisplayedData.begin();
115 auto numSamples = currentlyDisplayedData.size();
116
117 // condition to stop searching (lamda func)
118 auto condition = [](float x, float subseq, float trig)
119 {
120 bool firstCondition = std::abs(x - trig) < 0.05;
121 bool secondCondition = subseq > x;
122 return firstCondition && secondCondition;
123 };
124
125 // search in all currentlyDisplayedData
126 for (auto i = currentlyDisplayedData.begin(); i < currentlyDisplayedData.end() - 1; ++i)
127 {
128 firstToPlot = i;
129 // when found stop
130 if (condition(i[0], i[1], triggerPoint))
131 {
132 int distance = std::distance(currentlyDisplayedData.begin(), firstToPlot);
133 return distance;
134 }
135 }
136
137 // if not found return -1
138 int notFound = -1;
139 return notFound;
140}
141
142int FindMethods::decrescentFirst(float triggerLevel, std::vector<float> currentlyDisplayedData)
143{
144 // declare variables
145 auto data = currentlyDisplayedData.begin();
146 auto numSamples = currentlyDisplayedData.size();
147 std::vector<float>::iterator firstToPlot;
148
149 // condition to stop searching (lamda func)
150 auto condition = [](float x, float subseq, float trig)
151 {
152 bool firstCondition = std::abs(x - trig) < 0.05;
153 bool secondCondition = subseq < x;
154 return firstCondition && secondCondition;
155 };
156
157 // search in all currentlyDisplayedData
158 for (auto i = currentlyDisplayedData.begin(); i < currentlyDisplayedData.end() - 1; ++i)
159 {
160 firstToPlot = i;
161 // when found stop
162 if (condition(i[0], i[1], triggerLevel))
163 {
164 int distance = std::distance(currentlyDisplayedData.begin(), firstToPlot);
165 return distance;
166 }
167 }
168
169 // if not found return -1
170 int notFound = -1;
171 return notFound;
172}
173
174int FindMethods::autoDecrescentFirst(float triggerLevel, std::vector<float> currentlyDisplayedData)
175{
176 // declare variables
177 float max = *std::max_element(currentlyDisplayedData.begin(), currentlyDisplayedData.end());
178 float triggerPoint = max * triggerLevel;
179 std::vector<float>::iterator firstToPlot;
180
181 auto data = currentlyDisplayedData.begin();
182 auto numSamples = currentlyDisplayedData.size();
183
184 // condition to stop searching (lamda func)
185 auto condition = [](float x, float subseq, float trig)
186 {
187 bool firstCondition = std::abs(x - trig) < 0.05;
188 bool secondCondition = subseq < x;
189 return firstCondition && secondCondition;
190 };
191
192 // search in all currentlyDisplayedData
193 for (auto i = currentlyDisplayedData.begin(); i < currentlyDisplayedData.end() - 1; ++i)
194 {
195 firstToPlot = i;
196 // when found stop
197 if (condition(i[0], i[1], triggerPoint))
198 {
199 int distance = std::distance(currentlyDisplayedData.begin(), firstToPlot);
200 return distance;
201 }
202 }
203
204 // if not found return -1
205 int notFound = -1;
206 return notFound;
207}
208
209int BaseFinder::findFirstSample(float triggerLevel, std::vector<float> currentlyDisplayedData)
210{
211 // Return the value found from the currently active finder.
212 return this->findSample(triggerLevel, currentlyDisplayedData);
213}
void parameterChanged(const juce::String &parameterID, float newValue) override
std::function< int(float, std::vector< float >)> findSample
Definition: SampleFinder.h:81
int findFirstSample(float triggerLevel, std::vector< float > currentlyDisplayedData)
void parameterChanged(const juce::String &parameterID, float newValue) override
ManualSampleFinder manualFinder
Definition: SampleFinder.h:170
AutoSampleFinder autoFinder
Definition: SampleFinder.h:164
SampleFinder(bool isCrescent, bool isAuto)
void parameterChanged(const juce::String &parameterID, float newValue) override
int findFirstSample(float triggerLevel, std::vector< float > currentlyDisplayedData)
BaseFinder * currentFinder
Definition: SampleFinder.h:176
int autoCrescentFirst(float triggerLevel, std::vector< float > currentlyDisplayedData)
int crescentFirst(float triggerLevel, std::vector< float > currentlyDisplayedData)
int autoDecrescentFirst(float triggerLevel, std::vector< float > currentlyDisplayedData)
int decrescentFirst(float triggerLevel, std::vector< float > currentlyDisplayedData)