Spaces:
Runtime error
Runtime error
First sonification test, minus getting sentiment.
Browse files
app.py
CHANGED
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
from musicautobot.utils.setup_musescore import play_wav
|
@@ -45,11 +50,40 @@ learner = music_model_learner(
|
|
45 |
|
46 |
print("Ready to use.")
|
47 |
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
def sonify_text(text):
|
50 |
name = Path('C Major Scale.midi')
|
51 |
item = MusicItem.from_file(name, data.vocab)
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
|
55 |
if MIDI_File is not None:
|
@@ -64,7 +98,8 @@ def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
|
|
64 |
|
65 |
# create the model input object
|
66 |
if sonification:
|
67 |
-
|
|
|
68 |
else:
|
69 |
item = MusicItem.from_file(name, data.vocab)
|
70 |
|
@@ -77,7 +112,12 @@ def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
|
|
77 |
)
|
78 |
|
79 |
# convert to stream and then MIDI file
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
81 |
out = music21.midi.translate.streamToMidiFile(stream)
|
82 |
|
83 |
# save MIDI file
|
@@ -90,10 +130,14 @@ def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
|
|
90 |
FluidSynth(sound_font).midi_to_audio('result.midi', 'result.wav')
|
91 |
return 'result.wav'
|
92 |
|
|
|
|
|
|
|
|
|
93 |
iface = gr.Interface(
|
94 |
fn=process_midi,
|
95 |
inputs=[
|
96 |
-
gr.inputs.File(optional=True, label=
|
97 |
"text",
|
98 |
gr.inputs.Slider(0, 250, default=100, step=50),
|
99 |
gr.inputs.Radio([100, 200, 500], type="value", default=100)
|
|
|
1 |
+
"""
|
2 |
+
Find the inspiration for this project as well as the pretrained model
|
3 |
+
we used here: https://github.com/bearpelican/musicautobot
|
4 |
+
"""
|
5 |
+
|
6 |
import gradio as gr
|
7 |
|
8 |
from musicautobot.utils.setup_musescore import play_wav
|
|
|
50 |
|
51 |
print("Ready to use.")
|
52 |
|
53 |
+
musical_letters = 'abcdefg'
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
from music21 import note
|
58 |
|
59 |
+
def sonify_text(text, sentiment):
|
60 |
name = Path('C Major Scale.midi')
|
61 |
item = MusicItem.from_file(name, data.vocab)
|
62 |
+
|
63 |
+
note_names = [f"{letter.upper()}4" for letter in text.lower() if letter in musical_letters]
|
64 |
+
|
65 |
+
p = music21.stream.Part()
|
66 |
+
if sentiment == 'NEGATIVE':
|
67 |
+
# If negative, use TODO
|
68 |
+
p.append(music21.chord.Chord('C4 E4 G4', type='half')) # I
|
69 |
+
p.append(music21.chord.Chord('G3 B3 D4', type='half')) # V
|
70 |
+
p.append(music21.chord.Chord('A3 C4 E4', type='half')) # vi
|
71 |
+
else:
|
72 |
+
# If positive, use a partial progression I-V-vi in C Major.
|
73 |
+
p.append(music21.chord.Chord('C4 E4 G4', type='half')) # I
|
74 |
+
p.append(music21.chord.Chord('G3 B3 D4', type='half')) # V
|
75 |
+
p.append(music21.chord.Chord('A3 C4 E4', type='half')) # vi
|
76 |
+
|
77 |
+
notes = []
|
78 |
+
for note_name in note_names:
|
79 |
+
note_obj = note.Note(note_name)
|
80 |
+
note_obj.duration.type = "quarter"
|
81 |
+
p.append(note_obj)
|
82 |
+
|
83 |
+
s = music21.stream.Score([p])
|
84 |
+
|
85 |
+
musical_seed = MusicItem.from_stream(s, data.vocab)
|
86 |
+
return musical_seed
|
87 |
|
88 |
def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
|
89 |
if MIDI_File is not None:
|
|
|
98 |
|
99 |
# create the model input object
|
100 |
if sonification:
|
101 |
+
sentiment = 'POSITIVE'
|
102 |
+
item = sonify_text(Text_to_Sonify, sentiment)
|
103 |
else:
|
104 |
item = MusicItem.from_file(name, data.vocab)
|
105 |
|
|
|
112 |
)
|
113 |
|
114 |
# convert to stream and then MIDI file
|
115 |
+
if sonification:
|
116 |
+
# do not replay the musical seed if sonifying
|
117 |
+
stream = pred.to_stream()
|
118 |
+
else:
|
119 |
+
stream = full.to_stream()
|
120 |
+
|
121 |
out = music21.midi.translate.streamToMidiFile(stream)
|
122 |
|
123 |
# save MIDI file
|
|
|
130 |
FluidSynth(sound_font).midi_to_audio('result.midi', 'result.wav')
|
131 |
return 'result.wav'
|
132 |
|
133 |
+
midi_file_desc = """Upload your own MIDI file here.
|
134 |
+
If you do not have a MIDI file, add some text and we will turn it into music!
|
135 |
+
"""
|
136 |
+
|
137 |
iface = gr.Interface(
|
138 |
fn=process_midi,
|
139 |
inputs=[
|
140 |
+
gr.inputs.File(optional=True, label=midi_file_desc),
|
141 |
"text",
|
142 |
gr.inputs.Slider(0, 250, default=100, step=50),
|
143 |
gr.inputs.Radio([100, 200, 500], type="value", default=100)
|