-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
70 lines (56 loc) · 2.31 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
from pychord import Chord
import pretty_midi
import time
from revChatGPT.ChatGPT import Chatbot
global chatbot
chatbot = Chatbot({
"session_token": "YOUR-TOKEN" # place your token here
}, conversation_id=None, parent_id=None)
def create_midi(chords):
"""
This function is adapted from https://github.com/yuma-m/pychord/blob/main/examples/pychord-midi.py
"""
midi_data = pretty_midi.PrettyMIDI()
piano_program = pretty_midi.instrument_name_to_program('Acoustic Grand Piano')
piano = pretty_midi.Instrument(program=piano_program)
length = 1
for n, chord in enumerate(chords):
for note_name in chord.components_with_pitch(root_pitch=4):
note_number = pretty_midi.note_name_to_number(note_name)
note = pretty_midi.Note(velocity=100, pitch=note_number, start=n * length, end=(n + 1) * length)
piano.notes.append(note)
midi_data.instruments.append(piano)
timestr = time.strftime("%Y%m%d-%H%M%S")
midi_data.write(f'progression-{timestr}.mid')
def main():
user_input = input("Enter what kind of chord progression you would like to generate: ")
response = chatbot.ask(f"{user_input} in the format of this chord progression: Bb7 - Bb7 - Bb7 - Bb7\nEb7 - Eb7 - "
f"Bb7 - Bb7\nAb7 - Ab7 - Bb7 - Bb7\nEb7 - Bb7 - Ab7 - Eb7\nDo not write anything other than the chord progression itself.",
conversation_id=None,
parent_id=None)
print(response["message"])
lines = response["message"].split("\n")
letters = ["A", "B", "C", "D", "E", "F", "G"]
# removing the lines breaks
lines_nobreak = []
for i in lines:
if i[0] in letters:
lines_nobreak.append(i)
# making it a list of just the chord symbols
chords_str = []
for i in lines_nobreak:
for j in i.split(" - "):
if j.__contains__("\n"):
chords_str.append(j[0:-1])
else:
chords_str.append(j)
chords = [Chord(c) for c in chords_str]
create_midi(chords)
print("File created!")
end_input = input("Would you like to generate another midi file? (Y/N) ")
if end_input == "Y" or end_input == "y":
main()
else:
input("Press enter to close the program")
if __name__ == '__main__':
main()