Krotos Modules 3
Loading...
Searching...
No Matches
SampleDataTree.cpp
Go to the documentation of this file.
1namespace krotos
2{
3 const Identifier SamplesTree::Type::Samples{"SAMPLES"};
4 const Identifier SamplesTree::Type::Sample{"SAMPLE"};
5
6 const Identifier SamplesTree::Property::Name{"name"};
7 const Identifier SamplesTree::Property::Path{"path"};
8 const Identifier SamplesTree::Property::Selection{"selection"};
9 const Identifier SamplesTree::Property::Mute{"mute"};
10 const Identifier SamplesTree::Property::Solo{"solo"};
11 const Identifier SamplesTree::Property::SampleIndex{"sample_index"};
12 const Identifier SamplesTree::Property::SelectedSample{"selected_sample"};
13
15 : CustomParameterTree(Type::Samples),
16 sampleIndexValue(m_parameterTree, Property::SampleIndex, getUndoManager()),
17 selectedSampleValue(m_parameterTree, Property::SelectedSample, getUndoManager())
18 {
19 }
20
21 SamplesTree::SamplesTree(const Identifier& type)
22 : CustomParameterTree(type), sampleIndexValue(m_parameterTree, Property::SampleIndex, getUndoManager()),
23 selectedSampleValue(m_parameterTree, Property::SelectedSample, getUndoManager())
24 {
25 }
26
28 : CustomParameterTree(other.getParameterTree(), other.getUndoManager()),
29 sampleIndexValue(m_parameterTree, Property::SampleIndex, getUndoManager()),
30 selectedSampleValue(m_parameterTree, Property::SelectedSample, getUndoManager())
31 {
32 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
33 {
34 m_samples.add(std::make_unique<Sample>(getChild(i), getUndoManager()));
35 }
37 jassert(m_samples.size() == m_parameterTree.getNumChildren());
38 }
39
40 SamplesTree::SamplesTree(const ValueTree& other)
41 : CustomParameterTree(other), sampleIndexValue(m_parameterTree, Property::SampleIndex, getUndoManager()),
42 selectedSampleValue(m_parameterTree, Property::SelectedSample, getUndoManager())
43 {
44 if (m_parameterTree.getType() != Type::Samples)
45 {
46 jassertfalse; // Expected Samples Tree, got something else.
47 // Init to default, invalid state
48 m_parameterTree = ValueTree();
49 sampleIndexValue.resetToDefault();
50 selectedSampleValue.resetToDefault();
51 return;
52 }
53
54 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
55 {
56 if (getChild(i).getType() == Type::Sample)
57 {
58 m_samples.add(std::make_unique<Sample>(getChild(i), getUndoManager()));
59 }
60 else
61 {
62 jassertfalse; // Expected to only contain Sample children
63 }
64 }
65 jassert(m_samples.size() == m_parameterTree.getNumChildren());
66 }
67
69 {
72
73 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
74 {
75 m_samples.add(std::make_unique<Sample>(getChild(i), getUndoManager()));
76 }
78 jassert(m_samples.size() == m_parameterTree.getNumChildren());
79
82 return *this;
83 }
84
85 void SamplesTree::updateSample(const String& name, const String& path)
86 {
87 if (auto child = m_parameterTree.getChildWithProperty(Property::Name, name); child.isValid())
88 {
89 child.setProperty(Property::Name, name, getUndoManager());
90 child.setProperty(Property::Path, path, getUndoManager());
91 }
92 else
93 {
94 addSample(name, path, false);
95 }
96 }
97
98 void SamplesTree::addSample(const String& name, const String& path, bool isMuted)
99 {
100 ValueTree sample{Type::Sample,
101 {{Property::Name, name},
102 {Property::Path, path},
103 {Property::Selection, rangeToString(Range<int>())},
104 {Property::Mute, isMuted},
105 {Property::Solo, false}}};
106
107 addChild(sample);
108 }
109
111 {
112 auto treeToRemove = m_parameterTree.getChild(index);
113 removeChild(treeToRemove);
114 }
115
116 void SamplesTree::removeSample(const String& name)
117 {
118 auto treeToRemove = m_parameterTree.getChildWithProperty(Property::Name, name);
119 removeChild(treeToRemove);
120 }
121
122 const StringArray SamplesTree::getFiles() const
123 {
124 StringArray files;
125
126 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
127 {
128 files.add(getChild(i)[Property::Path].toString());
129 }
130
131 return files;
132 }
133
134 void SamplesTree::setSampleSelection(int index, const Range<int> selection)
135 {
136 jassert(index < m_parameterTree.getNumChildren());
137 getChild(index).setProperty(Property::Selection, rangeToString(selection), getUndoManager());
138 }
139
140 Range<int> SamplesTree::getSampleSelection(int index) const
141 {
143 }
144
145 std::vector<Range<int>> SamplesTree::getSampleSelections() const
146 {
147 std::vector<Range<int>> selections;
148
149 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
150 {
151 selections.push_back(stringToRange(getChild(i)[Property::Selection]));
152 }
153
154 return selections;
155 }
156
157 void SamplesTree::setSampleSelections(const std::vector<Range<int>>& selections)
158 {
159 for (int i = 0; i < selections.size(); i++)
160 {
161 getChild(i).setProperty(Property::Selection, rangeToString(selections[i]), getUndoManager());
162 }
163 }
164
165 String SamplesTree::rangeToString(const Range<int>& range)
166 {
167 return String(range.getStart()) + m_rangeDelimiter + String(range.getEnd()) + m_rangeDelimiter +
168 String(range.getLength());
169 }
170
171 Range<int> SamplesTree::stringToRange(const String& rangeString)
172 {
173 auto start = rangeString.upToFirstOccurrenceOf(m_rangeDelimiter, false, false);
174 auto end = rangeString.fromFirstOccurrenceOf(m_rangeDelimiter, false, false)
175 .upToFirstOccurrenceOf(m_rangeDelimiter, false, false);
176
177 return Range<int>(start.getIntValue(), end.getIntValue());
178 }
179
180 OwnedArray<SampleBrowser::Sample> SamplesTree::getSampleDataAsBrowserSamples()
181 {
182 OwnedArray<SampleBrowser::Sample> samples;
183
184 for (int i = 0; i < m_parameterTree.getNumChildren(); i++)
185 {
187 getChild(i).getPropertyAsValue(Property::Mute, nullptr),
188 getChild(i).getPropertyAsValue(Property::Solo, nullptr)));
189 }
190 return std::move(samples);
191 }
192
194 {
195 jassert(index >= 0 && (getNumSamples() == 0 || index < getNumSamples()));
196 sampleIndexValue = index;
197 }
198
199 // =============================================================================================
200 // ValueTree listener callbacks
201 // =============================================================================================
202
203 // Set up the lambda
204 void SamplesTree::valueTreeChildAdded(ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
205 {
206 int index = parentTree.indexOf(childWhichHasBeenAdded);
207 m_samples.add(std::make_unique<Sample>(childWhichHasBeenAdded, getUndoManager()));
208 if (sampleAdded)
209 {
210 jassert(childWhichHasBeenAdded.getType() == Type::Sample);
211 sampleAdded(*m_samples.getLast());
212 }
213 }
214
215 void SamplesTree::valueTreeChildRemoved(ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved,
216 int indexFromWhichChildWasRemoved)
217 {
218 if (sampleRemoved)
219 {
220 sampleRemoved(childWhichHasBeenRemoved, indexFromWhichChildWasRemoved);
221 }
222
223 m_samples.remove(indexFromWhichChildWasRemoved);
224 }
225
226 void SamplesTree::valueTreeParentChanged(ValueTree& treeWhoseParentHasChanged)
227 {
228 if (dropCompleted && treeWhoseParentHasChanged == m_parameterTree)
229 {
231 }
232 }
233
234 void SamplesTree::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged, const Identifier& property)
235 {
236 if (property == Property::SampleIndex && sampleIndexChanged)
237 {
238 sampleIndexChanged(treeWhosePropertyHasChanged[property]);
239 }
240 else if (property == Property::SelectedSample && selectedSampleChanged)
241 {
242 selectedSampleChanged(treeWhosePropertyHasChanged[property]);
243 }
244 }
245
246 // ======================================================================================
247 SamplesTree::Sample::Sample(ValueTree sampleTree, UndoManager* undoManager)
248 : valueTree(sampleTree), m_undoManager(undoManager),
249 muteValue(sampleTree, SamplesTree::Property::Mute, undoManager),
250 soloValue(sampleTree, SamplesTree::Property::Solo, undoManager),
251 pathValue(sampleTree, SamplesTree::Property::Path, undoManager),
252 nameValue(sampleTree, SamplesTree::Property::Name, undoManager)
253 {
254 valueTree.addListener(this);
255 }
256
257 void SamplesTree::Sample::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged,
258 const Identifier& property)
259 {
260 if (onChanged)
261 {
262 onChanged(property, treeWhosePropertyHasChanged.getProperty(property));
263 }
264
265 if (property == SamplesTree::Property::Mute)
266 {
267 if (muteChanged)
268 {
269 muteChanged();
270 }
271 }
272 else if (property == SamplesTree::Property::Solo)
273 {
274 if (soloChanged)
275 {
276 soloChanged();
277 }
278 }
279 }
280
281 Result SamplesTree::rewriteSamplePaths(ValueTree& engine, File newPath)
282 {
283 auto engineCustomParams = engine.getChildWithName(XmlType::Tag::customParams);
284
285 // Clear the legacy sample paths value; It doesn't need to be rewritten and will cause issues if left
286 auto deprecatedSamplePaths = engineCustomParams.getChildWithProperty(
288 if (deprecatedSamplePaths.isValid())
289 {
290 deprecatedSamplePaths.setProperty(XmlType::Property::value, juce::var(), nullptr);
291 }
292
293 auto samplesTree = engineCustomParams.getChildWithName(SamplesTree::Type::Samples);
294 if (samplesTree.hasType(SamplesTree::Type::Samples))
295 {
296 for (auto sample : samplesTree)
297 {
298 auto samplePath = sample.getProperty(SamplesTree::Property::Path).toString();
299 samplePath = utils::convertFilePathString(samplePath);
300
301 sample.setProperty(SamplesTree::Property::Path,
302 utils::StringsIntoPath(newPath.getFullPathName(), samplePath), nullptr);
303 }
304 return Result::ok();
305 }
306 else
307 {
308 jassertfalse;
309 const auto id = engine.getProperty(XmlType::Property::id).toString();
310 return Result::fail("Did not rewrite samples paths, no samples tree found in " + id);
311 }
312 }
313
314} // namespace krotos
Definition CustomParameterTree.h:15
void removeChild(const ValueTree &child)
Definition CustomParameterTree.cpp:52
UndoManager * getUndoManager() const
Definition CustomParameterTree.h:52
ValueTree m_parameterTree
Definition CustomParameterTree.h:113
const ValueTree & getParameterTree() const
Definition CustomParameterTree.h:55
const Identifier getType() const
Definition CustomParameterTree.h:58
ValueTree getChild(int index) const
Definition CustomParameterTree.cpp:54
void setUndoManager(UndoManager *um)
Definition CustomParameterTree.h:49
void addChild(const ValueTree &child, int index=-1)
Definition CustomParameterTree.cpp:47
ValueTree valueTree
Definition SampleDataTree.h:70
Sample(ValueTree tree, UndoManager *undoManager=nullptr)
Creates a Sample wrapper object.
Definition SampleDataTree.cpp:247
void valueTreePropertyChanged(ValueTree &treeWhosePropertyHasChanged, const Identifier &property) override
Definition SampleDataTree.cpp:257
A CustomParameterTree for managing a list of samples, with parameters for mute, solo,...
Definition SampleDataTree.h:9
void valueTreeParentChanged(ValueTree &treeWhoseParentHasChanged) override
Definition SampleDataTree.cpp:226
void setSampleIndex(int index)
Set the current index of the sample playback queue.
Definition SampleDataTree.cpp:193
std::function< void()> dropCompleted
Assign this to do something once a "drop" action has been completed. This is called internally by the...
Definition SampleDataTree.h:175
std::function< void(int sampleIndex)> sampleIndexChanged
Assign a lambda to this callback to do something when the sample_index property has been changed.
Definition SampleDataTree.h:181
void valueTreePropertyChanged(ValueTree &treeWhosePropertyHasChanged, const Identifier &property) override
Definition SampleDataTree.cpp:234
void valueTreeChildAdded(ValueTree &parentTree, ValueTree &childWhichHasBeenAdded) override
Definition SampleDataTree.cpp:204
static String rangeToString(const Range< int > &range)
Converts a range to a string in the format "start:end:length". Convert it back using stringToRange.
Definition SampleDataTree.cpp:165
std::function< void(Sample &newSample)> sampleAdded
Assign this to do something when a sample has been added to the tree.
Definition SampleDataTree.h:151
OwnedArray< Sample > m_samples
Definition SampleDataTree.h:283
static constexpr const char * m_rangeDelimiter
Definition SampleDataTree.h:284
CachedValue< int > sampleIndexValue
Get the current index of the sample playback queue.
Definition SampleDataTree.h:255
void valueTreeChildRemoved(ValueTree &parentTree, ValueTree &childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved) override
Definition SampleDataTree.cpp:215
OwnedArray< SampleBrowser::Sample > getSampleDataAsBrowserSamples()
Helper function to return an array of SampleBrowser::Sample objects populated from the SamplesTree....
Definition SampleDataTree.cpp:180
SamplesTree()
Creates a SamplesTree with a predefined type of Type::Samples.
Definition SampleDataTree.cpp:14
void removeSample(const String &name)
Removes a sample from the tree by name.
Definition SampleDataTree.cpp:116
CachedValue< int > selectedSampleValue
The index of the last sample to be selected by the user.
Definition SampleDataTree.h:260
void setSampleSelections(const std::vector< Range< int > > &selections)
Set the selection ranges for all samples in the tree. The index of the selection range in the vector ...
Definition SampleDataTree.cpp:157
void updateSample(const String &name, const String &path)
Updates a sample in the tree with a new name and path. If the sample exists, it's name and path will ...
Definition SampleDataTree.cpp:85
const int getNumSamples() const
Returns the number of samples in the samples tree.
Definition SampleDataTree.h:145
void addSample(const String &name, const String &path, bool isMuted)
Add a new sample into the tree.
Definition SampleDataTree.cpp:98
void setSampleSelection(int index, const Range< int > selection)
Set the selection range for a sample by index.
Definition SampleDataTree.cpp:134
static Range< int > stringToRange(const String &rangeString)
Converts a string to a range. The expected string is one generated from rangeToString....
Definition SampleDataTree.cpp:171
std::vector< Range< int > > getSampleSelections() const
Get the selection ranges for all samples in the tree. The index of the selection range in the vector ...
Definition SampleDataTree.cpp:145
std::function< void(ValueTree &childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved)> sampleRemoved
Assign this to do something when a sample has been removed from the tree.
Definition SampleDataTree.h:155
const StringArray getFiles() const
Compatibility method for KrotosSampleOscillatorSound which requires samples to be loaded all at once ...
Definition SampleDataTree.cpp:122
Range< int > getSampleSelection(int index) const
Get the selection range for a sample by index.
Definition SampleDataTree.cpp:140
static Result rewriteSamplePaths(ValueTree &engineKwidgetTree, File newAbsolutePath)
Rewrite relative SamplePath path values created by the PresetBundler into absolute ones of a given pa...
Definition SampleDataTree.cpp:281
std::function< void(int sampleIndex)> selectedSampleChanged
Assign a lambda to this callback to do something when the selected_sample property has been changed.
Definition SampleDataTree.h:187
SamplesTree & operator=(const SamplesTree &other)
Copy assignment operator.
Definition SampleDataTree.cpp:68
String convertFilePathString(const String &inputPath)
Converts a file path passed as a raw string, to the current platform. Has no effect if the path is al...
Definition helpers.cpp:129
String StringsIntoPath(Args... args)
Joins multiple string arguments into a path string.
Definition helpers.h:25
Definition AirAbsorptionFilter.cpp:2
static const String SamplePaths
Definition Kwidget_CoreEngine.h:66
Wrapper object for sample data used by the browser. The Value objects are shared reference objects wh...
Definition SampleBrowser.h:18
Definition SampleDataTree.h:18
static const Identifier Path
Definition SampleDataTree.h:20
static const Identifier Selection
Definition SampleDataTree.h:21
static const Identifier SelectedSample
Definition SampleDataTree.h:28
static const Identifier SampleIndex
Definition SampleDataTree.h:26
static const Identifier Name
Definition SampleDataTree.h:19
static const Identifier Solo
Definition SampleDataTree.h:23
static const Identifier Mute
Definition SampleDataTree.h:22
Definition SampleDataTree.h:12
static const Identifier Sample
Definition SampleDataTree.h:14
static const Identifier Samples
Definition SampleDataTree.h:13
static const Identifier id
Definition XmlType.h:41
static const Identifier value
Definition XmlType.h:42
static const Identifier customParams
Definition XmlType.h:20