-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSG.class.php
379 lines (342 loc) · 11 KB
/
DSG.class.php
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
373
374
375
376
377
378
<?php
// This program is copyright (c) 1993 - 2006 Jos Kunst, the Jos Kunst heirs.
// This program is free software. You can redistribute it and/or modify it
// under the terms of version 2 of the GNU General Public License, which you
// should have received with it.
// This program is distributed in the hope that it will be useful, but
// without any warranty, expressed or implied.
/**
* Utility functions to emulate Pascal data type 'set' with PHP arrays
*/
require('set_emulation.inc.php');
require('Chord.class.php');
/**
* Class which handles chord transformations to change the dissonance grading
*
* @package DSG
*/
class DSG
{
/**
* @var Chord
*/
private Chord $_chord;
/**
* @var Chord
*/
private Chord $_lastfedinchord;
/**
* Provides the class with a chord to work with
*
* @param Chord $chord
*/
public function setChord(Chord $chord)
{
$this->_chord = $chord;
}
/**
* Saves the last manually entered chord
*
* @param Chord $chord
*/
public function setLastFedInChord(Chord $chord)
{
$this->_lastfedinchord = $chord;
}
/**
* Returns the chord being worked on, in its current state
*
* @return Chord
*/
public function getCurrentChord(): Chord
{
return $this->_chord;
}
/**
* Returns the last manually entered chord
*
* @return Chord
*/
public function getLastFedInChord(): Chord
{
return $this->_lastfedinchord;
}
/**
* Changes the current chord as far as possible in the direction of the target DSG
*
* @param int $targetdsg the desired target DSG
*/
public function changeCurrentChordByTargetDSG(int $targetdsg)
{
while (1) {
$sofardsg = $this->_chord->getDissonance();
$this->_chord = $this->_targetrecto($targetdsg);
// this essentially meaning: if _targetrecto cannot change the dissonance
// any further in the direction of the desired target dissonance,
// i.e. if the dissonance before and after _targetrecto is the same, break out of the loop
if ($sofardsg == $this->_chord->getDissonance())
break;
}
}
/**
* Changes the current chord by Parallel Subset Motion. Possible values of $type:
* <b>inc</b> for increasing the dissonance or <b>dim</b> for diminishing the dissonance
*
* @param string $type the desired direction of the change: increasing or diminishing dissonance
*/
public function changeCurrentChordByPSM(string $type)
{
if ($type == 'inc') {
$this->_chord = $this->_psmdissrec();
} elseif ($type == 'dim') {
$this->_chord = $this->_psmconsrec();
}
}
/**
* Changes dissonance of a chord in the direction of the supplied target dissonance
*
* @param int $targetdiss the target dissonance
* @return Chord changed chord structure
*/
private function _targetrecto(int $targetdiss): Chord
{
$tmpchord = clone($this->_chord);
$chset = $tmpchord->getPitchSet();
if ($tmpchord->getDissonance() > $targetdiss) {
$excset = array();
while (1) {
/** @var $c1 Chord */
/** @var $c2 Chord */
/** @var $c3 Chord */
/** @var $c4 Chord */
list($pmax, $c1, $c2, $c3, $c4) = $this->_downgrade($tmpchord, $excset);
if (($c1->getDissonance() >= $targetdiss) && ($c1->getDissonance() < $tmpchord->getDissonance())) {
$tmpchord = $c1;
} elseif (($c2->getDissonance() >= $targetdiss) && ($c2->getDissonance() < $tmpchord->getDissonance())) {
$tmpchord = $c2;
} elseif (($c3->getDissonance() >= $targetdiss) && ($c3->getDissonance() < $tmpchord->getDissonance())) {
$tmpchord = $c3;
} elseif (($c4->getDissonance() >= $targetdiss) && ($c4->getDissonance() < $tmpchord->getDissonance())) {
$tmpchord = $c4;
}
$excset = array_addtoset($pmax, $excset);
if (array_subset($chset, $excset) || $tmpchord->getDissonance() == $targetdiss) {
break;
}
}
}
if ($tmpchord->getDissonance() < $targetdiss) {
$excset = array();
while (1) {
/** @var $c1 Chord */
/** @var $c2 Chord */
/** @var $c3 Chord */
/** @var $c4 Chord */
list($pmin, $c1, $c2, $c3, $c4) = $this->_upgrade($tmpchord, $excset);
if (($c1->getDissonance() <= $targetdiss) && ($c1->getDissonance() > $tmpchord->getDissonance())) {
$tmpchord = $c1;
} elseif (($c2->getDissonance() <= $targetdiss) && ($c2->getDissonance() > $tmpchord->getDissonance())) {
$tmpchord = $c2;
} elseif (($c3->getDissonance() <= $targetdiss) && ($c3->getDissonance() > $tmpchord->getDissonance())) {
$tmpchord = $c3;
} elseif (($c4->getDissonance() <= $targetdiss) && ($c4->getDissonance() > $tmpchord->getDissonance())) {
$tmpchord = $c4;
}
$excset = array_addtoset($pmin, $excset);
if (array_subset($chset, $excset) || $tmpchord->getDissonance() == $targetdiss) {
break;
}
}
}
return $tmpchord;
}
/**
* Orders the chords resulting from the four possible second steps of the least dissonant tone, from high to low dsg
*
* @param Chord $chord
* @param array $exceptpitchset pitches to be ignored by the findpmincontrib function
* @return array array with the least dissonant tone and four possible chords resulting from moving it
*/
private function _upgrade(Chord $chord, array $exceptpitchset): array
{
$retval = array();
$pmin = $chord->findPMinContrib($exceptpitchset);
// moveToneFourPossibleWays returns four chords
$chords = $chord->moveToneFourPossibleWays($pmin);
// Chords know how to sort themselves by their DSG
usort($chords, array('Chord', 'sortByDSG'));
/** @var $chord Chord */
// highest DSG first: reverse the sorted array
foreach(array_reverse($chords) as $chord) {
$retval[] = $chord;
}
array_unshift($retval, $pmin);
return $retval;
}
/**
* Orders the chords resulting from the four possible second steps of the most dissonant tone, from low to high dsg
*
* @param Chord $chord
* @param array $exceptpitchset array of pitches to be ignored by the findpmaxcontrib function
* @return array array with the most dissonant tone and four possible chords resulting from moving it
*/
private function _downgrade(Chord $chord, array $exceptpitchset): array
{
$retval = array();
$pmax = $chord->findPMaxContrib($exceptpitchset);
// moveToneFourPossibleWays returns four chords
$chords = $chord->moveToneFourPossibleWays($pmax);
// Chords know how to sort themselves by their DSG
usort($chords, array('Chord', 'sortByDSG'));
/** @var $chord Chord */
foreach($chords as $chord) {
$retval[] = $chord;
}
array_unshift($retval, $pmax);
return $retval;
}
/**
* Increases an analyzed chord's dissonance, using parallel subset motion
*
* @return Chord chord with increased dissonance
*/
private function _psmdissrec(): Chord
{
$moved = array();
$pmin = $this->_chord->findPMinContrib();
$tobemoved = array($pmin);
// trialchord: remove the least dissonant tone from the chord,
// replace with that tone one half-step higher
$trialchord = new Chord(
array_addtoset(
$pmin + 1, array_removefromset(
$tobemoved, $this->_chord->getPitchSet()
)
)
);
$trialpitchset = $trialchord->getPitchSet();
while (1) {
$rupwardchord = $trialchord;
$tobemoved = array_addtoset($trialchord->findPMaxDiff($this->_chord), $tobemoved);
foreach ($tobemoved as $p) {
$moved = array_addtoset($p + 1, $moved);
}
$trialchord = new Chord(
array_addtoset(
$moved, array_removefromset(
$tobemoved, $trialpitchset
)
)
);
if ($trialchord->getTotal() <= $rupwardchord->getTotal()) {
break;
}
$trialpitchset = $trialchord->getPitchSet();
}
$moved = array();
$tobemoved = array($pmin);
$trialchord = new Chord(
array_addtoset(
$pmin - 1, array_removefromset(
$tobemoved, $this->_chord->getPitchSet()
)
)
);
$trialpitchset = $trialchord->getPitchSet();
while (1) {
$rdownwardchord = $trialchord;
$tobemoved = array_addtoset($trialchord->findPMaxDiff($this->_chord), $tobemoved);
foreach ($tobemoved as $p) {
$moved = array_addtoset($p - 1, $moved);
}
$trialchord = new Chord(
array_addtoset(
$moved, array_removefromset(
$tobemoved, $trialpitchset
)
)
);
if ($trialchord->getTotal() <= $rdownwardchord->getTotal()) {
break;
}
$trialpitchset = $trialchord->getPitchSet();
}
if ($rdownwardchord->getTotal() >= $rupwardchord->getTotal()) {
return $rdownwardchord;
} else {
return $rupwardchord;
}
}
/**
* Diminishes an analyzed chord's dissonance, using parallel subset motion
*
* @return Chord chord with diminished dissonance
*/
private function _psmconsrec(): Chord
{
$moved = array();
$pmax = $this->_chord->findPMaxContrib();
$tobemoved = array($pmax);
// trialchord: remove the most dissonant tone from the chord,
// replace with that tone one half-step higher
$trialchord = new Chord(
array_addtoset(
$pmax + 1, array_removefromset(
$tobemoved, $this->_chord->getPitchSet()
)
)
);
$trialpitchset = $trialchord->getPitchSet();
while (1) {
$rupwardchord = $trialchord;
$tobemoved = array_addtoset($this->_chord->findPMaxDiff($trialchord), $tobemoved);
foreach ($tobemoved as $p) {
$moved = array_addtoset($p + 1, $moved);
}
$trialchord = new Chord(
array_addtoset(
$moved, array_removefromset(
$tobemoved, $trialpitchset
)
)
);
if ($trialchord->getTotal() >= $rupwardchord->getTotal()) {
break;
}
$trialpitchset = $trialchord->getPitchSet();
}
$moved = array();
$tobemoved = array($pmax);
$trialchord = new Chord(
array_addtoset(
$pmax - 1, array_removefromset(
$tobemoved, $this->_chord->getPitchSet()
)
)
);
$trialpitchset = $trialchord->getPitchSet();
while (1) {
$rdownwardchord = $trialchord;
$tobemoved = array_addtoset($this->_chord->findPMaxDiff($trialchord), $tobemoved);
foreach ($tobemoved as $p) {
$moved = array_addtoset($p - 1, $moved);
}
$trialchord = new Chord(
array_addtoset(
$moved, array_removefromset(
$tobemoved, $trialpitchset
)
)
);
if ($trialchord->getTotal() >= $rdownwardchord->getTotal()) {
break;
}
$trialpitchset = $trialchord->getPitchSet();
}
if ($rdownwardchord->getTotal() <= $rupwardchord->getTotal()) {
return $rdownwardchord;
} else {
return $rupwardchord;
}
}
}