def create_midi(prediction, instrument_type, i): # crée les fichiers .midi offset = 0 output_notes = [] for pattern in prediction: if pattern != 'Rest': if ('.' in pattern) or pattern.isdigit(): notes_in_chord = pattern.split('.') notes = [] for current_note in notes_in_chord: new_note = note.Note(int(current_note)) if str(instrument_type[i]) == "KeyboardInstrument": new_note.storedInstrument = instrument.KeyboardInstrument( ) if str(instrument_type[i]) == "Piano": new_note.storedInstrument = instrument.Piano() if str(instrument_type[i]) == "Harpsichord": new_note.storedInstrument = instrument.Harpsichord() if str(instrument_type[i]) == "Clavichord": new_note.storedInstrument = instrument.Clavichord() if str(instrument_type[i]) == "Celesta": new_note.storedInstrument = instrument.Celesta() if str(instrument_type[i]) == "ElectricBass": new_note.storedInstrument = instrument.ElectricBass() if str(instrument_type[i]) == "ElectricGuitar": new_note.storedInstrument = instrument.ElectricGuitar() if str(instrument_type[i]) == "StringInstrument": new_note.storedInstrument = instrument.StringInstrument( ) notes.append(new_note) new_chord = chord.Chord(notes) new_chord.offset = offset output_notes.append(new_chord) else: new_note = note.Note(pattern) new_note.offset = offset if str(instrument_type[i]) == "KeyboardInstrument": new_note.storedInstrument = instrument.KeyboardInstrument() if str(instrument_type[i]) == "Piano": new_note.storedInstrument = instrument.Piano() if str(instrument_type[i]) == "Harpsichord": new_note.storedInstrument = instrument.Harpsichord() if str(instrument_type[i]) == "Clavichord": new_note.storedInstrument = instrument.Clavichord() if str(instrument_type[i]) == "Celesta": new_note.storedInstrument = instrument.Celesta() if str(instrument_type[i]) == "ElectricBass": new_note.storedInstrument = instrument.ElectricBass() if str(instrument_type[i]) == "ElectricGuitar": new_note.storedInstrument = instrument.ElectricGuitar() if str(instrument_type[i]) == "StringInstrument": new_note.storedInstrument = instrument.StringInstrument() output_notes.append(new_note) offset += 0.5 midi_stream = stream.Stream(output_notes) midi_stream.write('midi', fp='music/' + str(instrument_type[i]) + '.mid')
def test(): from music21 import instrument as j sc1 = stream.Score() # instruments = [Piccolo(), Glockenspiel(), 72, 69, 41, 27, 47, 1, 1, 1, 1, 34] instrument = [ j.Piccolo(), j.Xylophone(), j.Clarinet(), j.Oboe(), j.Violin(), j.ElectricGuitar(), j.Harp(), j.Piano(), j.Piano(), j.Piano(), j.Piano(), j.ElectricBass() ] instrumentOctave = [3, 2, 2, 2, 1, 1, 1, 2, 1, 0, -1, -2] for i in range(12): inst = instrument[i] if i < 9: inst.midiChannel = i else: inst.midiChannel = i + 1 part = addPart(instrument=inst) if instrumentOctave[i] != 0: part.transpose(12 * instrumentOctave[i], inPlace=True) sc1.insert(0, part) sc1.show()
def __init__(self): super(MultiInstrumentTransformer, self).__init__( 'TransformerMultiInstrumentModel', "ml_models/Transformer_guitar.h5;ml_models/Transformer_bass_short.h5" ) self.target_instruments_str = ['Electric Guitar', 'Electric Bass'] self.target_instruments = [ instrument.ElectricGuitar(), instrument.ElectricBass() ] self.instrument_name = "guitar+bass" self.slice_len = 20
def __init__(self, instrument_str): if 'bass' in instrument_str.lower(): self.target_instrument_str = 'Electric Bass' self.target_instrument = instrument.ElectricBass() self.instrument_name = 'bass' db_name = "MarkovBass" if 'guitar' in instrument_str.lower(): self.target_instrument_str = 'Electric Guitar' self.target_instrument = instrument.ElectricGuitar() self.instrument_name = 'guitar' db_name = "MarkovGuitar" super(MarkovModel, self).__init__(db_name, "")
def __init__(self, instrument_str): if "guitar" in instrument_str: self.target_instrument_str = 'Electric Guitar' self.target_instrument = instrument.ElectricGuitar() self.instrument_name = "guitar" super(LSTMModel, self).__init__("LSTMModel", "ml_models/LSTM_guitar.h5") elif "bass" in instrument_str: self.target_instrument_str = 'Electric Bass' self.target_instrument = instrument.ElectricBass() self.instrument_name = "bass" super(LSTMModel, self).__init__("LSTMBassModel", "ml_models/LSTM_bass.h5") self.slice_len = 10
def __init__(self, instrument_str): if "bass" in instrument_str.lower(): self.target_instrument_str = "Electric Bass" self.target_instrument = instrument.ElectricBass() self.instrument_name = "bass" super(MusicVAE, self).__init__("MusicVAEBass", "ml_models/VAE_bassdecoder.h5") if "guitar" in instrument_str.lower(): self.target_instrument_str = "Electric Guitar" self.target_instrument = instrument.ElectricGuitar() self.instrument_name = "guitar" super(MusicVAE, self).__init__("MusicVAEGuitar", "ml_models/VAE_guitar_long_decoder.h5") self.slice_len = 256 self.latent_dim = 256
def __init__(self, instrument_str): if "guitar" in instrument_str: self.target_instrument_str = 'Electric Guitar' self.target_instrument = instrument.ElectricGuitar() self.instrument_name = "guitar" super(AttentionModel, self).__init__("TransformerModel", "ml_models/Transformer_guitar.h5") self.slice_len = 20 elif "bass" in instrument_str: self.target_instrument_str = 'Electric Bass' self.target_instrument = instrument.ElectricBass() self.instrument_name = "bass" super(AttentionModel, self).__init__("TransformerModelBass", "ml_models/Transformer_bass_short.h5") self.slice_len = 20
def get_instruments(genre, ontology): programs = [] if genre.label[0] == "Blues": programs.append(instrument.AcousticGuitar().midiProgram) programs.append(instrument.Harmonica().midiProgram) programs.append(instrument.TomTom().midiProgram) elif genre.label[0] == "Folk": programs.append(instrument.Banjo().midiProgram) programs.append(instrument.AcousticBass().midiProgram) programs.append(instrument.Piano().midiProgram) elif genre.label[0] == "Rock": programs.append(instrument.ElectricGuitar().midiProgram) programs.append(instrument.ElectricBass().midiProgram) programs.append(instrument.BassDrum().midiProgram) elif genre.label[0] == "Classical": programs.append(instrument.Violin().midiProgram) programs.append(instrument.Oboe().midiProgram) programs.append(instrument.Flute().midiProgram) programs.append(instrument.Viola().midiProgram) elif genre.label[0] == "Country": programs.append(instrument.AcousticGuitar().midiProgram) programs.append(instrument.Banjo().midiProgram) programs.append(instrument.TomTom().midiProgram) return programs
def build_midi(harmony, melody): chords_dict = get_chord_dicts()[1] song = [] for i, eighth in enumerate(melody): # eighth = multi_hot_to_pianoroll(piano_roll[:midi_range]) # now make_music returns pianorolls already # chord = one_hot_to_index(piano_roll[-chord_classes:]) # TODO add chord to midi # print(f'EIGHTH: {eighth}') # DEBUG song_notes = [] for note_ in eighth: note_name = NOTES[note_%12] note_octave = start_octave + note_//12 # starting from C2 song_notes.append(note_name + str(note_octave)) song_chords = [] full_chord = chords_dict[harmony[i]] if full_chord != '<unk>': for chord_ in full_chord: chord_name = NOTES[chord_%12] song_chords.append(chord_name + str(start_octave-1)) song.append(("REST" if len(song_notes) == 0 else song_notes, "REST" if len(song_chords) == 0 else song_chords)) notes_score = Score() notes_score.append(instrument.Piano()) chords_score = Score() chords_score.append(instrument.KeyboardInstrument()) bass_score = Score() bass_score.append(instrument.ElectricBass()) current_note_length = 0 current_chord_length = 0 for i, _ in enumerate(song): current_note_length += 0.5 current_chord_length += 0.5 # print(f'NOTE: {song[i][0]}\t\t\t- CHORD: {song[i][1]}') if i < len(song)-1: # note if song[i][0] != song[i+1][0]: if song[i][0] == "REST": notes_score.append(note.Rest(duration=Duration(current_note_length))) else: notes_score.append(chord.Chord([note.Note(nameWithOctave=note_name) for note_name in song[i][0]], duration=Duration(current_note_length))) current_note_length = 0 # chord if song[i][1] != song[i+1][1] or current_chord_length == 4: if song[i][1] == "REST": chords_score.append(note.Rest(duration=Duration(current_chord_length))) bass_score.append(note.Rest(duration=Duration(current_chord_length/4))) bass_score.append(note.Rest(duration=Duration(current_chord_length/4))) bass_score.append(note.Rest(duration=Duration(current_chord_length/2))) else: chords_score.append(chord.Chord([note.Note(nameWithOctave=chord_name) for chord_name in song[i][1]], duration=Duration(current_chord_length))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/4))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/4))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/2))) current_chord_length = 0 else: # note if song[i][0] == "REST": notes_score.append(note.Rest(duration=Duration(current_note_length))) else: notes_score.append(chord.Chord([note.Note(nameWithOctave=note_name) for note_name in song[i][0]], duration=Duration(current_note_length))) # chord if song[i][1] == "REST": chords_score.append(note.Rest(duration=Duration(current_chord_length))) bass_score.append(note.Rest(duration=Duration(current_chord_length/4))) bass_score.append(note.Rest(duration=Duration(current_chord_length/4))) bass_score.append(note.Rest(duration=Duration(current_chord_length/2))) else: chords_score.append(chord.Chord([note.Note(nameWithOctave=chord_name) for chord_name in song[i][1]], duration=Duration(current_chord_length))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/4))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/4))) bass_score.append(chord.Chord([note.Note(nameWithOctave=chord_name[:-1]+str(int(chord_name[-1])+1)) for chord_name in song[i][1]], duration=Duration(current_chord_length/2))) song_stream = Stream() song_stream.insert(0, notes_score) song_stream.insert(0, chords_score) song_stream.insert(0, bass_score) if not os.path.exists('melodies'): os.makedirs('melodies') dt = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") song_stream.write('midi', fp=f'melodies/generated_{dt}.mid')
def __init__(self): super(MultiInstrumentLSTM, self).__init__('LSTMMultiInstrumentModel', "ml_models/LSTM_multi.h5") self.target_instruments_str = ['Electric Guitar', 'Electric Bass', 'Piano'] self.target_instruments = [instrument.ElectricGuitar(), instrument.ElectricBass(), instrument.Percussion()] self.instrument_name = "guitar+bass" self.slice_len = 20
from music21 import converter, instrument s = converter.parse('./old.mid') for p in s.parts: p.insert(0, instrument.ElectricBass()) s.write('midi', './new.mid')