def to_midi(self, program=DEFAULT_SAVING_PROGRAM, resolution=DEFAULT_RESOLUTION, tempo=DEFAULT_TEMPO): midi = PrettyMIDI(resolution=resolution, initial_tempo=tempo) inst = Instrument(program, False, 'NoteSeq') inst.notes = copy.deepcopy(self.notes) midi.instruments.append(inst) return midi
def convert2midi(self): midi = PrettyMIDI(resolution=STATE_RESOLUTION, initial_tempo=STATE_TEMP) inst = Instrument(1, False, 'Note_Seqce') inst.notes = copy.deepcopy(self.notes) midi.instruments.append(inst) return midi
def write_midi(note_sequence, output_dir, filename): #make output directory pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True) #generate midi midi = PrettyMIDI() piano_track = Instrument(program=0, is_drum=False, name=filename) piano_track.notes = note_sequence midi.instruments.append(piano_track) output_name = output_dir + f"{filename}.midi" midi.write(output_name)
def write_to_midi(note_sequences, output_dir, n_to_write=None): if len(note_sequences) == 0: print("No note sequences to write out...") return #number of sequences to write out as MIDI files if n_to_write is None: n_to_write = len(note_sequences) #make the output directory if it doesn't already exist pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True) for i in range(n_to_write): #for note_sequence in note_sequences: midi = PrettyMIDI(initial_tempo=80) piano = Instrument(program=0, is_drum=False, name="test{}".format(i)) piano.notes = note_sequences[i] midi.instruments.append(piano) output_name = output_dir + "/test{}.midi".format(i) #with open(output_name, 'w') midi.write(output_name) print("Piano data successfully extracted from midis, navigate to {} to listen"\ .format(output_dir))
def read_one_midi_and_cut(filename, output, cut_length=16): data = PrettyMIDI(filename) notes = [] for track in data.instruments: notes += track.notes notes = sorted(notes, key=lambda note: note.start) #print(notes) end_time = 0 for note in reversed(notes): if note.end > end_time: end_time = note.end start_time = 0 end_time = max(0, end_time - cut_length) import random cut_start_time = random.uniform(start_time, end_time) cut_end_time = cut_start_time + cut_length print('%s: start=%.1f, end=%.1f' % (filename, cut_start_time, cut_end_time)) midi_length = len(notes) for i in reversed(range(midi_length)): if notes[i].start < cut_start_time or\ notes[i].start > cut_end_time: del notes[i] elif notes[i].end > cut_end_time: notes[i].end = cut_end_time for note in notes: note.start -= cut_start_time note.end -= cut_start_time midi_file = PrettyMIDI(resolution=220, initial_tempo=120) track = Instrument(0) track.notes = notes midi_file.instruments.append(track) midi_file.write(output)
def test_dataset_hook_slakh(benchmark_audio): # make a fake slakh directory. band = {"guitar": [30, 31], "drums": [127]} only_guitar = {"guitar": [30, 31]} empty = {} bad = {"guitar": [30], "guitar_2": [30]} with tempfile.TemporaryDirectory() as tmpdir: track_dir = os.path.join(tmpdir, "Track") os.mkdir(track_dir) # Create Metadata file metadata = "audio_dir: stems" metadata += "\nmidi_dir: MIDI" metadata += "\nstems:" metadata += "\n S00:" metadata += "\n program_num: 30" metadata += "\n S01:" metadata += "\n program_num: 127" metadata += "\n S02:" metadata += "\n program_num: 30" metadata += "\n S03:" metadata += "\n program_num: 30" metadata_path = os.path.join(track_dir, "metadata.yaml") metadata_file = open(metadata_path, "w") metadata_file.write(metadata) metadata_file.close() stems_dir = os.path.join(track_dir, "stems") midi_dir = os.path.join(track_dir, "MIDI") os.mkdir(stems_dir) os.mkdir(midi_dir) # Note: These aren't actually guitar and drums guitar_path1 = os.path.join(stems_dir, "S00.wav") guitar_path2 = os.path.join(stems_dir, "S02.wav") guitar_path3 = os.path.join(stems_dir, "S03.wav") drums_path = os.path.join(stems_dir, "S01.wav") mix_path = os.path.join(track_dir, "mix.wav") # making midi objects midi_0 = PrettyMIDI() midi_1 = PrettyMIDI() guitar = Instrument(30, name="guitar") guitar.notes = [Note(70, 59, 0, 1)] drum = Instrument(127, is_drum=True, name="drum") drum.notes = [Note(40, 30, 0, 1)] midi_0.instruments.append(guitar) midi_1.instruments.append(drum) midi_0.write(os.path.join(midi_dir, "S00.mid")) midi_1.write(os.path.join(midi_dir, "S01.mid")) midi_0.write(os.path.join(midi_dir, "S02.mid")) midi_0.write(os.path.join(midi_dir, "S03.mid")) midi_mix = PrettyMIDI() midi_mix.instruments += [guitar, drum] midi_mix.write(os.path.join(track_dir, "all_src.mid")) # Move them within directory shutil.copy(benchmark_audio['K0140.wav'], guitar_path1) shutil.copy(benchmark_audio['K0149.wav'], drums_path) # Make a mix from them. guitar_signal = nussl.AudioSignal(path_to_input_file=guitar_path1) drums_signal = nussl.AudioSignal(path_to_input_file=drums_path) guitar_signal.truncate_seconds(2) drums_signal.truncate_seconds(2) mix_signal = guitar_signal + drums_signal mix_signal.write_audio_to_file(mix_path) drums_signal.write_audio_to_file(drums_path) guitar_signal.write_audio_to_file(guitar_path1) guitar_signal.write_audio_to_file(guitar_path3) guitar_signal.write_audio_to_file(guitar_path2) # now that our fake slakh has been created, lets try some mixing band_slakh = nussl.datasets.Slakh(tmpdir, band, midi=True, make_submix=True, max_tracks_per_src=1) assert len(band_slakh) == 1 data = band_slakh[0] _mix_signal, _sources = data["mix"], data["sources"] assert np.allclose(mix_signal.audio_data, _mix_signal.audio_data) assert len(_sources) == 2 assert np.allclose(_sources["drums"].audio_data, drums_signal.audio_data) assert np.allclose(_sources["guitar"].audio_data, guitar_signal.audio_data) _midi_mix, _midi_sources = data["midi_mix"], data["midi_sources"] assert len(_midi_mix.instruments) == 2 assert len(_midi_sources) == 2 assert _midi_sources["guitar"][0].instruments[0].program == 30 assert _midi_sources["drums"][0].instruments[0].program == 127 band_slakh = nussl.datasets.Slakh(tmpdir, band, midi=True, make_submix=False) data = band_slakh[0] _mix_signal, _sources = data["mix"], data["sources"] assert isinstance(_sources["guitar"], list) assert len(_sources) == 2 assert len(_sources["guitar"]) == 3 assert len(_sources["drums"]) == 1 assert np.allclose( sum(_sources["guitar"]).audio_data, 3 * guitar_signal.audio_data) with pytest.raises(DataSetException): not_enough_instruments = nussl.datasets.Slakh( tmpdir, band, midi=True, make_submix=True, min_acceptable_sources=3) guitar_slakh = nussl.datasets.Slakh(tmpdir, only_guitar, make_submix=True, min_acceptable_sources=1, max_tracks_per_src=1) data = guitar_slakh[0] _guitar_signal, _sources = data["mix"], data["sources"] assert len(_sources) == 1 assert np.allclose(_sources["guitar"].audio_data, guitar_signal.audio_data) assert np.allclose(_guitar_signal.audio_data, guitar_signal.audio_data) with pytest.raises(DataSetException): empty_slakh = nussl.datasets.Slakh(tmpdir, empty, min_acceptable_sources=1) with pytest.raises(ValueError): nussl.datasets.Slakh(tmpdir, band, min_acceptable_sources=0) with pytest.raises(ValueError): nussl.datasets.Slakh(tmpdir, band, max_tracks_per_src=0) with pytest.raises(ValueError): bad_slakh = nussl.datasets.Slakh(tmpdir, bad)