muzic icon indicating copy to clipboard operation
muzic copied to clipboard

[MuseCoco] REMI to MIDI Translation

Open cpadilha opened this issue 2 years ago • 7 comments

Was anyone capable of fulfilling the music generation step? I mean, generating the MIDI files? It seems broken.

muzic/musecoco/2-attribute2music_model/midiprocessor/enc_remigen2_utils.py:252, in generate_midi_obj_from_remigen_token_list(token_list, vocab_manager, ignore_velocity, ticks_per_beat, ts, tempo, inst_id, velocity) 247 if ignore_velocity: 248 raise ValueError( 249 "Invalid token for velocity (%s) while setting ignore_velocity=True" % ('%s-%s' % item) 250 ) --> 252 assert last_item_type == const.DURATION_ABBR 253 cur_velocity = vocab_manager.convert_id_to_vel(item_value) 255 start_pos = cur_global_pos

AssertionError:

cpadilha avatar Oct 18 '23 20:10 cpadilha

Same issue. Did you manage to find a solution?

zoahmed-xyz avatar Dec 09 '23 18:12 zoahmed-xyz

I got remi files(0.txt and 1.txt) Then how to get midi file to remi files??

Subin-Kim46 avatar Jan 17 '24 05:01 Subin-Kim46

I got remi files(0.txt and 1.txt) Then how to get midi file to remi files??

I have the same problem that it only generates remi files. The solution is to fix bugs in 2-attribute2music_model/midiprocessor/enc_remigen2_utils.py

change miditoolkit.containers to miditoolkit.midi.containers

after that, you can run the scripts below to generate midi files

import os
from midiprocessor import MidiDecoder, MidiEncoder


date = "0117"
midi_root = f"./generation/{date}/linear_mask-1billion-attribute2music/infer_test/topk15-t1.0-ngram0"
midi_decoder = MidiDecoder("REMIGEN2")

print(midi_decoder)


midi_list = []
error_midi_list = []

for folder_name in os.listdir(midi_root):
    folder_dir = os.path.join(midi_root, folder_name)
    # not folder, Using_pred_labels.txt etc
    if os.path.isfile(folder_dir):
        continue
        
    # remi_name: 0.txt, 1.txt...
    remi_dir = os.path.join(folder_dir, 'remi')
    midi_dir = os.path.join(folder_dir, 'midi')


    for remi_name in os.listdir(remi_dir):
        with open(os.path.join(remi_dir, remi_name), 'r') as f:
            hypo_str = f.read()
        
        # since the seq looks like this: prefix <seq> remi
        # we have to get the remi tokens by discard the prefix tokens and <seq>
        # orginal:
        # remi_token = hypo_str.split(" ")[sep_pos[id_] + 1:]
        remi_token = hypo_str.split(" <sep> ")[1].split(" ")
        os.makedirs(midi_dir, exist_ok=True)
        midi_save_path = os.path.join(midi_dir, remi_name.replace('.txt', '.mid'))
        midi_list.append(midi_save_path)
        try:
            midi_obj = midi_decoder.decode_from_token_str_list(remi_token)
            midi_obj.dump(midi_save_path)
        except:
            error_midi_list.append(midi_save_path)



print(len(midi_list))

print(len(error_midi_list))

I also got the assertion error as @cpadilha did. 39/560 midi files have that problem.

feiyuehchen avatar Jan 18 '24 03:01 feiyuehchen

@feiyuehchen Thank you so much. This works well

Subin-Kim46 avatar Jan 18 '24 04:01 Subin-Kim46

@feiyuehchen Sir, I wonder one more thing.

What should I do to use the text I want as an input?

Subin-Kim46 avatar Jan 18 '24 04:01 Subin-Kim46

@Subin-Kim46 you could print hypo_str to know more

prefix_input  = hypo_str.split(" <sep> ")[0].split(" ")

feiyuehchen avatar Jan 18 '24 08:01 feiyuehchen

@feiyuehchen Thank you so much

Subin-Kim46 avatar Jan 19 '24 00:01 Subin-Kim46