Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,663 Bytes
f8c0a29 |
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 |
import pretty_midi
import librosa
import copy
import mido
import configparser
import os
import random
from mergeoggs import merge
def post_process(song_path, midi_path, output_dir):
song_name = song_path.split("/")[-1]
song_name = ".".join(song_name.split(".")[0:-1])
if not os.path.exists(f"{output_dir}/{song_name}"):
os.makedirs(f"{output_dir}/{song_name}")
merge(f"{output_dir}/{song_name}/song.ogg", song_path)
notes = pretty_midi.PrettyMIDI(midi_path)
output = copy.deepcopy(notes)
output.instruments = []
output.instruments.append(pretty_midi.Instrument(0, name="PART GUITAR"))
note_times = [note.start for note in notes.instruments[0].notes if note.pitch != 78]
for index,note in enumerate(notes.instruments[0].notes):
if note.pitch not in [72,73,74,75, 78]:
continue
new_pitch = note.pitch
duration = note.end - note.start
end = note.start + duration if duration > 0.5 else note.start + 0.1
new_note = pretty_midi.Note(velocity=100, pitch=new_pitch, start=note.start, end=end)
# add random note if solo strum
if note.pitch == 78 and note.start not in note_times:
extra_note = pretty_midi.Note(velocity=100, pitch=random.randint(72,75), start=note.start, end=end)
output.instruments[0].notes.append(extra_note)
output.instruments[0].notes.append(new_note)
output.write(f"{output_dir}/{song_name}/notes.mid")
output = mido.MidiFile(f"{output_dir}/{song_name}/notes.mid")
output.tracks[1].pop(1)
output.save(f"{output_dir}/{song_name}/notes.mid")
# write ini file
config = configparser.ConfigParser()
config.read('./song.ini')
config.set("song", "name", song_name.split(" - ")[1])
config.set("song", "artist", song_name.split(" - ")[0])
config.set("song", "charter", "Tim and Matthew")
with open(f"{output_dir}/{song_name}/song.ini", 'w') as configfile: # save
config.write(configfile)
# output_dir = "./clonehero"
# # for file in os.listdir("./processed/piano_midi"):
# # song_name = ".".join(file.split(".")[0:-1])
# song_name = "Dire Straits - Sultans of Swing"
# # song_name = "Aerosmith - Same Old Song & Dance"
# print(song_name)
# if not os.path.exists(f"{output_dir}/{song_name}"):
# os.makedirs(f"{output_dir}/{song_name}")
# # copy over song
# # shutil.copy(f"./processed/audio/{song_name}.ogg", f"{output_dir}/{song_name}/song.ogg")
# shutil.copy(f"./{song_name}.ogg", f"{output_dir}/{song_name}/song.ogg")
# # notes = pretty_midi.PrettyMIDI(f"./processed/piano_midi/{file}")
# notes = pretty_midi.PrettyMIDI(f"./sultans_ada.mid")
# # notes = pretty_midi.PrettyMIDI(f"./{song_name}.mid")
# output = copy.deepcopy(notes)
# output.instruments = []
# output.instruments.append(pretty_midi.Instrument(0, name="PART GUITAR"))
# last_start = 0
# note_times = [note.start for note in notes.instruments[0].notes if note.pitch != 78]
# total = 0
# outofrange = 0
# for index,note in enumerate(notes.instruments[0].notes):
# time_start = note.start
# # if time_start == last_start:
# # continue
# # if index % 2 != 0:
# # continue
# total+=1
# if note.pitch not in [71,72,73,74,75, 78]:
# outofrange+=1
# last_start = time_start
# # new_pitch = 71 + note.pitch % 5
# new_pitch = note.pitch
# duration = note.end - note.start
# end = note.start + duration if duration > 0.5 else note.start + 0.1
# new_note = pretty_midi.Note(velocity=100, pitch=new_pitch, start=note.start, end=end)
# # if strum
# if note.pitch == 78 and note.start not in note_times:
# extra_note = pretty_midi.Note(velocity=100, pitch=random.randint(71,75), start=note.start, end=end)
# output.instruments[0].notes.append(extra_note)
# # strum = pretty_midi.Note(velocity=100, pitch=78, start=note.start, end=end)
# output.instruments[0].notes.append(new_note)
# # output.instruments[0].notes.append(strum)
# print(f"Total notes: {total}")
# print(f"Out of range notes: {outofrange}")
# output.write(f"{output_dir}/{song_name}/notes.mid")
# output = mido.MidiFile(f"{output_dir}/{song_name}/notes.mid")
# output.tracks[1].pop(1)
# output.save(f"{output_dir}/{song_name}/notes.mid")
# # write ini file
# config = configparser.ConfigParser()
# config.read('./song.ini')
# config.set("song", "name", song_name.split(" - ")[1])
# config.set("song", "artist", song_name.split(" - ")[0])
# config.set("song", "charter", "Tim and Matthew")
# with open(f"{output_dir}/{song_name}/song.ini", 'w') as configfile: # save
# config.write(configfile)
|