-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (102 loc) · 4.24 KB
/
main.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
from fibonacci_music import *
from math import sqrt
if __name__ == '__main__':
# diatonic scales
diatonic_scales = {
0: [2, 2, 1, 2, 2, 2, 1], # ionico
1: [2, 1, 2, 2, 2, 1, 2], # dorico
2: [1, 2, 2, 2, 1, 2, 2], # frigio
3: [2, 2, 2, 1, 2, 2, 1], # lidio
4: [2, 2, 1, 2, 2, 1, 2], # misolidio
5: [2, 1, 2, 2, 1, 2, 2], # eolico
6: [1, 2, 2, 1, 2, 2, 2] # locrio
}
# chords positions/patterns and triad inversion
chords_and_inversion = {
# major chords
0: {
0: [0, 4, 3], # root position
1: [4, 3, 5], # 1st inversion
2: [7, 5, 4] # 2nd inversion
},
# minor chords
1: {
0: [0, 3, 4], # root position
1: [3, 4, 5], # 1st inversion
2: [7, 5, 3] # 2nd inversion
},
# diminished chord
2: {
0: [0, 3, 3], # root position
1: [3, 3, 6], # 1st inversion
2: [6, 6, 3] # 2nd inversion
},
# augmented chord
3: {
0: [0, 4, 4], # root position
1: [4, 4, 4], # 1st inversion
2: [8, 4, 4] # 2nd inversion
}
}
# chords left hand (piano)
l_piano_chords = {}
# important information
current_octave = 4
filename_midi = 'fibonacci.mid'
# useful array
all_tracks = []
# chromatic Scale
chromatic_scale = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
# define beats per minute
while True:
try:
beats_per_minute = int(input('Beats per minute to use (BPM): '))
break
except ValueError:
print('\nInsert a valid number!')
# get time signature, notes per bar and the sequences timers
time_signature, notes_per_bar, sequence_timers = make_time_signature()
# get the diatonic mode to use on midi project
diatonic_mode = diatonic_selection(diatonic_scales, '', None)
# select scale to use the previous diatonic mode
while True:
try:
print('\nScales available: ', chromatic_scale)
scale_to_use = input('Scale to use: ').upper()
index_scale_note = chromatic_scale.index(scale_to_use) # receive index number from `all_notes` array
break
except ValueError:
print('Invalid note!')
# get all notes on right hand (piano)
r_piano = make_diatonic_scale([], index_scale_note, current_octave, diatonic_mode, chromatic_scale)
# get all notes on left hand (piano)
current_octave -= 2
l_piano = make_diatonic_scale([], index_scale_note, current_octave, diatonic_mode, chromatic_scale)
# calculate chords for all notes available on left hand (piano)
l_piano_chords, inversion_used = chord_type({}, l_piano, chords_and_inversion, False, False, chromatic_scale)
# show some useful information to the user
print(f'\nScales in use:')
print(f'\t{scale_to_use} scale (Octave {current_octave}):', r_piano)
print(f'\t{scale_to_use} scale (Octave {current_octave}):', l_piano)
print('\n{scale_to_use} scale chords That will be used on left hand:')
for chords in l_piano_chords:
print(f'\t{chords}:', l_piano_chords[chords])
# build some calculations [`fibonacci_sequence`, `golden_ratio`]
fib, golden_ratio = fibonacci_builder('', None), {0: str((1 + sqrt(5)) / 2)}
# determine the pitches notes based on fibonacci sequence and golden ratio
notes_pitches = make_pitches([], [], [fib, golden_ratio], [r_piano, l_piano], l_piano_chords, inversion_used,
notes_per_bar, sequence_timers)
# create midi tracks
print('Creating stream. Please wait...')
for notes_pitch in notes_pitches:
all_tracks.append(create_track(beats_per_minute, time_signature, notes_pitch))
# create score to put all midi tracks together
score = music21.stream.Score()
for track in all_tracks:
score.insert(track)
# convert music21.stream to midi format
print('Converting stream to MIDI format. Please wait...')
final_midi = music21.midi.translate.streamToMidiFile(score)
# write the produced midi format
print('Creating file', filename_midi)
create_file(final_midi, filename_midi)