Repository navigation
Expand file tree
/
Copy pathmidipp_chords.h
More file actions
192 lines (185 loc) · 5.44 KB
/
Copy pathmidipp_chords.h
File metadata and controls
192 lines (185 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*-
* Copyright (c) 2018-2019 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _MIDIPP_CHORDS_H_
#define _MIDIPP_CHORDS_H_
#include "midipp.h"
struct MppChord {
uint32_t data[(MPP_MAX_CHORD_BANDS + 1 + 31) / 32];
void zero() { memset(data, 0, sizeof(data)); };
void set(size_t x) { data[x / 32] |= 1U << (x % 32); }
void clr(size_t x) { data[x / 32] &= ~(1U << (x % 32)); };
void tog(size_t x) { data[x / 32] ^= 1U << (x % 32); };
bool test(size_t x) const { return ((data[x / 32] >> (x % 32)) & 1); };
uint32_t order() {
uint32_t retval = 0;
for (int x = 0; x != MPP_MAX_CHORD_BANDS; x++) {
if (test(x))
retval++;
}
return (retval);
};
void inc(int step) {
int x;
int y = 0;
for (x = y = 0; x != MPP_MAX_CHORD_BANDS; x += step) {
if (test(x))
break;
}
for (; x != MPP_MAX_CHORD_BANDS && test(x); x += step) {
tog(x);
y += step;
}
if (x != MPP_MAX_CHORD_BANDS) {
tog(x);
y -= step;
}
/* preserve number of set bits */
for (x = 0; x != y; x += step)
set(x);
};
void dec(int step) {
int x;
int y = 0;
for (x = y = 0; x != MPP_MAX_CHORD_BANDS; x += step) {
if (test(x) == 0)
break;
}
for (; x != MPP_MAX_CHORD_BANDS && test(x) == 0; x += step) {
tog(x);
y += step;
}
if (x != MPP_MAX_CHORD_BANDS) {
tog(x);
y -= step;
}
/* preserve number of set bits */
for (x = 0; x != y; x += step)
clr(x);
};
void inv() {
int x = sizeof(data) / sizeof(data[0]);
while (x--)
data[x] ^= -1U;
};
void shr() {
int x = sizeof(data) / sizeof(data[0]);
int rem = 0;
while (x--) {
int next = (data[x] & 1) << 31;
data[x] = (data[x] >> 1) | rem;
rem = next;
}
};
void shl() {
int rem = 0;
for (int x = 0; x != sizeof(data) / sizeof(data[0]); x++) {
int next = (data[x] >> 31) & 1;
data[x] = (data[x] << 1) | rem;
rem = next;
}
};
int last_set() {
int rem = MPP_MAX_CHORD_BANDS;
while (rem--) {
if (test(rem))
return (rem);
}
return (-1);
};
void operator &=(const MppChord &other) {
int x = sizeof(data) / sizeof(data[0]);
while (x--)
data[x] &= other.data[x];
};
void operator |=(const MppChord &other) {
int x = sizeof(data) / sizeof(data[0]);
while (x--)
data[x] |= other.data[x];
};
bool operator <(const MppChord &other) const {
int x = sizeof(data) / sizeof(data[0]);
while (x--) {
if (data[x] < other.data[x])
return (1);
else if (data[x] > other.data[x])
return (0);
}
return (0);
};
bool operator >(const MppChord &other) const {
int x = sizeof(data) / sizeof(data[0]);
while (x--) {
if (data[x] > other.data[x])
return (1);
else if (data[x] < other.data[x])
return (0);
}
return (0);
};
bool operator ==(const MppChord &other) const {
int x = sizeof(data) / sizeof(data[0]);
while (x--) {
if (data[x] != other.data[x])
return (0);
}
return (1);
};
bool operator !=(const MppChord &other) const {
int x = sizeof(data) / sizeof(data[0]);
while (x--) {
if (data[x] != other.data[x])
return (1);
}
return (0);
};
void operator =(const MppChord &other) {
if (this == &other)
return;
int x = sizeof(data) / sizeof(data[0]);
while (x--) {
data[x] = other.data[x];
}
};
};
class MppScoreVariant {
public:
MppScoreVariant(uint32_t _chord, const char *a, const char *b = 0,
const char *c = 0, const char *d = 0);
~MppScoreVariant() { };
const char * pattern[4];
MppChord_t footprint[1];
uint32_t rots[1];
};
extern void MppRolUpChord(MppChord_t &input, int &delta);
extern void MppRolDownChord(MppChord_t &input, int &delta);
extern void MppNextChordRoot(MppChord_t &input, int step = 1);
extern void MppPrevChordRoot(MppChord_t &input, int step = 1);
extern MppChord_t MppFindChordRoot(MppChord_t, uint32_t * = 0, uint32_t * = 0);
extern int MppIsChord(QString &);
extern void MppChordToStringGeneric(MppChord_t mask, uint32_t rem, uint32_t bass, uint32_t is_chord, uint32_t step, QString &retval);
extern void MppStringToChordGeneric(MppChord_t &mask, uint32_t &rem, uint32_t &bass, uint32_t step, const QString &str);
extern const QString MppKeyToStringGeneric(int key, int sharp);
extern void MppStepChordGeneric(QString &str, int adjust, uint32_t sharp);
#endif /* _MIDIPP_CHORDS_H_ */