-
Notifications
You must be signed in to change notification settings - Fork 1
/
Score.java
644 lines (541 loc) · 14.5 KB
/
Score.java
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import org.jfugue.Pattern;
/**
* Handles musical details; stores the music score.
*
* @author Harry Allen
*/
public class Score
{
private Pattern score; // Stores music score
private int keySig; // Stores current key signature
private int tempo; // Stores tempo for music to be played
private double degree; // Represents the degree of the current pitch in the current key signature
private int note;
private int prevNote;
private int voices; // Represents the number of voices currently active in the score
private int layers; // Represents the number of layers currently active in a voice
private int beat;
private static String[] keySigs = // Stores key signatures
{ "C", "G", "D", "A", "E", "B", "Gb/F#", "Db", "Ab", "Eb", "Bb", "F" };
private static String[] notes = // Stores all possible notes
{ "C", "C#/Db", "D", "D#/Eb", "E", "F", "F#/Gb", "G", "G#/Ab", "A", "A#/Bb", "B" };
/**
* Default Constructor.
*/
public Score()
{
score = new Pattern();
keySig = 1;
tempo = 120;
degree = 1;
note = -1;
prevNote = -1;
voices = 1;
layers = 1;
beat = 1;
}
/**
* @return Value of the key signature
*/
public String getKey()
{
return keySigs[this.keySig - 1];
}
public int getKeyInt()
{
return keySig;
}
/**
* @return Returns value of tempo
*/
public int getTempo()
{
return this.tempo;
}
/**
* @return The number of voices currently present
*/
public int getVoices()
{
return voices;
}
/**
* @return The number of layers currently present
*/
public int getLayers()
{
return layers;
}
/**
* @return The current beat of the measure
*/
public int getBeat()
{
return beat;
}
/**
* @return The integer representation of the current note
*/
public int getNote()
{
return note;
}
/**
* @param note integer representation of the note to be returned
* @return The string representation of the note passed as parameter
*/
public String getNoteStr(int note)
{
return notes[note];
}
/**
* @return The integer representation of the previous note
*/
public int getPrevNote()
{
return prevNote;
}
/**
* @return The degree of the current note
*/
public double getDegree()
{
return degree;
}
/**
* @return The generated score
*/
public Pattern getScore()
{
return score;
}
public int[] getChord(int chordDegree)
{
int[] chord = new int[3];
int degree = 1;
int tonic = 0;
while (!notes[tonic].equals(keySigs[keySig-1]))
tonic = upHalfStep(tonic);
while(degree != chordDegree)
{
switch(degree)
{
case 1:
tonic = upWholeStep(tonic);
degree += 1;
break;
case 2:
tonic = upWholeStep(tonic);
degree += 1;
break;
case 3:
tonic = upHalfStep(tonic);
degree += 1;
break;
case 4:
tonic = upWholeStep(tonic);
degree += 1;
break;
case 5:
tonic = upWholeStep(tonic);
degree += 1;
break;
case 6:
tonic = upWholeStep(tonic);
degree += 1;
break;
case 7:
tonic = upHalfStep(tonic);
degree += 1.0;
break;
}
if (tonic == 12)
tonic = 0;
}
chord[0] = tonic;
chord[1] = (chord[0] + 4) % 12;
chord[2] = (chord[1] + 3) % 12;
return chord;
}
/**
* Sets the value of keySig and the key of the analyzer to the integer accepted. This forces the analyzer to reset and analyze MIDI files in the new key.
*
* @param key the new key signature
*/
public void setKey(int key)
{
if (key >= 1 && key <= 12)
{
if (key != this.keySig)
this.keySig = key;
}
else
this.keySig = 1;
}
/**
* Sets the value of tempo to the integer accepted.
*
* @param tempo the new tempo
*/
public void setTempo(int tempo)
{
this.tempo = tempo;
}
/**
* Sets the number of voices present to the parameter.
*
* @param newvoices new number of voices
*/
public void setVoices(int newvoices)
{
if (newvoices > 0 && newvoices <= 16)
voices = newvoices;
}
/**
* Sets the number of layers present to the parameter.
*
* @param newlayers new number of layers
*/
public void setLayers(int newlayers)
{
if (newlayers > 0 && newlayers <= 16)
layers = newlayers;
}
/**
* Sets the current note to the parameter and the previous note is recorded as prevNote.<br>
* Scale degree is recorded, with accidentals represented as numbers between integer scale degrees.
*
* @param newnote next note recorded
*/
public void setNote(int newnote)
{
prevNote = note;
note = newnote;
degree = 1.0;
int tonic = 0;
while (!notes[tonic].equals(keySigs[keySig-1]))
tonic = upHalfStep(tonic);
while (tonic != note)
tonic = upDegree(tonic);
if (tonic == 12)
tonic = 0;
if (degree == 8.0)
degree = 1.0;
}
/**
* Sets the current note to the parameter and the previous note is recorded as prevNote. <br>
* Scale degree is recorded, with accidentals represented as numbers between integer scale degrees.
*
* @param pitch the pitch of the note to be set
*/
public void setNotePitch(int pitch)
{
pitch = pitch % 12;
prevNote = note;
degree = 1.0;
int tonic = 0;
switch (pitch)
{
case 0:
note = 0;
break;
case 1:
note = 1;
break;
case 2:
note = 2;
break;
case 3:
note = 3;
break;
case 4:
note = 4;
break;
case 5:
note = 5;
break;
case 6:
note = 6;
break;
case 7:
note = 7;
break;
case 8:
note = 8;
break;
case 9:
note = 9;
break;
case 10:
note = 10;
break;
case 11:
note = 11;
break;
default:
note = 0;
break;
}
while (!notes[tonic].equals(keySigs[keySig-1]))
tonic = upHalfStep(tonic);
while (tonic != pitch)
tonic = upDegree(tonic);
if (tonic == 12)
tonic = 0;
if (degree == 8.0)
degree = 1.0;
}
/**
* @param next desired note
* @param pitch current pitch
* @param lowerBound lower boundary for pitch
* @param upperBound upper boundary for pitch
* @return Closest pitch matching next.
*/
public int findClosestPitch(int next, int pitch, int lowerBound, int upperBound)
{
int distanceup = 0;
int distancedown = 0;
int note = pitch % 12;
int original = note;
int originalpitch = pitch;
while (note != next)
{
note = upHalfStep(note);
pitch = upHalfStep(pitch);
if (note > 11)
note = 0;
if(pitch > upperBound)
{
distanceup = 99;
break;
}
++distanceup;
}
note = original;
pitch = originalpitch;
while (note != next)
{
note = downHalfStep(note);
pitch = downHalfStep(pitch);
if (note < 0)
note = 11;
if(pitch < lowerBound)
{
distancedown = 99;
break;
}
++distancedown;
}
note = original;
pitch = originalpitch;
if(distanceup < distancedown)
{
while (distanceup > 0)
{
note = upHalfStep(note);
if (note > 11)
note = 0;
pitch = upHalfStep(pitch);
if (pitch > upperBound)
pitch -= 24;
--distanceup;
}
}
else
{
while (distancedown > 0)
{
note = downHalfStep(note);
if (note < 0)
note = 11;
pitch = downHalfStep(pitch);
if (pitch < lowerBound)
pitch += 24;
--distancedown;
}
}
return pitch;
}
/**
* Increments the note passed and degree based on current scale degree.
*
* @param tonic note to increment.
* @return The modified parameter.
*/
private int upDegree(int note)
{
if (degree == 1.0)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 1.5)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 2.0)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 2.5)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 3.0)
{
note = upHalfStep(note);
degree += 1.0;
}
else if (degree == 4.0)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 4.5)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 5.0)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 5.5)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 6.0)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 6.5)
{
note = upHalfStep(note);
degree += 0.5;
}
else if (degree == 7.0)
{
note = upHalfStep(note);
degree += 1.0;
}
else
{
note = upHalfStep(note);
degree = 1.5;
}
if (note == 12)
note = 0;
return note;
}
/**
* Set score to the parameter.
*
* @param pattern new score
*/
public void setScore(Pattern pattern)
{
score = pattern;
}
/**
* Appends a voice to the score.
*
* @param newvoice voice to be appended
*/
public void appendPart(String newvoice, int instrument)
{
score.add(score.toString() + " V1 I" + instrument + " " + newvoice + " ");
}
public void appendNote(String note)
{
score.add(" " + note);
}
/**
* Resets the score in preparation for new music.
*/
public void resetScore()
{
voices = 1;
degree = 1;
layers = 1;
beat = 1;
note = -1;
prevNote = -1;
}
/**
* Increments the beat.
*/
public void nextBeat()
{
if (beat < 96)
beat += 32;
else
beat = 0;
}
/**
* Accepts an integer representing a note value, and increments it a half step.
*
* @param note the note to be modified
* @return The modified note.
*/
public int upHalfStep(int note)
{
return note + 1;
}
/**
* Accepts an integer representing a note value, and decrements it a half step.
*
* @param note the note to be modified
* @return The modified note.
*/
public int downHalfStep(int note)
{
return note - 1;
}
/**
* Accepts an integer representing a note value, and increments it a whole step.
*
* @param note the note to be modified
* @return The modified note.
*/
public int upWholeStep(int note)
{
return note + 2;
}
/**
* Accepts an integer representing a note value, and decrements it a whole step.
*
* @param note the note to be modified
* @return The modified note.
*/
public int downWholeStep(int note)
{
return note - 2;
}
/**
* Accepts an integer representing a note value, and takes it up an octave.
*
* @param note the note to be modified
* @return The modified note
*/
public int upOctave(int note)
{
return note + 12;
}
/**
* Accepts an integer representing a note value, and decrements it an octave.
*
* @param note the note to be modified
* @return The modified note.
*/
public int downOctave(int note)
{
return note - 12;
}
}