tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Musicality.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14class Chord
15{
16public:
17 enum ChordType
18 {
19 customChord = -2,
20 invalidChord = -1,
21 majorTriad = 0,
22 minorTriad,
23 diminishedTriad,
24 augmentedTriad,
25 majorSixthChord,
26 minorSixthChord,
27 dominatSeventhChord,
28 majorSeventhChord,
29 minorSeventhChord,
30 augmentedSeventhChord,
31 diminishedSeventhChord,
32 halfDiminishedSeventhChord,
33 minorMajorSeventhChord,
34 suspendedSecond,
35 suspendedFourth,
36 powerChord,
37 majorNinthChord,
38 dominantNinthChord,
39 minorMajorNinthChord,
40 minorDominantNinthChord,
41 augmentedMajorNinthChord,
42 augmentedDominantNinthChord,
43 halfDiminishedNinthChord,
44 halfDiminishedMinorNinthChord,
45 diminishedNinthChord,
46 diminishedMinorNinthChord,
47 // Update getAllChordType after adding / removing a chord
48 };
49
50 Chord (ChordType type = majorTriad);
51 Chord (juce::Array<int> steps, juce::String symbol); // creates a custom chord with steps
52
53 juce::String toString();
54 static Chord fromString (const juce::String&);
55
56 static juce::Array<ChordType> getAllChordType();
57
58 ChordType getType() const { return type; }
59 bool isValid() const { return type != invalidChord; }
60
61 juce::String getName() const;
62 juce::String getShortName() const;
63 juce::String getSymbol() const;
64 juce::Array<int> getSteps() const;
65 juce::Array<int> getSteps (int inversion) const;
66
67private:
68 ChordType type;
69 juce::Array<int> steps; // for custom chords
70 juce::String symbol; // for custom chords
71};
72
73//==============================================================================
74class Scale
75{
76public:
77 enum ScaleType
78 {
79 major = 0,
80 minor,
81 ionian,
82 dorian,
83 phrygian,
84 lydian,
85 mixolydian,
86 aeolian,
87 locrian,
88 melodicMinor,
89 harmonicMinor,
90 };
91
92 enum Steps
93 {
94 Whole = 0,
95 Half,
96 WholeHalf,
97 };
98
99 enum class Intervals
100 {
101 i = 0,
102 ii,
103 iii,
104 iv,
105 v,
106 vi,
107 vii
108 };
109
110 Scale (ScaleType type = major);
111
112 ScaleType getType() const { return type; }
113
114 juce::String getName() const;
115 juce::String getShortName() const;
116
117 static juce::StringArray getIntervalNames();
118
119 static juce::Array<ScaleType> getAllScaleTypes();
120 static juce::StringArray getScaleStrings();
121 static juce::String getNameForType (ScaleType type);
122 static juce::String getShortNameForType (ScaleType type);
123 static ScaleType getTypeFromName (juce::String name);
124
125 juce::Array<int> getSteps (int octaves = 1) const;
126
127 juce::String getIntervalName (Intervals interval) const;
128
129 juce::Array<Chord> getTriads() const { return triads; }
130 juce::Array<Chord> getSixths() const { return sixths; }
131 juce::Array<Chord> getSevenths() const { return sevenths; }
132
133private:
134 juce::Array<Chord> generateTriads (int offset) const;
135 juce::Array<Chord> generateSixths (int offset) const;
136 juce::Array<Chord> generateSevenths (int offset) const;
137
138 ScaleType type;
139 juce::Array<Steps> steps;
140 juce::Array<Chord> triads;
141 juce::Array<Chord> sixths;
142 juce::Array<Chord> sevenths;
143};
144
145//==============================================================================
147{
148public:
149 enum class Mode
150 {
151 off,
152 arpeggio,
153 chords,
154 bass,
155 melody
156 };
157
158 static const int scaleRootGlobalTrack;
159 static const int scaleRootChordTrack;
160
161 //==============================================================================
163 ~PatternGenerator() override;
164
165 //==============================================================================
166 BeatDuration getMinimumChordLength() const;
167 BeatDuration getMaximumChordLength() const;
168
169 void validateChordLengths();
170
171 //==============================================================================
173 {
174 ProgressionItem (PatternGenerator&, const juce::ValueTree&, bool temporary = false);
175 ~ProgressionItem() noexcept;
176
177 bool operator== (const ProgressionItem&) const noexcept;
178 bool isValid() { return chordName.get().isNotEmpty(); }
179
180 PatternGenerator& generator;
181 juce::ValueTree state;
182
183 juce::CachedValue<juce::String> chordName; // Chord name is simplified, use getChordName() to get unsimplified
184 juce::CachedValue<juce::String> pitches; // A comma seperated list of pitches used for custom chords, otherwise empty
186 juce::CachedValue<int> octave, inversion;
187
188 void setChordName (juce::String chord);
189 void setChordName (juce::String chord, juce::String pitches);
190
191 juce::String getChordName() const;
192
193 void setRoot (int root);
194 void setChord (int root, Chord::ChordType);
195
196 bool isRomanNumeral() const; // Is this chord stored in it's roman numberal format
197
198 Chord getChord (const Scale&) const;
199 juce::String getChordSymbol(); // Gets the chords name as Cmaj, Dmin, etc based on key
200
201 int getRootNote (int key, const Scale& scale);
202
203 private:
205 };
206
207 //==============================================================================
208
209 juce::StringArray getPossibleTriadNames() const;
210 juce::StringArray getPossibleSeventhNames() const;
211
212 int getChordProgressionLength() const;
213 const juce::Array<ProgressionItem*>& getChordProgression() const noexcept;
214 void setChordProgression (juce::ValueTree v);
215
216 juce::StringArray getChordProgressionChordNames (bool simplified) const;
217
220 void setChordProgressionFromChordNames (juce::StringArray progression);
221
222 void removeIndexFromProgression (int idx);
223 void removeRangeFromProgression (int start, int end);
224 void clearProgression();
225 void insertChordIntoProgression (int idx, juce::String chordName);
226 void insertChordIntoProgression (int idx, juce::String chordName, juce::String pitches);
227 void moveChordInProgression (int srcIdx, int dstIdx);
228 void duplicateChordInProgression (int idx);
229
230 void playGuideChord (int idx) const;
231
232 //==============================================================================
233 enum NoteType
234 {
235 ChordInKeyNote,
236 ChordNotInKeyNote,
237 InKeyNote,
238 NotInKeyNote
239 };
240
241 NoteType getTypeForNote (const MidiClip&, const MidiNote&);
242
243 //==============================================================================
244 juce::String formatChordName (juce::String simplifiedChordName) const;
245 juce::StringArray getArpStyles();
246
247 Clip& clip;
248 juce::ValueTree state;
251 juce::CachedValue<bool> autoUpdate, arpUpDown, arpPlayRoot, allNotes, octaveUp, octaveDown, spread;
252 juce::CachedValue<float> arpPatternLength, velocity, gate;
253 juce::CachedValue<BeatDuration> melodyNoteLength;
255 juce::CachedValue<int> scaleRoot, arpSteps, octave;
257
258 juce::ValueTree getChordPattern();
259 juce::ValueTree getBassPattern();
260
261 void setChordPattern (juce::ValueTree pattern);
262 void setBassPattern (juce::ValueTree pattern);
263
264 void generatePattern();
265
266 Scale getScaleAtBeat (BeatPosition) const;
267 int getNoteAtBeat (BeatPosition) const;
268
269 bool getAutoUpdate();
270 void setAutoUpdate (bool on);
271 void refreshPatternIfNeeded();
272
273 void editFinishedLoading();
274
275 BeatDuration getFlattenedChordProgression (juce::OwnedArray<ProgressionItem>& progression, bool globalTime = false);
276
277private:
278 const int maxChords = 64;
279
280 struct ProgressionList;
281 std::unique_ptr<ProgressionList> progressionList;
282
283 void valueTreeChanged() override {}
284 void valueTreePropertyChanged (juce::ValueTree&, const juce::Identifier&) override;
285
286 ChordClip* getChordClipAt (TimePosition) const;
287
288 MidiNote* addNote (MidiList& sequence, int pitch, BeatPosition startBeat, BeatDuration lengthInBeats,
289 int velocity, int colourIndex, juce::UndoManager*);
290
291 void clearPattern();
292 void generateArpPattern();
293 void generateChordPattern();
294 void generateBassPattern();
295 void generateMelodyPattern();
296
297 void updateHash();
298 void clearHash();
299 HashCode hashNotes (MidiList&, int version);
300
301 MidiClip* getMidiClip() const;
302
303 //==============================================================================
304 std::unique_ptr<juce::Timer> editLoadedCallback;
305
306 //==============================================================================
307 struct AutoUpdateManager;
308 std::unique_ptr<AutoUpdateManager> autoUpdateManager;
309
311};
312
313//==============================================================================
315{
316 KeyResult() = default;
317 KeyResult (double r_, int k_, Scale::ScaleType s_) : r (r_), key (k_), scale (s_) {}
318
319 double r = 0; // correlation coefficient
320 int key = 0; // midi note = 0 - 11
321 Scale::ScaleType scale = Scale::major; // major or minor
322
323 bool operator< (const KeyResult& other) const { return r < other.r; }
324};
325
326juce::Array<KeyResult> determineKeyOfNotes (const juce::Array<MidiNote*>& notes);
327
328}} // namespace tracktion { inline namespace engine
329
330namespace juce
331{
332 template <>
333 struct VariantConverter<tracktion::engine::Chord::ChordType>
334 {
335 static tracktion::engine::Chord::ChordType fromVar (const var& v) { return (tracktion::engine::Chord::ChordType) static_cast<int> (v); }
336 static var toVar (tracktion::engine::Chord::ChordType v) { return static_cast<int> (v); }
337 };
338
339
340 template <>
341 struct VariantConverter<tracktion::engine::Scale::ScaleType>
342 {
343 static tracktion::engine::Scale::ScaleType fromVar (const var& v) { return (tracktion::engine::Scale::ScaleType) static_cast<int> (v); }
344 static var toVar (tracktion::engine::Scale::ScaleType v) { return static_cast<int> (v); }
345 };
346
347 template <>
348 struct VariantConverter<tracktion::engine::PatternGenerator::Mode>
349 {
350 static tracktion::engine::PatternGenerator::Mode fromVar (const var& v) { return (tracktion::engine::PatternGenerator::Mode) static_cast<int> (v); }
351 static var toVar (tracktion::engine::PatternGenerator::Mode v) { return static_cast<int> (v); }
352 };
353}
Type get() const noexcept
A clip in an edit.
void setChordProgressionFromChordNames(juce::StringArray progression)
Sets a chord progression using chord roman numerals.
#define JUCE_LEAK_DETECTOR(OwnerClass)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Represents a duration in beats.
Represents a position in beats.
Represents a position in real-life time.