def _read_midi(self, input_file): """ Read midi file into note sequence. :param input_file: A string path to midi file. :return: Note sequence. """ note_sequence = mm.midi_file_to_note_sequence(input_file) # Handle sustain pedal in primer. if self.config.sustain: note_sequence = mm.apply_sustain_control_changes(note_sequence) # Trim to desired number of seconds. if note_sequence.total_time > self.config.max_length: LOGGER.warning( 'Note sequence %d is longer than max seconds %d, truncating.', note_sequence.total_time, self.config.max_length) note_sequence = mm.extract_subsequence(note_sequence, 0, self.config.max_length) # Whether or not remove drums. if any(note.is_drum for note in note_sequence.notes) and not self.config.use_drum: LOGGER.warning('Midi file contains drum sounds, removing.') notes = [note for note in note_sequence.notes if not note.is_drum] del note_sequence.notes[:] note_sequence.notes.extend(notes) # Set primer instrument and program. for note in note_sequence.notes: note.instrument = 1 note.program = 0 return note_sequence
def get_primer_ns(filename, max_length): """ Convert Midi file to note sequences for priming. :param filename: Midi file name. :param max_length: Maximum note sequence length for priming in seconds. :return: Note sequences for priming. """ primer_ns = mm.midi_file_to_note_sequence(filename) # Handle sustain pedal in primer. primer_ns = mm.apply_sustain_control_changes(primer_ns) # Trim to desired number of seconds. if primer_ns.total_time > max_length: LOGGER.warn( 'Primer duration %d is longer than max second %d, truncating.' % (primer_ns.total_time, max_length)) primer_ns = mm.extract_subsequence(primer_ns, 0, max_length) # Remove drums from primer if present. if any(note.is_drum for note in primer_ns.notes): LOGGER.warn('Primer contains drums; they will be removed.') notes = [note for note in primer_ns.notes if not note.is_drum] del primer_ns.notes[:] primer_ns.notes.extend(notes) # Set primer instrument and program. for note in primer_ns.notes: note.instrument = 1 note.program = 0 return primer_ns
def piano_continuation(primer): print("이함수도안돌아?") primer_ns = mm.midi_file_to_note_sequence(primer) # Handle sustain pedal in the primer. primer_ns = mm.apply_sustain_control_changes(primer_ns) # Trim to desired number of seconds. max_primer_seconds = 20 #@param {type:"slider", min:1, max:120} if primer_ns.total_time > max_primer_seconds: print('Primer is longer than %d seconds, truncating.' % max_primer_seconds) primer_ns = mm.extract_subsequence(primer_ns, 0, max_primer_seconds) # Remove drums from primer if present. if any(note.is_drum for note in primer_ns.notes): print('Primer contains drums; they will be removed.') notes = [note for note in primer_ns.notes if not note.is_drum] del primer_ns.notes[:] primer_ns.notes.extend(notes) # Set primer instrument and program. for note in primer_ns.notes: note.instrument = 1 note.program = 0 #note_sequence_to_midi_file(primer_ns, 'modified_'+primer) targets = uncondi_encoders['targets'].encode_note_sequence(primer_ns) # Remove the end token from the encoded primer. targets = targets[:-1] decode_length = max(0, 4096 - len(targets)) if len(targets) >= 4096: print( 'Primer has more events than maximum sequence length; nothing will be generated.' ) # Generate sample events. sample_ids = next(uncondi_samples)['outputs'] # Decode to NoteSequence. midi_filename = decode(sample_ids, encoder=uncondi_encoders['targets']) ns = mm.midi_file_to_note_sequence(midi_filename) # Append continuation to primer. continuation_ns = mm.concatenate_sequences([primer_ns, ns]) note_sequence_to_midi_file(continuation_ns, 'continuated_' + primer)
def generate_primer(self): """ Put something important here. """ if self.conditioned: raise ValueError("Should be using an unconditioned model!") primer_ns = self.sequence primer_ns = mm.apply_sustain_control_changes(primer_ns) max_primer_seconds = 10 if primer_ns.total_time > max_primer_seconds: print(f'Primer is longer than {max_primer_seconds} seconds, truncating.') # cut primer if it's too long primer_ns = mm.extract_subsequence( primer_ns, 0, max_primer_seconds) if any(note.is_drum for note in primer_ns.notes): print('Primer contains drums; they will be removed.') notes = [note for note in primer_ns.notes if not note.is_drum] del primer_ns.notes[:] primer_ns.notes.extend(notes) for note in primer_ns.notes: # make into piano note.instrument = 1 note.program = 0 self.targets = self.encoders['targets'].encode_note_sequence( primer_ns) # Remove the end token from the encoded primer. self.targets = self.targets[:-1] self.decode_length = max(0, 4096 - len(self.targets)) if len(self.targets) >= 4096: print('Primer has more events than maximum sequence length; nothing will be generated.') # Generate sample events. sample_ids = next(self.samples)['outputs'] midi_filename = self.decode( sample_ids, encoder=self.encoders['targets']) ns = mm.midi_file_to_note_sequence(midi_filename) # Append continuation to primer. continuation_ns = mm.concatenate_sequences([primer_ns, ns]) request_dict = self.put_request_dict generated_sequence_2_mp3(continuation_ns, f"{self.unique_id}", use_salamander=True, request_dict=request_dict)
def preprocess_sequence(sequence_tensor): """Preprocess a NoteSequence for training. Deserialize and apply sustain control changes. Args: sequence_tensor: The NoteSequence in serialized form. Returns: sequence: The preprocessed NoteSequence object. """ sequence = music_pb2.NoteSequence.FromString(sequence_tensor) sequence = mm.apply_sustain_control_changes(sequence) return sequence
def preprocess_sequence(sequence_tensor): """Preprocess a NoteSequence for training. Deserialize and apply sustain control changes. Args: sequence_tensor: The NoteSequence in serialized form. Returns: sequence: The preprocessed NoteSequence object. """ sequence = music_pb2.NoteSequence.FromString(sequence_tensor) sequence = mm.apply_sustain_control_changes(sequence) return sequence
def get_primer_ns(filename): """ Convert MIDI file to note sequences for priming. :param filename: MIDI file name. :return: Note sequences for priming. """ primer_note_sequence = mm.midi_file_to_note_sequence(filename) # Handle sustain pedal in primer. primer_note_sequence = mm.apply_sustain_control_changes( primer_note_sequence) # Set primer instrument and program. for note in primer_note_sequence.notes: note.instrument = 1 note.program = 0 return primer_note_sequence
def truncate_note_sequence(sequence, truncate_secs): """Truncates a NoteSequence to the given length.""" sus_sequence = mm.apply_sustain_control_changes(sequence) truncated_seq = music_pb2.NoteSequence() for note in sus_sequence.notes: start_time = note.start_time end_time = note.end_time if start_time > truncate_secs: continue if end_time > truncate_secs: end_time = truncate_secs modified_note = truncated_seq.notes.add() modified_note.MergeFrom(note) modified_note.start_time = start_time modified_note.end_time = end_time if truncated_seq.notes: truncated_seq.total_time = truncated_seq.notes[-1].end_time return truncated_seq
def truncate_note_sequence(sequence, truncate_secs): """Truncates a NoteSequence to the given length.""" sus_sequence = mm.apply_sustain_control_changes(sequence) truncated_seq = music_pb2.NoteSequence() for note in sus_sequence.notes: start_time = note.start_time end_time = note.end_time if start_time > truncate_secs: continue if end_time > truncate_secs: end_time = truncate_secs modified_note = truncated_seq.notes.add() modified_note.MergeFrom(note) modified_note.start_time = start_time modified_note.end_time = end_time if truncated_seq.notes: truncated_seq.total_time = truncated_seq.notes[-1].end_time return truncated_seq
def music_generator(primer='erik_gnossienne', primer_begin_buffer=10, primer_length=90, output_path='.', filename='./public/output'): SF2_PATH = './models/Yamaha-C5-Salamander-JNv5.1.sf2' SAMPLE_RATE = 16000 # Upload a MIDI file and convert to NoteSequence. def upload_midi(): data = list(files.upload().values()) if len(data) > 1: print('Multiple files uploaded; using only one.') return mm.midi_to_note_sequence(data[0]) # Decode a list of IDs. def decode(ids, encoder): ids = list(ids) if text_encoder.EOS_ID in ids: ids = ids[:ids.index(text_encoder.EOS_ID)] return encoder.decode(ids) model_name = 'transformer' hparams_set = 'transformer_tpu' ckpt_path = './models/checkpoints/unconditional_model_16.ckpt' class PianoPerformanceLanguageModelProblem(score2perf.Score2PerfProblem): @property def add_eos_symbol(self): return True problem = PianoPerformanceLanguageModelProblem() unconditional_encoders = problem.get_feature_encoders() # Set up HParams. hparams = trainer_lib.create_hparams(hparams_set=hparams_set) trainer_lib.add_problem_hparams(hparams, problem) hparams.num_hidden_layers = 16 hparams.sampling_method = 'random' # Set up decoding HParams. decode_hparams = decoding.decode_hparams() decode_hparams.alpha = 0.0 decode_hparams.beam_size = 1 # Create Estimator. run_config = trainer_lib.create_run_config(hparams) estimator = trainer_lib.create_estimator(model_name, hparams, run_config, decode_hparams=decode_hparams) # These values will be changed by subsequent cells. targets = [] decode_length = 0 # Create input generator (so we can adjust priming and # decode length on the fly). def input_generator(): global targets global decode_length while True: yield { 'targets': np.array([targets], dtype=np.int32), 'decode_length': np.array(decode_length, dtype=np.int32) } # Start the Estimator, loading from the specified checkpoint. input_fn = decoding.make_input_fn_from_generator(input_generator()) unconditional_samples = estimator.predict(input_fn, checkpoint_path=ckpt_path) # "Burn" one. _ = next(unconditional_samples) filenames = { 'C major arpeggio': './models/primers/c_major_arpeggio.mid', 'C major scale': './models/primers/c_major_scale.mid', 'Clair de Lune': './models/primers/clair_de_lune.mid', 'Classical': 'audio_midi/Classical_Piano_piano-midi.de_MIDIRip/bach/bach_846_format0.mid', 'erik_gymnopedie': 'audio_midi/erik_satie/gymnopedie_1_(c)oguri.mid', 'erik_gymnopedie_2': 'audio_midi/erik_satie/gymnopedie_2_(c)oguri.mid', 'erik_gymnopedie_3': 'audio_midi/erik_satie/gymnopedie_3_(c)oguri.mid', 'erik_gnossienne': 'audio_midi/erik_satie/gnossienne_1_(c)oguri.mid', 'erik_gnossienne_2': 'audio_midi/erik_satie/gnossienne_2_(c)oguri.mid', 'erik_gnossienne_3': 'audio_midi/erik_satie/gnossienne_3_(c)oguri.mid', 'erik_gnossienne_dery': 'audio_midi/erik_satie/gnossienne_1_(c)dery.mid', 'erik_gnossienne_dery_2': 'audio_midi/erik_satie/gnossienne_2_(c)dery.mid', 'erik_gnossienne_dery_3': 'audio_midi/erik_satie/gnossienne_3_(c)dery.mid', 'erik_gnossienne_dery_5': 'audio_midi/erik_satie/gnossienne_5_(c)dery.mid', 'erik_gnossienne_dery_6': 'audio_midi/erik_satie/gnossienne_6_(c)dery.mid', '1': 'audio_midi/erik_satie/1.mid', '2': 'audio_midi/erik_satie/2.mid', '3': 'audio_midi/erik_satie/3.mid', '4': 'audio_midi/erik_satie/4.mid', '5': 'audio_midi/erik_satie/5.mid', '6': 'audio_midi/erik_satie/6.mid', '7': 'audio_midi/erik_satie/7.mid', '8': 'audio_midi/erik_satie/8.mid', '9': 'audio_midi/erik_satie/9.mid', '10': 'audio_midi/erik_satie/10.mid', } # primer = 'C major scale' #if primer == 'Upload your own!': # primer_ns = upload_midi() #else: # # Use one of the provided primers. # primer_ns = mm.midi_file_to_note_sequence(filenames[primer]) primer_ns = mm.midi_file_to_note_sequence(filenames[primer]) # Handle sustain pedal in the primer. primer_ns = mm.apply_sustain_control_changes(primer_ns) # Trim to desired number of seconds. max_primer_seconds = primer_length if primer_ns.total_time > max_primer_seconds: print('Primer is longer than %d seconds, truncating.' % max_primer_seconds) primer_ns = mm.extract_subsequence( primer_ns, primer_begin_buffer, max_primer_seconds + primer_begin_buffer) # Remove drums from primer if present. if any(note.is_drum for note in primer_ns.notes): print('Primer contains drums; they will be removed.') notes = [note for note in primer_ns.notes if not note.is_drum] del primer_ns.notes[:] primer_ns.notes.extend(notes) # Set primer instrument and program. for note in primer_ns.notes: note.instrument = 1 note.program = 0 ## Play and plot the primer. #mm.play_sequence( # primer_ns, # synth=mm.fluidsynth, sample_rate=SAMPLE_RATE, sf2_path=SF2_PATH) #mm.plot_sequence(primer_ns) mm.sequence_proto_to_midi_file( primer_ns, join(output_path, 'primer_{}.mid'.format(filename))) targets = unconditional_encoders['targets'].encode_note_sequence(primer_ns) # Remove the end token from the encoded primer. targets = targets[:-1] decode_length = max(0, 10000 - len(targets)) if len(targets) >= 4096: print( 'Primer has more events than maximum sequence length; nothing will be generated.' ) # Generate sample events. sample_ids = next(unconditional_samples)['outputs'] # Decode to NoteSequence. midi_filename = decode(sample_ids, encoder=unconditional_encoders['targets']) ns = mm.midi_file_to_note_sequence(midi_filename) print('Sample IDs: {}'.format(sample_ids)) print('Sample IDs length: {}'.format(len(sample_ids))) print('Encoder: {}'.format(unconditional_encoders['targets'])) print('Unconditional Samples: {}'.format(unconditional_samples)) # print('{}'.format(ns)) # continuation_ns = mm.concatenate_sequences([primer_ns, ns]) continuation_ns = ns # mm.play_sequence( # continuation_ns, # synth=mm.fluidsynth, sample_rate=SAMPLE_RATE, sf2_path=SF2_PATH) # mm.plot_sequence(continuation_ns) # try: audio = mm.fluidsynth(continuation_ns, sample_rate=SAMPLE_RATE, sf2_path=SF2_PATH) normalizer = float(np.iinfo(np.int16).max) array_of_ints = np.array(np.asarray(audio) * normalizer, dtype=np.int16) wavfile.write(join(output_path, filename + '.wav'), SAMPLE_RATE, array_of_ints) print('[+] Output stored as {}'.format(filename + '.wav')) mm.sequence_proto_to_midi_file( continuation_ns, join(output_path, 'continuation_{}.mid'.format(filename)))
filenames = { 'C major arpeggio': '/content/primers/c_major_arpeggio.mid', 'C major scale': '/content/primers/c_major_scale.mid', 'Clair de Lune': '/content/primers/clair_de_lune.mid', } primer = 'C major scale' #@param ['C major arpeggio', 'C major scale', 'Clair de Lune', 'Upload your own!'] if primer == 'Upload your own!': primer_ns = upload_midi() else: # Use one of the provided primers. primer_ns = mm.midi_file_to_note_sequence(filenames[primer]) # Handle sustain pedal in the primer. primer_ns = mm.apply_sustain_control_changes(primer_ns) # Trim to desired number of seconds. max_primer_seconds = 20 #@param {type:"slider", min:1, max:120} if primer_ns.total_time > max_primer_seconds: print('Primer is longer than %d seconds, truncating.' % max_primer_seconds) primer_ns = mm.extract_subsequence( primer_ns, 0, max_primer_seconds) # Remove drums from primer if present. if any(note.is_drum for note in primer_ns.notes): print('Primer contains drums; they will be removed.') notes = [note for note in primer_ns.notes if not note.is_drum] del primer_ns.notes[:] primer_ns.notes.extend(notes)