Spaces:
Runtime error
Runtime error
Gradio App
Browse files- app.py +13 -0
- mergeoggs.py +15 -0
- post_processor.py +132 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from audio2hero import generate_midi
|
| 4 |
+
|
| 5 |
+
gradio_app = gr.Interface(
|
| 6 |
+
generate_midi,
|
| 7 |
+
inputs=gr.Audio(label="Input Audio", type="filepath"),
|
| 8 |
+
outputs=gr.File(label="Output MIDI"),
|
| 9 |
+
title="Audio2Hero AI Autocharter for CloneHero",
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
if __name__ == "__main__":
|
| 13 |
+
gradio_app.launch()
|
mergeoggs.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydub import AudioSegment
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def merge(out_dir, *files):
|
| 5 |
+
if len(files)==0:
|
| 6 |
+
raise ValueError("No files to merge")
|
| 7 |
+
combined = AudioSegment.from_file(files[0], format="ogg")
|
| 8 |
+
for file in files[1:]:
|
| 9 |
+
sound = AudioSegment.from_file(file, format="ogg")
|
| 10 |
+
combined = combined.overlay(sound)
|
| 11 |
+
combined.export(out_dir, format="ogg")
|
| 12 |
+
|
| 13 |
+
if __name__ == "__main__":
|
| 14 |
+
merge("merged.ogg","guitar.ogg", "bass.ogg", "song.ogg")
|
| 15 |
+
|
post_processor.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pretty_midi
|
| 2 |
+
import librosa
|
| 3 |
+
import copy
|
| 4 |
+
import mido
|
| 5 |
+
import configparser
|
| 6 |
+
import os
|
| 7 |
+
import random
|
| 8 |
+
from mergeoggs import merge
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def post_process(song_path, midi_path, output_dir):
|
| 13 |
+
song_name = song_path.split("/")[-1]
|
| 14 |
+
song_name = ".".join(song_name.split(".")[0:-1])
|
| 15 |
+
|
| 16 |
+
if not os.path.exists(f"{output_dir}/{song_name}"):
|
| 17 |
+
os.makedirs(f"{output_dir}/{song_name}")
|
| 18 |
+
|
| 19 |
+
merge(f"{output_dir}/{song_name}/song.ogg", song_path)
|
| 20 |
+
|
| 21 |
+
notes = pretty_midi.PrettyMIDI(midi_path)
|
| 22 |
+
|
| 23 |
+
output = copy.deepcopy(notes)
|
| 24 |
+
output.instruments = []
|
| 25 |
+
output.instruments.append(pretty_midi.Instrument(0, name="PART GUITAR"))
|
| 26 |
+
|
| 27 |
+
note_times = [note.start for note in notes.instruments[0].notes if note.pitch != 78]
|
| 28 |
+
for index,note in enumerate(notes.instruments[0].notes):
|
| 29 |
+
if note.pitch not in [72,73,74,75, 78]:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
new_pitch = note.pitch
|
| 33 |
+
duration = note.end - note.start
|
| 34 |
+
end = note.start + duration if duration > 0.5 else note.start + 0.1
|
| 35 |
+
new_note = pretty_midi.Note(velocity=100, pitch=new_pitch, start=note.start, end=end)
|
| 36 |
+
|
| 37 |
+
# add random note if solo strum
|
| 38 |
+
if note.pitch == 78 and note.start not in note_times:
|
| 39 |
+
extra_note = pretty_midi.Note(velocity=100, pitch=random.randint(72,75), start=note.start, end=end)
|
| 40 |
+
output.instruments[0].notes.append(extra_note)
|
| 41 |
+
|
| 42 |
+
output.instruments[0].notes.append(new_note)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
output.write(f"{output_dir}/{song_name}/notes.mid")
|
| 46 |
+
|
| 47 |
+
output = mido.MidiFile(f"{output_dir}/{song_name}/notes.mid")
|
| 48 |
+
output.tracks[1].pop(1)
|
| 49 |
+
output.save(f"{output_dir}/{song_name}/notes.mid")
|
| 50 |
+
|
| 51 |
+
# write ini file
|
| 52 |
+
config = configparser.ConfigParser()
|
| 53 |
+
config.read('./song.ini')
|
| 54 |
+
config.set("song", "name", song_name.split(" - ")[1])
|
| 55 |
+
config.set("song", "artist", song_name.split(" - ")[0])
|
| 56 |
+
config.set("song", "charter", "Tim and Matthew")
|
| 57 |
+
|
| 58 |
+
with open(f"{output_dir}/{song_name}/song.ini", 'w') as configfile: # save
|
| 59 |
+
config.write(configfile)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# output_dir = "./clonehero"
|
| 64 |
+
|
| 65 |
+
# # for file in os.listdir("./processed/piano_midi"):
|
| 66 |
+
# # song_name = ".".join(file.split(".")[0:-1])
|
| 67 |
+
# song_name = "Dire Straits - Sultans of Swing"
|
| 68 |
+
# # song_name = "Aerosmith - Same Old Song & Dance"
|
| 69 |
+
# print(song_name)
|
| 70 |
+
|
| 71 |
+
# if not os.path.exists(f"{output_dir}/{song_name}"):
|
| 72 |
+
# os.makedirs(f"{output_dir}/{song_name}")
|
| 73 |
+
|
| 74 |
+
# # copy over song
|
| 75 |
+
# # shutil.copy(f"./processed/audio/{song_name}.ogg", f"{output_dir}/{song_name}/song.ogg")
|
| 76 |
+
# shutil.copy(f"./{song_name}.ogg", f"{output_dir}/{song_name}/song.ogg")
|
| 77 |
+
|
| 78 |
+
# # notes = pretty_midi.PrettyMIDI(f"./processed/piano_midi/{file}")
|
| 79 |
+
# notes = pretty_midi.PrettyMIDI(f"./sultans_ada.mid")
|
| 80 |
+
# # notes = pretty_midi.PrettyMIDI(f"./{song_name}.mid")
|
| 81 |
+
|
| 82 |
+
# output = copy.deepcopy(notes)
|
| 83 |
+
# output.instruments = []
|
| 84 |
+
# output.instruments.append(pretty_midi.Instrument(0, name="PART GUITAR"))
|
| 85 |
+
# last_start = 0
|
| 86 |
+
|
| 87 |
+
# note_times = [note.start for note in notes.instruments[0].notes if note.pitch != 78]
|
| 88 |
+
|
| 89 |
+
# total = 0
|
| 90 |
+
# outofrange = 0
|
| 91 |
+
# for index,note in enumerate(notes.instruments[0].notes):
|
| 92 |
+
# time_start = note.start
|
| 93 |
+
# # if time_start == last_start:
|
| 94 |
+
# # continue
|
| 95 |
+
# # if index % 2 != 0:
|
| 96 |
+
# # continue
|
| 97 |
+
# total+=1
|
| 98 |
+
# if note.pitch not in [71,72,73,74,75, 78]:
|
| 99 |
+
# outofrange+=1
|
| 100 |
+
# last_start = time_start
|
| 101 |
+
# # new_pitch = 71 + note.pitch % 5
|
| 102 |
+
# new_pitch = note.pitch
|
| 103 |
+
# duration = note.end - note.start
|
| 104 |
+
# end = note.start + duration if duration > 0.5 else note.start + 0.1
|
| 105 |
+
# new_note = pretty_midi.Note(velocity=100, pitch=new_pitch, start=note.start, end=end)
|
| 106 |
+
|
| 107 |
+
# # if strum
|
| 108 |
+
# if note.pitch == 78 and note.start not in note_times:
|
| 109 |
+
# extra_note = pretty_midi.Note(velocity=100, pitch=random.randint(71,75), start=note.start, end=end)
|
| 110 |
+
# output.instruments[0].notes.append(extra_note)
|
| 111 |
+
# # strum = pretty_midi.Note(velocity=100, pitch=78, start=note.start, end=end)
|
| 112 |
+
# output.instruments[0].notes.append(new_note)
|
| 113 |
+
# # output.instruments[0].notes.append(strum)
|
| 114 |
+
|
| 115 |
+
# print(f"Total notes: {total}")
|
| 116 |
+
# print(f"Out of range notes: {outofrange}")
|
| 117 |
+
|
| 118 |
+
# output.write(f"{output_dir}/{song_name}/notes.mid")
|
| 119 |
+
|
| 120 |
+
# output = mido.MidiFile(f"{output_dir}/{song_name}/notes.mid")
|
| 121 |
+
# output.tracks[1].pop(1)
|
| 122 |
+
# output.save(f"{output_dir}/{song_name}/notes.mid")
|
| 123 |
+
|
| 124 |
+
# # write ini file
|
| 125 |
+
# config = configparser.ConfigParser()
|
| 126 |
+
# config.read('./song.ini')
|
| 127 |
+
# config.set("song", "name", song_name.split(" - ")[1])
|
| 128 |
+
# config.set("song", "artist", song_name.split(" - ")[0])
|
| 129 |
+
# config.set("song", "charter", "Tim and Matthew")
|
| 130 |
+
|
| 131 |
+
# with open(f"{output_dir}/{song_name}/song.ini", 'w') as configfile: # save
|
| 132 |
+
# config.write(configfile)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
pretty-midi
|
| 4 |
+
essentia
|
| 5 |
+
librosa
|
| 6 |
+
scipy
|
| 7 |
+
pydub
|
| 8 |
+
resampy
|