Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
chord_descriptor.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2018-2022 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
10#ifndef __AUDIO_CHORD_DESCRIPTOR_H__
11#define __AUDIO_CHORD_DESCRIPTOR_H__
12
13#include <cstdint>
14
15#include "dsp/position.h"
16#include "utils/yaml.h"
17
24#define CHORD_DESCRIPTOR_SCHEMA_VERSION 2
25
26#define CHORD_DESCRIPTOR_MAX_NOTES 48
27
28enum class MusicalNote
29{
30 NOTE_C = 0,
31 NOTE_CS,
32 NOTE_D,
33 NOTE_DS,
34 NOTE_E,
35 NOTE_F,
36 NOTE_FS,
37 NOTE_G,
38 NOTE_GS,
39 NOTE_A,
40 NOTE_AS,
41 NOTE_B
42};
43
47enum class ChordType
48{
49 CHORD_TYPE_NONE,
50 CHORD_TYPE_MAJ,
51 CHORD_TYPE_MIN,
52 CHORD_TYPE_DIM,
53 CHORD_TYPE_SUS4,
54 CHORD_TYPE_SUS2,
55 CHORD_TYPE_AUG,
56 CHORD_TYPE_CUSTOM,
57};
58
62enum class ChordAccent
63{
64 CHORD_ACC_NONE,
70 /* NOTE: all accents below assume 7 */
85};
86
93typedef struct ChordDescriptor
94{
97
99 MusicalNote root_note;
100
102 MusicalNote bass_note;
103
106
113
122 int notes[CHORD_DESCRIPTOR_MAX_NOTES];
123
131
137 MusicalNote root,
138 int has_bass,
139 MusicalNote bass,
140 ChordType type,
141 ChordAccent accent,
142 int inversion);
143
144static inline int
145chord_descriptor_get_max_inversion (const ChordDescriptor * const self)
146{
147 int max_inv = 2;
148 switch (self->accent)
149 {
150 case ChordAccent::CHORD_ACC_NONE:
151 break;
158 max_inv = 3;
159 break;
163 max_inv = 4;
164 break;
165 default:
166 break;
167 }
168
169 return max_inv;
170}
171
172static inline int
173chord_descriptor_get_min_inversion (const ChordDescriptor * const self)
174{
175 return -chord_descriptor_get_max_inversion (self);
176}
177
178static inline int
179chord_descriptor_are_notes_equal (int * notes_a, int * notes_b)
180{
181 /* 36 notes in Chord */
182 for (int i = 0; i < 36; i++)
183 {
184 if (notes_a[i] != notes_b[i])
185 return 0;
186 }
187 return 1;
188}
189
190static inline int
191chord_descriptor_is_equal (ChordDescriptor * a, ChordDescriptor * b)
192{
193 return a->has_bass == b->has_bass && a->root_note == b->root_note
194 && a->bass_note == b->bass_note && a->type == b->type
195 && chord_descriptor_are_notes_equal (a->notes, b->notes)
196 && a->inversion == b->inversion;
197}
198
205bool
207
214bool
216
222
223void
224chord_descriptor_copy (ChordDescriptor * dest, const ChordDescriptor * src);
225
229const char *
231
235const char *
237
241const char *
243
249char *
251
255NONNULL void
256chord_descriptor_to_string (const ChordDescriptor * chord, char * str);
257
262NONNULL void
264
268NONNULL void
270
275#endif
bool chord_descriptor_is_key_in_chord(ChordDescriptor *chord, MusicalNote key)
Returns if the given key is in the chord represented by the given ChordDescriptor.
ChordAccent
Chord accents.
ChordDescriptor * chord_descriptor_clone(const ChordDescriptor *src)
Clones the given ChordDescriptor.
const char * chord_descriptor_note_to_string(MusicalNote note)
Returns the musical note as a string (eg.
NONNULL void chord_descriptor_to_string(const ChordDescriptor *chord, char *str)
Returns the chord in human readable string.
const char * chord_descriptor_chord_accent_to_string(ChordAccent accent)
Returns the chord accent as a string (eg.
ChordDescriptor * chord_descriptor_new(MusicalNote root, int has_bass, MusicalNote bass, ChordType type, ChordAccent accent, int inversion)
Creates a ChordDescriptor.
char * chord_descriptor_to_new_string(const ChordDescriptor *chord)
Returns the chord in human readable string.
const char * chord_descriptor_chord_type_to_string(ChordType type)
Returns the chord type as a string (eg.
NONNULL void chord_descriptor_update_notes(ChordDescriptor *self)
Updates the notes array based on the current settings.
NONNULL void chord_descriptor_free(ChordDescriptor *self)
Frees the ChordDescriptor.
bool chord_descriptor_is_key_bass(ChordDescriptor *chord, MusicalNote key)
Returns if key is the bass or root note of chord.
ChordType
Chord type.
@ CHORD_ACC_S9
15 semitones.
@ CHORD_ACC_S5_b13
8 and 16 semitones.
@ CHORD_ACC_b9
13 semitones.
@ CHORD_ACC_b5_S11
6 and 18 semitones.
@ CHORD_ACC_11
17 semitones.
@ CHORD_ACC_9
14 semitones.
@ CHORD_ACC_7
b7 is 10 semitones from chord root, or 9 if the chord is diminished.
@ CHORD_ACC_j7
Maj7 is 11 semitones from the root.
@ CHORD_ACC_6_13
9 and 21 semitones.
Position struct and API.
A ChordDescriptor describes a chord and is not linked to any specific object by itself.
int notes[CHORD_DESCRIPTOR_MAX_NOTES]
Only used if custom chord.
ChordAccent accent
Chord accent.
MusicalNote bass_note
Bass note 1 octave below.
bool has_bass
Has bass note or not.
int inversion
0 no inversion, less than 0 highest note(s) drop an octave, greater than 0 lowest note(s) receive an ...
MusicalNote root_note
Root note.
ChordType type
Chord type.
YAML utils.