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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
chord.cc -- implement Chord
source file of the GNU LilyPond music typesetter
(c) 1999--2003 Jan Nieuwenhuizen <janneke@gnu.org>
*/
#include "chord.hh"
#include "event.hh"
#include "warn.hh"
#include "music-list.hh"
#include "event.hh"
SCM
Chord::base_pitches (SCM tonic)
{
SCM base = SCM_EOL;
SCM major = Pitch (0, 2, 0).smobbed_copy ();
SCM minor = Pitch (0, 2, -1).smobbed_copy ();
base = gh_cons (tonic, base);
base = gh_cons (ly_pitch_transpose (ly_car (base), major), base);
base = gh_cons (ly_pitch_transpose (ly_car (base), minor), base);
return scm_reverse_x (base, SCM_EOL);
}
SCM
Chord::transpose_pitches (SCM tonic, SCM pitches)
{
/* map?
hoe doe je lambda in C?
*/
SCM transposed = SCM_EOL;
for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
{
transposed = gh_cons (ly_pitch_transpose (tonic, ly_car (i)),
transposed);
}
return scm_reverse_x (transposed, SCM_EOL);
}
/*
burp, in SCM duw je gewoon een (if (= (step x) 7) (...)) door pitches
Lower step STEP.
If step == 0, lower all.
*/
SCM
Chord::lower_step (SCM tonic, SCM pitches, SCM step)
{
SCM lowered = SCM_EOL;
for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
{
SCM p = ly_car (i);
if (gh_equal_p (step_scm (tonic, ly_car (i)), step)
|| gh_scm2int (step) == 0)
{
p = ly_pitch_transpose (p, Pitch (0, 0, -1).smobbed_copy ());
}
lowered = gh_cons (p, lowered);
}
return scm_reverse_x (lowered, SCM_EOL);
}
/* Return member that has same notename, disregarding octave or alterations */
SCM
Chord::member_notename (SCM p, SCM pitches)
{
/* If there's an exact match, make sure to return that */
SCM member = gh_member (p, pitches);
if (member == SCM_BOOL_F)
{
for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
{
/*
Urg, eindelijk gevonden: () != #f, kan maar niet aan wennen.
Anders kon iets korter...
*/
if (unsmob_pitch (p)->notename_
== unsmob_pitch (ly_car (i))->notename_)
{
member = ly_car (i);
break;
}
}
}
else
member = ly_car (member);
return member;
}
/* Return member that has same notename and alteration, disregarding octave */
SCM
Chord::member_pitch (SCM p, SCM pitches)
{
/* If there's an exact match, make sure to return that */
SCM member = gh_member (p, pitches);
if (member == SCM_BOOL_F)
{
for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
{
if (unsmob_pitch (p)->notename_
== unsmob_pitch (ly_car (i))->notename_
&& unsmob_pitch (p)->alteration_
== unsmob_pitch (ly_car (i))->alteration_)
{
member = ly_car (i);
break;
}
}
}
else
member = ly_car (member);
return member;
}
SCM
Chord::step_scm (SCM tonic, SCM p)
{
/* De Pitch intervaas is nog beetje sleutelgat? */
int i = unsmob_pitch (p)->notename_
- unsmob_pitch (tonic)->notename_
+ (unsmob_pitch (p)->octave_
- unsmob_pitch (tonic)->octave_) * 7;
while (i < 0)
i += 7;
i++;
return scm_int2num (i);
}
/*
Assuming that PITCHES is a chord, with tonic (CAR PITCHES), find
missing thirds, only considering notenames. Eg, for
PITCHES = c gis d'
return
MISSING = e b'
*/
SCM
Chord::missing_thirds (SCM pitches)
{
SCM thirds = SCM_EOL;
/* is the third c-e, d-f, etc. small or large? */
int minormajor_a[] = {0, -1, -1, 0, 0, -1, -1};
for (int i=0; i < 7; i++)
thirds = gh_cons (Pitch (0, 2, minormajor_a[i]).smobbed_copy (),
thirds);
thirds = scm_vector (scm_reverse_x (thirds, SCM_EOL));
SCM tonic = ly_car (pitches);
SCM last = tonic;
SCM missing = SCM_EOL;
for (SCM i = pitches; gh_pair_p (i);)
{
SCM p = ly_car (i);
int step = gh_scm2int (step_scm (tonic, p));
if (unsmob_pitch (last)->notename_ == unsmob_pitch (p)->notename_)
{
int third = (unsmob_pitch (last)->notename_
- unsmob_pitch (tonic)-> notename_ + 7) % 7;
last = ly_pitch_transpose (last, scm_vector_ref (thirds, scm_int2num (third)));
}
if (step > gh_scm2int (step_scm (tonic, last)))
{
while (step > gh_scm2int (step_scm (tonic, last)))
{
missing = gh_cons (last, missing);
int third = (unsmob_pitch (last)->notename_
- unsmob_pitch (tonic)->notename_ + 7) % 7;
last = ly_pitch_transpose (last, scm_vector_ref (thirds,
scm_int2num (third)));
}
}
else
{
i = ly_cdr (i);
}
}
return lower_step (tonic, missing, scm_int2num (7));
}
/* Return PITCHES with PITCH added not as lowest note */
SCM
Chord::add_above_tonic (SCM pitch, SCM pitches)
{
/* Should we maybe first make sure that PITCH is below tonic? */
if (pitches != SCM_EOL)
while (Pitch::less_p (pitch, ly_car (pitches)) == SCM_BOOL_T)
pitch = ly_pitch_transpose (pitch, Pitch (1, 0, 0).smobbed_copy ());
pitches = gh_cons (pitch, pitches);
return scm_sort_list (pitches, Pitch::less_p_proc);
}
/* Return PITCHES with PITCH added as lowest note */
SCM
Chord::add_below_tonic (SCM pitch, SCM pitches)
{
if (pitches != SCM_EOL)
while (Pitch::less_p (ly_car (pitches), pitch) == SCM_BOOL_T)
pitch = ly_pitch_transpose (pitch, Pitch (-1, 0, 0).smobbed_copy ());
return gh_cons (pitch, pitches);
}
/*
Parser stuff
Construct from parser output:
PITCHES is the plain chord, it does not include bass or inversion
Part of Chord:: namespace for now, because we do lots of
chord-manipulating stuff.
*/
SCM
Chord::tonic_add_sub_to_pitches (SCM tonic, SCM add, SCM sub)
{
/* urg: catch dim modifier: 3rd, 5th, 7th, .. should be lowered */
bool dim_b = false;
for (SCM i = add; gh_pair_p (i); i = ly_cdr (i))
{
Pitch* p = unsmob_pitch (ly_car (i));
/* Ugr
This chord modifier stuff should really be fixed
Cmaj7 yields C 7/7-
*/
if (p->get_octave () == -100)
{
p->octave_ = 0;
dim_b = true;
}
}
add = transpose_pitches (tonic, add);
add = lower_step (tonic, add, scm_int2num (7));
add = scm_sort_list (add, Pitch::less_p_proc);
add = ly_unique (add);
sub = transpose_pitches (tonic, sub);
sub = lower_step (tonic, sub, scm_int2num (7));
sub = scm_sort_list (sub, Pitch::less_p_proc);
/* default chord includes upto 5: <1, 3, 5> */
add = gh_cons (tonic, add);
SCM tmp = add;
SCM fifth = ly_last (base_pitches (tonic));
int highest_step = gh_scm2int (step_scm (tonic, ly_last (tmp)));
if (highest_step < 5)
tmp = ly_snoc (fifth, tmp);
else if (dim_b)
{
add = lower_step (tonic, add, scm_int2num (5));
add = lower_step (tonic, add, scm_int2num (7));
}
/* find missing thirds */
SCM missing = missing_thirds (tmp);
if (highest_step < 5)
missing = ly_snoc (fifth, missing);
/* if dim modifier is given: lower all missing */
if (dim_b)
missing = lower_step (tonic, missing, scm_int2num (0));
/* if additions include any 3, don't add third */
SCM third = ly_cadr (base_pitches (tonic));
if (member_notename (third, add) != SCM_BOOL_F)
missing = scm_delete (third, missing);
/* if additions include any 4, assume sus4 and don't add third implicitely
C-sus (4) = c f g (1 4 5) */
SCM sus = ly_pitch_transpose (tonic, Pitch (0, 3, 0).smobbed_copy ());
if (member_notename (sus, add) != SCM_BOOL_F)
missing = scm_delete (third, missing);
/* if additions include some 5, don't add fifth */
if (member_notename (fifth, add) != SCM_BOOL_F)
missing = scm_delete (fifth, missing);
/* complete the list of thirds to be added */
add = gh_append2 (missing, add);
add = scm_sort_list (add, Pitch::less_p_proc);
SCM pitches = SCM_EOL;
/* Add all that aren't subtracted */
for (SCM i = add; gh_pair_p (i); i = ly_cdr (i))
{
SCM p = ly_car (i);
SCM s = member_notename (p, sub);
if (s != SCM_BOOL_F)
sub = scm_delete (s, sub);
else
pitches = gh_cons (p, pitches);
}
pitches = scm_sort_list (pitches, Pitch::less_p_proc);
for (SCM i = sub; gh_pair_p (i); i = ly_cdr (i))
warning (_f ("invalid subtraction: not part of chord: %s",
unsmob_pitch (ly_car (i))->string ()));
return pitches;
}
/* --Het lijkt me dat dit in het paarse gedeelte moet. */
Music *
Chord::get_chord (SCM tonic, SCM add, SCM sub, SCM inversion, SCM bass, SCM dur)
{
SCM pitches = tonic_add_sub_to_pitches (tonic, add, sub);
SCM list = SCM_EOL;
if (inversion != SCM_EOL)
{
/* If inversion requested, check first if the note is part of chord */
SCM s = member_pitch (inversion, pitches);
if (s != SCM_BOOL_F)
{
/* Then, delete and add as base note, ie: the inversion */
pitches = scm_delete (s, pitches);
Music * n = make_music_by_name (ly_symbol2scm ("NoteEvent"));
n->set_mus_property ("pitch", ly_car (add_below_tonic (s, pitches)));
n->set_mus_property ("duration", dur);
n->set_mus_property ("inversion", SCM_BOOL_T);
list = gh_cons (n->self_scm (), list);
scm_gc_unprotect_object (n->self_scm ());
}
else
warning (_f ("invalid inversion pitch: not part of chord: %s",
unsmob_pitch (inversion)->string ()));
}
/* Bass is easy, just add if requested */
if (bass != SCM_EOL)
{
Music * n = make_music_by_name (ly_symbol2scm ("NoteEvent"));
n->set_mus_property ("pitch", ly_car (add_below_tonic (bass, pitches)));
n->set_mus_property ("duration", dur);
n->set_mus_property ("bass", SCM_BOOL_T);
list = gh_cons (n->self_scm (), list);
scm_gc_unprotect_object (n->self_scm ());
}
for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
{
Music * n = make_music_by_name(ly_symbol2scm ("NoteEvent"));
n->set_mus_property ("pitch", ly_car (i));
n->set_mus_property ("duration", dur);
list = gh_cons (n->self_scm (), list);
scm_gc_unprotect_object (n->self_scm ());
}
Music * v = make_music_by_name(ly_symbol2scm ("EventChord"));
v->set_mus_property ("elements", list);
return v;
}
|