def _generate_melody(self): melody_config_id = self.melody_bundle.generator_details.id melody_config = melody_rnn_model.default_configs[melody_config_id] generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(melody_config), details=melody_config.details, steps_per_quarter=melody_config.steps_per_quarter, checkpoint=melody_rnn_generate.get_checkpoint(), bundle=self.melody_bundle) generator_options = generator_pb2.GeneratorOptions() generator_options.args['temperature'].float_value = self.temperature generator_options.args['beam_size'].int_value = 1 generator_options.args['branch_factor'].int_value = 1 generator_options.args['steps_per_iteration'].int_value = 1 primer_melody = magenta.music.Melody(self.accumulated_primer_melody) qpm = magenta.music.DEFAULT_QUARTERS_PER_MINUTE primer_sequence = primer_melody.to_sequence(qpm=qpm) seconds_per_step = 60.0 / qpm / generator.steps_per_quarter # Set the start time to begin on the next step after the last note ends. last_end_time = (max( n.end_time for n in primer_sequence.notes) if primer_sequence.notes else 0) melody_total_seconds = last_end_time * 3 generate_section = generator_options.generate_sections.add( start_time=last_end_time + seconds_per_step, end_time=melody_total_seconds) generated_sequence = generator.generate(primer_sequence, generator_options) self.generated_melody = [n.pitch for n in generated_sequence.notes] # Get rid of primer melody. self.generated_melody = self.generated_melody[ len(self.accumulated_primer_melody):] # Make sure generated melody is not too long. self.generated_melody = self.generated_melody[:self.max_robot_length] self.accumulated_primer_melody = []
def generate_drums(): """Generate a new drum groove by querying the model.""" global drums_bundle global generated_drums global playable_notes global seed_drum_sequence global num_steps global qpm global total_seconds global temperature drums_config_id = drums_bundle.generator_details.id drums_config = drums_rnn_model.default_configs[drums_config_id] generator = drums_rnn_sequence_generator.DrumsRnnSequenceGenerator( model=drums_rnn_model.DrumsRnnModel(drums_config), details=drums_config.details, steps_per_quarter=drums_config.steps_per_quarter, checkpoint=melody_rnn_generate.get_checkpoint(), bundle=drums_bundle) generator_options = generator_pb2.GeneratorOptions() generator_options.args['temperature'].float_value = temperature generator_options.args['beam_size'].int_value = 1 generator_options.args['branch_factor'].int_value = 1 generator_options.args['steps_per_iteration'].int_value = 1 if seed_drum_sequence is None: primer_drums = magenta.music.DrumTrack([frozenset([36])]) primer_sequence = primer_drums.to_sequence(qpm=qpm) local_num_steps = num_steps else: primer_sequence = seed_drum_sequence local_num_steps = num_steps * 2 tempo = primer_sequence.tempos.add() tempo.qpm = qpm step_length = 60. / qpm / 4.0 total_seconds = local_num_steps * step_length # Set the start time to begin on the next step after the last note ends. last_end_time = (max( n.end_time for n in primer_sequence.notes) if primer_sequence.notes else 0) generator_options.generate_sections.add(start_time=last_end_time + step_length, end_time=total_seconds) generated_sequence = generator.generate(primer_sequence, generator_options) generated_sequence = sequences_lib.quantize_note_sequence( generated_sequence, 4) if seed_drum_sequence is not None: i = 0 while i < len(generated_sequence.notes): if generated_sequence.notes[i].quantized_start_step < num_steps: del generated_sequence.notes[i] else: generated_sequence.notes[i].quantized_start_step -= num_steps generated_sequence.notes[i].quantized_end_step -= num_steps i += 1 drum_pattern = [(n.pitch, n.quantized_start_step, n.quantized_end_step) for n in generated_sequence.notes] # First clear the last drum pattern. if len(playable_notes) > 0: playable_notes = SortedList( [x for x in playable_notes if x.type != 'drums'], key=lambda x: x.onset) for p, s, e in drum_pattern: playable_notes.add( PlayableNote(type='drums', note=[], instrument=DRUM_MAPPING[p], onset=s))