[go: up one dir, main page]

Menu

[r19]: / trunk / Scale.cpp  Maximize  Restore  History

Download this file

310 lines (284 with data), 8.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyleft 2013 Chris Korda
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or any later version.
/*
chris korda
revision history:
rev date comments
00 23aug13 initial version
01 18mar15 move find into base class
02 19mar15 in FindNearest and FindLeastInterval, assert if empty
03 04apr15 add interval names
04 12jun15 in IntervalNames, reverse difference
05 31aug15 overload HarmonizeNote to constrain to chord tones
06 03dec15 in HarmonizeNote, for harmony below, reverse cycle
07 17dec15 in HarmonizeNote, if interval is octave, allow octaves
scale container
*/
#include "stdafx.h"
#include "Scale.h"
#include "RandList.h"
int CScale::GetTonality() const
{
int third = abs(GetAt(0).LeastInterval(GetAt(2)));
return(third == Eb ? CNote::MINOR : CNote::MAJOR);
}
void CScale::CopyInts(const int *pScale, int nCount)
{
SetSize(nCount);
for (int iNote = 0; iNote < nCount; iNote++)
SetAt(iNote, pScale[iNote]);
}
bool CScale::operator==(const CScale& Scale) const
{
if (Scale.m_Size != m_Size)
return(FALSE);
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
if (GetAt(iNote) != Scale[iNote])
return(FALSE);
}
return(TRUE);
}
bool CScale::operator<(const CScale& Scale) const
{
int notes = min(m_Size, Scale.m_Size);
for (int iNote = 0; iNote < notes; iNote++) {
if (GetAt(iNote) < Scale[iNote])
return(TRUE);
if (GetAt(iNote) > Scale[iNote])
return(FALSE);
}
return(m_Size < Scale.m_Size);
}
void CScale::Print(CNote Key, int Tonality) const
{
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++)
_tprintf(_T("%s "), GetAt(iNote).Name(Key, Tonality));
fputc('\n', stdout);
}
void CScale::PrintMidi(CNote Key, int Tonality) const
{
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++)
_tprintf(_T("%s "), GetAt(iNote).MidiName(Key, Tonality));
fputc('\n', stdout);
}
CString CScale::NoteNames(CNote Key, int Tonality) const
{
CString s;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
if (iNote)
s += ' ';
s += GetAt(iNote).Name(Key, Tonality);
}
return(s);
}
CString CScale::NoteMidiNames(CNote Key, int Tonality) const
{
CString s;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
if (iNote)
s += ' ';
s += GetAt(iNote).MidiName(Key, Tonality);
}
return(s);
}
CString CScale::IntervalNames(CNote Key, int Tonality) const
{
CString s;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
if (iNote)
s += ' ';
s += GetAt(iNote).IntervalName(Key, Tonality);
}
return(s);
}
void CScale::Normalize()
{
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++)
GetAt(iNote).Normalize();
}
int CScale::FindNormal(CNote Note) const
{
Note.Normalize();
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
if (GetAt(iNote).Normal() == Note) // if note was found
return(iNote);
}
return(-1); // fail
}
int CScale::FindNearest(CNote Note) const
{
ASSERT(!IsEmpty()); // scale can't be empty
int iNearest = 0;
int MinDelta = INT_MAX;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
int delta = abs(Note - GetAt(iNote)); // compute delta
if (!delta) // if exact match
return(iNote);
if (delta < MinDelta) { // if nearer than previous candidate
iNearest = iNote;
MinDelta = delta;
}
}
return(iNearest);
}
int CScale::FindLeastInterval(CNote Note) const
{
ASSERT(!IsEmpty()); // scale can't be empty
Note.Normalize();
int iNearest = 0;
int MinDelta = INT_MAX;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
int delta = abs(GetAt(iNote).Normal().LeastInterval(Note));
if (!delta) // if exact match
return(iNote);
if (delta < MinDelta) { // if nearer than previous candidate
iNearest = iNote;
MinDelta = delta;
}
}
return(iNearest);
}
int CScale::FindLeastInterval(CNote Note, CNote Exclude) const
{
ASSERT(!IsEmpty()); // scale can't be empty
Note.Normalize();
int iNearest = 0;
int MinDelta = INT_MAX;
int notes = m_Size;
for (int iNote = 0; iNote < notes; iNote++) {
CNote TryNote(GetAt(iNote).Normal());
if (TryNote == Exclude) // if candidate is excluded
continue; // skip it
int delta = abs(TryNote.LeastInterval(Note));
if (!delta) // if exact match
return(iNote);
if (delta < MinDelta) { // if nearer than previous candidate
iNearest = iNote;
MinDelta = delta;
}
}
return(iNearest);
}
int CScale::FindNearestSmooth(CNote Note) const
{
int deviation;
int iNote = FindNearest(Note, deviation);
int size = GetSize();
if (deviation > 0) {
int iPrev = iNote - 1;
if (iPrev < 0)
iPrev = size - 1;
int iNext = iNote + 1;
if (iNext >= size)
iNext = 0;
int iNext2 = iNext + 1;
if (iNext2 >= size)
iNext2 = 0;
int DeltaNext = abs(Note.LeastInterval(GetAt(iNext2)));
int DeltaPrev = abs(Note.LeastInterval(GetAt(iPrev)));
if (DeltaNext < DeltaPrev)
iNote = iNext;
}
return(iNote);
}
void CScale::Intersection(const CScale& Scale)
{
// reverse iterate for deletion stability
int notes = m_Size;
for (int iNote = notes - 1; iNote >= 0; iNote--) { // for each of our notes
int iDegree = Scale.FindNormal(GetAt(iNote)); // find note in argument
if (iDegree < 0) // if note not found
RemoveAt(iNote); // remove note
}
}
void CScale::Difference(const CScale& Scale)
{
int notes = Scale.m_Size;
for (int iNote = 0; iNote < notes; iNote++) { // for each of argument notes
int iDegree = FindNormal(Scale[iNote]); // find note
if (iDegree >= 0) // if note was found
RemoveAt(iDegree); // remove note
}
}
int CScale::HarmonizeDegree(int Degree, int Interval) const
{
int HarmDegree = CNote::Mod(Degree + Interval, m_Size);
int delta = GetAt(HarmDegree) - GetAt(Degree);
if (Interval > 0) { // if harmonizing above
if (delta < 0)
delta = delta + OCTAVE;
} else { // harmonizing below
if (delta > 0)
delta = delta - OCTAVE;
}
return(delta + Interval / m_Size * OCTAVE);
}
CNote CScale::HarmonizeNote(CNote Note, int Interval) const
{
int iNote = FindNearest(Note.Normal());
return(Note + HarmonizeDegree(iNote, Interval));
}
CNote CScale::HarmonizeNote(CNote Note, int Interval, int ChordInfo) const
{
// This method optionally constrains the harmony to a chord. The low word of
// ChordInfo is the chord's size, in notes; if it's zero, the harmony isn't
// constrained. The high word of ChordInfo is the chord's diatonic degree,
// in cycle of thirds steps from the root: 0=1, 1=3, 2=5, 3=7, 4=2, 5=4, 6=6.
CNote InNote(Note.Normal());
int iNote = FindNearest(InNote);
CNote HarmNote(Note + HarmonizeDegree(iNote, Interval));
int ChordSize = LOWORD(ChordInfo);
if (!ChordSize) // if unconstrained
return(HarmNote);
int ChordDegree = HIWORD(ChordInfo);
CScale ch;
ch.SetSize(ChordSize);
int nSize = GetSize();
if (Interval >= 0) { // if harmony above melody
for (int i = 0; i < ChordSize; i++) // for each chord tone
ch[i] = GetAt(CNote::Mod((ChordDegree + i) * 2, nSize)); // cycle of thirds
} else { // harmony below melody
// reverse cycle so ties resolve upward; prevents most crossovers
for (int i = 0; i < ChordSize; i++) // for each chord tone
ch[ChordSize - 1 - i] = GetAt(CNote::Mod((ChordDegree + i) * 2, nSize)); // cycle of thirds
}
int iNearest;
if (!(Interval % 7)) // if nominal interval is octave or multiple of one
iNearest = ch.FindLeastInterval(HarmNote); // allow octave harmony
else // nominal interval isn't octave
iNearest = ch.FindLeastInterval(HarmNote, InNote); // exclude input note
CNote NearestNote(ch[iNearest]);
NearestNote.ShiftToNearestOctave(HarmNote);
return(NearestNote);
}
void CScale::Randomize()
{
if (m_Size > 1) {
int notes = m_Size;
int PrevLastNote = GetAt(notes - 1);
for (int iNote = notes - 1; iNote > 0; iNote--) {
int iRand = CRandList::Rand(iNote + 1);
int tmp = GetAt(iRand);
GetAt(iRand) = GetAt(iNote);
GetAt(iNote) = tmp;
}
if (GetAt(0) == PrevLastNote) {
int iRand = CRandList::Rand(notes - 1) + 1;
int tmp = GetAt(iRand);
GetAt(iRand) = GetAt(0);
GetAt(0) = tmp;
}
}
}