-
Notifications
You must be signed in to change notification settings - Fork 1
/
song.py
800 lines (639 loc) · 34.8 KB
/
song.py
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
"""
Author: Karine Miras - 9/2019
"""
#from midi2audio import FluidSynth
from midiutil import MIDIFile
import math
import numpy as np
import random
class Song:
def __init__(self, _song_id='',
_num_bars=12,
_presets=[1, 2, 8, 11, 15, 16, 17, 18, 22, 23],
_tempo_pool={'min': 120, 'mean': 150, 'std': 20, 'max': 170},
_silent_bars_range=[0.1, 0.2],
_user_solo=True):
# Identification of the track/individual
self.song_id = _song_id
self.genotype = {}
self.phenotype = None
# Key of the scale
self.key = None
# Scale category
self.scale_type = None
# Modality of the key
self.scale_mode = None
# All pitches available within the key
self.scale_keyboard_full = None
self.scale_keyboard_filtered = None
# Metronome/speed
self.tempo = None
self.times = None
self.drummed = None
self.progression = []
self.progression_type = None
self.preset = None
self.silent_bars = None
self.karaoke_chords = None
self.karaoke_roles = None
self.roles = []
self.user_solo = _user_solo
self.duration_pool = {
'semibreve': 1*4,
'minima': 1*2,
'seminima': 1,
'colcheia': 1/2,
'semicolcheia': 1/4
}
self.beat = self.duration_pool['seminima']
# quaternary is more likely
self.times_pool = [3] + [4]*6
# Use multiples of 12
self.num_bars = _num_bars
# Tracks info
self.tracks = {
'percussion': 3,
'bass': 0,
'harmony': 1,
'solo': 2
}
self.melody_granularities = [3, 4] #[2, 3, 4]
# Channels left and right
self.num_channels = 2
# Volumes by track
self.volumes = {
'bass': 130,
'harmony': 80,
'solo': 150,
'percussion': 110
}
# Central C is normally C4 (60) - but in Garage band, it is 72
# Lowest pool (C1)
self.low_ref1 = 36
# Second lowest pool (C2)
self.low_ref2 = 48
self.melody_reach = 36
self.karaoke_bars = 24
self.pitch_pool = range(self.low_ref1, self.low_ref1+12, 1)
self.num_octaves = 5
self.drummed_prob = 1
# Labels for all pitches of the keyboard, regardless key
self.pitch_labels = {}
self.scale_types = ['pentatonic',
'pentablues']
self.scale_modes = [
'major',
'minor']
# Free has more chance of happening than fixed progressions.
self.progression_types = ['blues', 'fall']
self.progression_types.extend(['free'] * 4)
self.drumms_styles = ['rock', 'rock', 'fast_rock', 'long_dance', 'short_dance', 'puts']
self.intervals_dic = {
'T': 0,
'2m': 1,
'2M': 2,
'3m': 3,
'3M': 4,
'4J': 5,
'4A': 6,
'5J': 7,
'6m': 8,
'6M': 9,
'7m': 10,
'7M': 11,
'8J': 12
}
# instruments preset
self.presets = _presets
# Minimum and maximum speed in BPM.
self.tempo_pool = _tempo_pool
self.silent_bars_range = _silent_bars_range
# export 'all' tracks together or 'track' by track
self.tracks_granularity = 'all'
def build_pitch_labels(self):
# accidents = ['#', 'b']
pitch_labels_basic ={36: 'C',
37: 'Db',
38: 'D',
39: 'Eb',
40: 'E',
41: 'F',
42: 'F#',
43: 'G',
44: 'Ab',
45: 'A',
46: 'Bb',
47: 'B'}
self.pitch_labels = pitch_labels_basic.copy()
self.pitch_labels[0] = 'rest'
for key, value in pitch_labels_basic.items():
for oct in range(0, self.num_octaves):
self.pitch_labels[key+12*oct] = value
def choices(self):
self.tempo = int(min(max(np.random.normal(self.tempo_pool['mean'], self.tempo_pool['std'], 1)[0],
self.tempo_pool['min']),
self.tempo_pool['max']))
self.key = random.choice(self.pitch_pool)
self.silent_bars = random.uniform(self.silent_bars_range[0], self.silent_bars_range[1])
self.scale_mode = random.choice(self.scale_modes)
self.scale_type = random.choice(self.scale_types)
self.preset = random.choice(self.presets)
if self.times is None:
self.times = random.choice(self.times_pool)
if self.progression_type is None:
self.progression_type = random.choice(self.progression_types)
self.drummed = True if random.uniform(0, 1) <= self.drummed_prob else False
def compose(self):
self.build_pitch_labels()
self.build_scale()
self.compose_progression()
self.compose_bass(track=self.tracks['bass'],
channel=self.tracks['bass'])
self.compose_harmony(track=self.tracks['harmony'],
channel=self.tracks['harmony'])
self.compose_solo(track=self.tracks['solo'],
channel=self.tracks['solo'])
self.compose_percussion(track=self.tracks['percussion'],
channel=self.tracks['percussion'])
def initialize_song(self):
self.choices()
self.compose()
def build_scale(self):
# intervals_filtered is for solo, while intervals_full is for harmony/bass
if self.scale_mode == 'minor':
intervals_filtered = [self.intervals_dic['T'], self.intervals_dic['3m'],
self.intervals_dic['4J'], self.intervals_dic['5J'],
self.intervals_dic['7m']]
else:
intervals_filtered = [self.intervals_dic['T'], self.intervals_dic['2M'],
self.intervals_dic['3M'], self.intervals_dic['5J'],
self.intervals_dic['6M']]
intervals_full = list.copy(intervals_filtered)
if self.scale_mode == 'minor':
intervals_full.extend([self.intervals_dic['2M'], self.intervals_dic['6m']])
else:
intervals_full.extend([self.intervals_dic['7M'], self.intervals_dic['4J']])
# Pentablues has an extra interval.
if self.scale_type == 'pentablues':
if self.scale_mode == 'minor':
intervals_filtered.insert(3, intervals_filtered[2] + self.intervals_dic['2m'])
else:
intervals_filtered.insert(2, intervals_filtered[1] + self.intervals_dic['2m'])
# Multiple full octaves with their due intervals.
self.scale_keyboard_filtered = [None] * (self.num_octaves * len(intervals_filtered))
self.scale_keyboard_full = [None] * (self.num_octaves * len(intervals_full))
for idx, interval in enumerate(intervals_filtered):
for octave in range(0, self.num_octaves):
self.scale_keyboard_filtered[idx + len(intervals_filtered) * octave] = self.key + interval + octave * 12
for idx, interval in enumerate(intervals_full):
for octave in range(0, self.num_octaves):
self.scale_keyboard_full[idx + len(intervals_full) * octave] = self.key + interval + octave * 12
def progression_scale_keyboard(self):
keyboard = list(filter(lambda x: x < self.key + 12, self.scale_keyboard_full))
return keyboard
def compose_progression(self):
local_scale_keyboard = self.progression_scale_keyboard()
# using bar as unit
self.progression = []
if self.progression_type == 'blues':
for idx_piece in range(0, int(self.num_bars / 12)):
self.progression.extend([self.key,
self.key,
self.key,
self.key,
self.key + self.intervals_dic['4J'],
self.key + self.intervals_dic['4J'],
self.key,
self.key,
self.key + self.intervals_dic['5J'],
self.key + self.intervals_dic['4J'],
self.key,
self.key + self.intervals_dic['5J']
])
if self.progression_type == 'fall':
if self.scale_mode == 'minor':
for idx_piece in range(0, int(self.num_bars / 12)):
self.progression.extend(3 *
[self.key, self.key - self.intervals_dic['2M'],
self.key - self.intervals_dic['3M'],
self.key - self.intervals_dic['4J']])
else:
# I only like this fall for minors
self.progression_type = 'free'
if self.progression_type == 'free':
self.progression.append(self.key)
composed_bars = 0
repetitions = np.array([1, 2, 3, 4])
repetitions_chances = np.array([0.35, 0.40, 0.15, 0.10])
# repetitions_chances = repetitions / repetitions.sum()
while composed_bars < self.num_bars - 2:
repetition = np.random.choice(repetitions, 1, p=repetitions_chances)[0]
key = random.choice(local_scale_keyboard)
if composed_bars + repetition > self.num_bars - 2:
repetition = self.num_bars - 2 - composed_bars
for i in range(0, repetition):
self.progression.append(key)
composed_bars += 1
self.progression.append(self.key)
def percussion_scale_keyboard(self):
keyboard = list(range(self.low_ref1, self.low_ref1 + 24))
return keyboard
def compose_percussion(self, track, channel):
percussion = []
local_percussion_keyboard = self.percussion_scale_keyboard()
idx_previous_drum = None
percussion_timeline = 0
drumms_styles = random.choice(self.drumms_styles)
if self.drummed:
for bar in range(0, self.num_bars):
melody_bar = []
# kick and snare for quaternary
if self.times == 4:
# leave space for opening turn
if percussion_timeline > 0:
if drumms_styles == 'rock':
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 2})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 3})
if drumms_styles == 'fast_rock':
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2,
'time': percussion_timeline + self.beat * 2})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2,
'time': percussion_timeline + self.beat * 2.5})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2,
'time': percussion_timeline + self.beat * 3})
if drumms_styles == 'long_dance':
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 2.5})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 3.5})
if drumms_styles == 'short_dance':
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 2.5})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 3})
if drumms_styles == 'puts':
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat, 'time': percussion_timeline})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat, 'time': percussion_timeline + self.beat})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat, 'time': percussion_timeline+ self.beat * 2})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat, 'time': percussion_timeline + self.beat * 3})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat, 'time': percussion_timeline})
# kick and snare for ternary
if self.times == 3:
# leave space for opening turn
if percussion_timeline > 0:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1,
'duration': self.beat / 2, 'time': percussion_timeline})
# space for ending turns
if random.uniform(0, 1) <= 0.75:
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat})
melody_bar.append({'track': track, 'channel': channel, 'pitch': self.low_ref1 + 2,
'duration': self.beat / 2, 'time': percussion_timeline + self.beat * 2})
# extras
percussion_timeline = self.get_melody_bar(melody_bar,
local_percussion_keyboard,
track,
channel,
percussion_timeline,
idx_previous_drum)
percussion.append(melody_bar)
else:
percussion.append([{'track': track, 'channel': channel, 'pitch': 0,
'duration': 1 / 64, 'time': percussion_timeline}])
self.genotype['percussion'] = percussion
def solo_scale_keyboard(self):
keyboard = list(filter(lambda x: x >= self.low_ref2
and x <= self.low_ref2 + self.melody_reach,
self.scale_keyboard_filtered))
return keyboard
def compose_solo(self, track, channel):
solo = []
local_scale_keyboard_filtered = self.solo_scale_keyboard()
idx_previous_pitch = None
solo_timeline = 0
if self.user_solo:
roles_order = ['aj']*8+['user']*4
else:
roles_order = ['aj'] * 12
for bar in range(0, int((self.num_bars+1)/12)):
self.roles = self.roles + roles_order
roles_order.reverse()
for bar in range(0, self.num_bars):
melody_bar = []
if self.roles[bar] == 'aj':
# there is a chance of having empty bars
if random.uniform(0, 1) <= self.silent_bars:
solo_timeline += self.times * self.beat
idx_previous_pitch = None
else:
solo_timeline = self.get_melody_bar(melody_bar,
local_scale_keyboard_filtered,
track,
channel,
solo_timeline,
idx_previous_pitch)
else:
solo_timeline += self.times * self.beat
idx_previous_pitch = None
solo.append(melody_bar)
# this hack adds a tiny note at the end, to avoid prunning of the track when having empty bars in the end
solo[-1].append({'track': track, 'channel': channel, 'pitch': 1,
'duration': 0.01, 'time': solo_timeline})
self.genotype['solo'] = solo
def get_melody_bar(self, melody_bar, local_scale_keyboard_filtered, track, channel, melody_timeline, idx_previous_pitch):
total_time_bar = 0
while total_time_bar < self.times * self.beat:
duration = self.get_melody_duration()
if total_time_bar + duration <= self.times * self.beat:
pitch, idx_previous_pitch = self.get_melody_pitch(local_scale_keyboard_filtered, idx_previous_pitch)
# rest is added as a note, but accounts in the timeline
if pitch != 0:
melody_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': melody_timeline})
total_time_bar = total_time_bar + duration
melody_timeline = melody_timeline + duration
return melody_timeline
def get_melody_duration(self):
melody_granularity = random.choice(self.melody_granularities)
duration_pool = np.array([self.duration_pool[value] for value in self.duration_pool])
idx_distances = np.arange(0, len(self.duration_pool))
list_distances = abs(idx_distances - np.where(duration_pool == self.beat)[0])
list_distances = 1 / (list_distances + 1)
list_distances = pow(list_distances, melody_granularity)
list_distances = list_distances / list_distances.sum()
duration = np.random.choice(duration_pool, 1, p=list_distances)[0]
return duration
def get_melody_pitch(self, local_scale_keyboard_filtered, idx_previous_pitch):
if idx_previous_pitch is None:
idx_pitch = random.choice(range(0, len(local_scale_keyboard_filtered)))
pitch = local_scale_keyboard_filtered[idx_pitch]
else:
idx_distances = np.arange(0, len(local_scale_keyboard_filtered))
list_distances = abs(idx_distances - idx_previous_pitch)
list_distances = 1 / (list_distances + 1)
melody_granularity = random.choice(self.melody_granularities)
list_distances = pow(list_distances, melody_granularity)
# pitch repetition happens a little bit more than the average
# (the plus one above garantees this and avoids division by zero)
list_distances[np.where(list_distances == 1)] = np.average(list_distances)
list_distances = list_distances / list_distances.sum()
# the more distant a pitch, the less likely it is to be selected
idx_pitch = np.random.choice(idx_distances, 1, p=list_distances)[0]
pitch = local_scale_keyboard_filtered[idx_pitch]
# the pitches more than an octave higher than the previous pitch became rests
if abs(local_scale_keyboard_filtered[idx_pitch] - local_scale_keyboard_filtered[idx_previous_pitch]) > 12:
# additionally, there is a chance the note becomes a rest anyway???
# or random.uniform(0, 1) <= 0.15:
pitch = 0
idx_pitch = None
return pitch, idx_pitch
def compose_bass(self, track, channel):
bass_timeline = 0
bass_progression = []
for pitch in self.progression:
bass_progression_bar = []
bass_timeline = self.compose_bass_progression_bar(bass_progression_bar, pitch, track, channel, bass_timeline)
bass_progression.append(bass_progression_bar)
self.genotype['bass'] = bass_progression
def compose_bass_progression_bar(self, bass_progression_bar, pitch, track, channel, bass_timeline):
# some variations work either only for quaternay or ternary
if self.times == 3:
variation = random.choice(range(1, 3 + 1))
if self.times == 4:
variation = random.choice(range(2, 7 + 1))
# waltz (made for ternary)
if variation == 1:
duration = self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['5J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['5J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic full bar
if variation == 2:
duration = self.times * self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic per beat
if variation == 3:
for i in range(0, self.times):
duration = self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic levadinha (made for quaternary)
if variation == 4:
duration = self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration + self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration + self.beat / 2
duration = self.beat / 2
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic eight alternation (made for quaternary)
if variation == 5:
duration = self.beat * 2
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['8J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic-fifth alternation (made for quaternary)
if variation == 6:
duration = self.beat * 2
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['5J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
# tonic-fifth-eight arpeggio (made for quaternary)
if variation == 7:
duration = self.beat
bass_progression_bar.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['5J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['8J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
bass_progression_bar.append(
{'track': track, 'channel': channel, 'pitch': pitch + self.intervals_dic['5J'],
'duration': duration, 'time': bass_timeline})
bass_timeline = bass_timeline + duration
return bass_timeline
def compose_harmony(self, track, channel):
harmony_timeline = 0
harmony_progression = []
for pitch in self.progression:
chord = []
# there is a chance first bar is silent
if harmony_timeline == 0 and random.uniform(0, 1) <= 0.5:
harmony_timeline += self.times * self.beat
else:
harmony_timeline = self.compose_chord(chord, pitch, track, channel, harmony_timeline)
harmony_progression.append(chord)
self.genotype['harmony'] = harmony_progression
def compose_chord(self, chord, pitch, track, channel, harmony_timeline):
duration = self.times * self.beat
# harmony should not go as low as bass
if pitch < self.low_ref2 - 12:
pitch = pitch + self.intervals_dic['8J']
chord.append({'track': track, 'channel': channel, 'pitch': pitch,
'duration': duration, 'time': harmony_timeline})
pitch_third = pitch + self.intervals_dic['3m']
if self.scale_keyboard_full.count(pitch_third) == 0:
pitch_third += 1
pitch_fifth = pitch + self.intervals_dic['5J']
if self.scale_keyboard_full.count(pitch_fifth) == 0:
pitch_fifth += -1
chord.append({'track': track, 'channel': channel, 'pitch': pitch_third,
'duration': duration, 'time': harmony_timeline})
chord.append({'track': track, 'channel': channel, 'pitch': pitch_fifth,
'duration': duration, 'time': harmony_timeline})
harmony_timeline = harmony_timeline + duration
self.inversion(chord)
return harmony_timeline
def inversion(self, chord):
# apply none, fifth, or fifth inversion
for aux_i in range(0, random.choice([0, 3])):
# inverts fifth
if aux_i == 1:
if chord[2]['pitch'] - 12 >= self.low_ref1:
chord[2]['pitch'] = chord[2]['pitch'] - 12
# inverts third
if aux_i == 2:
if chord[2]['pitch'] - 12 >= self.low_ref1:
chord[1]['pitch'] = chord[1]['pitch'] - 12
def get_song_duration(self):
total_beats = self.beat * self.times * self.num_bars
minute_portion = total_beats / self.tempo
seconds = math.ceil(minute_portion * 60)
return seconds
def build_midi(self, path='', song_id='', export_phenotype=False):
if self.tracks_granularity == 'track' and export_phenotype:
for track in self.genotype:
self.phenotype = MIDIFile(
numTracks=len(self.tracks),
deinterleave=False)
self.phenotype.addTempo(0, # track
0, # time
self.tempo)
self.add_notes_midi(track)
self.export_midi(path+'/phenotypes/'+song_id+'_'+track)
if self.tracks_granularity == 'all':
self.phenotype = MIDIFile(
numTracks=len(self.tracks),
deinterleave=False)
self.phenotype.addTempo(0, #track
0, #time
self.tempo)
self.add_notes_midi()
if export_phenotype:
self.export_midi(path+'/phenotypes/'+song_id)
def build_karaoke(self):
self.karaoke_chords = []
self.karaoke_roles = []
num_bars_karaoke = self.karaoke_bars
ini_indx = 0
fin_indx = num_bars_karaoke
chords = []
for chord in self.genotype['harmony']:
mode = ''
if len(chord) == 0:
chords.append('-')
else:
if abs(chord[0]['pitch'] - chord[2]['pitch']) % 2 == 0:
mode = '°'
else:
if abs(chord[0]['pitch'] - chord[1]['pitch']) % 2 > 0:
mode = 'm'
chord = self.pitch_labels[chord[0]['pitch']] + mode
chords.append(chord)
while fin_indx <= len(chords):
chords_sequence = chords[ini_indx:fin_indx]
roles_sequence = self.roles[ini_indx:fin_indx]
if len(chords_sequence) == num_bars_karaoke:
self.karaoke_chords.append(chords_sequence)
self.karaoke_roles.append(roles_sequence)
ini_indx = fin_indx - 12
fin_indx = fin_indx + 12
def add_notes_midi(self, track_export=None):
for track in self.genotype:
if track_export is None or track == track_export:
for bar in range(0, len(self.genotype[track])):
for idx_note in range(0, len(self.genotype[track][bar])):
self.phenotype.addNote(self.genotype[track][bar][idx_note]['track'],
self.genotype[track][bar][idx_note]['channel'],
self.genotype[track][bar][idx_note]['pitch'],
self.genotype[track][bar][idx_note]['time'],
self.genotype[track][bar][idx_note]['duration'],
self.volumes[track])
def export_metadata(self):
pass
def export_midi(self, name):
with open(name+'.mid', "wb") as output_file:
self.phenotype.writeFile(output_file)
file1 = open(name+".txt", "w")
file1.write(str(self.get_song_duration()))
file1.close()
def play_midi(self):
if self.tracks_granularity == 'all':
fs = FluidSynth()
fs.play_midi(self.song_id+'.mid')
else:
print('MIDIs were exported track by track, thus will not play.')